tama_p1.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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. bool portrait_mode = false;
  13. bool in_menu = false;
  14. int menu_cursor = 2;
  15. const int menu_items = 3;
  16. static const Icon* icons_list[] = {
  17. &I_icon_0,
  18. &I_icon_1,
  19. &I_icon_2,
  20. &I_icon_3,
  21. &I_icon_4,
  22. &I_icon_5,
  23. &I_icon_6,
  24. &I_icon_7,
  25. };
  26. // static void draw_landscape(Canvas* const canvas, void* cb_ctx)
  27. static void draw_landscape(Canvas* const canvas) {
  28. // FURI_LOG_D(TAG, "Drawing frame");
  29. // Calculate positioning
  30. uint16_t canv_width = canvas_width(canvas);
  31. uint16_t canv_height = canvas_height(canvas);
  32. uint16_t lcd_matrix_scaled_width = 32 * TAMA_SCREEN_SCALE_FACTOR;
  33. uint16_t lcd_matrix_scaled_height = 16 * TAMA_SCREEN_SCALE_FACTOR;
  34. // uint16_t lcd_matrix_top = 0;
  35. uint16_t lcd_matrix_top = (canv_height - lcd_matrix_scaled_height) / 2;
  36. uint16_t lcd_matrix_left = (canv_width - lcd_matrix_scaled_width) / 2;
  37. uint16_t lcd_icon_upper_top = lcd_matrix_top - TAMA_LCD_ICON_SIZE - TAMA_LCD_ICON_MARGIN;
  38. uint16_t lcd_icon_upper_left = lcd_matrix_left;
  39. uint16_t lcd_icon_lower_top = lcd_matrix_top + lcd_matrix_scaled_height + TAMA_LCD_ICON_MARGIN;
  40. uint16_t lcd_icon_lower_left = lcd_matrix_left;
  41. uint16_t lcd_icon_spacing_horiz =
  42. (lcd_matrix_scaled_width - (4 * TAMA_LCD_ICON_SIZE)) / 3 + TAMA_LCD_ICON_SIZE;
  43. uint16_t y = lcd_matrix_top;
  44. for(uint8_t row = 0; row < 16; ++row) {
  45. uint16_t x = lcd_matrix_left;
  46. uint32_t row_pixels = g_ctx->framebuffer[row];
  47. for(uint8_t col = 0; col < 32; ++col) {
  48. if(row_pixels & 1) {
  49. canvas_draw_box(canvas, x, y, TAMA_SCREEN_SCALE_FACTOR, TAMA_SCREEN_SCALE_FACTOR);
  50. }
  51. x += TAMA_SCREEN_SCALE_FACTOR;
  52. row_pixels >>= 1;
  53. }
  54. y += TAMA_SCREEN_SCALE_FACTOR;
  55. }
  56. // Start drawing icons
  57. uint8_t lcd_icons = g_ctx->icons;
  58. // Draw top icons
  59. y = lcd_icon_upper_top;
  60. // y = 64 - TAMA_LCD_ICON_SIZE;
  61. uint16_t x_ic = lcd_icon_upper_left;
  62. for(uint8_t i = 0; i < 4; ++i) {
  63. if(lcd_icons & 1) {
  64. canvas_draw_icon(canvas, x_ic, y, icons_list[i]);
  65. }
  66. // x_ic += TAMA_LCD_ICON_SIZE + 4;
  67. x_ic += lcd_icon_spacing_horiz;
  68. lcd_icons >>= 1;
  69. }
  70. // Draw bottom icons
  71. y = lcd_icon_lower_top;
  72. x_ic = lcd_icon_lower_left;
  73. for(uint8_t i = 4; i < 8; ++i) {
  74. // canvas_draw_frame(canvas, x_ic, y, TAMA_LCD_ICON_SIZE, TAMA_LCD_ICON_SIZE);
  75. if(lcd_icons & 1) {
  76. canvas_draw_icon(canvas, x_ic, y, icons_list[i]);
  77. }
  78. x_ic += lcd_icon_spacing_horiz;
  79. lcd_icons >>= 1;
  80. }
  81. }
  82. // static void draw_portrait(Canvas* const canvas, void* cb_ctx)
  83. static void draw_portrait(Canvas* const canvas) {
  84. // FURI_LOG_D(TAG, "Drawing frame");
  85. // Calculate positioning
  86. // uint16_t canv_width = canvas_width(canvas);
  87. uint16_t canv_height = canvas_height(canvas);
  88. uint16_t lcd_matrix_scaled_width = 32 * TAMA_SCREEN_SCALE_FACTOR;
  89. uint16_t lcd_matrix_scaled_height = 16 * TAMA_SCREEN_SCALE_FACTOR;
  90. // uint16_t lcd_matrix_top = 0;
  91. uint16_t lcd_matrix_top = (canv_height - lcd_matrix_scaled_height) / 2;
  92. // uint16_t lcd_matrix_left = (canv_width - lcd_matrix_scaled_width) / 2;
  93. uint16_t lcd_matrix_left = 64 - TAMA_LCD_ICON_SIZE;
  94. uint16_t lcd_icon_upper_left = lcd_matrix_left;
  95. uint16_t lcd_icon_lower_left = lcd_matrix_left;
  96. uint16_t lcd_icon_spacing_horiz =
  97. (lcd_matrix_scaled_width - (4 * TAMA_LCD_ICON_SIZE)) / 3 + TAMA_LCD_ICON_SIZE;
  98. uint16_t y = lcd_matrix_top; // 64
  99. for(uint8_t row = 0; row < 16; ++row) {
  100. uint16_t x = 128; // lcd_matrix_left
  101. uint32_t row_pixels = g_ctx->framebuffer[row];
  102. for(uint8_t col = 0; col < 32; ++col) {
  103. if(row_pixels & 1) {
  104. canvas_draw_box(
  105. canvas, y + 32, x - 66, TAMA_SCREEN_SCALE_FACTOR, TAMA_SCREEN_SCALE_FACTOR);
  106. }
  107. x -= TAMA_SCREEN_SCALE_FACTOR;
  108. row_pixels >>= 1;
  109. }
  110. y += TAMA_SCREEN_SCALE_FACTOR;
  111. }
  112. // Start drawing icons
  113. uint8_t lcd_icons = g_ctx->icons;
  114. // Draw top icons
  115. // y = lcd_icon_upper_top;
  116. y = 30;
  117. // y = 64 - TAMA_LCD_ICON_SIZE;
  118. uint16_t x_ic = lcd_icon_upper_left;
  119. // uint16_t x_ic = 64 - TAMA_LCD_ICON_SIZE;
  120. for(uint8_t i = 0; i < 4; ++i) {
  121. if(lcd_icons & 1) {
  122. canvas_draw_icon(canvas, y, x_ic, icons_list[i]);
  123. }
  124. x_ic -= lcd_icon_spacing_horiz; // TAMA_LCD_ICON_SIZE + 4;
  125. lcd_icons >>= 1;
  126. }
  127. // Draw bottom icons
  128. y = 84; // lcd_icon_lower_top
  129. x_ic = lcd_icon_lower_left; // 64 - TAMA_LCD_ICON_SIZE
  130. for(uint8_t i = 4; i < 8; ++i) {
  131. // canvas_draw_frame(canvas, x_ic, y, TAMA_LCD_ICON_SIZE, TAMA_LCD_ICON_SIZE);
  132. if(lcd_icons & 1) {
  133. canvas_draw_icon(canvas, y, x_ic, icons_list[i]);
  134. }
  135. x_ic -= lcd_icon_spacing_horiz;
  136. lcd_icons >>= 1;
  137. }
  138. }
  139. // static void draw_menu_portrait(Canvas* const canvas, void* cb_ctx) {}
  140. // static void draw_menu_landscape(Canvas* const canvas, void* cb_ctx)
  141. static void draw_menu_landscape(Canvas* const canvas) {
  142. canvas_draw_frame(canvas, 0, 0, 128, 64);
  143. canvas_draw_str_aligned(canvas, 64, 6, AlignCenter, AlignCenter, "Menu");
  144. canvas_draw_line(canvas, 0, 10, 128, 10);
  145. switch(menu_cursor) {
  146. case 0:
  147. canvas_draw_circle(canvas, 5, 20, 2);
  148. break;
  149. case 1:
  150. canvas_draw_circle(canvas, 5, 35, 2);
  151. break;
  152. case 2:
  153. canvas_draw_circle(canvas, 5, 50, 2);
  154. break;
  155. }
  156. if(portrait_mode) {
  157. canvas_draw_str(canvas, 10, 25, "Orientation: Portrait");
  158. } else {
  159. canvas_draw_str(canvas, 10, 25, "Orientation: Landscape");
  160. }
  161. canvas_draw_str(canvas, 10, 40, "A+C (mute/change time)");
  162. canvas_draw_str(canvas, 10, 55, "Close menu");
  163. }
  164. static void tama_p1_draw_callback(Canvas* const canvas, void* cb_ctx) {
  165. furi_assert(cb_ctx);
  166. FuriMutex* const mutex = cb_ctx;
  167. if(furi_mutex_acquire(mutex, 25) != FuriStatusOk) return;
  168. if(g_ctx->rom == NULL) {
  169. canvas_set_font(canvas, FontPrimary);
  170. canvas_draw_str(canvas, 30, 30, "No ROM");
  171. } else if(g_ctx->halted) {
  172. canvas_set_font(canvas, FontPrimary);
  173. canvas_draw_str(canvas, 30, 30, "Halted");
  174. } else {
  175. if(in_menu) {
  176. if(portrait_mode) {
  177. // draw_menu_portrait(canvas);
  178. draw_menu_landscape(canvas);
  179. } else {
  180. draw_menu_landscape(canvas);
  181. }
  182. } else if(portrait_mode) {
  183. draw_portrait(canvas);
  184. } else {
  185. draw_landscape(canvas);
  186. }
  187. }
  188. furi_mutex_release(mutex);
  189. }
  190. static void tama_p1_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
  191. furi_assert(event_queue);
  192. TamaEvent event = {.type = EventTypeInput, .input = *input_event};
  193. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  194. }
  195. static void tama_p1_update_timer_callback(FuriMessageQueue* event_queue) {
  196. furi_assert(event_queue);
  197. TamaEvent event = {.type = EventTypeTick};
  198. furi_message_queue_put(event_queue, &event, 0);
  199. }
  200. static void tama_p1_load_state() {
  201. state_t* state;
  202. uint8_t buf[4];
  203. bool error = false;
  204. state = tamalib_get_state();
  205. Storage* storage = furi_record_open(RECORD_STORAGE);
  206. File* file = storage_file_alloc(storage);
  207. if(storage_file_open(file, TAMA_SAVE_PATH, FSAM_READ, FSOM_OPEN_EXISTING)) {
  208. storage_file_read(file, &buf, 4);
  209. if(buf[0] != (uint8_t)STATE_FILE_MAGIC[0] || buf[1] != (uint8_t)STATE_FILE_MAGIC[1] ||
  210. buf[2] != (uint8_t)STATE_FILE_MAGIC[2] || buf[3] != (uint8_t)STATE_FILE_MAGIC[3]) {
  211. FURI_LOG_E(TAG, "FATAL: Wrong state file magic in \"%s\" !\n", TAMA_SAVE_PATH);
  212. error = true;
  213. }
  214. storage_file_read(file, &buf, 1);
  215. if(buf[0] != STATE_FILE_VERSION) {
  216. FURI_LOG_E(TAG, "FATAL: Unsupported version");
  217. error = true;
  218. }
  219. if(!error) {
  220. FURI_LOG_D(TAG, "Reading save.bin");
  221. storage_file_read(file, &buf, 2);
  222. *(state->pc) = buf[0] | ((buf[1] & 0x1F) << 8);
  223. storage_file_read(file, &buf, 2);
  224. *(state->x) = buf[0] | ((buf[1] & 0xF) << 8);
  225. storage_file_read(file, &buf, 2);
  226. *(state->y) = buf[0] | ((buf[1] & 0xF) << 8);
  227. storage_file_read(file, &buf, 1);
  228. *(state->a) = buf[0] & 0xF;
  229. storage_file_read(file, &buf, 1);
  230. *(state->b) = buf[0] & 0xF;
  231. storage_file_read(file, &buf, 1);
  232. *(state->np) = buf[0] & 0x1F;
  233. storage_file_read(file, &buf, 1);
  234. *(state->sp) = buf[0];
  235. storage_file_read(file, &buf, 1);
  236. *(state->flags) = buf[0] & 0xF;
  237. storage_file_read(file, &buf, 4);
  238. *(state->tick_counter) = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
  239. storage_file_read(file, &buf, 4);
  240. *(state->clk_timer_timestamp) = buf[0] | (buf[1] << 8) | (buf[2] << 16) |
  241. (buf[3] << 24);
  242. storage_file_read(file, &buf, 4);
  243. *(state->prog_timer_timestamp) = buf[0] | (buf[1] << 8) | (buf[2] << 16) |
  244. (buf[3] << 24);
  245. storage_file_read(file, &buf, 1);
  246. *(state->prog_timer_enabled) = buf[0] & 0x1;
  247. storage_file_read(file, &buf, 1);
  248. *(state->prog_timer_data) = buf[0];
  249. storage_file_read(file, &buf, 1);
  250. *(state->prog_timer_rld) = buf[0];
  251. storage_file_read(file, &buf, 4);
  252. *(state->call_depth) = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
  253. FURI_LOG_D(TAG, "Restoring Interupts");
  254. for(uint32_t i = 0; i < INT_SLOT_NUM; i++) {
  255. storage_file_read(file, &buf, 1);
  256. state->interrupts[i].factor_flag_reg = buf[0] & 0xF;
  257. storage_file_read(file, &buf, 1);
  258. state->interrupts[i].mask_reg = buf[0] & 0xF;
  259. storage_file_read(file, &buf, 1);
  260. state->interrupts[i].triggered = buf[0] & 0x1;
  261. }
  262. /* First 640 half bytes correspond to the RAM */
  263. FURI_LOG_D(TAG, "Restoring RAM");
  264. for(uint32_t i = 0; i < MEM_RAM_SIZE; i++) {
  265. storage_file_read(file, &buf, 1);
  266. SET_RAM_MEMORY(state->memory, i + MEM_RAM_ADDR, buf[0] & 0xF);
  267. }
  268. /* I/Os are from 0xF00 to 0xF7F */
  269. FURI_LOG_D(TAG, "Restoring I/O");
  270. for(uint32_t i = 0; i < MEM_IO_SIZE; i++) {
  271. storage_file_read(file, &buf, 1);
  272. SET_IO_MEMORY(state->memory, i + MEM_IO_ADDR, buf[0] & 0xF);
  273. }
  274. FURI_LOG_D(TAG, "Refreshing Hardware");
  275. tamalib_refresh_hw();
  276. }
  277. }
  278. storage_file_close(file);
  279. storage_file_free(file);
  280. furi_record_close(RECORD_STORAGE);
  281. }
  282. static void tama_p1_save_state() {
  283. // Saving state
  284. FURI_LOG_D(TAG, "Saving Gamestate");
  285. uint8_t buf[4];
  286. state_t* state;
  287. uint32_t offset = 0;
  288. state = tamalib_get_state();
  289. Storage* storage = furi_record_open(RECORD_STORAGE);
  290. File* file = storage_file_alloc(storage);
  291. if(storage_file_open(file, TAMA_SAVE_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  292. buf[0] = (uint8_t)STATE_FILE_MAGIC[0];
  293. buf[1] = (uint8_t)STATE_FILE_MAGIC[1];
  294. buf[2] = (uint8_t)STATE_FILE_MAGIC[2];
  295. buf[3] = (uint8_t)STATE_FILE_MAGIC[3];
  296. offset += storage_file_write(file, &buf, sizeof(buf));
  297. buf[0] = STATE_FILE_VERSION & 0xFF;
  298. offset += storage_file_write(file, &buf, 1);
  299. buf[0] = *(state->pc) & 0xFF;
  300. buf[1] = (*(state->pc) >> 8) & 0x1F;
  301. offset += storage_file_write(file, &buf, 2);
  302. buf[0] = *(state->x) & 0xFF;
  303. buf[1] = (*(state->x) >> 8) & 0xF;
  304. offset += storage_file_write(file, &buf, 2);
  305. buf[0] = *(state->y) & 0xFF;
  306. buf[1] = (*(state->y) >> 8) & 0xF;
  307. offset += storage_file_write(file, &buf, 2);
  308. buf[0] = *(state->a) & 0xF;
  309. offset += storage_file_write(file, &buf, 1);
  310. buf[0] = *(state->b) & 0xF;
  311. offset += storage_file_write(file, &buf, 1);
  312. buf[0] = *(state->np) & 0x1F;
  313. offset += storage_file_write(file, &buf, 1);
  314. buf[0] = *(state->sp) & 0xFF;
  315. offset += storage_file_write(file, &buf, 1);
  316. buf[0] = *(state->flags) & 0xF;
  317. offset += storage_file_write(file, &buf, 1);
  318. buf[0] = *(state->tick_counter) & 0xFF;
  319. buf[1] = (*(state->tick_counter) >> 8) & 0xFF;
  320. buf[2] = (*(state->tick_counter) >> 16) & 0xFF;
  321. buf[3] = (*(state->tick_counter) >> 24) & 0xFF;
  322. offset += storage_file_write(file, &buf, sizeof(buf));
  323. buf[0] = *(state->clk_timer_timestamp) & 0xFF;
  324. buf[1] = (*(state->clk_timer_timestamp) >> 8) & 0xFF;
  325. buf[2] = (*(state->clk_timer_timestamp) >> 16) & 0xFF;
  326. buf[3] = (*(state->clk_timer_timestamp) >> 24) & 0xFF;
  327. offset += storage_file_write(file, &buf, sizeof(buf));
  328. buf[0] = *(state->prog_timer_timestamp) & 0xFF;
  329. buf[1] = (*(state->prog_timer_timestamp) >> 8) & 0xFF;
  330. buf[2] = (*(state->prog_timer_timestamp) >> 16) & 0xFF;
  331. buf[3] = (*(state->prog_timer_timestamp) >> 24) & 0xFF;
  332. offset += storage_file_write(file, &buf, sizeof(buf));
  333. buf[0] = *(state->prog_timer_enabled) & 0x1;
  334. offset += storage_file_write(file, &buf, 1);
  335. buf[0] = *(state->prog_timer_data) & 0xFF;
  336. offset += storage_file_write(file, &buf, 1);
  337. buf[0] = *(state->prog_timer_rld) & 0xFF;
  338. offset += storage_file_write(file, &buf, 1);
  339. buf[0] = *(state->call_depth) & 0xFF;
  340. buf[1] = (*(state->call_depth) >> 8) & 0xFF;
  341. buf[2] = (*(state->call_depth) >> 16) & 0xFF;
  342. buf[3] = (*(state->call_depth) >> 24) & 0xFF;
  343. offset += storage_file_write(file, &buf, sizeof(buf));
  344. for(uint32_t i = 0; i < INT_SLOT_NUM; i++) {
  345. buf[0] = state->interrupts[i].factor_flag_reg & 0xF;
  346. offset += storage_file_write(file, &buf, 1);
  347. buf[0] = state->interrupts[i].mask_reg & 0xF;
  348. offset += storage_file_write(file, &buf, 1);
  349. buf[0] = state->interrupts[i].triggered & 0x1;
  350. offset += storage_file_write(file, &buf, 1);
  351. }
  352. /* First 640 half bytes correspond to the RAM */
  353. for(uint32_t i = 0; i < MEM_RAM_SIZE; i++) {
  354. buf[0] = GET_RAM_MEMORY(state->memory, i + MEM_RAM_ADDR) & 0xF;
  355. offset += storage_file_write(file, &buf, 1);
  356. }
  357. /* I/Os are from 0xF00 to 0xF7F */
  358. for(uint32_t i = 0; i < MEM_IO_SIZE; i++) {
  359. buf[0] = GET_IO_MEMORY(state->memory, i + MEM_IO_ADDR) & 0xF;
  360. offset += storage_file_write(file, &buf, 1);
  361. }
  362. }
  363. storage_file_close(file);
  364. storage_file_free(file);
  365. furi_record_close(RECORD_STORAGE);
  366. FURI_LOG_D(TAG, "Finished Writing %lu", offset);
  367. }
  368. static int32_t tama_p1_worker(void* context) {
  369. bool running = true;
  370. FuriMutex* mutex = context;
  371. while(furi_mutex_acquire(mutex, FuriWaitForever) != FuriStatusOk) furi_delay_tick(1);
  372. cpu_sync_ref_timestamp();
  373. LL_TIM_EnableCounter(TIM2);
  374. tama_p1_load_state();
  375. while(running) {
  376. if(furi_thread_flags_get()) {
  377. running = false;
  378. } else {
  379. // FURI_LOG_D(TAG, "Stepping");
  380. // for (int i = 0; i < 100; ++i)
  381. tamalib_step();
  382. }
  383. }
  384. LL_TIM_DisableCounter(TIM2);
  385. furi_mutex_release(mutex);
  386. return 0;
  387. }
  388. static void tama_p1_init(TamaApp* const ctx) {
  389. g_ctx = ctx;
  390. memset(ctx, 0, sizeof(TamaApp));
  391. tama_p1_hal_init(&ctx->hal);
  392. // Load ROM
  393. Storage* storage = furi_record_open(RECORD_STORAGE);
  394. FileInfo fi;
  395. if(storage_common_stat(storage, TAMA_ROM_PATH, &fi) == FSE_OK) {
  396. File* rom_file = storage_file_alloc(storage);
  397. if(storage_file_open(rom_file, TAMA_ROM_PATH, FSAM_READ, FSOM_OPEN_EXISTING)) {
  398. ctx->rom = malloc((size_t)fi.size);
  399. uint8_t* buf_ptr = ctx->rom;
  400. size_t read = 0;
  401. while(read < fi.size) {
  402. size_t to_read = fi.size - read;
  403. if(to_read > UINT16_MAX) to_read = UINT16_MAX;
  404. uint16_t now_read = storage_file_read(rom_file, buf_ptr, (uint16_t)to_read);
  405. read += now_read;
  406. buf_ptr += now_read;
  407. }
  408. // Reorder endianess of ROM
  409. for(size_t i = 0; i < fi.size; i += 2) {
  410. uint8_t b = ctx->rom[i];
  411. ctx->rom[i] = ctx->rom[i + 1];
  412. ctx->rom[i + 1] = b & 0xF;
  413. }
  414. }
  415. storage_file_close(rom_file);
  416. storage_file_free(rom_file);
  417. }
  418. furi_record_close(RECORD_STORAGE);
  419. if(ctx->rom != NULL) {
  420. // Init TIM2
  421. // 64KHz
  422. LL_TIM_InitTypeDef tim_init = {
  423. .Prescaler = 999,
  424. .CounterMode = LL_TIM_COUNTERMODE_UP,
  425. .Autoreload = 0xFFFFFFFF,
  426. };
  427. LL_TIM_Init(TIM2, &tim_init);
  428. LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL);
  429. LL_TIM_DisableCounter(TIM2);
  430. LL_TIM_SetCounter(TIM2, 0);
  431. // Init TamaLIB
  432. tamalib_register_hal(&ctx->hal);
  433. tamalib_init((u12_t*)ctx->rom, NULL, 64000);
  434. tamalib_set_speed(1);
  435. // TODO: implement fast forwarding
  436. ctx->fast_forward_done = true;
  437. // Start stepping thread
  438. ctx->thread = furi_thread_alloc();
  439. furi_thread_set_name(ctx->thread, "TamaLIB");
  440. furi_thread_set_stack_size(ctx->thread, 1024);
  441. furi_thread_set_callback(ctx->thread, tama_p1_worker);
  442. furi_thread_set_context(ctx->thread, g_state_mutex);
  443. furi_thread_start(ctx->thread);
  444. }
  445. }
  446. static void tama_p1_deinit(TamaApp* const ctx) {
  447. if(ctx->rom != NULL) {
  448. tamalib_release();
  449. furi_thread_free(ctx->thread);
  450. free(ctx->rom);
  451. }
  452. }
  453. int32_t tama_p1_app(void* p) {
  454. UNUSED(p);
  455. TamaApp* ctx = malloc(sizeof(TamaApp));
  456. g_state_mutex = furi_mutex_alloc(FuriMutexTypeRecursive);
  457. tama_p1_init(ctx);
  458. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(TamaEvent));
  459. ViewPort* view_port = view_port_alloc();
  460. view_port_draw_callback_set(view_port, tama_p1_draw_callback, g_state_mutex);
  461. view_port_input_callback_set(view_port, tama_p1_input_callback, event_queue);
  462. Gui* gui = furi_record_open(RECORD_GUI);
  463. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  464. FuriTimer* timer =
  465. furi_timer_alloc(tama_p1_update_timer_callback, FuriTimerTypePeriodic, event_queue);
  466. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 30);
  467. // portrait_mode = false;
  468. // in_menu = false;
  469. // menu_cursor = 0;
  470. for(bool running = true; running;) {
  471. TamaEvent event;
  472. FuriStatus event_status = furi_message_queue_get(event_queue, &event, FuriWaitForever);
  473. if(event_status == FuriStatusOk) {
  474. // Local override with acquired context
  475. if(furi_mutex_acquire(g_state_mutex, FuriWaitForever) != FuriStatusOk) continue;
  476. if(event.type == EventTypeTick) {
  477. // FURI_LOG_D(TAG, "EventTypeTick");
  478. view_port_update(view_port);
  479. } else if(event.type == EventTypeInput) {
  480. FURI_LOG_D(
  481. TAG,
  482. "EventTypeInput: %ld %d %d",
  483. event.input.sequence,
  484. event.input.key,
  485. event.input.type);
  486. InputType input_type = event.input.type;
  487. btn_state_t tama_btn_state = 0;
  488. if(input_type == InputTypePress)
  489. tama_btn_state = BTN_STATE_PRESSED;
  490. else if(input_type == InputTypeRelease)
  491. tama_btn_state = BTN_STATE_RELEASED;
  492. if(in_menu) {
  493. // if(input_type == InputTypePress) {
  494. // if(portrait_mode)
  495. if(false) { // TODO: Add portrait menu
  496. // switch(event.input.key) {
  497. if(event.input.key == InputKeyLeft &&
  498. event.input.type == InputTypePress) { // Up
  499. if(menu_cursor > 0) {
  500. menu_cursor -= 1;
  501. } else {
  502. menu_cursor = menu_items - 1;
  503. }
  504. } else if(
  505. event.input.key == InputKeyRight &&
  506. event.input.type == InputTypePress) { // Down
  507. if(menu_cursor < menu_items - 1) {
  508. menu_cursor += 1;
  509. } else {
  510. menu_cursor = 0;
  511. }
  512. }
  513. // else if (event.input.key==InputKeyDown) {// Left
  514. // }
  515. // else if (event.input.key==InputKeyUp) { // Right
  516. // }
  517. else if(event.input.key == InputKeyOk) {
  518. bool temp = false;
  519. switch(menu_cursor) {
  520. case 0:
  521. portrait_mode = false;
  522. break;
  523. case 1:
  524. // mute tamagotchi
  525. tamalib_set_button(BTN_LEFT, BTN_STATE_PRESSED);
  526. tamalib_set_button(BTN_RIGHT, BTN_STATE_PRESSED);
  527. temp = true;
  528. break;
  529. case 2:
  530. default:
  531. if(temp) {
  532. tamalib_set_button(BTN_LEFT, BTN_STATE_RELEASED);
  533. tamalib_set_button(BTN_RIGHT, BTN_STATE_RELEASED);
  534. }
  535. in_menu = false;
  536. break;
  537. }
  538. }
  539. // else if (event.input.key==InputKeyBack) {
  540. // in_menu = false;
  541. // }
  542. // }
  543. } else { // landscape
  544. // switch(event.input.key) {
  545. if(event.input.key == InputKeyUp && event.input.type == InputTypePress) {
  546. if(menu_cursor > 0) {
  547. menu_cursor -= 1;
  548. } else {
  549. menu_cursor = menu_items - 1;
  550. }
  551. } else if(
  552. event.input.key == InputKeyDown &&
  553. event.input.type == InputTypePress) {
  554. if(menu_cursor < menu_items - 1) {
  555. menu_cursor += 1;
  556. } else {
  557. menu_cursor = 0;
  558. }
  559. }
  560. // else if (event.input.key==InputKeyLeft) {}
  561. // else if (event.input.key==InputKeyRight) {}
  562. else if(event.input.key == InputKeyOk) {
  563. switch(menu_cursor) {
  564. case 0:
  565. // portrait_mode = true;
  566. portrait_mode = !portrait_mode;
  567. break;
  568. case 1:
  569. // mute tamagotchi
  570. tamalib_set_button(BTN_LEFT, tama_btn_state);
  571. tamalib_set_button(BTN_RIGHT, tama_btn_state);
  572. break;
  573. case 2:
  574. default:
  575. in_menu = false;
  576. break;
  577. }
  578. }
  579. // else if (event.input.key==InputKeyBack) {
  580. // // in_menu = false;
  581. // }
  582. // }
  583. }
  584. // }
  585. } else { // out of menu
  586. if(input_type == InputTypePress || input_type == InputTypeRelease) {
  587. if(portrait_mode) {
  588. if(event.input.key == InputKeyDown) {
  589. tamalib_set_button(BTN_LEFT, tama_btn_state);
  590. } else if(event.input.key == InputKeyOk) {
  591. tamalib_set_button(BTN_MIDDLE, tama_btn_state);
  592. } else if(event.input.key == InputKeyRight) {
  593. tamalib_set_button(BTN_MIDDLE, tama_btn_state);
  594. } else if(event.input.key == InputKeyUp) {
  595. tamalib_set_button(BTN_RIGHT, tama_btn_state);
  596. } else if(event.input.key == InputKeyLeft) {
  597. in_menu = true;
  598. // portrait_mode = false;
  599. } else if(
  600. event.input.key == InputKeyBack &&
  601. event.input.type == InputTypeShort) {
  602. tama_p1_save_state();
  603. }
  604. } else {
  605. if(event.input.key == InputKeyLeft) {
  606. tamalib_set_button(BTN_LEFT, tama_btn_state);
  607. } else if(event.input.key == InputKeyOk) {
  608. tamalib_set_button(BTN_MIDDLE, tama_btn_state);
  609. } else if(event.input.key == InputKeyDown) {
  610. tamalib_set_button(BTN_MIDDLE, tama_btn_state);
  611. } else if(event.input.key == InputKeyRight) {
  612. tamalib_set_button(BTN_RIGHT, tama_btn_state);
  613. } else if(event.input.key == InputKeyUp) {
  614. in_menu = true;
  615. // portrait_mode = true;
  616. // mute tamagotchi
  617. // tamalib_set_button(BTN_LEFT, tama_btn_state);
  618. // tamalib_set_button(BTN_RIGHT, tama_btn_state);
  619. } else if(
  620. event.input.key == InputKeyBack &&
  621. event.input.type == InputTypeShort) {
  622. tama_p1_save_state();
  623. }
  624. }
  625. }
  626. }
  627. if(event.input.key == InputKeyBack && event.input.type == InputTypeLong &&
  628. !in_menu) {
  629. furi_timer_stop(timer);
  630. running = false;
  631. tama_p1_save_state();
  632. }
  633. }
  634. furi_mutex_release(g_state_mutex);
  635. } else {
  636. // Timeout
  637. // FURI_LOG_D(TAG, "Timed out");
  638. }
  639. }
  640. if(ctx->rom != NULL) {
  641. furi_thread_flags_set(furi_thread_get_id(ctx->thread), 1);
  642. furi_thread_join(ctx->thread);
  643. }
  644. furi_timer_free(timer);
  645. view_port_enabled_set(view_port, false);
  646. gui_remove_view_port(gui, view_port);
  647. furi_record_close(RECORD_GUI);
  648. view_port_free(view_port);
  649. furi_message_queue_free(event_queue);
  650. furi_mutex_free(g_state_mutex);
  651. tama_p1_deinit(ctx);
  652. free(ctx);
  653. return 0;
  654. }