camera.c 10 KB

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