camera_suite_view_camera.c 16 KB

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