camera_suite_view_camera.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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 CameraSuiteViewCamera* current_instance = NULL;
  11. bool is_inverted = false;
  12. struct CameraSuiteViewCamera {
  13. CameraSuiteViewCameraCallback callback;
  14. FuriStreamBuffer* rx_stream;
  15. FuriThread* worker_thread;
  16. View* view;
  17. void* context;
  18. };
  19. void camera_suite_view_camera_set_callback(
  20. CameraSuiteViewCamera* instance,
  21. CameraSuiteViewCameraCallback callback,
  22. void* context) {
  23. furi_assert(instance);
  24. furi_assert(callback);
  25. instance->callback = callback;
  26. instance->context = context;
  27. }
  28. // Function to draw pixels on the canvas based on camera orientation
  29. static void draw_pixel_by_orientation(Canvas* canvas, uint8_t x, uint8_t y, uint8_t orientation) {
  30. switch(orientation) {
  31. case 0: // Camera rotated 0 degrees (right side up, default)
  32. canvas_draw_dot(canvas, x, y);
  33. break;
  34. case 1: // Camera rotated 90 degrees
  35. canvas_draw_dot(canvas, y, FRAME_WIDTH - 1 - x);
  36. break;
  37. case 2: // Camera rotated 180 degrees (upside down)
  38. canvas_draw_dot(canvas, FRAME_WIDTH - 1 - x, FRAME_HEIGHT - 1 - y);
  39. break;
  40. case 3: // Camera rotated 270 degrees
  41. canvas_draw_dot(canvas, FRAME_HEIGHT - 1 - y, x);
  42. break;
  43. default:
  44. break;
  45. }
  46. }
  47. static void camera_suite_view_camera_draw(Canvas* canvas, void* _model) {
  48. UartDumpModel* model = _model;
  49. // Clear the screen.
  50. canvas_set_color(canvas, ColorBlack);
  51. // Draw the frame.
  52. canvas_draw_frame(canvas, 0, 0, FRAME_WIDTH, FRAME_HEIGHT);
  53. CameraSuite* app = current_instance->context;
  54. for(size_t p = 0; p < FRAME_BUFFER_LENGTH; ++p) {
  55. uint8_t x = p % ROW_BUFFER_LENGTH; // 0 .. 15
  56. uint8_t y = p / ROW_BUFFER_LENGTH; // 0 .. 63
  57. for(uint8_t i = 0; i < 8; ++i) {
  58. if((model->pixels[p] & (1 << (7 - i))) != 0) {
  59. draw_pixel_by_orientation(canvas, (x * 8) + i, y, app->orientation);
  60. }
  61. }
  62. }
  63. // Draw the guide if the camera is not initialized.
  64. if(!model->initialized) {
  65. canvas_draw_icon(canvas, 74, 16, &I_DolphinCommon_56x48);
  66. canvas_set_font(canvas, FontSecondary);
  67. canvas_draw_str(canvas, 8, 12, "Connect the ESP32-CAM");
  68. canvas_draw_str(canvas, 20, 24, "VCC - 3V3");
  69. canvas_draw_str(canvas, 20, 34, "GND - GND");
  70. canvas_draw_str(canvas, 20, 44, "U0R - TX");
  71. canvas_draw_str(canvas, 20, 54, "U0T - RX");
  72. }
  73. }
  74. static void save_image(void* _model) {
  75. UartDumpModel* model = _model;
  76. // This pointer is used to access the storage.
  77. Storage* storage = furi_record_open(RECORD_STORAGE);
  78. // This pointer is used to access the filesystem.
  79. File* file = storage_file_alloc(storage);
  80. // Store path in local variable.
  81. const char* folderName = EXT_PATH("DCIM");
  82. // Create the folder name for the image file if it does not exist.
  83. if(storage_common_stat(storage, folderName, NULL) == FSE_NOT_EXIST) {
  84. storage_simply_mkdir(storage, folderName);
  85. }
  86. // This pointer is used to access the file name.
  87. FuriString* file_name = furi_string_alloc();
  88. // Get the current date and time.
  89. FuriHalRtcDateTime datetime = {0};
  90. furi_hal_rtc_get_datetime(&datetime);
  91. // Create the file name.
  92. furi_string_printf(
  93. file_name,
  94. EXT_PATH("DCIM/%.4d%.2d%.2d-%.2d%.2d%.2d.bmp"),
  95. datetime.year,
  96. datetime.month,
  97. datetime.day,
  98. datetime.hour,
  99. datetime.minute,
  100. datetime.second);
  101. // Open the file for writing. If the file does not exist (it shouldn't),
  102. // create it.
  103. bool result =
  104. storage_file_open(file, furi_string_get_cstr(file_name), FSAM_WRITE, FSOM_OPEN_ALWAYS);
  105. // Free the file name after use.
  106. furi_string_free(file_name);
  107. // If the file was opened successfully, write the bitmap header and the
  108. // image data.
  109. if(result) {
  110. CameraSuite* app = current_instance->context;
  111. if(app->flash) {
  112. if(app->jpeg) {
  113. // Turn on local jpeg save. When this is enabled the ESP32-CAM
  114. // will save the image to the SD card and saving the image to
  115. // the Flipper SD card will be disabled/skipped.
  116. unsigned char jpeg_on = 'J';
  117. furi_hal_uart_tx(FuriHalUartIdUSART1, &jpeg_on, 1);
  118. } else {
  119. unsigned char jpeg_off = 'j';
  120. // Turn off local jpeg save.
  121. furi_hal_uart_tx(FuriHalUartIdUSART1, &jpeg_off, 1);
  122. }
  123. // Initiate the onboard ESP32-CAM picture sequence. So far this
  124. // includes turning on the flash and alternatively saving jpeg
  125. // locally to the ESP32-CAM SD card if enabled.
  126. unsigned char take_picture = 'P';
  127. furi_hal_uart_tx(FuriHalUartIdUSART1, &take_picture, 1);
  128. }
  129. // If saving jpeg is enabled locally to the ESP32-CAM SD card, skip
  130. // writing the image data to the Flipper Zero SD card.
  131. if(!app->jpeg) {
  132. // Write locally to the Flipper Zero SD card in the DCIM folder.
  133. int8_t row_buffer[ROW_BUFFER_LENGTH];
  134. if(is_inverted) {
  135. for(size_t i = 64; i > 0; --i) {
  136. for(size_t j = 0; j < ROW_BUFFER_LENGTH; ++j) {
  137. row_buffer[j] = model->pixels[((i - 1) * ROW_BUFFER_LENGTH) + j];
  138. }
  139. storage_file_write(file, row_buffer, ROW_BUFFER_LENGTH);
  140. }
  141. } else {
  142. for(size_t i = 0; i < 64; ++i) {
  143. for(size_t j = 0; j < ROW_BUFFER_LENGTH; ++j) {
  144. row_buffer[j] = model->pixels[i * ROW_BUFFER_LENGTH + j];
  145. }
  146. storage_file_write(file, row_buffer, ROW_BUFFER_LENGTH);
  147. }
  148. }
  149. }
  150. }
  151. // Close the file.
  152. storage_file_close(file);
  153. // Free up memory.
  154. storage_file_free(file);
  155. }
  156. static void camera_suite_view_camera_model_init(UartDumpModel* const model) {
  157. for(size_t i = 0; i < FRAME_BUFFER_LENGTH; i++) {
  158. model->pixels[i] = 0;
  159. }
  160. }
  161. static bool camera_suite_view_camera_input(InputEvent* event, void* context) {
  162. furi_assert(context);
  163. CameraSuiteViewCamera* instance = context;
  164. if(event->type == InputTypeRelease) {
  165. switch(event->key) {
  166. default: // Stop all sounds, reset the LED.
  167. with_view_model(
  168. instance->view,
  169. UartDumpModel * model,
  170. {
  171. UNUSED(model);
  172. camera_suite_play_bad_bump(instance->context);
  173. camera_suite_stop_all_sound(instance->context);
  174. camera_suite_led_set_rgb(instance->context, 0, 0, 0);
  175. },
  176. true);
  177. break;
  178. }
  179. } else if(event->type == InputTypePress) {
  180. uint8_t data[1];
  181. switch(event->key) {
  182. case InputKeyBack:
  183. // Stop the camera stream.
  184. data[0] = 's';
  185. // Go back to the main menu.
  186. with_view_model(
  187. instance->view,
  188. UartDumpModel * model,
  189. {
  190. UNUSED(model);
  191. instance->callback(CameraSuiteCustomEventSceneCameraBack, instance->context);
  192. },
  193. true);
  194. break;
  195. case InputKeyLeft:
  196. // Camera: Toggle invert on the ESP32-CAM.
  197. data[0] = '<';
  198. // Toggle invert state locally.
  199. is_inverted = !is_inverted;
  200. with_view_model(
  201. instance->view,
  202. UartDumpModel * model,
  203. {
  204. UNUSED(model);
  205. camera_suite_play_happy_bump(instance->context);
  206. camera_suite_play_input_sound(instance->context);
  207. camera_suite_led_set_rgb(instance->context, 0, 0, 255);
  208. instance->callback(CameraSuiteCustomEventSceneCameraLeft, instance->context);
  209. },
  210. true);
  211. break;
  212. case InputKeyRight:
  213. // Camera: Enable/disable dithering.
  214. data[0] = '>';
  215. with_view_model(
  216. instance->view,
  217. UartDumpModel * model,
  218. {
  219. UNUSED(model);
  220. camera_suite_play_happy_bump(instance->context);
  221. camera_suite_play_input_sound(instance->context);
  222. camera_suite_led_set_rgb(instance->context, 0, 0, 255);
  223. instance->callback(CameraSuiteCustomEventSceneCameraRight, instance->context);
  224. },
  225. true);
  226. break;
  227. case InputKeyUp:
  228. // Camera: Increase contrast.
  229. data[0] = 'C';
  230. with_view_model(
  231. instance->view,
  232. UartDumpModel * model,
  233. {
  234. UNUSED(model);
  235. camera_suite_play_happy_bump(instance->context);
  236. camera_suite_play_input_sound(instance->context);
  237. camera_suite_led_set_rgb(instance->context, 0, 0, 255);
  238. instance->callback(CameraSuiteCustomEventSceneCameraUp, instance->context);
  239. },
  240. true);
  241. break;
  242. case InputKeyDown:
  243. // Camera: Reduce contrast.
  244. data[0] = 'c';
  245. with_view_model(
  246. instance->view,
  247. UartDumpModel * model,
  248. {
  249. UNUSED(model);
  250. camera_suite_play_happy_bump(instance->context);
  251. camera_suite_play_input_sound(instance->context);
  252. camera_suite_led_set_rgb(instance->context, 0, 0, 255);
  253. instance->callback(CameraSuiteCustomEventSceneCameraDown, instance->context);
  254. },
  255. true);
  256. break;
  257. case InputKeyOk: {
  258. // Take picture.
  259. with_view_model(
  260. instance->view,
  261. UartDumpModel * model,
  262. {
  263. // If flash is enabled, flash the onboard ESP32-CAM LED.
  264. camera_suite_play_happy_bump(instance->context);
  265. camera_suite_play_input_sound(instance->context);
  266. camera_suite_led_set_rgb(instance->context, 0, 0, 255);
  267. save_image(model);
  268. instance->callback(CameraSuiteCustomEventSceneCameraOk, instance->context);
  269. },
  270. true);
  271. return true;
  272. }
  273. case InputKeyMAX:
  274. break;
  275. }
  276. // Send `data` to the ESP32-CAM
  277. furi_hal_uart_tx(FuriHalUartIdUSART1, data, 1);
  278. }
  279. return true;
  280. }
  281. static void camera_suite_view_camera_exit(void* context) {
  282. furi_assert(context);
  283. }
  284. static void camera_suite_view_camera_enter(void* context) {
  285. // Check `context` for null. If it is null, abort program, else continue.
  286. furi_assert(context);
  287. // Cast `context` to `CameraSuiteViewCamera*` and store it in `instance`.
  288. CameraSuiteViewCamera* instance = (CameraSuiteViewCamera*)context;
  289. // Assign the current instance to the global variable
  290. current_instance = instance;
  291. uint8_t data[1];
  292. data[0] = 'S'; // Uppercase `S` to start the camera
  293. // Send `data` to the ESP32-CAM
  294. furi_hal_uart_tx(FuriHalUartIdUSART1, data, 1);
  295. // Delay for 50ms to make sure the camera is started before sending any other commands.
  296. furi_delay_ms(50);
  297. // Initialize the camera with the selected dithering option from options.
  298. CameraSuite* instanceContext = instance->context;
  299. switch(instanceContext->dither) {
  300. case 0: // Floyd Steinberg
  301. data[0] = '0';
  302. break;
  303. case 1: // Stucki
  304. data[0] = '1';
  305. break;
  306. case 2: // Jarvis Judice Ninke
  307. data[0] = '2';
  308. break;
  309. }
  310. // Send `data` to the ESP32-CAM
  311. furi_hal_uart_tx(FuriHalUartIdUSART1, data, 1);
  312. with_view_model(
  313. instance->view,
  314. UartDumpModel * model,
  315. { camera_suite_view_camera_model_init(model); },
  316. true);
  317. }
  318. static void camera_on_irq_cb(UartIrqEvent uartIrqEvent, uint8_t data, void* context) {
  319. // Check `context` for null. If it is null, abort program, else continue.
  320. furi_assert(context);
  321. // Cast `context` to `CameraSuiteViewCamera*` and store it in `instance`.
  322. CameraSuiteViewCamera* instance = context;
  323. // If `uartIrqEvent` is `UartIrqEventRXNE`, send the data to the
  324. // `rx_stream` and set the `WorkerEventRx` flag.
  325. if(uartIrqEvent == UartIrqEventRXNE) {
  326. furi_stream_buffer_send(instance->rx_stream, &data, 1, 0);
  327. furi_thread_flags_set(furi_thread_get_id(instance->worker_thread), WorkerEventRx);
  328. }
  329. }
  330. static void process_ringbuffer(UartDumpModel* model, uint8_t byte) {
  331. // The first HEADER_LENGTH bytes are reserved for header information.
  332. if(model->ringbuffer_index < HEADER_LENGTH) {
  333. // Validate the start of row characters 'Y' and ':'.
  334. if(model->ringbuffer_index == 0 && byte != 'Y') {
  335. // Incorrect start of frame; reset.
  336. return;
  337. }
  338. if(model->ringbuffer_index == 1 && byte != ':') {
  339. // Incorrect start of frame; reset.
  340. model->ringbuffer_index = 0;
  341. return;
  342. }
  343. if(model->ringbuffer_index == 2) {
  344. // Assign the third byte as the row identifier.
  345. model->row_identifier = byte;
  346. }
  347. model->ringbuffer_index++; // Increment index for the next byte.
  348. return;
  349. }
  350. // Store pixel value directly after the header.
  351. model->row_ringbuffer[model->ringbuffer_index - HEADER_LENGTH] = byte;
  352. model->ringbuffer_index++; // Increment index for the next byte.
  353. // Check whether the ring buffer is filled.
  354. if(model->ringbuffer_index >= RING_BUFFER_LENGTH) {
  355. model->ringbuffer_index = 0; // Reset the ring buffer index.
  356. model->initialized = true; // Set the connection as successfully established.
  357. // Compute the starting index for the row in the pixel buffer.
  358. size_t row_start_index = model->row_identifier * ROW_BUFFER_LENGTH;
  359. // Ensure the row start index is within the valid range.
  360. if(row_start_index > LAST_ROW_INDEX) {
  361. row_start_index = 0; // Reset to a safe value in case of an overflow.
  362. }
  363. // Flush the contents of the ring buffer to the pixel buffer.
  364. for(size_t i = 0; i < ROW_BUFFER_LENGTH; ++i) {
  365. model->pixels[row_start_index + i] = model->row_ringbuffer[i];
  366. }
  367. }
  368. }
  369. static int32_t camera_worker(void* context) {
  370. furi_assert(context);
  371. CameraSuiteViewCamera* instance = context;
  372. while(1) {
  373. uint32_t events =
  374. furi_thread_flags_wait(WORKER_EVENTS_MASK, FuriFlagWaitAny, FuriWaitForever);
  375. furi_check((events & FuriFlagError) == 0);
  376. if(events & WorkerEventStop) {
  377. break;
  378. } else if(events & WorkerEventRx) {
  379. size_t length = 0;
  380. do {
  381. size_t intended_data_size = 64;
  382. uint8_t data[intended_data_size];
  383. length =
  384. furi_stream_buffer_receive(instance->rx_stream, data, intended_data_size, 0);
  385. if(length > 0) {
  386. with_view_model(
  387. instance->view,
  388. UartDumpModel * model,
  389. {
  390. for(size_t i = 0; i < length; i++) {
  391. process_ringbuffer(model, data[i]);
  392. }
  393. },
  394. false);
  395. }
  396. } while(length > 0);
  397. with_view_model(
  398. instance->view, UartDumpModel * model, { UNUSED(model); }, true);
  399. }
  400. }
  401. return 0;
  402. }
  403. CameraSuiteViewCamera* camera_suite_view_camera_alloc() {
  404. // Allocate memory for the instance
  405. CameraSuiteViewCamera* instance = malloc(sizeof(CameraSuiteViewCamera));
  406. // Allocate the view object
  407. instance->view = view_alloc();
  408. // Allocate a stream buffer
  409. instance->rx_stream = furi_stream_buffer_alloc(2048, 1);
  410. // Allocate model
  411. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(UartDumpModel));
  412. // Set context
  413. view_set_context(instance->view, instance);
  414. // Set draw callback
  415. view_set_draw_callback(instance->view, (ViewDrawCallback)camera_suite_view_camera_draw);
  416. // Set input callback
  417. view_set_input_callback(instance->view, camera_suite_view_camera_input);
  418. // Set enter callback
  419. view_set_enter_callback(instance->view, camera_suite_view_camera_enter);
  420. // Set exit callback
  421. view_set_exit_callback(instance->view, camera_suite_view_camera_exit);
  422. // Initialize camera model
  423. with_view_model(
  424. instance->view,
  425. UartDumpModel * model,
  426. { camera_suite_view_camera_model_init(model); },
  427. true);
  428. // Allocate a thread for this camera to run on.
  429. FuriThread* thread = furi_thread_alloc_ex("UsbUartWorker", 2048, camera_worker, instance);
  430. instance->worker_thread = thread;
  431. furi_thread_start(instance->worker_thread);
  432. // Enable uart listener
  433. furi_hal_console_disable();
  434. furi_hal_uart_set_br(FuriHalUartIdUSART1, 230400);
  435. furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, camera_on_irq_cb, instance);
  436. return instance;
  437. }
  438. void camera_suite_view_camera_free(CameraSuiteViewCamera* instance) {
  439. furi_assert(instance);
  440. with_view_model(
  441. instance->view, UartDumpModel * model, { UNUSED(model); }, true);
  442. view_free(instance->view);
  443. free(instance);
  444. }
  445. View* camera_suite_view_camera_get_view(CameraSuiteViewCamera* instance) {
  446. furi_assert(instance);
  447. return instance->view;
  448. }