camera_suite_view_camera.c 23 KB

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