camera_suite_view_camera.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. static void camera_suite_view_camera_draw(Canvas* canvas, UartDumpModel* model) {
  28. // Clear the screen.
  29. canvas_set_color(canvas, ColorBlack);
  30. // Draw the frame.
  31. canvas_draw_frame(canvas, 0, 0, FRAME_WIDTH, FRAME_HEIGHT);
  32. CameraSuite* app = current_instance->context;
  33. // Draw the pixels with rotation.
  34. for(size_t p = 0; p < FRAME_BUFFER_LENGTH; ++p) {
  35. uint8_t x = p % ROW_BUFFER_LENGTH; // 0 .. 15
  36. uint8_t y = p / ROW_BUFFER_LENGTH; // 0 .. 63
  37. // Apply rotation
  38. int16_t rotated_x, rotated_y;
  39. switch(app->orientation) {
  40. case 1: // 90 degrees
  41. rotated_x = y;
  42. rotated_y = FRAME_WIDTH - 1 - x;
  43. break;
  44. case 2: // 180 degrees
  45. rotated_x = FRAME_WIDTH - 1 - x;
  46. rotated_y = FRAME_HEIGHT - 1 - y;
  47. break;
  48. case 3: // 270 degrees
  49. rotated_x = FRAME_HEIGHT - 1 - y;
  50. rotated_y = x;
  51. break;
  52. case 0: // 0 degrees
  53. default:
  54. rotated_x = x;
  55. rotated_y = y;
  56. break;
  57. }
  58. for(uint8_t i = 0; i < 8; ++i) {
  59. if((model->pixels[p] & (1 << (7 - i))) != 0) {
  60. // Adjust the coordinates based on the new screen dimensions
  61. uint16_t screen_x, screen_y;
  62. switch(app->orientation) {
  63. case 1: // 90 degrees
  64. screen_x = rotated_x;
  65. screen_y = FRAME_HEIGHT - 8 + (rotated_y * 8) + i;
  66. break;
  67. case 2: // 180 degrees
  68. screen_x = FRAME_WIDTH - 8 + (rotated_x * 8) + i;
  69. screen_y = FRAME_HEIGHT - 1 - rotated_y;
  70. break;
  71. case 3: // 270 degrees
  72. screen_x = FRAME_WIDTH - 1 - rotated_x;
  73. screen_y = rotated_y * 8 + i;
  74. break;
  75. case 0: // 0 degrees
  76. default:
  77. screen_x = rotated_x * 8 + i;
  78. screen_y = rotated_y;
  79. break;
  80. }
  81. canvas_draw_dot(canvas, screen_x, screen_y);
  82. }
  83. }
  84. }
  85. // Draw the guide if the camera is not initialized.
  86. if(!model->initialized) {
  87. canvas_draw_icon(canvas, 74, 16, &I_DolphinCommon_56x48);
  88. canvas_set_font(canvas, FontSecondary);
  89. canvas_draw_str(canvas, 8, 12, "Connect the ESP32-CAM");
  90. canvas_draw_str(canvas, 20, 24, "VCC - 3V3");
  91. canvas_draw_str(canvas, 20, 34, "GND - GND");
  92. canvas_draw_str(canvas, 20, 44, "U0R - TX");
  93. canvas_draw_str(canvas, 20, 54, "U0T - RX");
  94. }
  95. }
  96. static void camera_suite_view_camera_model_init(UartDumpModel* const model) {
  97. for(size_t i = 0; i < FRAME_BUFFER_LENGTH; i++) {
  98. model->pixels[i] = 0;
  99. }
  100. }
  101. static bool camera_suite_view_camera_input(InputEvent* event, void* context) {
  102. furi_assert(context);
  103. CameraSuiteViewCamera* instance = context;
  104. if(event->type == InputTypeRelease) {
  105. switch(event->key) {
  106. default: // Stop all sounds, reset the LED.
  107. with_view_model(
  108. instance->view,
  109. UartDumpModel * model,
  110. {
  111. UNUSED(model);
  112. camera_suite_play_bad_bump(instance->context);
  113. camera_suite_stop_all_sound(instance->context);
  114. camera_suite_led_set_rgb(instance->context, 0, 0, 0);
  115. },
  116. true);
  117. break;
  118. }
  119. // Send `data` to the ESP32-CAM
  120. } else if(event->type == InputTypePress) {
  121. uint8_t data[1];
  122. switch(event->key) {
  123. case InputKeyBack:
  124. // Stop the camera stream.
  125. data[0] = 's';
  126. // Go back to the main menu.
  127. with_view_model(
  128. instance->view,
  129. UartDumpModel * model,
  130. {
  131. UNUSED(model);
  132. instance->callback(CameraSuiteCustomEventSceneCameraBack, instance->context);
  133. },
  134. true);
  135. break;
  136. case InputKeyLeft:
  137. // Camera: Invert.
  138. data[0] = '<';
  139. with_view_model(
  140. instance->view,
  141. UartDumpModel * model,
  142. {
  143. UNUSED(model);
  144. camera_suite_play_happy_bump(instance->context);
  145. camera_suite_play_input_sound(instance->context);
  146. camera_suite_led_set_rgb(instance->context, 0, 0, 255);
  147. instance->callback(CameraSuiteCustomEventSceneCameraLeft, instance->context);
  148. },
  149. true);
  150. break;
  151. case InputKeyRight:
  152. // Camera: Enable/disable dithering.
  153. data[0] = '>';
  154. with_view_model(
  155. instance->view,
  156. UartDumpModel * model,
  157. {
  158. UNUSED(model);
  159. camera_suite_play_happy_bump(instance->context);
  160. camera_suite_play_input_sound(instance->context);
  161. camera_suite_led_set_rgb(instance->context, 0, 0, 255);
  162. instance->callback(CameraSuiteCustomEventSceneCameraRight, instance->context);
  163. },
  164. true);
  165. break;
  166. case InputKeyUp:
  167. // Camera: Increase contrast.
  168. data[0] = 'C';
  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(CameraSuiteCustomEventSceneCameraUp, instance->context);
  178. },
  179. true);
  180. break;
  181. case InputKeyDown:
  182. // Camera: Reduce contrast.
  183. data[0] = 'c';
  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(CameraSuiteCustomEventSceneCameraDown, instance->context);
  193. },
  194. true);
  195. break;
  196. case InputKeyOk:
  197. // Switch dithering types.
  198. data[0] = 'D';
  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(CameraSuiteCustomEventSceneCameraOk, instance->context);
  208. },
  209. true);
  210. break;
  211. case InputKeyMAX:
  212. break;
  213. }
  214. // Send `data` to the ESP32-CAM
  215. furi_hal_uart_tx(FuriHalUartIdUSART1, data, 1);
  216. }
  217. return true;
  218. }
  219. static void camera_suite_view_camera_exit(void* context) {
  220. furi_assert(context);
  221. }
  222. static void camera_suite_view_camera_enter(void* context) {
  223. // Check `context` for null. If it is null, abort program, else continue.
  224. furi_assert(context);
  225. // Cast `context` to `CameraSuiteViewCamera*` and store it in `instance`.
  226. CameraSuiteViewCamera* instance = (CameraSuiteViewCamera*)context;
  227. // Assign the current instance to the global variable
  228. current_instance = instance;
  229. uint8_t data[1];
  230. data[0] = 'S'; // Uppercase `S` to start the camera
  231. // Send `data` to the ESP32-CAM
  232. furi_hal_uart_tx(FuriHalUartIdUSART1, data, 1);
  233. with_view_model(
  234. instance->view,
  235. UartDumpModel * model,
  236. { camera_suite_view_camera_model_init(model); },
  237. true);
  238. }
  239. static void camera_on_irq_cb(UartIrqEvent uartIrqEvent, uint8_t data, void* context) {
  240. // Check `context` for null. If it is null, abort program, else continue.
  241. furi_assert(context);
  242. // Cast `context` to `CameraSuiteViewCamera*` and store it in `instance`.
  243. CameraSuiteViewCamera* instance = context;
  244. // If `uartIrqEvent` is `UartIrqEventRXNE`, send the data to the
  245. // `rx_stream` and set the `WorkerEventRx` flag.
  246. if(uartIrqEvent == UartIrqEventRXNE) {
  247. furi_stream_buffer_send(instance->rx_stream, &data, 1, 0);
  248. furi_thread_flags_set(furi_thread_get_id(instance->worker_thread), WorkerEventRx);
  249. }
  250. }
  251. static void process_ringbuffer(UartDumpModel* model, uint8_t byte) {
  252. // First char has to be 'Y' in the buffer.
  253. if(model->ringbuffer_index == 0 && byte != 'Y') {
  254. return;
  255. }
  256. // Second char has to be ':' in the buffer or reset.
  257. if(model->ringbuffer_index == 1 && byte != ':') {
  258. model->ringbuffer_index = 0;
  259. process_ringbuffer(model, byte);
  260. return;
  261. }
  262. // Assign current byte to the ringbuffer.
  263. model->row_ringbuffer[model->ringbuffer_index] = byte;
  264. // Increment the ringbuffer index.
  265. ++model->ringbuffer_index;
  266. // Let's wait 'till the buffer fills.
  267. if(model->ringbuffer_index < RING_BUFFER_LENGTH) {
  268. return;
  269. }
  270. // Flush the ringbuffer to the framebuffer.
  271. model->ringbuffer_index = 0; // Reset the ringbuffer
  272. model->initialized = true; // Established the connection successfully.
  273. size_t row_start_index =
  274. model->row_ringbuffer[2] * ROW_BUFFER_LENGTH; // Third char will determine the row number
  275. if(row_start_index > LAST_ROW_INDEX) { // Failsafe
  276. row_start_index = 0;
  277. }
  278. for(size_t i = 0; i < ROW_BUFFER_LENGTH; ++i) {
  279. model->pixels[row_start_index + i] =
  280. model->row_ringbuffer[i + 3]; // Writing the remaining 16 bytes into the frame buffer
  281. }
  282. }
  283. static int32_t camera_worker(void* context) {
  284. furi_assert(context);
  285. CameraSuiteViewCamera* instance = context;
  286. while(1) {
  287. uint32_t events =
  288. furi_thread_flags_wait(WORKER_EVENTS_MASK, FuriFlagWaitAny, FuriWaitForever);
  289. furi_check((events & FuriFlagError) == 0);
  290. if(events & WorkerEventStop) {
  291. break;
  292. } else if(events & WorkerEventRx) {
  293. size_t length = 0;
  294. do {
  295. size_t intended_data_size = 64;
  296. uint8_t data[intended_data_size];
  297. length =
  298. furi_stream_buffer_receive(instance->rx_stream, data, intended_data_size, 0);
  299. if(length > 0) {
  300. with_view_model(
  301. instance->view,
  302. UartDumpModel * model,
  303. {
  304. for(size_t i = 0; i < length; i++) {
  305. process_ringbuffer(model, data[i]);
  306. }
  307. },
  308. false);
  309. }
  310. } while(length > 0);
  311. with_view_model(
  312. instance->view, UartDumpModel * model, { UNUSED(model); }, true);
  313. }
  314. }
  315. return 0;
  316. }
  317. CameraSuiteViewCamera* camera_suite_view_camera_alloc() {
  318. CameraSuiteViewCamera* instance = malloc(sizeof(CameraSuiteViewCamera));
  319. instance->view = view_alloc();
  320. instance->rx_stream = furi_stream_buffer_alloc(2048, 1);
  321. // Set up views
  322. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(UartDumpModel));
  323. view_set_context(instance->view, instance); // furi_assert crashes in events without this
  324. view_set_draw_callback(instance->view, (ViewDrawCallback)camera_suite_view_camera_draw);
  325. view_set_input_callback(instance->view, camera_suite_view_camera_input);
  326. view_set_enter_callback(instance->view, camera_suite_view_camera_enter);
  327. view_set_exit_callback(instance->view, camera_suite_view_camera_exit);
  328. with_view_model(
  329. instance->view,
  330. UartDumpModel * model,
  331. { camera_suite_view_camera_model_init(model); },
  332. true);
  333. instance->worker_thread = furi_thread_alloc_ex("UsbUartWorker", 2048, camera_worker, instance);
  334. furi_thread_start(instance->worker_thread);
  335. // Enable uart listener
  336. furi_hal_console_disable();
  337. furi_hal_uart_set_br(FuriHalUartIdUSART1, 230400);
  338. furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, camera_on_irq_cb, instance);
  339. return instance;
  340. }
  341. void camera_suite_view_camera_free(CameraSuiteViewCamera* instance) {
  342. furi_assert(instance);
  343. with_view_model(
  344. instance->view, UartDumpModel * model, { UNUSED(model); }, true);
  345. view_free(instance->view);
  346. free(instance);
  347. }
  348. View* camera_suite_view_camera_get_view(CameraSuiteViewCamera* instance) {
  349. furi_assert(instance);
  350. return instance->view;
  351. }