camera_suite_view_camera.c 23 KB

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