gb_live_camera.c 11 KB

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