camera_suite_view_camera.cpp 24 KB

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