wii_anal.c 18 KB

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