wii_ec.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #ifndef WII_EC_H_
  2. #define WII_EC_H_
  3. #include <stdbool.h>
  4. #include <furi.h>
  5. #include "wii_ec_nunchuck.h"
  6. #include "wii_ec_classic.h"
  7. #include "wii_ec_udraw.h"
  8. #include "bc_logging.h"
  9. //----------------------------------------------------------------------------- ----------------------------------------
  10. // Crypto key (PSK), base register : {0x40..0x4F}[2][8]
  11. #define ENC_LEN (2 * 8)
  12. // Controller State data, base register : {0x00..0x05}[6]
  13. #define JOY_LEN (6)
  14. // Calibration data, base register : {0x20..0x2F}[16]
  15. #define CAL_LEN (16)
  16. // Controller ID, base register : {0xFA..0xFF}[6]
  17. #define PID_LEN (6)
  18. //----------------------------------------------------------------------------- ----------------------------------------
  19. // Perhipheral specific parameters union
  20. //
  21. typedef union ecDec {
  22. ecDecNunchuck_t nunchuck;
  23. ecDecClassic_t classic;
  24. } ecDec_t;
  25. //-----------------------------------------------------------------------------
  26. typedef union ecCal {
  27. // 0=lowest seen ; 1=min ; 2=mid ; 3=max ; 4=highest seen
  28. ecCalNunchuck_t nunchuck[5];
  29. ecCalClassic_t classic[5];
  30. } ecCal_t;
  31. //----------------------------------------------------------------------------- ----------------------------------------
  32. // Wii Extension Controller events
  33. //
  34. typedef enum wiiEcEventType {
  35. WIIEC_NONE,
  36. WIIEC_CONN, // Connect
  37. WIIEC_DISCONN, // Disconnect
  38. WIIEC_PRESS, // Press button
  39. WIIEC_RELEASE, // Release button
  40. WIIEC_ANALOG, // Analogue change (Joystick/Trigger)
  41. WIIEC_ACCEL, // Accelerometer change
  42. } wiiEcEventType_t;
  43. //-----------------------------------------------------------------------------
  44. typedef struct wiiEcEvent {
  45. wiiEcEventType_t type; // event type
  46. char in; // input (see device specific options)
  47. uint32_t val; // new value - meaningless for digital button presses
  48. } wiiEcEvent_t;
  49. //----------------------------------------------------------------------------- ----------------------------------------
  50. // Known perhipheral types
  51. //
  52. typedef enum ecPid {
  53. PID_UNKNOWN = 0,
  54. PID_FIRST = 1,
  55. PID_NUNCHUCK = PID_FIRST,
  56. // If you're wise, ONLY edit this section
  57. PID_NUNCHUCK_R2,
  58. PID_CLASSIC,
  59. PID_CLASSIC_PRO,
  60. PID_BALANCE,
  61. PID_GH_GUITAR,
  62. PID_GH_DRUMS,
  63. PID_TURNTABLE,
  64. PID_TAIKO_DRUMS,
  65. PID_UDRAW, //! same as drawsome?
  66. // -----
  67. PID_ERROR,
  68. PID_NULL,
  69. PID_CNT,
  70. } ecPid_t;
  71. //-----------------------------------------------------------------------------
  72. // Calibration strategies
  73. //
  74. typedef enum ecCalib {
  75. CAL_FACTORY = 0x01, // (re)set to factory defaults
  76. CAL_TRACK = 0x02, // track maximum and minimum values seen
  77. CAL_RESET = 0x04, // initialise ready for software calibration
  78. CAL_RANGE = 0x08, // perform software calibration step
  79. CAL_CENTRE = 0x10, // reset centre point of joystick
  80. CAL_NOTJOY = 0x20, // do NOT calibrate the joystick
  81. } ecCalib_t;
  82. //-----------------------------------------------------------------------------
  83. // ecId table entry
  84. //
  85. typedef struct ecId {
  86. uint8_t id[6]; // 6 byte ID string returned by Extension Controller
  87. char* name; // Friendly name
  88. scene_t scene; // Default scene
  89. bool (*init)(wiiEC_t*); // Additional initialisation code
  90. void (*decode)(wiiEC_t*); // Decode function
  91. void (*check)(wiiEC_t*, FuriMessageQueue*); // check (for action) function
  92. void (*calib)(wiiEC_t*, ecCalib_t); // calibrate analogue controllers [SOFTWARE]
  93. void (*show)(Canvas* const, state_t* const); // Draw scene
  94. bool (*keys)(const eventMsg_t* const, state_t* const); // Interpret keys
  95. } ecId_t;
  96. //-----------------------------------------------------------------------------
  97. // List of known perhipherals
  98. //
  99. // More perhipheral ID codes here: https://wiibrew.org/wiki/Wiimote/Extension_Controllers#The_New_Way
  100. //
  101. extern const ecId_t ecId[PID_CNT];
  102. //----------------------------------------------------------------------------- ----------------------------------------
  103. // Data pertaining to a single Perhipheral instance
  104. //
  105. typedef struct wiiEC {
  106. // Perhipheral state
  107. bool init; // Initialised?
  108. uint8_t pid[PID_LEN]; // PID string - eg. {0x00, 0x00, 0xA4, 0x20, 0x00, 0x00}
  109. ecPid_t pidx; // Index in to ecId table
  110. const char* sid; // just for convenience
  111. bool encrypt; // encryption enabled?
  112. uint8_t encKey[ENC_LEN]; // encryption key
  113. uint8_t calF[CAL_LEN]; // factory calibration data (not software)
  114. uint8_t joy[JOY_LEN]; // Perhipheral raw data
  115. ecDec_t dec[2]; // device specific decode (two, so we can spot changes)
  116. int decN; // which decode set is most recent {0, 1}
  117. ecCal_t calS; // software calibration data
  118. } wiiEC_t;
  119. //----------------------------------------------------------------------------- ----------------------------------------
  120. // Function prototypes
  121. //
  122. // top level calls will work out which sub-function to call
  123. // top level check() function will handle connect/disconnect messages
  124. //
  125. #include <gui/gui.h> // Canvas
  126. typedef struct wiiEC wiiEC_t;
  127. typedef enum ecCalib ecCalib_t;
  128. typedef struct state state_t;
  129. typedef struct eventMsg eventMsg_t;
  130. void ecDecode(wiiEC_t* const pec);
  131. void ecPoll(wiiEC_t* const pec, FuriMessageQueue* const queue);
  132. void ecCalibrate(wiiEC_t* const pec, ecCalib_t c);
  133. void ec_show(Canvas* const canvas, state_t* const state);
  134. bool ec_key(const eventMsg_t* const msg, state_t* const state);
  135. #endif //WII_EC_H_