wii_anal.c 18 KB

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