camera_suite_view_camera.c 19 KB

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