tama_p1.c 44 KB

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