camera.c 9.8 KB

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