gb_live_camera.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. if(!model->initialized) {
  9. canvas_set_font(canvas, FontPrimary);
  10. canvas_draw_str(canvas, 8, 28, "GAME BOY");
  11. canvas_draw_icon(canvas, 76, 8, &I_gbcam_48x49);
  12. canvas_set_font(canvas, FontSecondary);
  13. canvas_draw_str(canvas, 8, 18, "WAITING");
  14. canvas_set_font(canvas, FontPrimary);
  15. canvas_draw_str(canvas, 8, 38, "CAMERA...");
  16. canvas_set_font(canvas, FontSecondary);
  17. canvas_draw_str(canvas, 9, 47, "Insert Cartridge");
  18. elements_button_center(canvas, "Ok");
  19. } else {
  20. for(size_t p = 0; p < FRAME_BUFFER_LENGTH; ++p) {
  21. uint8_t x = p % ROW_BUFFER_LENGTH; // 0 .. 15
  22. uint8_t y = p / ROW_BUFFER_LENGTH; // 0 .. 63
  23. for(uint8_t i = 0; i < 8; ++i) {
  24. if((model->pixels[p] & (1 << (7 - i))) == 0) {
  25. canvas_draw_dot(canvas, (x * 8) + i, y);
  26. }
  27. }
  28. }
  29. }
  30. }
  31. void get_timefilename(FuriString* name) {
  32. DateTime datetime = {0};
  33. furi_hal_rtc_get_datetime(&datetime);
  34. furi_string_printf(
  35. name,
  36. "%s/%.4d%.2d%.2d-%.2d%.2d%.2d.bmp",
  37. MALVEKE_APP_FOLDER_PHOTOS,
  38. datetime.year,
  39. datetime.month,
  40. datetime.day,
  41. datetime.hour,
  42. datetime.minute,
  43. datetime.second);
  44. }
  45. static void save_image(void* context) {
  46. UartEchoApp* app = context;
  47. furi_assert(app);
  48. NotificationApp* notifications = furi_record_open(RECORD_NOTIFICATION);
  49. // We need a storage struct (gain accesso to the filesystem API )
  50. Storage* storage = furi_record_open(RECORD_STORAGE);
  51. // storage_file_alloc gives to us a File pointer using the Storage API.
  52. File* file = storage_file_alloc(storage);
  53. // if(storage_common_stat(storage, IMAGE_FILE_DIRECTORY_PATH, NULL) == FSE_NOT_EXIST) {
  54. // storage_simply_mkdir(storage, IMAGE_FILE_DIRECTORY_PATH);
  55. // }
  56. // Create MALVEKE dir
  57. if(storage_common_stat(storage, MALVEKE_APP_FOLDER, NULL) == FSE_NOT_EXIST) {
  58. storage_simply_mkdir(storage, MALVEKE_APP_FOLDER);
  59. }
  60. // Create MALVEKE Photos dir
  61. if(storage_common_stat(storage, MALVEKE_APP_FOLDER_PHOTOS, NULL) == FSE_NOT_EXIST) {
  62. storage_simply_mkdir(storage, MALVEKE_APP_FOLDER_PHOTOS);
  63. }
  64. // create file name
  65. FuriString* file_name = furi_string_alloc();
  66. get_timefilename(file_name);
  67. // this functions open a file, using write access and creates new file if not exist.
  68. bool result =
  69. storage_file_open(file, furi_string_get_cstr(file_name), FSAM_WRITE, FSOM_OPEN_ALWAYS);
  70. //bool result = storage_file_open(file, EXT_PATH("DCIM/test.bmp"), FSAM_WRITE, FSOM_OPEN_ALWAYS);
  71. furi_string_free(file_name);
  72. if(result) {
  73. storage_file_write(file, bitmap_header, BITMAP_HEADER_LENGTH);
  74. with_view_model(
  75. app->view,
  76. UartDumpModel * model,
  77. {
  78. int8_t row_buffer[ROW_BUFFER_LENGTH];
  79. for(size_t i = 64; i > 0; --i) {
  80. for(size_t j = 0; j < ROW_BUFFER_LENGTH; ++j) {
  81. row_buffer[j] = model->pixels[((i - 1) * ROW_BUFFER_LENGTH) + j];
  82. }
  83. storage_file_write(file, row_buffer, ROW_BUFFER_LENGTH);
  84. }
  85. },
  86. false);
  87. }
  88. // Closing the "file descriptor"
  89. storage_file_close(file);
  90. // Freeing up memory
  91. storage_file_free(file);
  92. notification_message(notifications, result ? &sequence_success : &sequence_error);
  93. }
  94. static bool gb_live_camera_view_input_callback(InputEvent* event, void* context) {
  95. UartEchoApp* instance = context;
  96. if(event->type == InputTypePress) {
  97. if(event->key == InputKeyUp) {
  98. const char gblivecamera_command_enable_dithering[] = "gblivecamera -D\n";
  99. furi_hal_serial_tx(
  100. instance->serial_handle_uart,
  101. (uint8_t*)gblivecamera_command_enable_dithering,
  102. strlen(gblivecamera_command_enable_dithering));
  103. } else if(event->key == InputKeyDown) {
  104. const char gblivecamera_command_disable_dithering[] = "gblivecamera -d\n";
  105. furi_hal_serial_tx(
  106. instance->serial_handle_uart,
  107. (uint8_t*)gblivecamera_command_disable_dithering,
  108. strlen(gblivecamera_command_disable_dithering));
  109. } else if(event->key == InputKeyRight) {
  110. const char gblivecamera_command_increase_exposure[] = "gblivecamera -E\n";
  111. furi_hal_serial_tx(
  112. instance->serial_handle_uart,
  113. (uint8_t*)gblivecamera_command_increase_exposure,
  114. strlen(gblivecamera_command_increase_exposure));
  115. } else if(event->key == InputKeyLeft) {
  116. const char gblivecamera_command_decrease_exposure[] = "gblivecamera -e\n";
  117. furi_hal_serial_tx(
  118. instance->serial_handle_uart,
  119. (uint8_t*)gblivecamera_command_decrease_exposure,
  120. strlen(gblivecamera_command_decrease_exposure));
  121. } else if(event->key == InputKeyOk) {
  122. with_view_model(
  123. instance->view,
  124. UartDumpModel * model,
  125. {
  126. if(!model->initialized) {
  127. // model->initialized = true; // We've successfully established the connection
  128. const char gblivecamera_command[] = "gblivecamera\n\n";
  129. furi_hal_serial_tx(
  130. instance->serial_handle_uart,
  131. (uint8_t*)gblivecamera_command,
  132. strlen(gblivecamera_command));
  133. } else {
  134. save_image(context);
  135. }
  136. },
  137. true);
  138. }
  139. }
  140. return false;
  141. }
  142. static uint32_t gb_live_camera_exit(void* context) {
  143. UartEchoApp* app = context;
  144. const char stop_command[] = "stopgblivecamera\n";
  145. // furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t*)stop_command, strlen(stop_command));
  146. furi_hal_serial_tx(app->serial_handle_uart, (uint8_t*)stop_command, strlen(stop_command));
  147. return VIEW_NONE;
  148. }
  149. static void gb_live_camera_on_irq_cb(
  150. FuriHalSerialHandle* handle,
  151. FuriHalSerialRxEvent event,
  152. void* context) {
  153. furi_assert(context);
  154. UartEchoApp* app = context;
  155. if(event == FuriHalSerialRxEventData) {
  156. uint8_t data = furi_hal_serial_async_rx(handle);
  157. furi_stream_buffer_send(app->rx_stream, &data, 1, 0);
  158. furi_thread_flags_set(furi_thread_get_id(app->worker_thread), WorkerEventRx);
  159. }
  160. }
  161. static void process_ringbuffer(UartDumpModel* model, uint8_t byte) {
  162. //// 1. Phase: filling the ringbuffer
  163. if(model->ringbuffer_index == 0 && byte != 'Y') { // First char has to be 'Y' in the buffer.
  164. return;
  165. }
  166. if(model->ringbuffer_index == 1 &&
  167. byte != ':') { // Second char has to be ':' in the buffer or reset.
  168. model->ringbuffer_index = 0;
  169. process_ringbuffer(model, byte);
  170. return;
  171. }
  172. model->row_ringbuffer[model->ringbuffer_index] =
  173. byte; // Assign current byte to the ringbuffer;
  174. ++model->ringbuffer_index; // Increment the ringbuffer index
  175. if(model->ringbuffer_index < RING_BUFFER_LENGTH) { // Let's wait 'till the buffer fills.
  176. return;
  177. }
  178. //// 2. Phase: flushing the ringbuffer to the framebuffer
  179. model->ringbuffer_index = 0; // Let's reset the ringbuffer
  180. model->initialized = true; // We've successfully established the connection
  181. size_t row_start_index =
  182. model->row_ringbuffer[2] * ROW_BUFFER_LENGTH; // Third char will determine the row number
  183. if(row_start_index > LAST_ROW_INDEX) { // Failsafe
  184. row_start_index = 0;
  185. }
  186. for(size_t i = 0; i < ROW_BUFFER_LENGTH; ++i) {
  187. model->pixels[row_start_index + i] =
  188. model->row_ringbuffer[i + 3]; // Writing the remaining 16 bytes into the frame buffer
  189. }
  190. }
  191. static int32_t gb_live_camera_worker(void* context) {
  192. furi_assert(context);
  193. UartEchoApp* app = context;
  194. while(1) {
  195. uint32_t events =
  196. furi_thread_flags_wait(WORKER_EVENTS_MASK, FuriFlagWaitAny, FuriWaitForever);
  197. furi_check((events & FuriFlagError) == 0);
  198. if(events & WorkerEventStop) break;
  199. if(events & WorkerEventRx) {
  200. size_t length = 0;
  201. do {
  202. size_t intended_data_size = 64;
  203. uint8_t data[intended_data_size];
  204. length = furi_stream_buffer_receive(app->rx_stream, data, intended_data_size, 0);
  205. if(length > 0) {
  206. with_view_model(
  207. app->view,
  208. UartDumpModel * model,
  209. {
  210. for(size_t i = 0; i < length; i++) {
  211. process_ringbuffer(model, data[i]);
  212. }
  213. },
  214. false);
  215. }
  216. } while(length > 0);
  217. notification_message(app->notification, &sequence_notification);
  218. with_view_model(app->view, UartDumpModel * model, { UNUSED(model); }, true);
  219. }
  220. }
  221. return 0;
  222. }
  223. static UartEchoApp* gb_live_camera_app_alloc() {
  224. UartEchoApp* app = malloc(sizeof(UartEchoApp));
  225. app->rx_stream = furi_stream_buffer_alloc(2048, 1);
  226. // Gui
  227. app->gui = furi_record_open(RECORD_GUI);
  228. app->notification = furi_record_open(RECORD_NOTIFICATION);
  229. // View dispatcher
  230. app->view_dispatcher = view_dispatcher_alloc();
  231. view_dispatcher_enable_queue(app->view_dispatcher);
  232. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  233. // Turn backlight on
  234. notification_message(app->notification, &sequence_display_backlight_enforce_on);
  235. // Views
  236. app->view = view_alloc();
  237. view_set_context(app->view, app);
  238. view_set_draw_callback(app->view, gb_live_camera_view_draw_callback);
  239. view_set_input_callback(app->view, gb_live_camera_view_input_callback);
  240. view_allocate_model(app->view, ViewModelTypeLocking, sizeof(UartDumpModel));
  241. with_view_model(
  242. app->view,
  243. UartDumpModel * model,
  244. {
  245. for(size_t i = 0; i < FRAME_BUFFER_LENGTH; i++) {
  246. model->pixels[i] = 0;
  247. }
  248. },
  249. true);
  250. view_set_previous_callback(app->view, gb_live_camera_exit);
  251. view_dispatcher_add_view(app->view_dispatcher, 0, app->view);
  252. view_dispatcher_switch_to_view(app->view_dispatcher, 0);
  253. app->worker_thread = furi_thread_alloc_ex("UsbUartWorker", 2048, gb_live_camera_worker, app);
  254. furi_thread_start(app->worker_thread);
  255. // Enable uart listener (UART & UART1)
  256. app->serial_handle_uart = furi_hal_serial_control_acquire(FuriHalSerialIdUsart);
  257. if(!app->serial_handle_uart) {
  258. furi_delay_ms(5000);
  259. }
  260. furi_check(app->serial_handle_uart);
  261. furi_hal_serial_init(app->serial_handle_uart, BAUDRATE);
  262. app->serial_handle_lp_uart = furi_hal_serial_control_acquire(FuriHalSerialIdLpuart);
  263. if(!app->serial_handle_lp_uart) {
  264. furi_delay_ms(5000);
  265. }
  266. furi_check(app->serial_handle_lp_uart);
  267. furi_hal_serial_init(app->serial_handle_lp_uart, BAUDRATE);
  268. furi_hal_serial_async_rx_start(
  269. app->serial_handle_lp_uart, gb_live_camera_on_irq_cb, app, false);
  270. // furi_hal_console_disable();
  271. // furi_hal_uart_set_br(FuriHalUartIdUSART1, 115200);
  272. // furi_hal_uart_init(FuriHalUartIdLPUART1, 115200);
  273. // furi_hal_uart_set_br(FuriHalUartIdLPUART1, 115200);
  274. // furi_hal_uart_set_irq_cb(FuriHalUartIdLPUART1, gb_live_camera_on_irq_cb, app);
  275. // furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, gb_live_camera_on_irq_cb, app);
  276. furi_hal_power_enable_otg();
  277. furi_delay_ms(1);
  278. return app;
  279. }
  280. static void gb_live_camera_app_free(UartEchoApp* app) {
  281. furi_assert(app);
  282. furi_thread_flags_set(furi_thread_get_id(app->worker_thread), WorkerEventStop);
  283. furi_thread_join(app->worker_thread);
  284. furi_thread_free(app->worker_thread);
  285. furi_hal_serial_deinit(app->serial_handle_uart);
  286. furi_hal_serial_control_release(app->serial_handle_uart);
  287. furi_hal_serial_deinit(app->serial_handle_lp_uart);
  288. furi_hal_serial_control_release(app->serial_handle_lp_uart);
  289. notification_message(app->notification, &sequence_display_backlight_enforce_auto);
  290. // Free views
  291. view_dispatcher_remove_view(app->view_dispatcher, 0);
  292. view_free(app->view);
  293. view_dispatcher_free(app->view_dispatcher);
  294. // Close gui record
  295. furi_record_close(RECORD_GUI);
  296. furi_record_close(RECORD_NOTIFICATION);
  297. app->gui = NULL;
  298. furi_stream_buffer_free(app->rx_stream);
  299. // Free rest
  300. free(app);
  301. }
  302. int32_t gb_live_camera_app(void* p) {
  303. UNUSED(p);
  304. // Disable expansion protocol to avoid interference with UART Handle
  305. Expansion* expansion = furi_record_open(RECORD_EXPANSION);
  306. expansion_disable(expansion);
  307. UartEchoApp* app = gb_live_camera_app_alloc();
  308. view_dispatcher_run(app->view_dispatcher);
  309. gb_live_camera_app_free(app);
  310. furi_hal_power_disable_otg();
  311. // Return previous state of expansion
  312. expansion_enable(expansion);
  313. furi_record_close(RECORD_EXPANSION);
  314. return 0;
  315. }