malveke_gb_emulator.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. const char gbemulator_command_OK[] = "S\n";
  64. furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t*)gbemulator_command_OK, strlen(gbemulator_command_OK));
  65. },
  66. false);
  67. }
  68. }
  69. return false;
  70. }
  71. static uint32_t malveke_gb_emulator_exit(void* context) {
  72. UNUSED(context);
  73. const char stop_command[] = "stopgblivecamera\n";
  74. furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t*)stop_command, strlen(stop_command));
  75. return VIEW_NONE;
  76. }
  77. static void malveke_gb_emulator_on_irq_cb(UartIrqEvent ev, uint8_t data, void* context) {
  78. furi_assert(context);
  79. UartEchoApp* app = context;
  80. if(ev == UartIrqEventRXNE) {
  81. furi_stream_buffer_send(app->rx_stream, &data, 1, 0);
  82. furi_thread_flags_set(furi_thread_get_id(app->worker_thread), WorkerEventRx);
  83. }
  84. }
  85. static void process_ringbuffer(UartDumpModel* model, uint8_t byte) {
  86. //// 1. Phase: filling the ringbuffer
  87. if (model->ringbuffer_index == 0 && byte != 'Y'){ // First char has to be 'Y' in the buffer.
  88. return;
  89. }
  90. if (model->ringbuffer_index == 1 && byte != ':'){ // Second char has to be ':' in the buffer or reset.
  91. model->ringbuffer_index = 0;
  92. process_ringbuffer(model, byte);
  93. return;
  94. }
  95. model->row_ringbuffer[model->ringbuffer_index] = byte; // Assign current byte to the ringbuffer;
  96. ++model->ringbuffer_index; // Increment the ringbuffer index
  97. if (model->ringbuffer_index < RING_BUFFER_LENGTH){ // Let's wait 'till the buffer fills.
  98. return;
  99. }
  100. //// 2. Phase: flushing the ringbuffer to the framebuffer
  101. model->ringbuffer_index = 0; // Let's reset the ringbuffer
  102. model->initialized = true; // We've successfully established the connection
  103. size_t row_start_index = model->row_ringbuffer[2] * ROW_BUFFER_LENGTH; // Third char will determine the row number
  104. if (row_start_index > LAST_ROW_INDEX){ // Failsafe
  105. row_start_index = 0;
  106. }
  107. for (size_t i = 0; i < ROW_BUFFER_LENGTH; ++i) {
  108. model->pixels[row_start_index + i] = model->row_ringbuffer[i+3]; // Writing the remaining 16 bytes into the frame buffer
  109. }
  110. }
  111. static int32_t malveke_gb_emulator_worker(void* context) {
  112. furi_assert(context);
  113. UartEchoApp* app = context;
  114. while(1) {
  115. uint32_t events =
  116. furi_thread_flags_wait(WORKER_EVENTS_MASK, FuriFlagWaitAny, FuriWaitForever);
  117. furi_check((events & FuriFlagError) == 0);
  118. if(events & WorkerEventStop) break;
  119. if(events & WorkerEventRx) {
  120. size_t length = 0;
  121. do {
  122. size_t intended_data_size = 64;
  123. uint8_t data[intended_data_size];
  124. length = furi_stream_buffer_receive(app->rx_stream, data, intended_data_size, 0);
  125. if(length > 0) {
  126. with_view_model(
  127. app->view,
  128. UartDumpModel * model, {
  129. for(size_t i = 0; i < length; i++) {
  130. process_ringbuffer(model, data[i]);
  131. }
  132. },
  133. false);
  134. }
  135. } while(length > 0);
  136. notification_message(app->notification, &sequence_notification);
  137. with_view_model(app->view, UartDumpModel * model, { UNUSED(model); }, true);
  138. }
  139. }
  140. return 0;
  141. }
  142. static UartEchoApp* malveke_gb_emulator_app_alloc() {
  143. UartEchoApp* app = malloc(sizeof(UartEchoApp));
  144. app->rx_stream = furi_stream_buffer_alloc(2048, 1);
  145. // Gui
  146. app->gui = furi_record_open(RECORD_GUI);
  147. app->notification = furi_record_open(RECORD_NOTIFICATION);
  148. // View dispatcher
  149. app->view_dispatcher = view_dispatcher_alloc();
  150. view_dispatcher_enable_queue(app->view_dispatcher);
  151. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  152. // Views
  153. app->view = view_alloc();
  154. view_set_context(app->view, app);
  155. view_set_draw_callback(app->view, malveke_gb_emulator_view_draw_callback);
  156. view_set_input_callback(app->view, malveke_gb_emulator_view_input_callback);
  157. view_allocate_model(app->view, ViewModelTypeLocking, sizeof(UartDumpModel));
  158. with_view_model(
  159. app->view,
  160. UartDumpModel * model,
  161. {
  162. for(size_t i = 0; i < FRAME_BUFFER_LENGTH; i++) {
  163. model->pixels[i] = 0;
  164. }
  165. },
  166. true);
  167. view_set_previous_callback(app->view, malveke_gb_emulator_exit);
  168. view_dispatcher_add_view(app->view_dispatcher, 0, app->view);
  169. view_dispatcher_switch_to_view(app->view_dispatcher, 0);
  170. app->worker_thread = furi_thread_alloc_ex("UsbUartWorker", 2048, malveke_gb_emulator_worker, app);
  171. furi_thread_start(app->worker_thread);
  172. // Enable uart listener (UART & UART1)
  173. // furi_hal_console_disable();
  174. furi_hal_uart_set_br(FuriHalUartIdUSART1, 115200);
  175. furi_hal_uart_init(FuriHalUartIdLPUART1, 250000);
  176. furi_hal_uart_set_br(FuriHalUartIdLPUART1, 250000);
  177. furi_hal_uart_set_irq_cb(FuriHalUartIdLPUART1, malveke_gb_emulator_on_irq_cb, app);
  178. // furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, malveke_gb_emulator_on_irq_cb, app);
  179. furi_hal_power_enable_otg();
  180. furi_delay_ms(1);
  181. return app;
  182. }
  183. static void malveke_gb_emulator_app_free(UartEchoApp* app) {
  184. furi_assert(app);
  185. furi_thread_flags_set(furi_thread_get_id(app->worker_thread), WorkerEventStop);
  186. furi_thread_join(app->worker_thread);
  187. furi_thread_free(app->worker_thread);
  188. // furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, NULL, NULL);
  189. furi_hal_uart_set_irq_cb(FuriHalUartIdLPUART1, NULL, NULL);
  190. furi_hal_uart_deinit(FuriHalUartIdLPUART1);
  191. // Free views
  192. view_dispatcher_remove_view(app->view_dispatcher, 0);
  193. view_free(app->view);
  194. view_dispatcher_free(app->view_dispatcher);
  195. // Close gui record
  196. furi_record_close(RECORD_GUI);
  197. furi_record_close(RECORD_NOTIFICATION);
  198. app->gui = NULL;
  199. furi_stream_buffer_free(app->rx_stream);
  200. // Free rest
  201. free(app);
  202. }
  203. int32_t malveke_gb_emulator_app(void* p) {
  204. UNUSED(p);
  205. UartEchoApp* app = malveke_gb_emulator_app_alloc();
  206. view_dispatcher_run(app->view_dispatcher);
  207. malveke_gb_emulator_app_free(app);
  208. furi_hal_power_disable_otg();
  209. return 0;
  210. }