camera.c 10 KB

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