malveke_gb_emulator.c 9.1 KB

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