camera_suite_view_camera.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. #include "../camera_suite.h"
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. #include <input/input.h>
  5. #include <gui/elements.h>
  6. #include <dolphin/dolphin.h>
  7. #include "../helpers/camera_suite_haptic.h"
  8. #include "../helpers/camera_suite_speaker.h"
  9. #include "../helpers/camera_suite_led.h"
  10. void camera_suite_view_camera_set_callback(
  11. CameraSuiteViewCamera* instance,
  12. CameraSuiteViewCameraCallback callback,
  13. void* context) {
  14. furi_assert(instance);
  15. furi_assert(callback);
  16. instance->callback = callback;
  17. instance->context = context;
  18. }
  19. // Function to draw pixels on the canvas based on camera orientation
  20. static void draw_pixel_by_orientation(Canvas* canvas, uint8_t x, uint8_t y, uint8_t orientation) {
  21. furi_assert(canvas);
  22. furi_assert(x);
  23. furi_assert(y);
  24. furi_assert(orientation);
  25. switch(orientation) {
  26. default:
  27. case 0: { // Camera rotated 0 degrees (right side up, default)
  28. canvas_draw_dot(canvas, x, y);
  29. break;
  30. }
  31. case 1: { // Camera rotated 90 degrees
  32. canvas_draw_dot(canvas, y, FRAME_WIDTH - 1 - x);
  33. break;
  34. }
  35. case 2: { // Camera rotated 180 degrees (upside down)
  36. canvas_draw_dot(canvas, FRAME_WIDTH - 1 - x, FRAME_HEIGHT - 1 - y);
  37. break;
  38. }
  39. case 3: { // Camera rotated 270 degrees
  40. canvas_draw_dot(canvas, FRAME_HEIGHT - 1 - y, x);
  41. break;
  42. }
  43. }
  44. }
  45. static void camera_suite_view_camera_draw(Canvas* canvas, void* model) {
  46. furi_assert(canvas);
  47. furi_assert(model);
  48. UartDumpModel* uartDumpModel = model;
  49. // Clear the screen.
  50. canvas_set_color(canvas, ColorBlack);
  51. // Draw the frame.
  52. canvas_draw_frame(canvas, 0, 0, FRAME_WIDTH, FRAME_HEIGHT);
  53. for(size_t p = 0; p < FRAME_BUFFER_LENGTH; ++p) {
  54. uint8_t x = p % ROW_BUFFER_LENGTH; // 0 .. 15
  55. uint8_t y = p / ROW_BUFFER_LENGTH; // 0 .. 63
  56. for(uint8_t i = 0; i < 8; ++i) {
  57. if((uartDumpModel->pixels[p] & (1 << (7 - i))) != 0) {
  58. draw_pixel_by_orientation(canvas, (x * 8) + i, y, uartDumpModel->orientation);
  59. }
  60. }
  61. }
  62. // Draw the guide if the camera is not initialized.
  63. if(!uartDumpModel->initialized) {
  64. canvas_draw_icon(canvas, 74, 16, &I_DolphinCommon_56x48);
  65. canvas_set_font(canvas, FontSecondary);
  66. canvas_draw_str(canvas, 8, 12, "Connect the ESP32-CAM");
  67. canvas_draw_str(canvas, 20, 24, "VCC - 3V3");
  68. canvas_draw_str(canvas, 20, 34, "GND - GND");
  69. canvas_draw_str(canvas, 20, 44, "U0R - TX");
  70. canvas_draw_str(canvas, 20, 54, "U0T - RX");
  71. }
  72. }
  73. static void save_image(void* model) {
  74. furi_assert(model);
  75. UartDumpModel* uartDumpModel = model;
  76. // This pointer is used to access the storage.
  77. Storage* storage = furi_record_open(RECORD_STORAGE);
  78. // This pointer is used to access the filesystem.
  79. File* file = storage_file_alloc(storage);
  80. // Store path in local variable.
  81. const char* folderName = EXT_PATH("DCIM");
  82. // Create the folder name for the image file if it does not exist.
  83. if(storage_common_stat(storage, folderName, NULL) == FSE_NOT_EXIST) {
  84. storage_simply_mkdir(storage, folderName);
  85. }
  86. // This pointer is used to access the file name.
  87. FuriString* file_name = furi_string_alloc();
  88. // Get the current date and time.
  89. FuriHalRtcDateTime datetime = {0};
  90. furi_hal_rtc_get_datetime(&datetime);
  91. // Create the file name.
  92. furi_string_printf(
  93. file_name,
  94. EXT_PATH("DCIM/%.4d%.2d%.2d-%.2d%.2d%.2d.bmp"),
  95. datetime.year,
  96. datetime.month,
  97. datetime.day,
  98. datetime.hour,
  99. datetime.minute,
  100. datetime.second);
  101. // Open the file for writing. If the file does not exist (it shouldn't),
  102. // create it.
  103. bool result =
  104. storage_file_open(file, furi_string_get_cstr(file_name), FSAM_WRITE, FSOM_OPEN_ALWAYS);
  105. // Free the file name after use.
  106. furi_string_free(file_name);
  107. // @todo - Add functionaly for saving images inverted if necessary.
  108. // Invert pixel values if necessary.
  109. // if(!model->inverted) { }
  110. for(size_t i = 0; i < FRAME_BUFFER_LENGTH; ++i) {
  111. uartDumpModel->pixels[i] = ~uartDumpModel->pixels[i];
  112. }
  113. // If the file was opened successfully, write the bitmap header and the
  114. // image data.
  115. if(result) {
  116. // Write BMP Header
  117. storage_file_write(file, bitmap_header, BITMAP_HEADER_LENGTH);
  118. // @todo - Add a function for saving the image directly from the
  119. // ESP32-CAM to the Flipper Zero SD card.
  120. // Write locally to the Flipper Zero SD card in the DCIM folder.
  121. int8_t row_buffer[ROW_BUFFER_LENGTH];
  122. // @todo - Save image based on orientation.
  123. for(size_t i = 64; i > 0; --i) {
  124. for(size_t j = 0; j < ROW_BUFFER_LENGTH; ++j) {
  125. row_buffer[j] = uartDumpModel->pixels[((i - 1) * ROW_BUFFER_LENGTH) + j];
  126. }
  127. storage_file_write(file, row_buffer, ROW_BUFFER_LENGTH);
  128. }
  129. }
  130. // Close the file.
  131. storage_file_close(file);
  132. // Free up memory.
  133. storage_file_free(file);
  134. }
  135. static void camera_suite_view_camera_model_init(UartDumpModel* const model) {
  136. furi_assert(model);
  137. for(size_t i = 0; i < FRAME_BUFFER_LENGTH; i++) {
  138. model->pixels[i] = 0;
  139. }
  140. }
  141. static bool camera_suite_view_camera_input(InputEvent* event, void* context) {
  142. furi_assert(context);
  143. furi_assert(event);
  144. CameraSuiteViewCamera* instance = context;
  145. if(event->type == InputTypeRelease) {
  146. switch(event->key) {
  147. default: // Stop all sounds, reset the LED.
  148. with_view_model(
  149. instance->view,
  150. UartDumpModel * model,
  151. {
  152. UNUSED(model);
  153. camera_suite_play_bad_bump(instance->context);
  154. camera_suite_stop_all_sound(instance->context);
  155. camera_suite_led_set_rgb(instance->context, 0, 0, 0);
  156. },
  157. true);
  158. break;
  159. }
  160. } else if(event->type == InputTypePress) {
  161. uint8_t data[1];
  162. switch(event->key) {
  163. // Camera: Stop stream.
  164. case InputKeyBack: {
  165. // Go back to the main menu.
  166. with_view_model(
  167. instance->view,
  168. UartDumpModel * model,
  169. {
  170. UNUSED(model);
  171. instance->callback(CameraSuiteCustomEventSceneCameraBack, instance->context);
  172. },
  173. true);
  174. break;
  175. }
  176. // Camera: Toggle invert on the ESP32-CAM.
  177. case InputKeyLeft: {
  178. data[0] = '<';
  179. with_view_model(
  180. instance->view,
  181. UartDumpModel * model,
  182. {
  183. camera_suite_play_happy_bump(instance->context);
  184. camera_suite_play_input_sound(instance->context);
  185. camera_suite_led_set_rgb(instance->context, 0, 0, 255);
  186. model->inverted = !model->inverted;
  187. instance->callback(CameraSuiteCustomEventSceneCameraLeft, instance->context);
  188. },
  189. true);
  190. break;
  191. }
  192. // Camera: Enable/disable dithering.
  193. case InputKeyRight: {
  194. data[0] = '>';
  195. with_view_model(
  196. instance->view,
  197. UartDumpModel * model,
  198. {
  199. UNUSED(model);
  200. camera_suite_play_happy_bump(instance->context);
  201. camera_suite_play_input_sound(instance->context);
  202. camera_suite_led_set_rgb(instance->context, 0, 0, 255);
  203. instance->callback(CameraSuiteCustomEventSceneCameraRight, instance->context);
  204. },
  205. true);
  206. break;
  207. }
  208. // Camera: Increase contrast.
  209. case InputKeyUp: {
  210. data[0] = 'C';
  211. with_view_model(
  212. instance->view,
  213. UartDumpModel * model,
  214. {
  215. UNUSED(model);
  216. camera_suite_play_happy_bump(instance->context);
  217. camera_suite_play_input_sound(instance->context);
  218. camera_suite_led_set_rgb(instance->context, 0, 0, 255);
  219. instance->callback(CameraSuiteCustomEventSceneCameraUp, instance->context);
  220. },
  221. true);
  222. break;
  223. }
  224. // Camera: Reduce contrast.
  225. case InputKeyDown: {
  226. data[0] = 'c';
  227. with_view_model(
  228. instance->view,
  229. UartDumpModel * model,
  230. {
  231. UNUSED(model);
  232. camera_suite_play_happy_bump(instance->context);
  233. camera_suite_play_input_sound(instance->context);
  234. camera_suite_led_set_rgb(instance->context, 0, 0, 255);
  235. instance->callback(CameraSuiteCustomEventSceneCameraDown, instance->context);
  236. },
  237. true);
  238. break;
  239. }
  240. // Camera: Take picture.
  241. case InputKeyOk: {
  242. // Save picture directly to ESP32-CAM.
  243. // @todo - Add this functionality.
  244. // data[0] = 'P';
  245. // furi_hal_uart_tx(FuriHalUartIdUSART1, data, 1);
  246. with_view_model(
  247. instance->view,
  248. UartDumpModel * model,
  249. {
  250. camera_suite_play_long_bump(instance->context);
  251. camera_suite_play_input_sound(instance->context);
  252. camera_suite_led_set_rgb(instance->context, 0, 0, 255);
  253. // Take a picture if the data is 'P'.
  254. save_image(model);
  255. instance->callback(CameraSuiteCustomEventSceneCameraOk, instance->context);
  256. },
  257. true);
  258. }
  259. // Camera: Do nothing.
  260. case InputKeyMAX:
  261. default: {
  262. break;
  263. }
  264. }
  265. // Send `data` to the ESP32-CAM.
  266. furi_hal_uart_tx(FuriHalUartIdUSART1, data, 1);
  267. }
  268. return true;
  269. }
  270. static void camera_suite_view_camera_exit(void* context) {
  271. UNUSED(context);
  272. // Stop camera stream.
  273. uint8_t stop_camera = 's';
  274. furi_hal_uart_tx(FuriHalUartIdUSART1, &stop_camera, 1);
  275. furi_delay_ms(50);
  276. }
  277. static void camera_suite_view_camera_enter(void* context) {
  278. furi_assert(context);
  279. // Get the camera suite instance context.
  280. CameraSuiteViewCamera* instance = (CameraSuiteViewCamera*)context;
  281. // Get the camera suite instance context.
  282. CameraSuite* instance_context = instance->context;
  283. // Start camera stream.
  284. uint8_t start_camera = 'S';
  285. furi_hal_uart_tx(FuriHalUartIdUSART1, &start_camera, 1);
  286. furi_delay_ms(50);
  287. // Get/set dither type.
  288. uint8_t dither_type = instance_context->dither;
  289. furi_hal_uart_tx(FuriHalUartIdUSART1, &dither_type, 1);
  290. furi_delay_ms(50);
  291. uint32_t orientation = instance_context->orientation;
  292. with_view_model(
  293. instance->view,
  294. UartDumpModel * model,
  295. {
  296. model->inverted = false;
  297. model->orientation = orientation;
  298. camera_suite_view_camera_model_init(model);
  299. },
  300. true);
  301. }
  302. static void camera_on_irq_cb(UartIrqEvent uartIrqEvent, uint8_t data, void* context) {
  303. furi_assert(uartIrqEvent);
  304. furi_assert(data);
  305. furi_assert(context);
  306. // Cast `context` to `CameraSuiteViewCamera*` and store it in `instance`.
  307. CameraSuiteViewCamera* instance = context;
  308. // If `uartIrqEvent` is `UartIrqEventRXNE`, send the data to the
  309. // `rx_stream` and set the `WorkerEventRx` flag.
  310. if(uartIrqEvent == UartIrqEventRXNE) {
  311. furi_stream_buffer_send(instance->rx_stream, &data, 1, 0);
  312. furi_thread_flags_set(furi_thread_get_id(instance->worker_thread), WorkerEventRx);
  313. }
  314. }
  315. static void process_ringbuffer(UartDumpModel* model, uint8_t const byte) {
  316. furi_assert(model);
  317. furi_assert(byte);
  318. // The first HEADER_LENGTH bytes are reserved for header information.
  319. if(model->ringbuffer_index < HEADER_LENGTH) {
  320. // Validate the start of row characters 'Y' and ':'.
  321. if(model->ringbuffer_index == 0 && byte != 'Y') {
  322. // Incorrect start of frame; reset.
  323. return;
  324. }
  325. if(model->ringbuffer_index == 1 && byte != ':') {
  326. // Incorrect start of frame; reset.
  327. model->ringbuffer_index = 0;
  328. return;
  329. }
  330. if(model->ringbuffer_index == 2) {
  331. // Assign the third byte as the row identifier.
  332. model->row_identifier = byte;
  333. }
  334. model->ringbuffer_index++; // Increment index for the next byte.
  335. return;
  336. }
  337. // Store pixel value directly after the header.
  338. model->row_ringbuffer[model->ringbuffer_index - HEADER_LENGTH] = byte;
  339. model->ringbuffer_index++; // Increment index for the next byte.
  340. // Check whether the ring buffer is filled.
  341. if(model->ringbuffer_index >= RING_BUFFER_LENGTH) {
  342. model->ringbuffer_index = 0; // Reset the ring buffer index.
  343. model->initialized = true; // Set the connection as successfully established.
  344. // Compute the starting index for the row in the pixel buffer.
  345. size_t row_start_index = model->row_identifier * ROW_BUFFER_LENGTH;
  346. // Ensure the row start index is within the valid range.
  347. if(row_start_index > LAST_ROW_INDEX) {
  348. row_start_index = 0; // Reset to a safe value in case of an overflow.
  349. }
  350. // Flush the contents of the ring buffer to the pixel buffer.
  351. for(size_t i = 0; i < ROW_BUFFER_LENGTH; ++i) {
  352. model->pixels[row_start_index + i] = model->row_ringbuffer[i];
  353. }
  354. }
  355. }
  356. static int32_t camera_worker(void* context) {
  357. furi_assert(context);
  358. CameraSuiteViewCamera* instance = context;
  359. while(1) {
  360. uint32_t events =
  361. furi_thread_flags_wait(WORKER_EVENTS_MASK, FuriFlagWaitAny, FuriWaitForever);
  362. furi_check((events & FuriFlagError) == 0);
  363. if(events & WorkerEventStop) {
  364. break;
  365. } else if(events & WorkerEventRx) {
  366. size_t length = 0;
  367. do {
  368. size_t intended_data_size = 64;
  369. uint8_t data[intended_data_size];
  370. length =
  371. furi_stream_buffer_receive(instance->rx_stream, data, intended_data_size, 0);
  372. if(length > 0) {
  373. with_view_model(
  374. instance->view,
  375. UartDumpModel * model,
  376. {
  377. for(size_t i = 0; i < length; i++) {
  378. process_ringbuffer(model, data[i]);
  379. }
  380. },
  381. false);
  382. }
  383. } while(length > 0);
  384. with_view_model(
  385. instance->view, UartDumpModel * model, { UNUSED(model); }, true);
  386. }
  387. }
  388. return 0;
  389. }
  390. CameraSuiteViewCamera* camera_suite_view_camera_alloc() {
  391. // Allocate memory for the instance
  392. CameraSuiteViewCamera* instance = malloc(sizeof(CameraSuiteViewCamera));
  393. // Allocate the view object
  394. instance->view = view_alloc();
  395. // Allocate a stream buffer
  396. instance->rx_stream = furi_stream_buffer_alloc(2048, 1);
  397. // Allocate model
  398. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(UartDumpModel));
  399. // Set context for the view
  400. view_set_context(instance->view, instance);
  401. // Set draw callback
  402. view_set_draw_callback(instance->view, (ViewDrawCallback)camera_suite_view_camera_draw);
  403. // Set input callback
  404. view_set_input_callback(instance->view, camera_suite_view_camera_input);
  405. // Set enter callback
  406. view_set_enter_callback(instance->view, camera_suite_view_camera_enter);
  407. // Set exit callback
  408. view_set_exit_callback(instance->view, camera_suite_view_camera_exit);
  409. // Initialize camera model
  410. with_view_model(
  411. instance->view,
  412. UartDumpModel * model,
  413. { camera_suite_view_camera_model_init(model); },
  414. true);
  415. // Allocate a thread for this camera to run on.
  416. FuriThread* thread = furi_thread_alloc_ex("UsbUartWorker", 2048, camera_worker, instance);
  417. instance->worker_thread = thread;
  418. furi_thread_start(instance->worker_thread);
  419. // Enable uart listener
  420. furi_hal_console_disable();
  421. // 115200 is the default baud rate for the ESP32-CAM.
  422. furi_hal_uart_set_br(FuriHalUartIdUSART1, 230400);
  423. // Enable UART1 and set the IRQ callback.
  424. furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, camera_on_irq_cb, instance);
  425. return instance;
  426. }
  427. void camera_suite_view_camera_free(CameraSuiteViewCamera* instance) {
  428. furi_assert(instance);
  429. // Remove the IRQ callback.
  430. furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, NULL, NULL);
  431. // Free the worker thread.
  432. furi_thread_free(instance->worker_thread);
  433. // Free the allocated stream buffer.
  434. furi_stream_buffer_free(instance->rx_stream);
  435. // Re-enable the console.
  436. furi_hal_console_enable();
  437. with_view_model(
  438. instance->view, UartDumpModel * model, { UNUSED(model); }, true);
  439. view_free(instance->view);
  440. free(instance);
  441. }
  442. View* camera_suite_view_camera_get_view(CameraSuiteViewCamera* instance) {
  443. furi_assert(instance);
  444. return instance->view;
  445. }