camera_suite_view_camera.c 23 KB

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