wii_ec.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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_CLASSIC,
  66. PID_BALANCE,
  67. PID_GH_GUITAR,
  68. PID_GH_DRUMS,
  69. PID_TURNTABLE,
  70. PID_TAIKO_DRUMS,
  71. PID_UDRAW, //! same as drawsome?
  72. // -----
  73. PID_ERROR,
  74. PID_NULL,
  75. PID_CNT,
  76. }
  77. ecPid_t;
  78. //-----------------------------------------------------------------------------
  79. // Calibration strategies
  80. //
  81. typedef
  82. enum ecCalib {
  83. CAL_FACTORY = 0x01, // (re)set to factory defaults
  84. CAL_TRACK = 0x02, // track maximum and minimum values seen
  85. CAL_RESET = 0x04, // initialise ready for software calibration
  86. CAL_RANGE = 0x08, // perform software calibration step
  87. CAL_CENTRE = 0x10, // reset centre point of joystick
  88. CAL_NOTJOY = 0x20, // do NOT calibrate the joystick
  89. }
  90. ecCalib_t;
  91. //-----------------------------------------------------------------------------
  92. // ecId table entry
  93. //
  94. typedef
  95. struct ecId {
  96. uint8_t id[6]; // 6 byte ID string returned by Extension Controller
  97. char* name; // Friendly name
  98. scene_t scene; // Default scene
  99. bool (*init)(wiiEC_t*); // Additional initialisation code
  100. void (*decode)(wiiEC_t*); // Decode function
  101. void (*check)(wiiEC_t*, FuriMessageQueue*); // check (for action) function
  102. void (*calib)(wiiEC_t*, ecCalib_t); // calibrate analogue controllers [SOFTWARE]
  103. void (*show)(Canvas* const, state_t* const); // Draw scene
  104. bool (*keys)(const eventMsg_t* const, state_t* const); // Interpret keys
  105. }
  106. ecId_t;
  107. //-----------------------------------------------------------------------------
  108. // List of known perhipherals
  109. //
  110. // More perhipheral ID codes here: https://wiibrew.org/wiki/Wiimote/Extension_Controllers#The_New_Way
  111. //
  112. extern const ecId_t ecId[PID_CNT] ;
  113. //----------------------------------------------------------------------------- ----------------------------------------
  114. // Data pertaining to a single Perhipheral instance
  115. //
  116. typedef
  117. struct wiiEC {
  118. // Perhipheral state
  119. bool init; // Initialised?
  120. uint8_t pid[PID_LEN]; // PID string - eg. {0x00, 0x00, 0xA4, 0x20, 0x00, 0x00}
  121. ecPid_t pidx; // Index in to ecId table
  122. const char* sid; // just for convenience
  123. bool encrypt; // encryption enabled?
  124. uint8_t encKey[ENC_LEN]; // encryption key
  125. uint8_t calF[CAL_LEN]; // factory calibration data (not software)
  126. uint8_t joy[JOY_LEN]; // Perhipheral raw data
  127. ecDec_t dec[2]; // device specific decode (two, so we can spot changes)
  128. int decN; // which decode set is most recent {0, 1}
  129. ecCal_t calS; // software calibration data
  130. }
  131. wiiEC_t;
  132. //----------------------------------------------------------------------------- ----------------------------------------
  133. // Function prototypes
  134. //
  135. // top level calls will work out which sub-function to call
  136. // top level check() function will handle connect/disconnect messages
  137. //
  138. #include <gui/gui.h> // Canvas
  139. typedef struct wiiEC wiiEC_t ;
  140. typedef enum ecCalib ecCalib_t ;
  141. typedef struct state state_t ;
  142. typedef struct eventMsg eventMsg_t ;
  143. void ecDecode (wiiEC_t* const pec) ;
  144. void ecPoll (wiiEC_t* const pec, FuriMessageQueue* const queue) ;
  145. void ecCalibrate (wiiEC_t* const pec, ecCalib_t c) ;
  146. void ec_show ( Canvas* const canvas, state_t* const state) ;
  147. bool ec_key (const eventMsg_t* const msg, state_t* const state) ;
  148. #endif //WII_EC_H_