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