camera_suite_view_camera.c 18 KB

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