camera_suite_view_camera.c 24 KB

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