gb_live_camera.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. #include "gb_live_camera.h"
  2. #include <malveke_gb_live_camera_icons.h>
  3. static void gb_live_camera_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_set_font(canvas, FontPrimary);
  19. canvas_draw_str(canvas, 8, 31, "GAME BOY");
  20. canvas_draw_icon(canvas, 76, 8, &I_gbcam_48x49);
  21. canvas_set_font(canvas, FontSecondary);
  22. canvas_draw_str(canvas, 8, 21, "WAITING");
  23. canvas_set_font(canvas, FontPrimary);
  24. canvas_draw_str(canvas, 8, 41, "CAMERA...");
  25. canvas_set_font(canvas, FontSecondary);
  26. canvas_draw_str(canvas, 9, 50, "Insert Cartridge");
  27. }
  28. }
  29. void get_timefilename(FuriString* name) {
  30. FuriHalRtcDateTime datetime = {0};
  31. furi_hal_rtc_get_datetime(&datetime);
  32. furi_string_printf(
  33. name,
  34. EXT_PATH("DCIM/%.4d%.2d%.2d-%.2d%.2d%.2d.bmp"),
  35. datetime.year,
  36. datetime.month,
  37. datetime.day,
  38. datetime.hour,
  39. datetime.minute,
  40. datetime.second);
  41. }
  42. static void save_image(void* context) {
  43. UartEchoApp* app = context;
  44. furi_assert(app);
  45. NotificationApp* notifications = furi_record_open(RECORD_NOTIFICATION);
  46. // We need a storage struct (gain accesso to the filesystem API )
  47. Storage* storage = furi_record_open(RECORD_STORAGE);
  48. // storage_file_alloc gives to us a File pointer using the Storage API.
  49. File* file = storage_file_alloc(storage);
  50. if(storage_common_stat(storage, IMAGE_FILE_DIRECTORY_PATH, NULL) == FSE_NOT_EXIST) {
  51. storage_simply_mkdir(storage, IMAGE_FILE_DIRECTORY_PATH);
  52. }
  53. // create file name
  54. FuriString* file_name = furi_string_alloc();
  55. get_timefilename(file_name);
  56. // this functions open a file, using write access and creates new file if not exist.
  57. bool result = storage_file_open(file, furi_string_get_cstr(file_name), FSAM_WRITE, FSOM_OPEN_ALWAYS);
  58. //bool result = storage_file_open(file, EXT_PATH("DCIM/test.bmp"), FSAM_WRITE, FSOM_OPEN_ALWAYS);
  59. furi_string_free(file_name);
  60. if (result){
  61. storage_file_write(file, bitmap_header, BITMAP_HEADER_LENGTH);
  62. with_view_model(app->view, UartDumpModel * model, {
  63. int8_t row_buffer[ROW_BUFFER_LENGTH];
  64. for (size_t i = 64; i > 0; --i) {
  65. for (size_t j = 0; j < ROW_BUFFER_LENGTH; ++j){
  66. row_buffer[j] = model->pixels[((i-1)*ROW_BUFFER_LENGTH) + j];
  67. }
  68. storage_file_write(file, row_buffer, ROW_BUFFER_LENGTH);
  69. }
  70. }, false);
  71. }
  72. // Closing the "file descriptor"
  73. storage_file_close(file);
  74. // Freeing up memory
  75. storage_file_free(file);
  76. notification_message(notifications, result ? &sequence_success : &sequence_error);
  77. }
  78. static bool gb_live_camera_view_input_callback(InputEvent* event, void* context) {
  79. if (event->type == InputTypePress){
  80. if (event->key == InputKeyUp){
  81. const char gblivecamera_command_enable_dithering[] = "gblivecamera -D\n";
  82. furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t*)gblivecamera_command_enable_dithering, strlen(gblivecamera_command_enable_dithering));
  83. }
  84. else if (event->key == InputKeyDown){
  85. const char gblivecamera_command_disable_dithering[] = "gblivecamera -d\n";
  86. furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t*)gblivecamera_command_disable_dithering, strlen(gblivecamera_command_disable_dithering));
  87. }
  88. else if (event->key == InputKeyRight){
  89. const char gblivecamera_command_increase_exposure[] = "gblivecamera -E\n";
  90. furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t*)gblivecamera_command_increase_exposure, strlen(gblivecamera_command_increase_exposure));
  91. }
  92. else if (event->key == InputKeyLeft){
  93. const char gblivecamera_command_decrease_exposure[] = "gblivecamera -e\n";
  94. furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t*)gblivecamera_command_decrease_exposure, strlen(gblivecamera_command_decrease_exposure));
  95. }
  96. else if (event->key == InputKeyOk){
  97. save_image(context);
  98. }
  99. }
  100. return false;
  101. }
  102. static uint32_t gb_live_camera_exit(void* context) {
  103. UNUSED(context);
  104. return VIEW_NONE;
  105. }
  106. static void gb_live_camera_on_irq_cb(UartIrqEvent ev, uint8_t data, void* context) {
  107. furi_assert(context);
  108. UartEchoApp* app = context;
  109. if(ev == UartIrqEventRXNE) {
  110. furi_stream_buffer_send(app->rx_stream, &data, 1, 0);
  111. furi_thread_flags_set(furi_thread_get_id(app->worker_thread), WorkerEventRx);
  112. }
  113. }
  114. static void process_ringbuffer(UartDumpModel* model, uint8_t byte) {
  115. //// 1. Phase: filling the ringbuffer
  116. if (model->ringbuffer_index == 0 && byte != 'Y'){ // First char has to be 'Y' in the buffer.
  117. return;
  118. }
  119. if (model->ringbuffer_index == 1 && byte != ':'){ // Second char has to be ':' in the buffer or reset.
  120. model->ringbuffer_index = 0;
  121. process_ringbuffer(model, byte);
  122. return;
  123. }
  124. model->row_ringbuffer[model->ringbuffer_index] = byte; // Assign current byte to the ringbuffer;
  125. ++model->ringbuffer_index; // Increment the ringbuffer index
  126. if (model->ringbuffer_index < RING_BUFFER_LENGTH){ // Let's wait 'till the buffer fills.
  127. return;
  128. }
  129. //// 2. Phase: flushing the ringbuffer to the framebuffer
  130. model->ringbuffer_index = 0; // Let's reset the ringbuffer
  131. model->initialized = true; // We've successfully established the connection
  132. size_t row_start_index = model->row_ringbuffer[2] * ROW_BUFFER_LENGTH; // Third char will determine the row number
  133. if (row_start_index > LAST_ROW_INDEX){ // Failsafe
  134. row_start_index = 0;
  135. }
  136. for (size_t i = 0; i < ROW_BUFFER_LENGTH; ++i) {
  137. model->pixels[row_start_index + i] = model->row_ringbuffer[i+3]; // Writing the remaining 16 bytes into the frame buffer
  138. }
  139. }
  140. static int32_t gb_live_camera_worker(void* context) {
  141. furi_assert(context);
  142. UartEchoApp* app = context;
  143. while(1) {
  144. uint32_t events =
  145. furi_thread_flags_wait(WORKER_EVENTS_MASK, FuriFlagWaitAny, FuriWaitForever);
  146. furi_check((events & FuriFlagError) == 0);
  147. if(events & WorkerEventStop) break;
  148. if(events & WorkerEventRx) {
  149. size_t length = 0;
  150. do {
  151. size_t intended_data_size = 64;
  152. uint8_t data[intended_data_size];
  153. length = furi_stream_buffer_receive(app->rx_stream, data, intended_data_size, 0);
  154. if(length > 0) {
  155. with_view_model(
  156. app->view,
  157. UartDumpModel * model, {
  158. for(size_t i = 0; i < length; i++) {
  159. process_ringbuffer(model, data[i]);
  160. }
  161. },
  162. false);
  163. }
  164. } while(length > 0);
  165. notification_message(app->notification, &sequence_notification);
  166. with_view_model(app->view, UartDumpModel * model, { UNUSED(model); }, true);
  167. }
  168. }
  169. return 0;
  170. }
  171. static UartEchoApp* gb_live_camera_app_alloc() {
  172. UartEchoApp* app = malloc(sizeof(UartEchoApp));
  173. app->rx_stream = furi_stream_buffer_alloc(2048, 1);
  174. // Gui
  175. app->gui = furi_record_open(RECORD_GUI);
  176. app->notification = furi_record_open(RECORD_NOTIFICATION);
  177. // View dispatcher
  178. app->view_dispatcher = view_dispatcher_alloc();
  179. view_dispatcher_enable_queue(app->view_dispatcher);
  180. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  181. // Views
  182. app->view = view_alloc();
  183. view_set_context(app->view, app);
  184. view_set_draw_callback(app->view, gb_live_camera_view_draw_callback);
  185. view_set_input_callback(app->view, gb_live_camera_view_input_callback);
  186. view_allocate_model(app->view, ViewModelTypeLocking, sizeof(UartDumpModel));
  187. with_view_model(
  188. app->view,
  189. UartDumpModel * model,
  190. {
  191. for(size_t i = 0; i < FRAME_BUFFER_LENGTH; i++) {
  192. model->pixels[i] = 0;
  193. }
  194. },
  195. true);
  196. view_set_previous_callback(app->view, gb_live_camera_exit);
  197. view_dispatcher_add_view(app->view_dispatcher, 0, app->view);
  198. view_dispatcher_switch_to_view(app->view_dispatcher, 0);
  199. app->worker_thread = furi_thread_alloc_ex("UsbUartWorker", 2048, gb_live_camera_worker, app);
  200. furi_thread_start(app->worker_thread);
  201. // Enable uart listener
  202. furi_hal_console_disable();
  203. furi_hal_uart_set_br(FuriHalUartIdUSART1, 115200);
  204. furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, gb_live_camera_on_irq_cb, app);
  205. // furi_hal_uart_init(FuriHalUartIdLPUART1, 115200);
  206. // furi_hal_uart_set_br(FuriHalUartIdLPUART1, 115200);
  207. // furi_hal_uart_set_irq_cb(FuriHalUartIdLPUART1, gb_live_camera_on_irq_cb, app);
  208. const char gblivecamera_command[] = "gblivecamera\n\n";
  209. // furi_hal_uart_tx(FuriHalUartIdLPUART1, (uint8_t*)gblivecamera_command, strlen(gblivecamera_command));
  210. furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t*)gblivecamera_command, strlen(gblivecamera_command));
  211. // }
  212. // furi_delay_ms(1);
  213. return app;
  214. }
  215. static void gb_live_camera_app_free(UartEchoApp* app) {
  216. furi_assert(app);
  217. const char stop_command[] = "stopgblivecamera\n";
  218. // furi_hal_uart_tx(FuriHalUartIdLPUART1, (uint8_t*)stop_command, strlen(stop_command));
  219. furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t*)stop_command, strlen(stop_command));
  220. furi_delay_ms(500);
  221. // furi_hal_console_enable(); // this will also clear IRQ callback so thread is no longer referenced
  222. furi_thread_flags_set(furi_thread_get_id(app->worker_thread), WorkerEventStop);
  223. furi_thread_join(app->worker_thread);
  224. furi_thread_free(app->worker_thread);
  225. // Free views
  226. view_dispatcher_remove_view(app->view_dispatcher, 0);
  227. view_free(app->view);
  228. view_dispatcher_free(app->view_dispatcher);
  229. // Close gui record
  230. furi_record_close(RECORD_GUI);
  231. furi_record_close(RECORD_NOTIFICATION);
  232. app->gui = NULL;
  233. furi_stream_buffer_free(app->rx_stream);
  234. // Free rest
  235. free(app);
  236. }
  237. int32_t gb_live_camera_app(void* p) {
  238. UNUSED(p);
  239. UartEchoApp* app = gb_live_camera_app_alloc();
  240. view_dispatcher_run(app->view_dispatcher);
  241. gb_live_camera_app_free(app);
  242. furi_hal_power_disable_otg();
  243. return 0;
  244. }