wii_ec.h 5.3 KB

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