wii_anal.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. //----------------------------------------------------------------------------- ----------------------------------------
  2. // Includes
  3. //
  4. // System libs
  5. #include <stdlib.h> // malloc
  6. #include <stdint.h> // uint32_t
  7. #include <stdarg.h> // __VA_ARGS__
  8. #include <stdio.h>
  9. #include <ctype.h>
  10. // FlipperZero libs
  11. #include <furi.h> // Core API
  12. #include <gui/gui.h> // GUI (screen/keyboard) API
  13. #include <input/input.h> // GUI Input extensions
  14. #include <notification/notification_messages.h>
  15. // Do this first!
  16. #define ERR_C_ // Do this in precisely ONE file
  17. #include "err.h" // Error numbers & messages
  18. #include "bc_logging.h"
  19. // Local headers
  20. #include "wii_anal.h" // Various enums and struct declarations
  21. #include "wii_i2c.h" // Wii i2c functions
  22. #include "wii_ec.h" // Wii Extension Controller functions (eg. draw)
  23. #include "wii_anal_keys.h" // key mappings
  24. #include "gfx/images.h" // Images
  25. #include "wii_anal_lcd.h" // Drawing functions
  26. #include "wii_anal_ec.h" // Wii controller events
  27. #include "wii_anal_ver.h" // Version number
  28. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  29. // OOOOO // SSSSS CCCCC AAA L L BBBB AAA CCCC K K SSSSS
  30. // O O /// S C A A L L B B A A C K K S
  31. // O O /// SSSSS C AAAAA L L BBBB AAAAA C KKK SSSSS
  32. // O O /// S C A A L L B B A A C K K S
  33. // OOOOO // SSSSS CCCCC A A LLLLL LLLLL BBBB A A CCCC K K SSSSS
  34. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  35. //+============================================================================ ========================================
  36. // OS Callback : Timer tick
  37. // We register this function to be called when the OS signals a timer 'tick' event
  38. //
  39. static void cbTimer(void* ctx) {
  40. ENTER;
  41. furi_assert(ctx);
  42. FuriMessageQueue* queue = ctx;
  43. eventMsg_t message = {.id = EVID_TICK};
  44. furi_message_queue_put(queue, &message, 0);
  45. LEAVE;
  46. return;
  47. }
  48. //+============================================================================ ========================================
  49. // OS Callback : Keypress
  50. // We register this function to be called when the OS detects a keypress
  51. //
  52. static void cbInput(InputEvent* event, void* ctx) {
  53. ENTER;
  54. <<<<<<< HEAD
  55. FuriMessageQueue* queue = ctx;
  56. furi_assert(queue);
  57. =======
  58. furi_assert(ctx);
  59. >>>>>>> 19c5ebf73a10da65de9107d6c33364e8524b0016
  60. furi_assert(event);
  61. FuriMessageQueue* queue = ctx;
  62. // Put an "input" event message on the message queue
  63. eventMsg_t message = {.id = EVID_KEY, .input = *event};
  64. furi_message_queue_put(queue, &message, FuriWaitForever);
  65. LEAVE;
  66. return;
  67. }
  68. //+============================================================================
  69. // Show version number
  70. //
  71. static void showVer(Canvas* const canvas) {
  72. show(canvas, 0, 59, &img_3x5_v, SHOW_SET_BLK);
  73. show(canvas, 4, 59, VER_MAJ, SHOW_SET_BLK);
  74. canvas_draw_frame(canvas, 8, 62, 2, 2);
  75. show(canvas, 11, 59, VER_MIN, SHOW_SET_BLK);
  76. canvas_draw_frame(canvas, 15, 62, 2, 2);
  77. show(canvas, 18, 59, VER_SUB, SHOW_SET_BLK);
  78. }
  79. //+============================================================================
  80. // OS Callback : Draw request
  81. // We register this function to be called when the OS requests that the screen is redrawn
  82. //
  83. // We actually instruct the OS to perform this request, after we update the interface
  84. // I guess it's possible that this instruction may able be issued by other threads !?
  85. //
  86. static void cbDraw(Canvas* const canvas, void* ctx) {
  87. ENTER;
  88. furi_assert(canvas);
  89. furi_assert(ctx);
  90. state_t* state = ctx;
  91. // Try to acquire the mutex for the plugin state variables, timeout = 25mS
  92. if(furi_mutex_acquire(state->mutex, 25) != FuriStatusOk) return;
  93. switch(state->scene) {
  94. //---------------------------------------------------------------------
  95. case SCENE_SPLASH:
  96. show(canvas, 2, 0, &img_csLogo_FULL, SHOW_SET_BLK);
  97. canvas_set_font(canvas, FontSecondary);
  98. canvas_draw_str_aligned(canvas, 64, 43, AlignCenter, AlignTop, "Wii Extension Controller");
  99. canvas_draw_str_aligned(canvas, 64, 55, AlignCenter, AlignTop, "Protocol Analyser");
  100. showVer(canvas);
  101. break;
  102. //---------------------------------------------------------------------
  103. case SCENE_RIP:
  104. show(canvas, 0, 0, &img_RIP, SHOW_SET_BLK);
  105. break;
  106. //---------------------------------------------------------------------
  107. case SCENE_WAIT:
  108. #define xo 2
  109. show(canvas, 3 + xo, 10, &img_ecp_port, SHOW_SET_BLK);
  110. BOX_TL(22 + xo, 6, 82 + xo, 23); // 3v3
  111. BOX_TL(48 + xo, 21, 82 + xo, 23); // C1
  112. BOX_BL(22 + xo, 41, 82 + xo, 58); // C0
  113. BOX_BL(48 + xo, 41, 82 + xo, 44); // Gnd
  114. show(canvas, 90 + xo, 3, &img_6x8_3, SHOW_SET_BLK); // 3v3
  115. show(canvas, 97 + xo, 3, &img_6x8_v, SHOW_SET_BLK);
  116. show(canvas, 104 + xo, 3, &img_6x8_3, SHOW_SET_BLK);
  117. show(canvas, 90 + xo, 18, &img_6x8_C, SHOW_SET_BLK); // C1 <->
  118. show(canvas, 98 + xo, 18, &img_6x8_1, SHOW_SET_BLK);
  119. show(canvas, 107 + xo, 16, &img_ecp_SDA, SHOW_SET_BLK);
  120. show(canvas, 90 + xo, 40, &img_6x8_G, SHOW_SET_BLK); // Gnd
  121. show(canvas, 97 + xo, 40, &img_6x8_n, SHOW_SET_BLK);
  122. show(canvas, 104 + xo, 40, &img_6x8_d, SHOW_SET_BLK);
  123. show(canvas, 90 + xo, 54, &img_6x8_C, SHOW_SET_BLK); // C0 _-_-
  124. show(canvas, 98 + xo, 54, &img_6x8_0, SHOW_SET_BLK);
  125. show(canvas, 108 + xo, 54, &img_ecp_SCL, SHOW_SET_BLK);
  126. show(canvas, 0, 0, &img_csLogo_Small, SHOW_SET_BLK);
  127. showVer(canvas);
  128. #undef xo
  129. break;
  130. //---------------------------------------------------------------------
  131. case SCENE_DEBUG:
  132. canvas_set_font(canvas, FontSecondary);
  133. show(canvas, 0, 0, &img_key_U, SHOW_SET_BLK);
  134. canvas_draw_str_aligned(canvas, 11, 0, AlignLeft, AlignTop, "Initialise Perhipheral");
  135. show(canvas, 0, 11, &img_key_OK, SHOW_SET_BLK);
  136. canvas_draw_str_aligned(canvas, 11, 11, AlignLeft, AlignTop, "Read values [see log]");
  137. show(canvas, 0, 23, &img_key_D, SHOW_SET_BLK);
  138. canvas_draw_str_aligned(canvas, 11, 22, AlignLeft, AlignTop, "Restart Scanner");
  139. show(canvas, 0, 33, &img_key_Back, SHOW_SET_BLK);
  140. canvas_draw_str_aligned(canvas, 11, 33, AlignLeft, AlignTop, "Exit");
  141. break;
  142. //---------------------------------------------------------------------
  143. default:
  144. if(state->ec.pidx >= PID_ERROR) {
  145. ERROR("%s : bad PID = %d", __func__, state->ec.pidx);
  146. } else {
  147. if((state->scene == SCENE_DUMP) || !ecId[state->ec.pidx].show)
  148. ecId[PID_UNKNOWN].show(canvas, state);
  149. else
  150. ecId[state->ec.pidx].show(canvas, state);
  151. }
  152. break;
  153. }
  154. // Release the mutex
  155. furi_mutex_release(state->mutex);
  156. LEAVE;
  157. return;
  158. }
  159. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  160. // SSSSS TTTTT AAA TTTTT EEEEE V V AAA RRRR IIIII AAA BBBB L EEEEE SSSSS
  161. // S T A A T E V V A A R R I A A B B L E S
  162. // SSSSS T AAAAA T EEE V V AAAAA RRRR I AAAAA BBBB L EEE SSSSS
  163. // S T A A T E V V A A R R I A A B B L E S
  164. // SSSSS T A A T EEEEE V A A R R IIIII A A BBBB LLLLL EEEEE SSSSS
  165. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  166. //+============================================================================ ========================================
  167. // Initialise plugin state variables
  168. //
  169. static inline bool stateInit(state_t* const state) {
  170. ENTER;
  171. furi_assert(state);
  172. bool rv = true; // assume success
  173. // Enable the main loop
  174. state->run = true;
  175. // Timer
  176. state->timerEn = false;
  177. state->timer = NULL;
  178. state->timerHz = furi_kernel_get_tick_frequency();
  179. state->fps = 30;
  180. // Scene
  181. state->scene = SCENE_SPLASH;
  182. state->scenePrev = SCENE_NONE;
  183. state->scenePegg = SCENE_NONE;
  184. state->hold = 0; // show hold meters (-1=lowest, 0=current, +1=highest}
  185. state->calib = CAL_TRACK;
  186. state->pause = false; // animation running
  187. state->apause = false; // auto-pause animation
  188. // Notifications
  189. state->notify = NULL;
  190. // Perhipheral
  191. state->ec.init = false;
  192. state->ec.pidx = PID_UNKNOWN;
  193. state->ec.sid = ecId[state->ec.pidx].name;
  194. // Controller data
  195. memset(state->ec.pid, 0xC5, PID_LEN); // Cyborg 5ystems
  196. memset(state->ec.calF, 0xC5, CAL_LEN);
  197. memset(state->ec.joy, 0xC5, JOY_LEN);
  198. // Encryption details
  199. state->ec.encrypt = false;
  200. memset(state->ec.encKey, 0x00, ENC_LEN);
  201. // Seed the PRNG
  202. // CYCCNT --> lib/STM32CubeWB/Drivers/CMSIS/Include/core_cm7.h
  203. // srand(DWT->CYCCNT);
  204. LEAVE;
  205. return rv;
  206. }
  207. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  208. // MM MM AAA IIIII N N
  209. // M M M A A I NN N
  210. // M M M AAAAA I N N N
  211. // M M A A I N NN
  212. // M M A A IIIII N N
  213. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  214. //+============================================================================ ========================================
  215. // Enable/Disable scanning
  216. //
  217. void timerEn(state_t* state, bool on) {
  218. ENTER;
  219. furi_assert(state);
  220. // ENable scanning
  221. if(on) {
  222. if(state->timerEn) {
  223. WARN(wii_errs[WARN_SCAN_START]);
  224. } else {
  225. // Set the timer to fire at 'fps' times/second
  226. if(furi_timer_start(state->timer, state->timerHz / state->fps) == FuriStatusOk) {
  227. state->timerEn = true;
  228. INFO("%s : monitor started", __func__);
  229. } else {
  230. ERROR(wii_errs[ERR_TIMER_START]);
  231. }
  232. }
  233. // DISable scanning
  234. } else {
  235. if(!state->timerEn) {
  236. WARN(wii_errs[WARN_SCAN_STOP]);
  237. } else {
  238. // Stop the timer
  239. if(furi_timer_stop(state->timer) == FuriStatusOk) {
  240. state->timerEn = false;
  241. INFO("%s : monitor stopped", __func__);
  242. } else {
  243. ERROR(wii_errs[ERR_TIMER_STOP]);
  244. }
  245. }
  246. }
  247. LEAVE;
  248. return;
  249. }
  250. //+============================================================================ ========================================
  251. // Plugin entry point
  252. //
  253. int32_t wii_ec_anal(void) {
  254. ENTER;
  255. // ===== Variables =====
  256. err_t error = 0; // assume success
  257. Gui* gui = NULL;
  258. ViewPort* vpp = NULL;
  259. state_t* state = NULL;
  260. FuriMessageQueue* queue = NULL;
  261. const uint32_t queueSz = 20; // maximum messages in queue
  262. uint32_t tmo = (3.5f * 1000); // timeout splash screen after N seconds
  263. // The queue will contain plugin event-messages
  264. // --> local
  265. eventMsg_t msg = {0};
  266. INFO("BEGIN");
  267. // ===== Message queue =====
  268. // 1. Create a message queue (for up to 8 (keyboard) event messages)
  269. if(!(queue = furi_message_queue_alloc(queueSz, sizeof(msg)))) {
  270. ERROR(wii_errs[(error = ERR_MALLOC_QUEUE)]);
  271. goto bail;
  272. }
  273. // ===== Create GUI Interface =====
  274. // 2. Create a GUI interface
  275. if(!(gui = furi_record_open("gui"))) {
  276. ERROR(wii_errs[(error = ERR_NO_GUI)]);
  277. goto bail;
  278. }
  279. // ===== Plugin state variables =====
  280. // 3. Allocate space on the heap for the plugin state variables
  281. if(!(state = malloc(sizeof(state_t)))) {
  282. ERROR(wii_errs[(error = ERR_MALLOC_STATE)]);
  283. goto bail;
  284. }
  285. // 4. Initialise the plugin state variables
  286. if(!stateInit(state)) {
  287. // error message(s) is/are output by stateInit()
  288. error = 15;
  289. goto bail;
  290. }
  291. // 5. Create a mutex for (reading/writing) the plugin state variables
  292. if(!(state->mutex = furi_mutex_alloc(FuriMutexTypeNormal))) {
  293. ERROR(wii_errs[(error = ERR_NO_MUTEX)]);
  294. goto bail;
  295. }
  296. // ===== Viewport =====
  297. // 6. Allocate space on the heap for the viewport
  298. if(!(vpp = view_port_alloc())) {
  299. ERROR(wii_errs[(error = ERR_MALLOC_VIEW)]);
  300. goto bail;
  301. }
  302. // 7a. Register a callback for input events
  303. view_port_input_callback_set(vpp, cbInput, queue);
  304. // 7b. Register a callback for draw events
  305. view_port_draw_callback_set(vpp, cbDraw, state);
  306. // ===== Start GUI Interface =====
  307. // 8. Attach the viewport to the GUI
  308. gui_add_view_port(gui, vpp, GuiLayerFullscreen);
  309. // ===== Timer =====
  310. // 9. Allocate a timer
  311. if(!(state->timer = furi_timer_alloc(cbTimer, FuriTimerTypePeriodic, queue))) {
  312. ERROR(wii_errs[(error = ERR_NO_TIMER)]);
  313. goto bail;
  314. }
  315. // === System Notifications ===
  316. // 10. Acquire a handle for the system notification queue
  317. if(!(state->notify = furi_record_open(RECORD_NOTIFICATION))) {
  318. ERROR(wii_errs[(error = ERR_NO_NOTIFY)]);
  319. goto bail;
  320. }
  321. patBacklight(state); // Turn on the backlight [qv. remote FAP launch]
  322. INFO("INITIALISED");
  323. // ==================== Main event loop ====================
  324. if(state->run) do {
  325. //bool redraw = false;
  326. FuriStatus status = FuriStatusErrorTimeout;
  327. // Wait for a message
  328. // while ((status = furi_message_queue_get(queue, &msg, tmo)) == FuriStatusErrorTimeout) ;
  329. status = furi_message_queue_get(queue, &msg, tmo);
  330. // Clear splash screen
  331. if((state->scene == SCENE_SPLASH) &&
  332. (state->scenePrev == SCENE_NONE) && // Initial splash
  333. ((status == FuriStatusErrorTimeout) || // timeout
  334. ((msg.id == EVID_KEY) && (msg.input.type == InputTypeShort))) // or <any> key-short
  335. ) {
  336. tmo = 60 * 1000; // increase message-wait timeout to 60secs
  337. timerEn(state, true); // start scanning the i2c bus
  338. status = FuriStatusOk; // pass status check
  339. msg.id = EVID_NONE; // valid msg ID
  340. sceneSet(state, SCENE_WAIT); // move to wait screen
  341. }
  342. // Check for queue errors
  343. if(status != FuriStatusOk) {
  344. switch(status) {
  345. case FuriStatusErrorTimeout:
  346. DEBUG(wii_errs[DEBUG_QUEUE_TIMEOUT]);
  347. continue;
  348. case FuriStatusError:
  349. ERROR(wii_errs[(error = ERR_QUEUE_RTOS)]);
  350. goto bail;
  351. case FuriStatusErrorResource:
  352. ERROR(wii_errs[(error = ERR_QUEUE_RESOURCE)]);
  353. goto bail;
  354. case FuriStatusErrorParameter:
  355. ERROR(wii_errs[(error = ERR_QUEUE_BADPRM)]);
  356. goto bail;
  357. case FuriStatusErrorNoMemory:
  358. ERROR(wii_errs[(error = ERR_QUEUE_NOMEM)]);
  359. goto bail;
  360. case FuriStatusErrorISR:
  361. ERROR(wii_errs[(error = ERR_QUEUE_ISR)]);
  362. goto bail;
  363. default:
  364. ERROR(wii_errs[(error = ERR_QUEUE_UNK)]);
  365. goto bail;
  366. }
  367. }
  368. // Read successful
  369. // *** Try to lock the plugin state variables ***
  370. if(furi_mutex_acquire(state->mutex, FuriWaitForever) != FuriStatusOk) {
  371. ERROR(wii_errs[(error = ERR_MUTEX_BLOCK)]);
  372. goto bail;
  373. }
  374. // *** Handle events ***
  375. switch(msg.id) {
  376. //---------------------------------------------
  377. case EVID_TICK: // Timer events
  378. //! I would prefer to have ecPoll() called by cbTimer()
  379. //! ...but how does cbTimer() get the required access to the state variables? Namely: 'state->ec'
  380. //! So, for now, the timer pushes a message to call ecPoll()
  381. //! which, in turn, will push WIIEC event meesages! <facepalm>
  382. ecPoll(&state->ec, queue);
  383. break;
  384. //---------------------------------------------
  385. case EVID_WIIEC: // WiiMote Perhipheral
  386. evWiiEC(&msg, state);
  387. break;
  388. //---------------------------------------------
  389. case EVID_KEY: // Key events
  390. patBacklight(state);
  391. evKey(&msg, state);
  392. break;
  393. //---------------------------------------------
  394. case EVID_NONE:
  395. break;
  396. //---------------------------------------------
  397. default: // Unknown event
  398. WARN("Unknown message.ID [%d]", msg.id);
  399. break;
  400. }
  401. // *** Try to release the plugin state variables ***
  402. if(furi_mutex_release(state->mutex) != FuriStatusOk) {
  403. ERROR(wii_errs[(error = ERR_MUTEX_RELEASE)]);
  404. goto bail;
  405. }
  406. // *** Update the GUI screen via the viewport ***
  407. view_port_update(vpp);
  408. } while(state->run);
  409. // ===== Game Over =====
  410. INFO("USER EXIT");
  411. bail:
  412. // 10. Release system notification queue
  413. if(state && state->notify) {
  414. furi_record_close(RECORD_NOTIFICATION);
  415. state->notify = NULL;
  416. }
  417. // 9. Stop the timer
  418. if(state && state->timer) {
  419. (void)furi_timer_stop(state->timer);
  420. furi_timer_free(state->timer);
  421. state->timer = NULL;
  422. state->timerEn = false;
  423. }
  424. // 8. Detach the viewport
  425. gui_remove_view_port(gui, vpp);
  426. // 7. No need to unreqgister the callbacks
  427. // ...they will go when the viewport is destroyed
  428. // 6. Destroy the viewport
  429. if(vpp) {
  430. view_port_enabled_set(vpp, false);
  431. view_port_free(vpp);
  432. vpp = NULL;
  433. }
  434. // 5. Free the mutex
  435. if(state && state->mutex) {
  436. furi_mutex_free(state->mutex);
  437. state->mutex = NULL;
  438. }
  439. // 4. Free up state pointer(s)
  440. // none
  441. // 3. Free the plugin state variables
  442. if(state) {
  443. free(state);
  444. state = NULL;
  445. }
  446. // 2. Close the GUI
  447. furi_record_close("gui");
  448. // 1. Destroy the message queue
  449. if(queue) {
  450. furi_message_queue_free(queue);
  451. queue = NULL;
  452. }
  453. INFO("CLEAN EXIT ... Exit code: %d", error);
  454. LEAVE;
  455. return (int32_t)error;
  456. }