tama_p1.c 44 KB

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