tama_p1.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  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, void* ctx) {
  431. FuriMessageQueue* event_queue = ctx;
  432. furi_assert(event_queue);
  433. TamaEvent event = {.type = EventTypeInput, .input = *input_event};
  434. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  435. }
  436. static void tama_p1_update_timer_callback(void* ctx) {
  437. FuriMessageQueue* event_queue = ctx;
  438. furi_assert(event_queue);
  439. TamaEvent event = {.type = EventTypeTick};
  440. furi_message_queue_put(event_queue, &event, 0);
  441. }
  442. static void tama_p1_load_state() {
  443. state_t* state;
  444. uint8_t buf[4];
  445. bool error = false;
  446. state = tamalib_get_state();
  447. Storage* storage = furi_record_open(RECORD_STORAGE);
  448. File* file = storage_file_alloc(storage);
  449. if(storage_file_open(file, TAMA_SAVE_PATH, FSAM_READ, FSOM_OPEN_EXISTING)) {
  450. storage_file_read(file, &buf, 4);
  451. if(buf[0] != (uint8_t)STATE_FILE_MAGIC[0] || buf[1] != (uint8_t)STATE_FILE_MAGIC[1] ||
  452. buf[2] != (uint8_t)STATE_FILE_MAGIC[2] || buf[3] != (uint8_t)STATE_FILE_MAGIC[3]) {
  453. FURI_LOG_E(TAG, "FATAL: Wrong state file magic in \"%s\" !\n", TAMA_SAVE_PATH);
  454. error = true;
  455. }
  456. storage_file_read(file, &buf, 1);
  457. if(buf[0] != STATE_FILE_VERSION) {
  458. FURI_LOG_E(TAG, "FATAL: Unsupported version");
  459. error = true;
  460. }
  461. if(!error) {
  462. FURI_LOG_D(TAG, "Reading save.bin");
  463. storage_file_read(file, &buf, 2);
  464. *(state->pc) = buf[0] | ((buf[1] & 0x1F) << 8);
  465. storage_file_read(file, &buf, 2);
  466. *(state->x) = buf[0] | ((buf[1] & 0xF) << 8);
  467. storage_file_read(file, &buf, 2);
  468. *(state->y) = buf[0] | ((buf[1] & 0xF) << 8);
  469. storage_file_read(file, &buf, 1);
  470. *(state->a) = buf[0] & 0xF;
  471. storage_file_read(file, &buf, 1);
  472. *(state->b) = buf[0] & 0xF;
  473. storage_file_read(file, &buf, 1);
  474. *(state->np) = buf[0] & 0x1F;
  475. storage_file_read(file, &buf, 1);
  476. *(state->sp) = buf[0];
  477. storage_file_read(file, &buf, 1);
  478. *(state->flags) = buf[0] & 0xF;
  479. storage_file_read(file, &buf, 4);
  480. *(state->tick_counter) = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
  481. storage_file_read(file, &buf, 4);
  482. *(state->clk_timer_timestamp) = buf[0] | (buf[1] << 8) | (buf[2] << 16) |
  483. (buf[3] << 24);
  484. storage_file_read(file, &buf, 4);
  485. *(state->prog_timer_timestamp) = buf[0] | (buf[1] << 8) | (buf[2] << 16) |
  486. (buf[3] << 24);
  487. storage_file_read(file, &buf, 1);
  488. *(state->prog_timer_enabled) = buf[0] & 0x1;
  489. storage_file_read(file, &buf, 1);
  490. *(state->prog_timer_data) = buf[0];
  491. storage_file_read(file, &buf, 1);
  492. *(state->prog_timer_rld) = buf[0];
  493. storage_file_read(file, &buf, 4);
  494. *(state->call_depth) = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
  495. FURI_LOG_D(TAG, "Restoring Interupts");
  496. for(uint32_t i = 0; i < INT_SLOT_NUM; i++) {
  497. storage_file_read(file, &buf, 1);
  498. state->interrupts[i].factor_flag_reg = buf[0] & 0xF;
  499. storage_file_read(file, &buf, 1);
  500. state->interrupts[i].mask_reg = buf[0] & 0xF;
  501. storage_file_read(file, &buf, 1);
  502. state->interrupts[i].triggered = buf[0] & 0x1;
  503. }
  504. /* First 640 half bytes correspond to the RAM */
  505. FURI_LOG_D(TAG, "Restoring RAM");
  506. for(uint32_t i = 0; i < MEM_RAM_SIZE; i++) {
  507. storage_file_read(file, &buf, 1);
  508. SET_RAM_MEMORY(state->memory, i + MEM_RAM_ADDR, buf[0] & 0xF);
  509. }
  510. /* I/Os are from 0xF00 to 0xF7F */
  511. FURI_LOG_D(TAG, "Restoring I/O");
  512. for(uint32_t i = 0; i < MEM_IO_SIZE; i++) {
  513. storage_file_read(file, &buf, 1);
  514. SET_IO_MEMORY(state->memory, i + MEM_IO_ADDR, buf[0] & 0xF);
  515. }
  516. FURI_LOG_D(TAG, "Refreshing Hardware");
  517. tamalib_refresh_hw();
  518. }
  519. }
  520. storage_file_close(file);
  521. storage_file_free(file);
  522. furi_record_close(RECORD_STORAGE);
  523. }
  524. static void tama_p1_save_state() {
  525. // Saving state
  526. FURI_LOG_D(TAG, "Saving Gamestate");
  527. uint8_t buf[4];
  528. state_t* state;
  529. uint32_t offset = 0;
  530. state = tamalib_get_state();
  531. Storage* storage = furi_record_open(RECORD_STORAGE);
  532. File* file = storage_file_alloc(storage);
  533. if(storage_file_open(file, TAMA_SAVE_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  534. buf[0] = (uint8_t)STATE_FILE_MAGIC[0];
  535. buf[1] = (uint8_t)STATE_FILE_MAGIC[1];
  536. buf[2] = (uint8_t)STATE_FILE_MAGIC[2];
  537. buf[3] = (uint8_t)STATE_FILE_MAGIC[3];
  538. offset += storage_file_write(file, &buf, sizeof(buf));
  539. buf[0] = STATE_FILE_VERSION & 0xFF;
  540. offset += storage_file_write(file, &buf, 1);
  541. buf[0] = *(state->pc) & 0xFF;
  542. buf[1] = (*(state->pc) >> 8) & 0x1F;
  543. offset += storage_file_write(file, &buf, 2);
  544. buf[0] = *(state->x) & 0xFF;
  545. buf[1] = (*(state->x) >> 8) & 0xF;
  546. offset += storage_file_write(file, &buf, 2);
  547. buf[0] = *(state->y) & 0xFF;
  548. buf[1] = (*(state->y) >> 8) & 0xF;
  549. offset += storage_file_write(file, &buf, 2);
  550. buf[0] = *(state->a) & 0xF;
  551. offset += storage_file_write(file, &buf, 1);
  552. buf[0] = *(state->b) & 0xF;
  553. offset += storage_file_write(file, &buf, 1);
  554. buf[0] = *(state->np) & 0x1F;
  555. offset += storage_file_write(file, &buf, 1);
  556. buf[0] = *(state->sp) & 0xFF;
  557. offset += storage_file_write(file, &buf, 1);
  558. buf[0] = *(state->flags) & 0xF;
  559. offset += storage_file_write(file, &buf, 1);
  560. buf[0] = *(state->tick_counter) & 0xFF;
  561. buf[1] = (*(state->tick_counter) >> 8) & 0xFF;
  562. buf[2] = (*(state->tick_counter) >> 16) & 0xFF;
  563. buf[3] = (*(state->tick_counter) >> 24) & 0xFF;
  564. offset += storage_file_write(file, &buf, sizeof(buf));
  565. buf[0] = *(state->clk_timer_timestamp) & 0xFF;
  566. buf[1] = (*(state->clk_timer_timestamp) >> 8) & 0xFF;
  567. buf[2] = (*(state->clk_timer_timestamp) >> 16) & 0xFF;
  568. buf[3] = (*(state->clk_timer_timestamp) >> 24) & 0xFF;
  569. offset += storage_file_write(file, &buf, sizeof(buf));
  570. buf[0] = *(state->prog_timer_timestamp) & 0xFF;
  571. buf[1] = (*(state->prog_timer_timestamp) >> 8) & 0xFF;
  572. buf[2] = (*(state->prog_timer_timestamp) >> 16) & 0xFF;
  573. buf[3] = (*(state->prog_timer_timestamp) >> 24) & 0xFF;
  574. offset += storage_file_write(file, &buf, sizeof(buf));
  575. buf[0] = *(state->prog_timer_enabled) & 0x1;
  576. offset += storage_file_write(file, &buf, 1);
  577. buf[0] = *(state->prog_timer_data) & 0xFF;
  578. offset += storage_file_write(file, &buf, 1);
  579. buf[0] = *(state->prog_timer_rld) & 0xFF;
  580. offset += storage_file_write(file, &buf, 1);
  581. buf[0] = *(state->call_depth) & 0xFF;
  582. buf[1] = (*(state->call_depth) >> 8) & 0xFF;
  583. buf[2] = (*(state->call_depth) >> 16) & 0xFF;
  584. buf[3] = (*(state->call_depth) >> 24) & 0xFF;
  585. offset += storage_file_write(file, &buf, sizeof(buf));
  586. for(uint32_t i = 0; i < INT_SLOT_NUM; i++) {
  587. buf[0] = state->interrupts[i].factor_flag_reg & 0xF;
  588. offset += storage_file_write(file, &buf, 1);
  589. buf[0] = state->interrupts[i].mask_reg & 0xF;
  590. offset += storage_file_write(file, &buf, 1);
  591. buf[0] = state->interrupts[i].triggered & 0x1;
  592. offset += storage_file_write(file, &buf, 1);
  593. }
  594. /* First 640 half bytes correspond to the RAM */
  595. for(uint32_t i = 0; i < MEM_RAM_SIZE; i++) {
  596. buf[0] = GET_RAM_MEMORY(state->memory, i + MEM_RAM_ADDR) & 0xF;
  597. offset += storage_file_write(file, &buf, 1);
  598. }
  599. /* I/Os are from 0xF00 to 0xF7F */
  600. for(uint32_t i = 0; i < MEM_IO_SIZE; i++) {
  601. buf[0] = GET_IO_MEMORY(state->memory, i + MEM_IO_ADDR) & 0xF;
  602. offset += storage_file_write(file, &buf, 1);
  603. }
  604. }
  605. storage_file_close(file);
  606. storage_file_free(file);
  607. furi_record_close(RECORD_STORAGE);
  608. FURI_LOG_D(TAG, "Finished Writing %lu", offset);
  609. }
  610. static int32_t tama_p1_worker(void* context) {
  611. bool running = true;
  612. FuriMutex* mutex = context;
  613. while(furi_mutex_acquire(mutex, FuriWaitForever) != FuriStatusOk) furi_delay_tick(1);
  614. cpu_sync_ref_timestamp();
  615. LL_TIM_EnableCounter(TIM2);
  616. tama_p1_load_state();
  617. while(running) {
  618. if(furi_thread_flags_get()) {
  619. running = false;
  620. } else {
  621. // FURI_LOG_D(TAG, "Stepping"); // enabling this cause blank screen somehow
  622. // for (int i = 0; i < 100; ++i)
  623. tamalib_step(); // tamalib_mainloop();
  624. }
  625. }
  626. LL_TIM_DisableCounter(TIM2);
  627. furi_mutex_release(mutex);
  628. return 0;
  629. }
  630. static void tama_p1_init(TamaApp* const ctx) {
  631. g_ctx = ctx;
  632. memset(ctx, 0, sizeof(TamaApp));
  633. tama_p1_hal_init(&ctx->hal);
  634. // Load ROM
  635. Storage* storage = furi_record_open(RECORD_STORAGE);
  636. storage_common_migrate(storage, EXT_PATH("tama_p1"), STORAGE_APP_DATA_PATH_PREFIX);
  637. FileInfo fi;
  638. if(storage_common_stat(storage, TAMA_ROM_PATH, &fi) == FSE_OK) {
  639. File* rom_file = storage_file_alloc(storage);
  640. if(storage_file_open(rom_file, TAMA_ROM_PATH, FSAM_READ, FSOM_OPEN_EXISTING)) {
  641. ctx->rom = malloc((size_t)fi.size);
  642. uint8_t* buf_ptr = ctx->rom;
  643. size_t read = 0;
  644. while(read < fi.size) {
  645. size_t to_read = fi.size - read;
  646. if(to_read > UINT16_MAX) to_read = UINT16_MAX;
  647. uint16_t now_read = storage_file_read(rom_file, buf_ptr, (uint16_t)to_read);
  648. read += now_read;
  649. buf_ptr += now_read;
  650. }
  651. // Reorder endianess of ROM
  652. for(size_t i = 0; i < fi.size; i += 2) {
  653. uint8_t b = ctx->rom[i];
  654. ctx->rom[i] = ctx->rom[i + 1];
  655. ctx->rom[i + 1] = b & 0xF;
  656. }
  657. }
  658. storage_file_close(rom_file);
  659. storage_file_free(rom_file);
  660. }
  661. furi_record_close(RECORD_STORAGE);
  662. if(ctx->rom != NULL) {
  663. // Init TIM2
  664. // 64KHz
  665. furi_hal_bus_enable(FuriHalBusTIM2);
  666. LL_TIM_InitTypeDef tim_init = {
  667. .Prescaler = 999,
  668. .CounterMode = LL_TIM_COUNTERMODE_UP,
  669. .Autoreload = 0xFFFFFFFF,
  670. };
  671. LL_TIM_Init(TIM2, &tim_init);
  672. LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL);
  673. LL_TIM_DisableCounter(TIM2);
  674. LL_TIM_SetCounter(TIM2, 0);
  675. // Init TamaLIB
  676. tamalib_register_hal(&ctx->hal);
  677. tamalib_init((u12_t*)ctx->rom, NULL, 64000);
  678. tamalib_set_speed(speed);
  679. // TODO: implement fast forwarding
  680. // ctx->fast_forward_done = true;
  681. // Start stepping thread
  682. ctx->thread = furi_thread_alloc();
  683. furi_thread_set_name(ctx->thread, "TamaLIB");
  684. furi_thread_set_stack_size(ctx->thread, 2 * 1024);
  685. furi_thread_set_callback(ctx->thread, tama_p1_worker);
  686. furi_thread_set_context(ctx->thread, g_state_mutex);
  687. furi_thread_start(ctx->thread);
  688. }
  689. }
  690. static void tama_p1_deinit(TamaApp* const ctx) {
  691. if(ctx->rom != NULL) {
  692. tamalib_release();
  693. furi_thread_free(ctx->thread);
  694. furi_hal_bus_disable(FuriHalBusTIM2);
  695. free(ctx->rom);
  696. }
  697. }
  698. int32_t tama_p1_app(void* p) {
  699. UNUSED(p);
  700. TamaApp* ctx = malloc(sizeof(TamaApp));
  701. g_state_mutex = furi_mutex_alloc(FuriMutexTypeRecursive);
  702. tama_p1_init(ctx);
  703. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(TamaEvent));
  704. ViewPort* view_port = view_port_alloc();
  705. view_port_draw_callback_set(view_port, tama_p1_draw_callback, g_state_mutex);
  706. view_port_input_callback_set(view_port, tama_p1_input_callback, event_queue);
  707. Gui* gui = furi_record_open(RECORD_GUI);
  708. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  709. FuriTimer* timer =
  710. furi_timer_alloc(tama_p1_update_timer_callback, FuriTimerTypePeriodic, event_queue);
  711. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 30);
  712. // in_menu = false;
  713. // menu_cursor = 2;
  714. for(bool running = true; running;) {
  715. TamaEvent event;
  716. FuriStatus event_status = furi_message_queue_get(event_queue, &event, FuriWaitForever);
  717. if(event_status == FuriStatusOk) {
  718. // Local override with acquired context
  719. if(furi_mutex_acquire(g_state_mutex, FuriWaitForever) != FuriStatusOk) continue;
  720. if(event.type == EventTypeTick) {
  721. // FURI_LOG_D(TAG, "EventTypeTick");
  722. view_port_update(view_port);
  723. } else if(event.type == EventTypeInput) {
  724. FURI_LOG_D(
  725. TAG,
  726. "EventTypeInput: %ld %d %d",
  727. event.input.sequence,
  728. event.input.key,
  729. event.input.type);
  730. // InputType input_type = event.input.type; // idk why this is a variable
  731. btn_state_t tama_btn_state = 0; // BTN_STATE_RELEASED is 0
  732. if(in_menu) {
  733. // if(menu_cursor >= 2 &&
  734. // (event.input.key == InputKeyUp || event.input.key == InputKeyDown)) {
  735. // tama_btn_state = BTN_STATE_RELEASED;
  736. // }
  737. if(event.input.key == InputKeyBack) {
  738. tama_btn_state = BTN_STATE_RELEASED;
  739. in_menu = false;
  740. } else if(event.input.key == InputKeyUp && event.input.type == InputTypePress) {
  741. tama_btn_state = BTN_STATE_RELEASED;
  742. if(menu_cursor > 0) {
  743. menu_cursor -= 1;
  744. } else {
  745. menu_cursor = menu_items - 1;
  746. }
  747. if(menu_cursor >= menu_items - 2 && sub_menu_last > 0) {
  748. sub_menu_buttons = 1;
  749. sub_menu_last = 1;
  750. } else {
  751. sub_menu_buttons = 0;
  752. sub_menu_last = 0;
  753. }
  754. } else if(event.input.key == InputKeyDown && event.input.type == InputTypePress) {
  755. tama_btn_state = BTN_STATE_RELEASED;
  756. if(menu_cursor < menu_items - 1) {
  757. menu_cursor += 1;
  758. } else {
  759. menu_cursor = 0;
  760. }
  761. if(menu_cursor >= menu_items - 2 && sub_menu_buttons > 0) {
  762. sub_menu_buttons = 1;
  763. sub_menu_last = 1;
  764. } else {
  765. sub_menu_buttons = 0;
  766. sub_menu_last = 0;
  767. }
  768. } else if(event.input.key == InputKeyLeft && event.input.type == InputTypePress) {
  769. switch(menu_cursor) {
  770. case 0:
  771. switch(layout_mode) {
  772. case 0:
  773. layout_mode = 4;
  774. m = InputKeyRight;
  775. a = InputKeyUp;
  776. b = InputKeyLeft;
  777. c = InputKeyDown;
  778. break;
  779. case 1:
  780. layout_mode -= 1; // 0
  781. m = InputKeyUp;
  782. a = InputKeyLeft;
  783. b = InputKeyDown;
  784. c = InputKeyRight;
  785. break;
  786. case 2:
  787. layout_mode -= 1; // 1
  788. m = InputKeyUp;
  789. a = InputKeyLeft;
  790. b = InputKeyDown;
  791. c = InputKeyRight;
  792. break;
  793. case 3:
  794. layout_mode -= 1; // 2
  795. m = InputKeyUp;
  796. a = InputKeyLeft;
  797. b = InputKeyDown;
  798. c = InputKeyRight;
  799. break;
  800. case 4:
  801. layout_mode -= 1; // 3
  802. m = InputKeyLeft;
  803. a = InputKeyDown;
  804. b = InputKeyRight;
  805. c = InputKeyUp;
  806. break;
  807. }
  808. break;
  809. case 1:
  810. speed_down();
  811. break;
  812. case 2:
  813. tama_btn_state = BTN_STATE_RELEASED;
  814. switch(sub_menu_buttons) {
  815. case 0:
  816. sub_menu_buttons = 3;
  817. break;
  818. case 1:
  819. case 2:
  820. case 3:
  821. sub_menu_buttons -= 1;
  822. break;
  823. default:
  824. break;
  825. }
  826. break;
  827. case menu_items - 1:
  828. switch(sub_menu_last) {
  829. case 0:
  830. sub_menu_last = 2;
  831. break;
  832. case 1:
  833. case 2:
  834. sub_menu_last -= 1;
  835. break;
  836. default:
  837. break;
  838. }
  839. break;
  840. default:
  841. break;
  842. }
  843. } else if(event.input.key == InputKeyRight && event.input.type == InputTypePress) {
  844. switch(menu_cursor) {
  845. case 0:
  846. switch(layout_mode) {
  847. case 0:
  848. layout_mode += 1; // 1
  849. m = InputKeyUp;
  850. a = InputKeyLeft;
  851. b = InputKeyDown;
  852. c = InputKeyRight;
  853. break;
  854. case 1:
  855. layout_mode += 1; // 2
  856. m = InputKeyUp;
  857. a = InputKeyLeft;
  858. b = InputKeyDown;
  859. c = InputKeyRight;
  860. break;
  861. case 2:
  862. layout_mode += 1; // 3
  863. m = InputKeyLeft;
  864. a = InputKeyDown;
  865. b = InputKeyRight;
  866. c = InputKeyUp;
  867. break;
  868. case 3:
  869. layout_mode += 1; // 4
  870. m = InputKeyRight;
  871. a = InputKeyUp;
  872. b = InputKeyLeft;
  873. c = InputKeyDown;
  874. break;
  875. case 4:
  876. layout_mode = 0;
  877. m = InputKeyUp;
  878. a = InputKeyLeft;
  879. b = InputKeyDown;
  880. c = InputKeyRight;
  881. break;
  882. }
  883. break;
  884. case 1:
  885. speed_up();
  886. break;
  887. case 2:
  888. tama_btn_state = BTN_STATE_RELEASED;
  889. switch(sub_menu_buttons) {
  890. case 0:
  891. case 1:
  892. case 2:
  893. sub_menu_buttons += 1;
  894. break;
  895. case 3:
  896. sub_menu_buttons = 0;
  897. break;
  898. default:
  899. break;
  900. }
  901. break;
  902. case menu_items - 1:
  903. switch(sub_menu_last) {
  904. case 0:
  905. case 1:
  906. sub_menu_last += 1;
  907. break;
  908. case 2:
  909. sub_menu_last = 0;
  910. break;
  911. default:
  912. break;
  913. }
  914. break;
  915. default:
  916. break;
  917. }
  918. } else if(event.input.key == InputKeyOk) {
  919. switch(menu_cursor) {
  920. case 0:
  921. if(event.input.type == InputTypePress) {
  922. switch(layout_mode) {
  923. case 0:
  924. layout_mode += 1; // 1
  925. m = InputKeyUp;
  926. a = InputKeyLeft;
  927. b = InputKeyDown;
  928. c = InputKeyRight;
  929. break;
  930. case 1:
  931. layout_mode += 1; // 2
  932. m = InputKeyUp;
  933. a = InputKeyLeft;
  934. b = InputKeyDown;
  935. c = InputKeyRight;
  936. break;
  937. case 2:
  938. layout_mode += 1; // 3
  939. m = InputKeyLeft;
  940. a = InputKeyDown;
  941. b = InputKeyRight;
  942. c = InputKeyUp;
  943. break;
  944. case 3:
  945. layout_mode += 1; // 4
  946. m = InputKeyRight;
  947. a = InputKeyUp;
  948. b = InputKeyLeft;
  949. c = InputKeyDown;
  950. break;
  951. case 4:
  952. layout_mode = 0;
  953. m = InputKeyUp;
  954. a = InputKeyLeft;
  955. b = InputKeyDown;
  956. c = InputKeyRight;
  957. break;
  958. }
  959. }
  960. break;
  961. case 1:
  962. if(event.input.type == InputTypePress) {
  963. speed_up();
  964. }
  965. break;
  966. case 2:
  967. if(event.input.type == InputTypePress)
  968. tama_btn_state = BTN_STATE_PRESSED;
  969. else if(event.input.type == InputTypeRelease)
  970. tama_btn_state = BTN_STATE_RELEASED;
  971. switch(sub_menu_buttons) {
  972. case 0: // A+C
  973. tamalib_set_button(BTN_LEFT, tama_btn_state);
  974. tamalib_set_button(BTN_RIGHT, tama_btn_state);
  975. break;
  976. case 1: // A
  977. tamalib_set_button(BTN_LEFT, tama_btn_state);
  978. break;
  979. case 2: // B
  980. tamalib_set_button(BTN_MIDDLE, tama_btn_state);
  981. break;
  982. case 3: // C
  983. tamalib_set_button(BTN_RIGHT, tama_btn_state);
  984. break;
  985. default:
  986. break;
  987. }
  988. break;
  989. case menu_items - 1:
  990. default:
  991. if(event.input.type == InputTypePress) {
  992. switch(sub_menu_last) {
  993. case 0: // close menu
  994. in_menu = false;
  995. break;
  996. case 1: // Save
  997. if(speed != 1) {
  998. uint8_t temp = speed;
  999. speed = 1;
  1000. tamalib_set_speed(speed);
  1001. furi_timer_stop(timer);
  1002. tama_p1_save_state();
  1003. furi_timer_start(
  1004. timer, furi_kernel_get_tick_frequency() / 30);
  1005. speed = temp;
  1006. tamalib_set_speed(speed);
  1007. } else {
  1008. furi_timer_stop(timer);
  1009. tama_p1_save_state();
  1010. furi_timer_start(
  1011. timer, furi_kernel_get_tick_frequency() / 30);
  1012. }
  1013. break;
  1014. case 2: // Save & Exit
  1015. if(speed != 1) {
  1016. speed = 1;
  1017. tamalib_set_speed(speed);
  1018. }
  1019. furi_timer_stop(timer);
  1020. tama_p1_save_state();
  1021. running = false;
  1022. break;
  1023. default:
  1024. break;
  1025. }
  1026. }
  1027. break;
  1028. }
  1029. }
  1030. } else { // out of menu
  1031. if(event.input.key == InputKeyBack && event.input.type == InputTypeLong) {
  1032. if(speed != 1) {
  1033. speed = 1;
  1034. tamalib_set_speed(speed);
  1035. }
  1036. furi_timer_stop(timer);
  1037. tama_p1_save_state();
  1038. running = false;
  1039. } else if(
  1040. event.input.type == InputTypePress ||
  1041. event.input.type == InputTypeRelease) {
  1042. if(event.input.key != InputKeyBack && event.input.key != m) {
  1043. if(event.input.type == InputTypePress)
  1044. tama_btn_state = BTN_STATE_PRESSED;
  1045. else if(event.input.type == InputTypeRelease)
  1046. tama_btn_state = BTN_STATE_RELEASED;
  1047. } else {
  1048. tama_btn_state = BTN_STATE_RELEASED;
  1049. }
  1050. if(event.input.key == m && event.input.type == InputTypePress) {
  1051. in_menu = true;
  1052. } else if(event.input.key == a) {
  1053. tamalib_set_button(BTN_LEFT, tama_btn_state);
  1054. } else if(event.input.key == b) {
  1055. tamalib_set_button(BTN_MIDDLE, tama_btn_state);
  1056. } else if(event.input.key == c) {
  1057. tamalib_set_button(BTN_RIGHT, tama_btn_state);
  1058. } else if(event.input.key == InputKeyOk) {
  1059. tamalib_set_button(BTN_MIDDLE, tama_btn_state);
  1060. }
  1061. }
  1062. }
  1063. }
  1064. furi_mutex_release(g_state_mutex);
  1065. }
  1066. // else {
  1067. // // Timeout
  1068. // FURI_LOG_D(TAG, "Timed out");
  1069. // }
  1070. }
  1071. if(ctx->rom != NULL) {
  1072. furi_thread_flags_set(furi_thread_get_id(ctx->thread), 1);
  1073. furi_thread_join(ctx->thread);
  1074. }
  1075. furi_timer_free(timer);
  1076. view_port_enabled_set(view_port, false);
  1077. gui_remove_view_port(gui, view_port);
  1078. furi_record_close(RECORD_GUI);
  1079. view_port_free(view_port);
  1080. furi_message_queue_free(event_queue);
  1081. furi_mutex_free(g_state_mutex);
  1082. tama_p1_deinit(ctx);
  1083. free(ctx);
  1084. return 0;
  1085. }