wii_ec.h 5.8 KB

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