tama_p1.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. #include <furi.h>
  2. #include <gui/gui.h>
  3. #include <input/input.h>
  4. #include <storage/storage.h>
  5. #include <stdlib.h>
  6. #include <stm32wbxx_ll_tim.h>
  7. #include "tamalib/tamalib.h"
  8. #include "tama.h"
  9. #include "compiled/assets_icons.h"
  10. TamaApp* g_ctx;
  11. FuriMutex* g_state_mutex;
  12. uint8_t layout_mode = 0; // 3: portrait => 4: portrait <=
  13. // 0: landscape (small) 1: landscape (big) 2: landscape (full)
  14. bool in_menu = false;
  15. uint8_t speed = 1;
  16. const uint8_t speed_options[] = {1, 2, 4};
  17. const uint8_t min_speed = 1;
  18. const uint8_t max_speed = 4;
  19. const uint8_t speed_options_size = 3;
  20. // = sizeof(speed_options) / sizeof(speed_options[0]);
  21. uint8_t menu_cursor = 3; // 0: layout mode; 1: speed; 2: A+C;
  22. const uint8_t menu_items = 4; // 3: Close menu, Save & Exit
  23. uint8_t sub_menu_buttons = 0;
  24. uint8_t sub_menu_last = 0;
  25. static const Icon* icons_list[] = {
  26. &I_icon_0,
  27. &I_icon_1,
  28. &I_icon_2,
  29. &I_icon_3,
  30. &I_icon_4,
  31. &I_icon_5,
  32. &I_icon_6,
  33. &I_icon_7,
  34. };
  35. static InputKey m = InputKeyUp;
  36. static InputKey a = InputKeyLeft;
  37. static InputKey b = InputKeyDown;
  38. static InputKey c = InputKeyRight;
  39. static void speed_up() {
  40. switch(speed) {
  41. case max_speed:
  42. speed = speed_options[0];
  43. break;
  44. default:
  45. for(uint8_t i = 0; i < speed_options_size - 1; i++) {
  46. if(speed == speed_options[i]) {
  47. speed = speed_options[i + 1];
  48. break;
  49. }
  50. }
  51. break;
  52. }
  53. tamalib_set_speed(speed);
  54. }
  55. static void speed_down() {
  56. switch(speed) {
  57. case min_speed:
  58. speed = speed_options[speed_options_size - 1];
  59. break;
  60. default:
  61. for(uint8_t i = speed_options_size - 1; i > 0; i--) {
  62. if(speed == speed_options[i]) {
  63. speed = speed_options[i - 1];
  64. break;
  65. }
  66. }
  67. break;
  68. }
  69. tamalib_set_speed(speed);
  70. }
  71. // static void draw_landscape(Canvas* const canvas, void* cb_ctx)
  72. static void draw_landscape(Canvas* const canvas, uint8_t scale) {
  73. // FURI_LOG_D(TAG, "Drawing frame");
  74. // Calculate positioning
  75. uint16_t canv_width = canvas_width(canvas);
  76. uint16_t canv_height = canvas_height(canvas);
  77. uint16_t lcd_matrix_scaled_width = 32 * scale;
  78. uint16_t lcd_matrix_scaled_height = 16 * scale;
  79. uint16_t lcd_matrix_top = (canv_height - lcd_matrix_scaled_height) / 2; // 0
  80. uint16_t lcd_matrix_left = (canv_width - lcd_matrix_scaled_width) / 2;
  81. // uint16_t lcd_icon_upper_top = lcd_matrix_top - TAMA_LCD_ICON_SIZE - TAMA_LCD_ICON_MARGIN;
  82. // uint16_t lcd_icon_lower_top = lcd_matrix_top + lcd_matrix_scaled_height + TAMA_LCD_ICON_MARGIN;
  83. uint16_t lcd_icon_upper_left = lcd_matrix_left;
  84. uint16_t lcd_icon_lower_left = lcd_matrix_left;
  85. uint16_t lcd_icon_spacing_horiz =
  86. (lcd_matrix_scaled_width - (4 * TAMA_LCD_ICON_SIZE)) / 3 + TAMA_LCD_ICON_SIZE;
  87. uint16_t y = lcd_matrix_top;
  88. for(uint8_t row = 0; row < 16; ++row) {
  89. uint16_t x = lcd_matrix_left;
  90. uint32_t row_pixels = g_ctx->framebuffer[row];
  91. for(uint8_t col = 0; col < 32; ++col) {
  92. if(row_pixels & 1) {
  93. canvas_draw_box(canvas, x, y, scale, scale);
  94. }
  95. x += scale;
  96. row_pixels >>= 1;
  97. }
  98. y += scale;
  99. }
  100. // Start drawing icons
  101. uint8_t lcd_icons = g_ctx->icons;
  102. // Draw top icons
  103. y = 0;
  104. uint16_t x_ic = lcd_icon_upper_left;
  105. for(uint8_t i = 0; i < 4; ++i) {
  106. if(lcd_icons & 1) {
  107. canvas_draw_icon(canvas, x_ic, y, icons_list[i]);
  108. }
  109. // x_ic += TAMA_LCD_ICON_SIZE + 4;
  110. // if(scale == 3) {
  111. // y += 16;
  112. // } else {
  113. x_ic += lcd_icon_spacing_horiz;
  114. // }
  115. lcd_icons >>= 1;
  116. }
  117. // Draw bottom icons
  118. y = 64 - TAMA_LCD_ICON_SIZE;
  119. // if(scale == 3) {
  120. // y = 0;
  121. // x_ic = 128 - TAMA_LCD_ICON_SIZE;
  122. // x_ic = 0;
  123. // } else {
  124. // y = 64 - TAMA_LCD_ICON_SIZE;
  125. x_ic = lcd_icon_lower_left;
  126. // }
  127. for(uint8_t i = 4; i < 8; ++i) {
  128. // canvas_draw_frame(canvas, x_ic, y, TAMA_LCD_ICON_SIZE, TAMA_LCD_ICON_SIZE);
  129. if(lcd_icons & 1) {
  130. canvas_draw_icon(canvas, x_ic, y, icons_list[i]);
  131. }
  132. // if(scale == 3) {
  133. // y += 16;
  134. // } else {
  135. x_ic += lcd_icon_spacing_horiz;
  136. // }
  137. lcd_icons >>= 1;
  138. }
  139. }
  140. // static void draw_portrait_right(Canvas* const canvas, void* cb_ctx)
  141. static void draw_portrait_right(Canvas* const canvas, uint8_t scale) {
  142. // FURI_LOG_D(TAG, "Drawing frame");
  143. // Calculate positioning
  144. // uint16_t canv_width = canvas_width(canvas);
  145. uint16_t canv_height = canvas_height(canvas);
  146. uint16_t lcd_matrix_scaled_width = 32 * scale;
  147. uint16_t lcd_matrix_scaled_height = 16 * scale;
  148. // uint16_t lcd_matrix_top = 0;
  149. uint16_t lcd_matrix_top = (canv_height - lcd_matrix_scaled_height) / 2;
  150. // uint16_t lcd_matrix_left = (canv_width - lcd_matrix_scaled_width) / 2;
  151. uint16_t lcd_matrix_left = 64 - TAMA_LCD_ICON_SIZE;
  152. uint16_t lcd_icon_upper_left = lcd_matrix_left;
  153. uint16_t lcd_icon_lower_left = lcd_matrix_left;
  154. uint16_t lcd_icon_spacing_horiz =
  155. (lcd_matrix_scaled_width - (4 * TAMA_LCD_ICON_SIZE)) / 3 + TAMA_LCD_ICON_SIZE;
  156. uint16_t y = lcd_matrix_top; // 64
  157. for(uint8_t row = 0; row < 16; ++row) {
  158. uint16_t x = 128; // lcd_matrix_left
  159. uint32_t row_pixels = g_ctx->framebuffer[row];
  160. for(uint8_t col = 0; col < 32; ++col) {
  161. if(row_pixels & 1) {
  162. canvas_draw_box(canvas, y + 32, x - 66, scale, scale);
  163. }
  164. x -= scale;
  165. row_pixels >>= 1;
  166. }
  167. y += scale;
  168. }
  169. // Start drawing icons
  170. uint8_t lcd_icons = g_ctx->icons;
  171. // Draw top icons
  172. // y = lcd_icon_upper_top;
  173. y = 30;
  174. // y = 64 - TAMA_LCD_ICON_SIZE;
  175. uint16_t x_ic = lcd_icon_upper_left;
  176. // uint16_t x_ic = 64 - TAMA_LCD_ICON_SIZE;
  177. for(uint8_t i = 0; i < 4; ++i) {
  178. if(lcd_icons & 1) {
  179. canvas_draw_icon(canvas, y, x_ic, icons_list[i]);
  180. }
  181. x_ic -= lcd_icon_spacing_horiz; // TAMA_LCD_ICON_SIZE + 4;
  182. lcd_icons >>= 1;
  183. }
  184. // Draw bottom icons
  185. y = 84; // lcd_icon_lower_top
  186. x_ic = lcd_icon_lower_left; // 64 - TAMA_LCD_ICON_SIZE
  187. for(uint8_t i = 4; i < 8; ++i) {
  188. // canvas_draw_frame(canvas, x_ic, y, TAMA_LCD_ICON_SIZE, TAMA_LCD_ICON_SIZE);
  189. if(lcd_icons & 1) {
  190. canvas_draw_icon(canvas, y, x_ic, icons_list[i]);
  191. }
  192. x_ic -= lcd_icon_spacing_horiz;
  193. lcd_icons >>= 1;
  194. }
  195. }
  196. static void draw_portrait_left(Canvas* const canvas, uint8_t scale) {
  197. // FURI_LOG_D(TAG, "Drawing frame");
  198. // Calculate positioning
  199. // uint16_t canv_width = canvas_width(canvas);
  200. // uint16_t canv_height = canvas_height(canvas);
  201. uint16_t lcd_matrix_scaled_width = 32 * scale;
  202. // uint16_t lcd_matrix_scaled_height = 16 * scale;
  203. // uint16_t lcd_matrix_top = 0;
  204. // uint16_t lcd_matrix_top = (canv_height - lcd_matrix_scaled_height) / 2;
  205. // uint16_t lcd_matrix_left = (canv_width - lcd_matrix_scaled_width) / 2;
  206. uint16_t lcd_matrix_left = 0;
  207. uint16_t lcd_icon_upper_left = lcd_matrix_left;
  208. uint16_t lcd_icon_lower_left = lcd_matrix_left;
  209. uint16_t lcd_icon_spacing_horiz =
  210. (lcd_matrix_scaled_width - (4 * TAMA_LCD_ICON_SIZE)) / 3 + TAMA_LCD_ICON_SIZE;
  211. // uint16_t y = 64 + lcd_matrix_top;
  212. uint16_t y = 64;
  213. for(uint8_t row = 0; row < 16; ++row) {
  214. uint16_t x = 0; // lcd_matrix_left
  215. uint32_t row_pixels = g_ctx->framebuffer[row];
  216. for(uint8_t col = 0; col < 32; ++col) {
  217. if(row_pixels & 1) {
  218. canvas_draw_box(canvas, y, x, scale, scale);
  219. }
  220. x += scale;
  221. row_pixels >>= 1;
  222. }
  223. y -= scale;
  224. }
  225. // Start drawing icons
  226. uint8_t lcd_icons = g_ctx->icons;
  227. // Draw top icons
  228. // y = lcd_icon_upper_top;
  229. y = 70;
  230. // y = 64 - TAMA_LCD_ICON_SIZE;
  231. uint16_t x_ic = lcd_icon_upper_left;
  232. // uint16_t x_ic = 64 - TAMA_LCD_ICON_SIZE;
  233. for(uint8_t i = 0; i < 4; ++i) {
  234. if(lcd_icons & 1) {
  235. canvas_draw_icon(canvas, y, x_ic, icons_list[i]);
  236. }
  237. x_ic += lcd_icon_spacing_horiz; // TAMA_LCD_ICON_SIZE + 4;
  238. lcd_icons >>= 1;
  239. }
  240. // Draw bottom icons
  241. y = 16; // lcd_icon_lower_top
  242. x_ic = lcd_icon_lower_left; // 64 - TAMA_LCD_ICON_SIZE
  243. for(uint8_t i = 4; i < 8; ++i) {
  244. // canvas_draw_frame(canvas, x_ic, y, TAMA_LCD_ICON_SIZE, TAMA_LCD_ICON_SIZE);
  245. if(lcd_icons & 1) {
  246. canvas_draw_icon(canvas, y, x_ic, icons_list[i]);
  247. }
  248. x_ic += lcd_icon_spacing_horiz;
  249. lcd_icons >>= 1;
  250. }
  251. }
  252. // static void draw_mini(Canvas* const canvas, uint16_t inX, uint16_t inY)
  253. static void draw_mini(Canvas* const canvas) {
  254. // Calculate positioning
  255. // uint16_t y = inY;
  256. const uint16_t y = 34;
  257. const uint16_t x = 84;
  258. uint16_t y1 = y;
  259. for(uint8_t row = 0; row < 16; ++row) {
  260. // uint16_t x = inX;
  261. uint16_t x1 = x;
  262. uint32_t row_pixels = g_ctx->framebuffer[row];
  263. for(uint8_t col = 0; col < 32; ++col) {
  264. if(row_pixels & 1) {
  265. canvas_draw_dot(canvas, x1, y1);
  266. }
  267. x1 += 1;
  268. row_pixels >>= 1;
  269. }
  270. y1 += 1;
  271. }
  272. // Start drawing icons
  273. uint8_t lcd_icons = g_ctx->icons;
  274. // Draw top icons
  275. uint16_t y2 = y - 2;
  276. uint16_t x_ic = x;
  277. for(uint8_t i = 0; i < 4; ++i) {
  278. if(lcd_icons & 1) {
  279. // canvas_draw_icon(canvas, x_ic, y, icons_list[i]);
  280. canvas_draw_line(canvas, x_ic, y2, x_ic + 6, y2);
  281. }
  282. x_ic += 8;
  283. lcd_icons >>= 1;
  284. }
  285. // Draw bottom icons
  286. y2 = y + 17;
  287. x_ic = x;
  288. for(uint8_t i = 4; i < 8; ++i) {
  289. // canvas_draw_frame(canvas, x_ic, y, TAMA_LCD_ICON_SIZE, TAMA_LCD_ICON_SIZE);
  290. if(lcd_icons & 1) {
  291. // canvas_draw_icon(canvas, x_ic, y, icons_list[i]);
  292. canvas_draw_line(canvas, x_ic, y2, x_ic + 6, y2);
  293. }
  294. x_ic += 8;
  295. lcd_icons >>= 1;
  296. }
  297. }
  298. static void draw_menu(Canvas* const canvas) {
  299. canvas_draw_frame(canvas, 0, 0, 128, 64);
  300. canvas_draw_str_aligned(canvas, 64, 6, AlignCenter, AlignCenter, "Menu");
  301. canvas_draw_line(canvas, 0, 10, 128, 10);
  302. draw_mini(canvas);
  303. // draw_mini(canvas, 34, 84);
  304. switch(menu_cursor) {
  305. case 0:
  306. canvas_draw_triangle(canvas, 4, 16, 6, 6, CanvasDirectionLeftToRight);
  307. break;
  308. case 1:
  309. canvas_draw_triangle(canvas, 4, 26, 6, 6, CanvasDirectionLeftToRight);
  310. break;
  311. case 2:
  312. switch(sub_menu_buttons) {
  313. case 0:
  314. canvas_draw_triangle(canvas, 4, 36, 6, 6, CanvasDirectionLeftToRight);
  315. break;
  316. case 1:
  317. canvas_draw_triangle(canvas, 47, 44, 6, 4, CanvasDirectionBottomToTop);
  318. break;
  319. case 2:
  320. canvas_draw_triangle(canvas, 57, 44, 6, 4, CanvasDirectionBottomToTop);
  321. break;
  322. case 3:
  323. canvas_draw_triangle(canvas, 67, 44, 6, 4, CanvasDirectionBottomToTop);
  324. break;
  325. default:
  326. break;
  327. }
  328. break;
  329. case menu_items - 1:
  330. switch(sub_menu_last) {
  331. case 0:
  332. canvas_draw_triangle(canvas, 4, 56, 6, 6, CanvasDirectionLeftToRight);
  333. break;
  334. case 1:
  335. canvas_draw_triangle(canvas, 36, 56, 6, 6, CanvasDirectionLeftToRight);
  336. break;
  337. case 2:
  338. canvas_draw_triangle(canvas, 67, 56, 6, 6, CanvasDirectionLeftToRight);
  339. break;
  340. default:
  341. break;
  342. }
  343. break;
  344. }
  345. switch(layout_mode) {
  346. case 0:
  347. canvas_draw_str(canvas, 12, 20, "Layout: Landscape (small)");
  348. break;
  349. case 1:
  350. canvas_draw_str(canvas, 12, 20, "Layout: Landscape (big)");
  351. break;
  352. case 2:
  353. canvas_draw_str(canvas, 12, 20, "Layout: Landscape (full)");
  354. break;
  355. case 3:
  356. canvas_draw_str(canvas, 12, 20, "Layout: Portrait =>");
  357. break;
  358. case 4:
  359. canvas_draw_str(canvas, 12, 20, "Layout: Portrait <=");
  360. break;
  361. default:
  362. canvas_draw_str(canvas, 12, 20, "Layout: ???");
  363. break;
  364. }
  365. switch(speed) { // match with speed_options
  366. // case 0: // freeze menu too
  367. // canvas_draw_str(canvas, 12, 30, "Speed: 0x");
  368. // break;
  369. case 1:
  370. canvas_draw_str(canvas, 12, 30, "Speed: 1x");
  371. break;
  372. case 2:
  373. canvas_draw_str(canvas, 12, 30, "Speed: 2x");
  374. break;
  375. case 4:
  376. canvas_draw_str(canvas, 12, 30, "Speed: 4x (max)");
  377. break;
  378. default:
  379. canvas_draw_str(canvas, 12, 30, "Speed ??x");
  380. break;
  381. }
  382. canvas_draw_str(canvas, 12, 40, "A+C");
  383. canvas_draw_str(canvas, 45, 40, "A");
  384. canvas_draw_str(canvas, 55, 40, "B");
  385. canvas_draw_str(canvas, 65, 40, "C");
  386. canvas_draw_str(canvas, 12, 60, "Close");
  387. canvas_draw_str(canvas, 44, 60, "Save");
  388. canvas_draw_str(canvas, 75, 60, "Save & Exit");
  389. }
  390. static void tama_p1_draw_callback(Canvas* const canvas, void* cb_ctx) {
  391. furi_assert(cb_ctx);
  392. FuriMutex* const mutex = cb_ctx;
  393. if(furi_mutex_acquire(mutex, 25) != FuriStatusOk) return;
  394. if(g_ctx->rom == NULL) {
  395. canvas_set_font(canvas, FontPrimary);
  396. canvas_draw_str(canvas, 30, 30, "No ROM");
  397. } else if(g_ctx->halted) {
  398. canvas_set_font(canvas, FontPrimary);
  399. canvas_draw_str(canvas, 30, 30, "Halted");
  400. } else {
  401. if(in_menu) {
  402. // switch(layout_mode)
  403. // draw_menu_landscape(canvas);
  404. draw_menu(canvas);
  405. } else {
  406. switch(layout_mode) {
  407. case 0:
  408. draw_landscape(canvas, TAMA_SCREEN_SCALE_FACTOR); // 2
  409. break;
  410. case 1:
  411. draw_landscape(canvas, 3);
  412. break;
  413. case 2:
  414. draw_landscape(canvas, 4);
  415. break;
  416. case 3:
  417. draw_portrait_right(canvas, TAMA_SCREEN_SCALE_FACTOR);
  418. break;
  419. case 4:
  420. draw_portrait_left(canvas, TAMA_SCREEN_SCALE_FACTOR);
  421. break;
  422. default:
  423. break;
  424. }
  425. }
  426. }
  427. furi_mutex_release(mutex);
  428. }
  429. static void tama_p1_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
  430. furi_assert(event_queue);
  431. TamaEvent event = {.type = EventTypeInput, .input = *input_event};
  432. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  433. }
  434. static void tama_p1_update_timer_callback(FuriMessageQueue* event_queue) {
  435. furi_assert(event_queue);
  436. TamaEvent event = {.type = EventTypeTick};
  437. furi_message_queue_put(event_queue, &event, 0);
  438. }
  439. static void tama_p1_load_state() {
  440. state_t* state;
  441. uint8_t buf[4];
  442. bool error = false;
  443. state = tamalib_get_state();
  444. Storage* storage = furi_record_open(RECORD_STORAGE);
  445. File* file = storage_file_alloc(storage);
  446. if(storage_file_open(file, TAMA_SAVE_PATH, FSAM_READ, FSOM_OPEN_EXISTING)) {
  447. storage_file_read(file, &buf, 4);
  448. if(buf[0] != (uint8_t)STATE_FILE_MAGIC[0] || buf[1] != (uint8_t)STATE_FILE_MAGIC[1] ||
  449. buf[2] != (uint8_t)STATE_FILE_MAGIC[2] || buf[3] != (uint8_t)STATE_FILE_MAGIC[3]) {
  450. FURI_LOG_E(TAG, "FATAL: Wrong state file magic in \"%s\" !\n", TAMA_SAVE_PATH);
  451. error = true;
  452. }
  453. storage_file_read(file, &buf, 1);
  454. if(buf[0] != STATE_FILE_VERSION) {
  455. FURI_LOG_E(TAG, "FATAL: Unsupported version");
  456. error = true;
  457. }
  458. if(!error) {
  459. FURI_LOG_D(TAG, "Reading save.bin");
  460. storage_file_read(file, &buf, 2);
  461. *(state->pc) = buf[0] | ((buf[1] & 0x1F) << 8);
  462. storage_file_read(file, &buf, 2);
  463. *(state->x) = buf[0] | ((buf[1] & 0xF) << 8);
  464. storage_file_read(file, &buf, 2);
  465. *(state->y) = buf[0] | ((buf[1] & 0xF) << 8);
  466. storage_file_read(file, &buf, 1);
  467. *(state->a) = buf[0] & 0xF;
  468. storage_file_read(file, &buf, 1);
  469. *(state->b) = buf[0] & 0xF;
  470. storage_file_read(file, &buf, 1);
  471. *(state->np) = buf[0] & 0x1F;
  472. storage_file_read(file, &buf, 1);
  473. *(state->sp) = buf[0];
  474. storage_file_read(file, &buf, 1);
  475. *(state->flags) = buf[0] & 0xF;
  476. storage_file_read(file, &buf, 4);
  477. *(state->tick_counter) = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
  478. storage_file_read(file, &buf, 4);
  479. *(state->clk_timer_timestamp) = buf[0] | (buf[1] << 8) | (buf[2] << 16) |
  480. (buf[3] << 24);
  481. storage_file_read(file, &buf, 4);
  482. *(state->prog_timer_timestamp) = buf[0] | (buf[1] << 8) | (buf[2] << 16) |
  483. (buf[3] << 24);
  484. storage_file_read(file, &buf, 1);
  485. *(state->prog_timer_enabled) = buf[0] & 0x1;
  486. storage_file_read(file, &buf, 1);
  487. *(state->prog_timer_data) = buf[0];
  488. storage_file_read(file, &buf, 1);
  489. *(state->prog_timer_rld) = buf[0];
  490. storage_file_read(file, &buf, 4);
  491. *(state->call_depth) = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
  492. FURI_LOG_D(TAG, "Restoring Interupts");
  493. for(uint32_t i = 0; i < INT_SLOT_NUM; i++) {
  494. storage_file_read(file, &buf, 1);
  495. state->interrupts[i].factor_flag_reg = buf[0] & 0xF;
  496. storage_file_read(file, &buf, 1);
  497. state->interrupts[i].mask_reg = buf[0] & 0xF;
  498. storage_file_read(file, &buf, 1);
  499. state->interrupts[i].triggered = buf[0] & 0x1;
  500. }
  501. /* First 640 half bytes correspond to the RAM */
  502. FURI_LOG_D(TAG, "Restoring RAM");
  503. for(uint32_t i = 0; i < MEM_RAM_SIZE; i++) {
  504. storage_file_read(file, &buf, 1);
  505. SET_RAM_MEMORY(state->memory, i + MEM_RAM_ADDR, buf[0] & 0xF);
  506. }
  507. /* I/Os are from 0xF00 to 0xF7F */
  508. FURI_LOG_D(TAG, "Restoring I/O");
  509. for(uint32_t i = 0; i < MEM_IO_SIZE; i++) {
  510. storage_file_read(file, &buf, 1);
  511. SET_IO_MEMORY(state->memory, i + MEM_IO_ADDR, buf[0] & 0xF);
  512. }
  513. FURI_LOG_D(TAG, "Refreshing Hardware");
  514. tamalib_refresh_hw();
  515. }
  516. }
  517. storage_file_close(file);
  518. storage_file_free(file);
  519. furi_record_close(RECORD_STORAGE);
  520. }
  521. static void tama_p1_save_state() {
  522. // Saving state
  523. FURI_LOG_D(TAG, "Saving Gamestate");
  524. uint8_t buf[4];
  525. state_t* state;
  526. uint32_t offset = 0;
  527. state = tamalib_get_state();
  528. Storage* storage = furi_record_open(RECORD_STORAGE);
  529. File* file = storage_file_alloc(storage);
  530. if(storage_file_open(file, TAMA_SAVE_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  531. buf[0] = (uint8_t)STATE_FILE_MAGIC[0];
  532. buf[1] = (uint8_t)STATE_FILE_MAGIC[1];
  533. buf[2] = (uint8_t)STATE_FILE_MAGIC[2];
  534. buf[3] = (uint8_t)STATE_FILE_MAGIC[3];
  535. offset += storage_file_write(file, &buf, sizeof(buf));
  536. buf[0] = STATE_FILE_VERSION & 0xFF;
  537. offset += storage_file_write(file, &buf, 1);
  538. buf[0] = *(state->pc) & 0xFF;
  539. buf[1] = (*(state->pc) >> 8) & 0x1F;
  540. offset += storage_file_write(file, &buf, 2);
  541. buf[0] = *(state->x) & 0xFF;
  542. buf[1] = (*(state->x) >> 8) & 0xF;
  543. offset += storage_file_write(file, &buf, 2);
  544. buf[0] = *(state->y) & 0xFF;
  545. buf[1] = (*(state->y) >> 8) & 0xF;
  546. offset += storage_file_write(file, &buf, 2);
  547. buf[0] = *(state->a) & 0xF;
  548. offset += storage_file_write(file, &buf, 1);
  549. buf[0] = *(state->b) & 0xF;
  550. offset += storage_file_write(file, &buf, 1);
  551. buf[0] = *(state->np) & 0x1F;
  552. offset += storage_file_write(file, &buf, 1);
  553. buf[0] = *(state->sp) & 0xFF;
  554. offset += storage_file_write(file, &buf, 1);
  555. buf[0] = *(state->flags) & 0xF;
  556. offset += storage_file_write(file, &buf, 1);
  557. buf[0] = *(state->tick_counter) & 0xFF;
  558. buf[1] = (*(state->tick_counter) >> 8) & 0xFF;
  559. buf[2] = (*(state->tick_counter) >> 16) & 0xFF;
  560. buf[3] = (*(state->tick_counter) >> 24) & 0xFF;
  561. offset += storage_file_write(file, &buf, sizeof(buf));
  562. buf[0] = *(state->clk_timer_timestamp) & 0xFF;
  563. buf[1] = (*(state->clk_timer_timestamp) >> 8) & 0xFF;
  564. buf[2] = (*(state->clk_timer_timestamp) >> 16) & 0xFF;
  565. buf[3] = (*(state->clk_timer_timestamp) >> 24) & 0xFF;
  566. offset += storage_file_write(file, &buf, sizeof(buf));
  567. buf[0] = *(state->prog_timer_timestamp) & 0xFF;
  568. buf[1] = (*(state->prog_timer_timestamp) >> 8) & 0xFF;
  569. buf[2] = (*(state->prog_timer_timestamp) >> 16) & 0xFF;
  570. buf[3] = (*(state->prog_timer_timestamp) >> 24) & 0xFF;
  571. offset += storage_file_write(file, &buf, sizeof(buf));
  572. buf[0] = *(state->prog_timer_enabled) & 0x1;
  573. offset += storage_file_write(file, &buf, 1);
  574. buf[0] = *(state->prog_timer_data) & 0xFF;
  575. offset += storage_file_write(file, &buf, 1);
  576. buf[0] = *(state->prog_timer_rld) & 0xFF;
  577. offset += storage_file_write(file, &buf, 1);
  578. buf[0] = *(state->call_depth) & 0xFF;
  579. buf[1] = (*(state->call_depth) >> 8) & 0xFF;
  580. buf[2] = (*(state->call_depth) >> 16) & 0xFF;
  581. buf[3] = (*(state->call_depth) >> 24) & 0xFF;
  582. offset += storage_file_write(file, &buf, sizeof(buf));
  583. for(uint32_t i = 0; i < INT_SLOT_NUM; i++) {
  584. buf[0] = state->interrupts[i].factor_flag_reg & 0xF;
  585. offset += storage_file_write(file, &buf, 1);
  586. buf[0] = state->interrupts[i].mask_reg & 0xF;
  587. offset += storage_file_write(file, &buf, 1);
  588. buf[0] = state->interrupts[i].triggered & 0x1;
  589. offset += storage_file_write(file, &buf, 1);
  590. }
  591. /* First 640 half bytes correspond to the RAM */
  592. for(uint32_t i = 0; i < MEM_RAM_SIZE; i++) {
  593. buf[0] = GET_RAM_MEMORY(state->memory, i + MEM_RAM_ADDR) & 0xF;
  594. offset += storage_file_write(file, &buf, 1);
  595. }
  596. /* I/Os are from 0xF00 to 0xF7F */
  597. for(uint32_t i = 0; i < MEM_IO_SIZE; i++) {
  598. buf[0] = GET_IO_MEMORY(state->memory, i + MEM_IO_ADDR) & 0xF;
  599. offset += storage_file_write(file, &buf, 1);
  600. }
  601. }
  602. storage_file_close(file);
  603. storage_file_free(file);
  604. furi_record_close(RECORD_STORAGE);
  605. FURI_LOG_D(TAG, "Finished Writing %lu", offset);
  606. }
  607. static int32_t tama_p1_worker(void* context) {
  608. bool running = true;
  609. FuriMutex* mutex = context;
  610. while(furi_mutex_acquire(mutex, FuriWaitForever) != FuriStatusOk) furi_delay_tick(1);
  611. cpu_sync_ref_timestamp();
  612. LL_TIM_EnableCounter(TIM2);
  613. tama_p1_load_state();
  614. while(running) {
  615. if(furi_thread_flags_get()) {
  616. running = false;
  617. } else {
  618. // FURI_LOG_D(TAG, "Stepping"); // enabling this cause blank screen somehow
  619. // for (int i = 0; i < 100; ++i)
  620. tamalib_step(); // tamalib_mainloop();
  621. }
  622. }
  623. LL_TIM_DisableCounter(TIM2);
  624. furi_mutex_release(mutex);
  625. return 0;
  626. }
  627. static void tama_p1_init(TamaApp* const ctx) {
  628. g_ctx = ctx;
  629. memset(ctx, 0, sizeof(TamaApp));
  630. tama_p1_hal_init(&ctx->hal);
  631. // Load ROM
  632. Storage* storage = furi_record_open(RECORD_STORAGE);
  633. FileInfo fi;
  634. if(storage_common_stat(storage, TAMA_ROM_PATH, &fi) == FSE_OK) {
  635. File* rom_file = storage_file_alloc(storage);
  636. if(storage_file_open(rom_file, TAMA_ROM_PATH, FSAM_READ, FSOM_OPEN_EXISTING)) {
  637. ctx->rom = malloc((size_t)fi.size);
  638. uint8_t* buf_ptr = ctx->rom;
  639. size_t read = 0;
  640. while(read < fi.size) {
  641. size_t to_read = fi.size - read;
  642. if(to_read > UINT16_MAX) to_read = UINT16_MAX;
  643. uint16_t now_read = storage_file_read(rom_file, buf_ptr, (uint16_t)to_read);
  644. read += now_read;
  645. buf_ptr += now_read;
  646. }
  647. // Reorder endianess of ROM
  648. for(size_t i = 0; i < fi.size; i += 2) {
  649. uint8_t b = ctx->rom[i];
  650. ctx->rom[i] = ctx->rom[i + 1];
  651. ctx->rom[i + 1] = b & 0xF;
  652. }
  653. }
  654. storage_file_close(rom_file);
  655. storage_file_free(rom_file);
  656. }
  657. furi_record_close(RECORD_STORAGE);
  658. if(ctx->rom != NULL) {
  659. // Init TIM2
  660. // 64KHz
  661. LL_TIM_InitTypeDef tim_init = {
  662. .Prescaler = 999,
  663. .CounterMode = LL_TIM_COUNTERMODE_UP,
  664. .Autoreload = 0xFFFFFFFF,
  665. };
  666. LL_TIM_Init(TIM2, &tim_init);
  667. LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL);
  668. LL_TIM_DisableCounter(TIM2);
  669. LL_TIM_SetCounter(TIM2, 0);
  670. // Init TamaLIB
  671. tamalib_register_hal(&ctx->hal);
  672. tamalib_init((u12_t*)ctx->rom, NULL, 64000);
  673. tamalib_set_speed(speed);
  674. // TODO: implement fast forwarding
  675. // ctx->fast_forward_done = true;
  676. // Start stepping thread
  677. ctx->thread = furi_thread_alloc();
  678. furi_thread_set_name(ctx->thread, "TamaLIB");
  679. furi_thread_set_stack_size(ctx->thread, 1024);
  680. furi_thread_set_callback(ctx->thread, tama_p1_worker);
  681. furi_thread_set_context(ctx->thread, g_state_mutex);
  682. furi_thread_start(ctx->thread);
  683. }
  684. }
  685. static void tama_p1_deinit(TamaApp* const ctx) {
  686. if(ctx->rom != NULL) {
  687. tamalib_release();
  688. furi_thread_free(ctx->thread);
  689. free(ctx->rom);
  690. }
  691. }
  692. int32_t tama_p1_app(void* p) {
  693. UNUSED(p);
  694. TamaApp* ctx = malloc(sizeof(TamaApp));
  695. g_state_mutex = furi_mutex_alloc(FuriMutexTypeRecursive);
  696. tama_p1_init(ctx);
  697. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(TamaEvent));
  698. ViewPort* view_port = view_port_alloc();
  699. view_port_draw_callback_set(view_port, tama_p1_draw_callback, g_state_mutex);
  700. view_port_input_callback_set(view_port, tama_p1_input_callback, event_queue);
  701. Gui* gui = furi_record_open(RECORD_GUI);
  702. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  703. FuriTimer* timer =
  704. furi_timer_alloc(tama_p1_update_timer_callback, FuriTimerTypePeriodic, event_queue);
  705. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 30);
  706. // in_menu = false;
  707. // menu_cursor = 2;
  708. for(bool running = true; running;) {
  709. TamaEvent event;
  710. FuriStatus event_status = furi_message_queue_get(event_queue, &event, FuriWaitForever);
  711. if(event_status == FuriStatusOk) {
  712. // Local override with acquired context
  713. if(furi_mutex_acquire(g_state_mutex, FuriWaitForever) != FuriStatusOk) continue;
  714. if(event.type == EventTypeTick) {
  715. // FURI_LOG_D(TAG, "EventTypeTick");
  716. view_port_update(view_port);
  717. } else if(event.type == EventTypeInput) {
  718. FURI_LOG_D(
  719. TAG,
  720. "EventTypeInput: %ld %d %d",
  721. event.input.sequence,
  722. event.input.key,
  723. event.input.type);
  724. // InputType input_type = event.input.type; // idk why this is a variable
  725. btn_state_t tama_btn_state = 0; // BTN_STATE_RELEASED is 0
  726. if(in_menu) {
  727. if(menu_cursor == 2 &&
  728. (event.input.key == InputKeyUp || event.input.key == InputKeyDown)) {
  729. tama_btn_state = BTN_STATE_RELEASED;
  730. }
  731. if(event.input.key == InputKeyBack) {
  732. tama_btn_state = BTN_STATE_RELEASED;
  733. in_menu = false;
  734. } else if(event.input.key == InputKeyUp && event.input.type == InputTypePress) {
  735. if(menu_cursor > 0) {
  736. menu_cursor -= 1;
  737. } else {
  738. menu_cursor = menu_items - 1;
  739. }
  740. if(menu_cursor >= menu_items - 2 && sub_menu_last > 0) {
  741. sub_menu_buttons = 1;
  742. sub_menu_last = 1;
  743. } else {
  744. sub_menu_buttons = 0;
  745. sub_menu_last = 0;
  746. }
  747. } else if(event.input.key == InputKeyDown && event.input.type == InputTypePress) {
  748. if(menu_cursor < menu_items - 1) {
  749. menu_cursor += 1;
  750. } else {
  751. menu_cursor = 0;
  752. }
  753. if(menu_cursor >= menu_items - 2 && sub_menu_buttons > 0) {
  754. sub_menu_buttons = 1;
  755. sub_menu_last = 1;
  756. // sub_menu_last = 2;
  757. } else {
  758. sub_menu_buttons = 0;
  759. sub_menu_last = 0;
  760. }
  761. } else if(event.input.key == InputKeyLeft && event.input.type == InputTypePress) {
  762. switch(menu_cursor) {
  763. case 0:
  764. switch(layout_mode) {
  765. case 0:
  766. layout_mode = 4;
  767. m = InputKeyRight;
  768. a = InputKeyUp;
  769. b = InputKeyLeft;
  770. c = InputKeyDown;
  771. break;
  772. case 1:
  773. layout_mode -= 1; // 0
  774. m = InputKeyUp;
  775. a = InputKeyLeft;
  776. b = InputKeyDown;
  777. c = InputKeyRight;
  778. break;
  779. case 2:
  780. layout_mode -= 1; // 1
  781. m = InputKeyUp;
  782. a = InputKeyLeft;
  783. b = InputKeyDown;
  784. c = InputKeyRight;
  785. break;
  786. case 3:
  787. layout_mode -= 1; // 2
  788. m = InputKeyUp;
  789. a = InputKeyLeft;
  790. b = InputKeyDown;
  791. c = InputKeyRight;
  792. break;
  793. case 4:
  794. layout_mode -= 1; // 3
  795. m = InputKeyLeft;
  796. a = InputKeyDown;
  797. b = InputKeyRight;
  798. c = InputKeyUp;
  799. break;
  800. }
  801. break;
  802. case 1:
  803. speed_down();
  804. break;
  805. case 2:
  806. tama_btn_state = BTN_STATE_RELEASED;
  807. switch(sub_menu_buttons) {
  808. case 0:
  809. sub_menu_buttons = 3;
  810. break;
  811. case 1:
  812. case 2:
  813. case 3:
  814. sub_menu_buttons -= 1;
  815. break;
  816. default:
  817. break;
  818. }
  819. break;
  820. case menu_items - 1:
  821. switch(sub_menu_last) {
  822. case 0:
  823. sub_menu_last = 2;
  824. break;
  825. case 1:
  826. case 2:
  827. sub_menu_last -= 1;
  828. break;
  829. default:
  830. break;
  831. }
  832. break;
  833. default:
  834. break;
  835. }
  836. } else if(event.input.key == InputKeyRight && event.input.type == InputTypePress) {
  837. switch(menu_cursor) {
  838. case 0:
  839. switch(layout_mode) {
  840. case 0:
  841. layout_mode += 1; // 1
  842. m = InputKeyUp;
  843. a = InputKeyLeft;
  844. b = InputKeyDown;
  845. c = InputKeyRight;
  846. break;
  847. case 1:
  848. layout_mode += 1; // 2
  849. m = InputKeyUp;
  850. a = InputKeyLeft;
  851. b = InputKeyDown;
  852. c = InputKeyRight;
  853. break;
  854. case 2:
  855. layout_mode += 1; // 3
  856. m = InputKeyLeft;
  857. a = InputKeyDown;
  858. b = InputKeyRight;
  859. c = InputKeyUp;
  860. break;
  861. case 3:
  862. layout_mode += 1; // 4
  863. m = InputKeyRight;
  864. a = InputKeyUp;
  865. b = InputKeyLeft;
  866. c = InputKeyDown;
  867. break;
  868. case 4:
  869. layout_mode = 0;
  870. m = InputKeyUp;
  871. a = InputKeyLeft;
  872. b = InputKeyDown;
  873. c = InputKeyRight;
  874. break;
  875. }
  876. break;
  877. case 1:
  878. speed_up();
  879. break;
  880. case 2:
  881. tama_btn_state = BTN_STATE_RELEASED;
  882. switch(sub_menu_buttons) {
  883. case 0:
  884. case 1:
  885. case 2:
  886. sub_menu_buttons += 1;
  887. break;
  888. case 3:
  889. sub_menu_buttons = 0;
  890. break;
  891. default:
  892. break;
  893. }
  894. break;
  895. case menu_items - 1:
  896. switch(sub_menu_last) {
  897. case 0:
  898. case 1:
  899. sub_menu_last += 1;
  900. break;
  901. case 2:
  902. sub_menu_last = 0;
  903. break;
  904. default:
  905. break;
  906. }
  907. break;
  908. default:
  909. break;
  910. }
  911. } else if(event.input.key == InputKeyOk) {
  912. switch(menu_cursor) {
  913. case 0:
  914. if(event.input.type == InputTypePress) {
  915. switch(layout_mode) {
  916. case 0:
  917. layout_mode += 1; // 1
  918. m = InputKeyUp;
  919. a = InputKeyLeft;
  920. b = InputKeyDown;
  921. c = InputKeyRight;
  922. break;
  923. case 1:
  924. layout_mode += 1; // 2
  925. m = InputKeyUp;
  926. a = InputKeyLeft;
  927. b = InputKeyDown;
  928. c = InputKeyRight;
  929. break;
  930. case 2:
  931. layout_mode += 1; // 3
  932. m = InputKeyLeft;
  933. a = InputKeyDown;
  934. b = InputKeyRight;
  935. c = InputKeyUp;
  936. break;
  937. case 3:
  938. layout_mode += 1; // 4
  939. m = InputKeyRight;
  940. a = InputKeyUp;
  941. b = InputKeyLeft;
  942. c = InputKeyDown;
  943. break;
  944. case 4:
  945. layout_mode = 0;
  946. m = InputKeyUp;
  947. a = InputKeyLeft;
  948. b = InputKeyDown;
  949. c = InputKeyRight;
  950. break;
  951. }
  952. }
  953. break;
  954. case 1:
  955. if(event.input.type == InputTypePress) {
  956. speed_up();
  957. }
  958. break;
  959. case 2:
  960. if(event.input.type == InputTypePress)
  961. tama_btn_state = BTN_STATE_PRESSED;
  962. else if(event.input.type == InputTypeRelease)
  963. tama_btn_state = BTN_STATE_RELEASED;
  964. switch(sub_menu_buttons) {
  965. case 0: // A+C
  966. tamalib_set_button(BTN_LEFT, tama_btn_state);
  967. tamalib_set_button(BTN_RIGHT, tama_btn_state);
  968. break;
  969. case 1: // A
  970. tamalib_set_button(BTN_LEFT, tama_btn_state);
  971. break;
  972. case 2: // B
  973. tamalib_set_button(BTN_MIDDLE, tama_btn_state);
  974. break;
  975. case 3: // C
  976. tamalib_set_button(BTN_RIGHT, tama_btn_state);
  977. break;
  978. }
  979. break;
  980. case menu_items - 1:
  981. default:
  982. switch(sub_menu_last) {
  983. case 0: // close menu
  984. in_menu = false;
  985. break;
  986. case 1: // Save
  987. if(speed != 1) {
  988. // uint8_t temp = speed;
  989. // speed = 1;
  990. tamalib_set_speed(1);
  991. tama_p1_save_state();
  992. // speed = temp;
  993. tamalib_set_speed(speed);
  994. } else {
  995. tama_p1_save_state();
  996. }
  997. // in_menu = false;
  998. break;
  999. case 2: // Save & Exit
  1000. if(speed != 1) {
  1001. // speed = 1;
  1002. // tamalib_set_speed(speed);
  1003. tamalib_set_speed(1);
  1004. }
  1005. furi_timer_stop(timer);
  1006. tama_p1_save_state();
  1007. running = false;
  1008. break;
  1009. default:
  1010. break;
  1011. }
  1012. break;
  1013. }
  1014. }
  1015. } else { // out of menu // TODO: clean up code -.-
  1016. if(event.input.key == InputKeyBack && event.input.type == InputTypeLong) {
  1017. if(speed != 1) {
  1018. // speed = 1;
  1019. // tamalib_set_speed(speed);
  1020. tamalib_set_speed(1);
  1021. }
  1022. furi_timer_stop(timer);
  1023. tama_p1_save_state();
  1024. running = false;
  1025. }
  1026. if(event.input.type == InputTypePress ||
  1027. event.input.type == InputTypeRelease) {
  1028. if(event.input.key != InputKeyBack && event.input.key != m) {
  1029. if(event.input.type == InputTypePress)
  1030. tama_btn_state = BTN_STATE_PRESSED;
  1031. else if(event.input.type == InputTypeRelease)
  1032. tama_btn_state = BTN_STATE_RELEASED;
  1033. } else {
  1034. tama_btn_state = BTN_STATE_RELEASED;
  1035. }
  1036. if(event.input.key == m) {
  1037. tama_btn_state = BTN_STATE_RELEASED;
  1038. in_menu = true;
  1039. } else if(event.input.key == a) {
  1040. tamalib_set_button(BTN_LEFT, tama_btn_state);
  1041. } else if(event.input.key == b) {
  1042. tamalib_set_button(BTN_MIDDLE, tama_btn_state);
  1043. } else if(event.input.key == c) {
  1044. tamalib_set_button(BTN_RIGHT, tama_btn_state);
  1045. } else if(event.input.key == InputKeyOk) {
  1046. tamalib_set_button(BTN_MIDDLE, tama_btn_state);
  1047. }
  1048. }
  1049. }
  1050. }
  1051. furi_mutex_release(g_state_mutex);
  1052. }
  1053. // else {
  1054. // // Timeout
  1055. // FURI_LOG_D(TAG, "Timed out");
  1056. // }
  1057. }
  1058. if(ctx->rom != NULL) {
  1059. furi_thread_flags_set(furi_thread_get_id(ctx->thread), 1);
  1060. furi_thread_join(ctx->thread);
  1061. }
  1062. furi_timer_free(timer);
  1063. view_port_enabled_set(view_port, false);
  1064. gui_remove_view_port(gui, view_port);
  1065. furi_record_close(RECORD_GUI);
  1066. view_port_free(view_port);
  1067. furi_message_queue_free(event_queue);
  1068. furi_mutex_free(g_state_mutex);
  1069. tama_p1_deinit(ctx);
  1070. free(ctx);
  1071. return 0;
  1072. }