camera_suite_view_camera.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. );
  101. // Open the file for writing. If the file does not exist (it shouldn't),
  102. // create it.
  103. bool result = storage_file_open(
  104. file,
  105. furi_string_get_cstr(file_name),
  106. FSAM_WRITE, FSOM_OPEN_ALWAYS
  107. );
  108. // Free the file name after use.
  109. furi_string_free(file_name);
  110. // If the file was opened successfully, write the bitmap header and the
  111. // image data.
  112. if (result){
  113. storage_file_write(file, bitmap_header, BITMAP_HEADER_LENGTH);
  114. int8_t row_buffer[ROW_BUFFER_LENGTH];
  115. for (size_t i = 64; i > 0; --i) {
  116. for (size_t j = 0; j < ROW_BUFFER_LENGTH; ++j){
  117. row_buffer[j] = model->pixels[((i-1)*ROW_BUFFER_LENGTH) + j];
  118. }
  119. storage_file_write(file, row_buffer, ROW_BUFFER_LENGTH);
  120. }
  121. }
  122. // Close the file.
  123. storage_file_close(file);
  124. // Freeing up memory.
  125. storage_file_free(file);
  126. }
  127. static void camera_suite_view_camera_model_init(UartDumpModel* const model) {
  128. for(size_t i = 0; i < FRAME_BUFFER_LENGTH; i++) {
  129. model->pixels[i] = 0;
  130. }
  131. }
  132. static bool camera_suite_view_camera_input(InputEvent* event, void* context) {
  133. furi_assert(context);
  134. CameraSuiteViewCamera* instance = context;
  135. if(event->type == InputTypeRelease) {
  136. switch(event->key) {
  137. default: // Stop all sounds, reset the LED.
  138. with_view_model(
  139. instance->view,
  140. UartDumpModel * model,
  141. {
  142. UNUSED(model);
  143. camera_suite_play_bad_bump(instance->context);
  144. camera_suite_stop_all_sound(instance->context);
  145. camera_suite_led_set_rgb(instance->context, 0, 0, 0);
  146. },
  147. true);
  148. break;
  149. }
  150. } else if(event->type == InputTypePress) {
  151. uint8_t data[1];
  152. switch(event->key) {
  153. case InputKeyBack:
  154. // Stop the camera stream.
  155. data[0] = 's';
  156. // Go back to the main menu.
  157. with_view_model(
  158. instance->view,
  159. UartDumpModel * model,
  160. {
  161. UNUSED(model);
  162. instance->callback(CameraSuiteCustomEventSceneCameraBack, instance->context);
  163. },
  164. true);
  165. break;
  166. case InputKeyLeft:
  167. // Camera: Invert.
  168. data[0] = '<';
  169. with_view_model(
  170. instance->view,
  171. UartDumpModel * model,
  172. {
  173. UNUSED(model);
  174. camera_suite_play_happy_bump(instance->context);
  175. camera_suite_play_input_sound(instance->context);
  176. camera_suite_led_set_rgb(instance->context, 0, 0, 255);
  177. instance->callback(CameraSuiteCustomEventSceneCameraLeft, instance->context);
  178. },
  179. true);
  180. break;
  181. case InputKeyRight:
  182. // Camera: Enable/disable dithering.
  183. data[0] = '>';
  184. with_view_model(
  185. instance->view,
  186. UartDumpModel * model,
  187. {
  188. UNUSED(model);
  189. camera_suite_play_happy_bump(instance->context);
  190. camera_suite_play_input_sound(instance->context);
  191. camera_suite_led_set_rgb(instance->context, 0, 0, 255);
  192. instance->callback(CameraSuiteCustomEventSceneCameraRight, instance->context);
  193. },
  194. true);
  195. break;
  196. case InputKeyUp:
  197. // Camera: Increase contrast.
  198. data[0] = 'C';
  199. with_view_model(
  200. instance->view,
  201. UartDumpModel * model,
  202. {
  203. UNUSED(model);
  204. camera_suite_play_happy_bump(instance->context);
  205. camera_suite_play_input_sound(instance->context);
  206. camera_suite_led_set_rgb(instance->context, 0, 0, 255);
  207. instance->callback(CameraSuiteCustomEventSceneCameraUp, instance->context);
  208. },
  209. true);
  210. break;
  211. case InputKeyDown:
  212. // Camera: Reduce contrast.
  213. data[0] = 'c';
  214. with_view_model(
  215. instance->view,
  216. UartDumpModel * model,
  217. {
  218. UNUSED(model);
  219. camera_suite_play_happy_bump(instance->context);
  220. camera_suite_play_input_sound(instance->context);
  221. camera_suite_led_set_rgb(instance->context, 0, 0, 255);
  222. instance->callback(CameraSuiteCustomEventSceneCameraDown, instance->context);
  223. },
  224. true);
  225. break;
  226. case InputKeyOk: {
  227. CameraSuite* app = current_instance->context;
  228. // If flash is enabled, flash the onboard ESP32-CAM LED.
  229. if(app->flash) {
  230. data[0] = 'P';
  231. // Initialize the ESP32-CAM onboard torch immediately.
  232. furi_hal_uart_tx(FuriHalUartIdUSART1, data, 1);
  233. // Delay for 500ms to make sure flash is on before taking picture.
  234. furi_delay_ms(500);
  235. }
  236. // Take picture.
  237. with_view_model(
  238. instance->view,
  239. UartDumpModel * model,
  240. {
  241. camera_suite_play_happy_bump(instance->context);
  242. camera_suite_play_input_sound(instance->context);
  243. camera_suite_led_set_rgb(instance->context, 0, 0, 255);
  244. save_image(model);
  245. instance->callback(CameraSuiteCustomEventSceneCameraOk, instance->context);
  246. },
  247. true);
  248. return true;
  249. }
  250. case InputKeyMAX:
  251. break;
  252. }
  253. // Send `data` to the ESP32-CAM
  254. furi_hal_uart_tx(FuriHalUartIdUSART1, data, 1);
  255. }
  256. return true;
  257. }
  258. static void camera_suite_view_camera_exit(void* context) {
  259. furi_assert(context);
  260. }
  261. static void camera_suite_view_camera_enter(void* context) {
  262. // Check `context` for null. If it is null, abort program, else continue.
  263. furi_assert(context);
  264. // Cast `context` to `CameraSuiteViewCamera*` and store it in `instance`.
  265. CameraSuiteViewCamera* instance = (CameraSuiteViewCamera*)context;
  266. // Assign the current instance to the global variable
  267. current_instance = instance;
  268. uint8_t data[1];
  269. data[0] = 'S'; // Uppercase `S` to start the camera
  270. // Send `data` to the ESP32-CAM
  271. furi_hal_uart_tx(FuriHalUartIdUSART1, data, 1);
  272. // Delay for 50ms to make sure the camera is started before sending any other commands.
  273. furi_delay_ms(50);
  274. // Initialize the camera with the selected dithering option from options.
  275. CameraSuite* instanceContext = instance->context;
  276. switch(instanceContext->dither) {
  277. case 0: // Floyd Steinberg
  278. data[0] = '0';
  279. break;
  280. case 1: // Stucki
  281. data[0] = '1';
  282. break;
  283. case 2: // Jarvis Judice Ninke
  284. data[0] = '2';
  285. break;
  286. }
  287. // Send `data` to the ESP32-CAM
  288. furi_hal_uart_tx(FuriHalUartIdUSART1, data, 1);
  289. with_view_model(
  290. instance->view,
  291. UartDumpModel * model,
  292. { camera_suite_view_camera_model_init(model); },
  293. true);
  294. }
  295. static void camera_on_irq_cb(UartIrqEvent uartIrqEvent, uint8_t data, void* context) {
  296. // Check `context` for null. If it is null, abort program, else continue.
  297. furi_assert(context);
  298. // Cast `context` to `CameraSuiteViewCamera*` and store it in `instance`.
  299. CameraSuiteViewCamera* instance = context;
  300. // If `uartIrqEvent` is `UartIrqEventRXNE`, send the data to the
  301. // `rx_stream` and set the `WorkerEventRx` flag.
  302. if(uartIrqEvent == UartIrqEventRXNE) {
  303. furi_stream_buffer_send(instance->rx_stream, &data, 1, 0);
  304. furi_thread_flags_set(furi_thread_get_id(instance->worker_thread), WorkerEventRx);
  305. }
  306. }
  307. static void process_ringbuffer(UartDumpModel* model, uint8_t byte) {
  308. // First char has to be 'Y' in the buffer.
  309. if(model->ringbuffer_index == 0 && byte != 'Y') {
  310. return;
  311. }
  312. // Second char has to be ':' in the buffer or reset.
  313. if(model->ringbuffer_index == 1 && byte != ':') {
  314. model->ringbuffer_index = 0;
  315. process_ringbuffer(model, byte);
  316. return;
  317. }
  318. // Assign current byte to the ringbuffer.
  319. model->row_ringbuffer[model->ringbuffer_index] = byte;
  320. // Increment the ringbuffer index.
  321. ++model->ringbuffer_index;
  322. // Let's wait 'till the buffer fills.
  323. if(model->ringbuffer_index < RING_BUFFER_LENGTH) {
  324. return;
  325. }
  326. // Flush the ringbuffer to the framebuffer.
  327. model->ringbuffer_index = 0; // Reset the ringbuffer
  328. model->initialized = true; // Established the connection successfully.
  329. size_t row_start_index =
  330. model->row_ringbuffer[2] * ROW_BUFFER_LENGTH; // Third char will determine the row number
  331. if(row_start_index > LAST_ROW_INDEX) { // Failsafe
  332. row_start_index = 0;
  333. }
  334. for(size_t i = 0; i < ROW_BUFFER_LENGTH; ++i) {
  335. model->pixels[row_start_index + i] =
  336. model->row_ringbuffer[i + 3]; // Writing the remaining 16 bytes into the frame buffer
  337. }
  338. }
  339. static int32_t camera_worker(void* context) {
  340. furi_assert(context);
  341. CameraSuiteViewCamera* instance = context;
  342. while(1) {
  343. uint32_t events =
  344. furi_thread_flags_wait(WORKER_EVENTS_MASK, FuriFlagWaitAny, FuriWaitForever);
  345. furi_check((events & FuriFlagError) == 0);
  346. if(events & WorkerEventStop) {
  347. break;
  348. } else if(events & WorkerEventRx) {
  349. size_t length = 0;
  350. do {
  351. size_t intended_data_size = 64;
  352. uint8_t data[intended_data_size];
  353. length =
  354. furi_stream_buffer_receive(instance->rx_stream, data, intended_data_size, 0);
  355. if(length > 0) {
  356. with_view_model(
  357. instance->view,
  358. UartDumpModel * model,
  359. {
  360. for(size_t i = 0; i < length; i++) {
  361. process_ringbuffer(model, data[i]);
  362. }
  363. },
  364. false);
  365. }
  366. } while(length > 0);
  367. with_view_model(
  368. instance->view, UartDumpModel * model, { UNUSED(model); }, true);
  369. }
  370. }
  371. return 0;
  372. }
  373. CameraSuiteViewCamera* camera_suite_view_camera_alloc() {
  374. CameraSuiteViewCamera* instance = malloc(sizeof(CameraSuiteViewCamera));
  375. instance->view = view_alloc();
  376. instance->rx_stream = furi_stream_buffer_alloc(2048, 1);
  377. // Set up views
  378. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(UartDumpModel));
  379. view_set_context(instance->view, instance); // furi_assert crashes in events without this
  380. view_set_draw_callback(instance->view, (ViewDrawCallback)camera_suite_view_camera_draw);
  381. view_set_input_callback(instance->view, camera_suite_view_camera_input);
  382. view_set_enter_callback(instance->view, camera_suite_view_camera_enter);
  383. view_set_exit_callback(instance->view, camera_suite_view_camera_exit);
  384. with_view_model(
  385. instance->view,
  386. UartDumpModel * model,
  387. { camera_suite_view_camera_model_init(model); },
  388. true);
  389. instance->worker_thread = furi_thread_alloc_ex("UsbUartWorker", 2048, camera_worker, instance);
  390. furi_thread_start(instance->worker_thread);
  391. // Enable uart listener
  392. furi_hal_console_disable();
  393. furi_hal_uart_set_br(FuriHalUartIdUSART1, 230400);
  394. furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, camera_on_irq_cb, instance);
  395. return instance;
  396. }
  397. void camera_suite_view_camera_free(CameraSuiteViewCamera* instance) {
  398. furi_assert(instance);
  399. with_view_model(
  400. instance->view, UartDumpModel * model, { UNUSED(model); }, true);
  401. view_free(instance->view);
  402. free(instance);
  403. }
  404. View* camera_suite_view_camera_get_view(CameraSuiteViewCamera* instance) {
  405. furi_assert(instance);
  406. return instance->view;
  407. }