malveke_gb_emulator.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #include "malveke_gb_emulator.h"
  2. #include <malveke_gb_emulator_icons.h>
  3. static void malveke_gb_emulator_view_draw_callback(Canvas* canvas, void* _model) {
  4. UartDumpModel* model = _model;
  5. // Prepare canvas
  6. canvas_set_color(canvas, ColorBlack);
  7. canvas_draw_frame(canvas, 0, 0, FRAME_WIDTH, FRAME_HEIGTH);
  8. for(size_t p = 0; p < FRAME_BUFFER_LENGTH; ++p) {
  9. uint8_t x = p % ROW_BUFFER_LENGTH; // 0 .. 15
  10. uint8_t y = p / ROW_BUFFER_LENGTH; // 0 .. 63
  11. for(uint8_t i = 0; i < 8; ++i) {
  12. if((model->pixels[p] & (1 << (7 - i))) != 0) {
  13. canvas_draw_dot(canvas, (x * 8) + i, y);
  14. }
  15. }
  16. }
  17. if (!model->initialized){
  18. canvas_draw_icon(canvas, 60, 7, &I_malveke_67x49);
  19. canvas_set_font(canvas, FontSecondary);
  20. canvas_draw_str(canvas, 4, 25, "Connect");
  21. canvas_set_font(canvas, FontPrimary);
  22. canvas_draw_str(canvas, 4, 35, "MALVEKE");
  23. canvas_set_font(canvas, FontSecondary);
  24. canvas_draw_str(canvas, 4, 44, "into Flipper");
  25. elements_button_center(canvas, "Ok");
  26. // canvas_set_font(canvas, FontPrimary);
  27. // canvas_draw_str(canvas, 8, 28, "GAME BOY");
  28. // canvas_draw_icon(canvas, 76, 8, &I_gbcam_48x49);
  29. // canvas_set_font(canvas, FontSecondary);
  30. // canvas_draw_str(canvas, 8, 18, "WAITING");
  31. // canvas_set_font(canvas, FontPrimary);
  32. // canvas_draw_str(canvas, 8, 38, "CAMERA...");
  33. // canvas_set_font(canvas, FontSecondary);
  34. // canvas_draw_str(canvas, 9, 47, "Insert Cartridge");
  35. // elements_button_center(canvas, "Ok");
  36. }
  37. }
  38. static bool malveke_gb_emulator_view_input_callback(InputEvent* event, void* context) {
  39. UartEchoApp* instance = context;
  40. UNUSED(instance);
  41. if (event->type == InputTypePress){
  42. if (event->key == InputKeyUp){
  43. const char gbemulator_command_up[] = "U\n";
  44. furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t*)gbemulator_command_up, strlen(gbemulator_command_up));
  45. }
  46. else if (event->key == InputKeyDown){
  47. const char gbemulator_command_down[] = "D\n";
  48. furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t*)gbemulator_command_down, strlen(gbemulator_command_down));
  49. }
  50. else if (event->key == InputKeyRight){
  51. const char gbemulator_command_right[] = ">\n";
  52. furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t*)gbemulator_command_right, strlen(gbemulator_command_right));
  53. }
  54. else if (event->key == InputKeyLeft){
  55. const char gbemulator_command_left[] = "<\n";
  56. furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t*)gbemulator_command_left, strlen(gbemulator_command_left));
  57. }
  58. else if (event->key == InputKeyOk){
  59. with_view_model(
  60. instance->view,
  61. UartDumpModel * model,
  62. {
  63. if(!model->initialized) {
  64. model->initialized = true;
  65. } else {
  66. const char gbemulator_command_OK[] = "S\n";
  67. furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t*)gbemulator_command_OK, strlen(gbemulator_command_OK));
  68. }
  69. },
  70. false);
  71. }
  72. }
  73. return false;
  74. }
  75. static uint32_t malveke_gb_emulator_exit(void* context) {
  76. UNUSED(context);
  77. const char stop_command[] = "stopgblivecamera\n";
  78. furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t*)stop_command, strlen(stop_command));
  79. return VIEW_NONE;
  80. }
  81. static void malveke_gb_emulator_on_irq_cb(UartIrqEvent ev, uint8_t data, void* context) {
  82. furi_assert(context);
  83. UartEchoApp* app = context;
  84. if(ev == UartIrqEventRXNE) {
  85. furi_stream_buffer_send(app->rx_stream, &data, 1, 0);
  86. furi_thread_flags_set(furi_thread_get_id(app->worker_thread), WorkerEventRx);
  87. }
  88. }
  89. static void process_ringbuffer(UartDumpModel* model, uint8_t byte) {
  90. //// 1. Phase: filling the ringbuffer
  91. if (model->ringbuffer_index == 0 && byte != 'Y'){ // First char has to be 'Y' in the buffer.
  92. return;
  93. }
  94. if (model->ringbuffer_index == 1 && byte != ':'){ // Second char has to be ':' in the buffer or reset.
  95. model->ringbuffer_index = 0;
  96. process_ringbuffer(model, byte);
  97. return;
  98. }
  99. model->row_ringbuffer[model->ringbuffer_index] = byte; // Assign current byte to the ringbuffer;
  100. ++model->ringbuffer_index; // Increment the ringbuffer index
  101. if (model->ringbuffer_index < RING_BUFFER_LENGTH){ // Let's wait 'till the buffer fills.
  102. return;
  103. }
  104. //// 2. Phase: flushing the ringbuffer to the framebuffer
  105. model->ringbuffer_index = 0; // Let's reset the ringbuffer
  106. // model->initialized = true; // We've successfully established the connection
  107. size_t row_start_index = model->row_ringbuffer[2] * ROW_BUFFER_LENGTH; // Third char will determine the row number
  108. if (row_start_index > LAST_ROW_INDEX){ // Failsafe
  109. row_start_index = 0;
  110. }
  111. for (size_t i = 0; i < ROW_BUFFER_LENGTH; ++i) {
  112. model->pixels[row_start_index + i] = model->row_ringbuffer[i+3]; // Writing the remaining 16 bytes into the frame buffer
  113. }
  114. }
  115. static int32_t malveke_gb_emulator_worker(void* context) {
  116. furi_assert(context);
  117. UartEchoApp* app = context;
  118. while(1) {
  119. uint32_t events =
  120. furi_thread_flags_wait(WORKER_EVENTS_MASK, FuriFlagWaitAny, FuriWaitForever);
  121. furi_check((events & FuriFlagError) == 0);
  122. if(events & WorkerEventStop) break;
  123. if(events & WorkerEventRx) {
  124. size_t length = 0;
  125. do {
  126. size_t intended_data_size = 64;
  127. uint8_t data[intended_data_size];
  128. length = furi_stream_buffer_receive(app->rx_stream, data, intended_data_size, 0);
  129. if(length > 0) {
  130. with_view_model(
  131. app->view,
  132. UartDumpModel * model, {
  133. for(size_t i = 0; i < length; i++) {
  134. process_ringbuffer(model, data[i]);
  135. }
  136. },
  137. false);
  138. }
  139. } while(length > 0);
  140. notification_message(app->notification, &sequence_notification);
  141. with_view_model(app->view, UartDumpModel * model, { UNUSED(model); }, true);
  142. }
  143. }
  144. return 0;
  145. }
  146. static UartEchoApp* malveke_gb_emulator_app_alloc() {
  147. UartEchoApp* app = malloc(sizeof(UartEchoApp));
  148. app->rx_stream = furi_stream_buffer_alloc(2048, 1);
  149. // Gui
  150. app->gui = furi_record_open(RECORD_GUI);
  151. app->notification = furi_record_open(RECORD_NOTIFICATION);
  152. // View dispatcher
  153. app->view_dispatcher = view_dispatcher_alloc();
  154. view_dispatcher_enable_queue(app->view_dispatcher);
  155. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  156. // Views
  157. app->view = view_alloc();
  158. view_set_context(app->view, app);
  159. view_set_draw_callback(app->view, malveke_gb_emulator_view_draw_callback);
  160. view_set_input_callback(app->view, malveke_gb_emulator_view_input_callback);
  161. view_allocate_model(app->view, ViewModelTypeLocking, sizeof(UartDumpModel));
  162. with_view_model(
  163. app->view,
  164. UartDumpModel * model,
  165. {
  166. for(size_t i = 0; i < FRAME_BUFFER_LENGTH; i++) {
  167. model->pixels[i] = 0;
  168. }
  169. },
  170. true);
  171. view_set_previous_callback(app->view, malveke_gb_emulator_exit);
  172. view_dispatcher_add_view(app->view_dispatcher, 0, app->view);
  173. view_dispatcher_switch_to_view(app->view_dispatcher, 0);
  174. app->worker_thread = furi_thread_alloc_ex("UsbUartWorker", 2048, malveke_gb_emulator_worker, app);
  175. furi_thread_start(app->worker_thread);
  176. // Enable uart listener (UART & UART1)
  177. // furi_hal_console_disable();
  178. furi_hal_uart_set_br(FuriHalUartIdUSART1, 115200);
  179. furi_hal_uart_init(FuriHalUartIdLPUART1, 250000);
  180. furi_hal_uart_set_br(FuriHalUartIdLPUART1, 250000);
  181. furi_hal_uart_set_irq_cb(FuriHalUartIdLPUART1, malveke_gb_emulator_on_irq_cb, app);
  182. // furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, malveke_gb_emulator_on_irq_cb, app);
  183. furi_hal_power_enable_otg();
  184. furi_delay_ms(1);
  185. return app;
  186. }
  187. static void malveke_gb_emulator_app_free(UartEchoApp* app) {
  188. furi_assert(app);
  189. furi_thread_flags_set(furi_thread_get_id(app->worker_thread), WorkerEventStop);
  190. furi_thread_join(app->worker_thread);
  191. furi_thread_free(app->worker_thread);
  192. // furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, NULL, NULL);
  193. furi_hal_uart_set_irq_cb(FuriHalUartIdLPUART1, NULL, NULL);
  194. furi_hal_uart_deinit(FuriHalUartIdLPUART1);
  195. // Free views
  196. view_dispatcher_remove_view(app->view_dispatcher, 0);
  197. view_free(app->view);
  198. view_dispatcher_free(app->view_dispatcher);
  199. // Close gui record
  200. furi_record_close(RECORD_GUI);
  201. furi_record_close(RECORD_NOTIFICATION);
  202. app->gui = NULL;
  203. furi_stream_buffer_free(app->rx_stream);
  204. // Free rest
  205. free(app);
  206. }
  207. int32_t malveke_gb_emulator_app(void* p) {
  208. UNUSED(p);
  209. UartEchoApp* app = malveke_gb_emulator_app_alloc();
  210. view_dispatcher_run(app->view_dispatcher);
  211. malveke_gb_emulator_app_free(app);
  212. furi_hal_power_disable_otg();
  213. return 0;
  214. }