camera_suite_view_camera.c 19 KB

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