camera_suite_view_style_1.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. static CameraSuiteViewStyle1* current_instance = NULL;
  8. struct CameraSuiteViewStyle1 {
  9. CameraSuiteViewStyle1Callback callback;
  10. FuriStreamBuffer* rx_stream;
  11. FuriThread* worker_thread;
  12. View* view;
  13. int rotation_angle;
  14. void* context;
  15. };
  16. void camera_suite_view_style_1_set_callback(
  17. CameraSuiteViewStyle1* instance,
  18. CameraSuiteViewStyle1Callback callback,
  19. void* context) {
  20. furi_assert(instance);
  21. furi_assert(callback);
  22. instance->callback = callback;
  23. instance->context = context;
  24. }
  25. static void camera_suite_view_style_1_draw(Canvas* canvas, UartDumpModel* model) {
  26. // Clear the screen.
  27. canvas_set_color(canvas, ColorBlack);
  28. // Draw the frame.
  29. canvas_draw_frame(canvas, 0, 0, FRAME_WIDTH, FRAME_HEIGHT);
  30. // Draw the pixels with rotation.
  31. for(size_t p = 0; p < FRAME_BUFFER_LENGTH; ++p) {
  32. uint8_t x = p % ROW_BUFFER_LENGTH; // 0 .. 15
  33. uint8_t y = p / ROW_BUFFER_LENGTH; // 0 .. 63
  34. // Apply rotation
  35. int16_t rotated_x, rotated_y;
  36. switch(current_instance->rotation_angle) {
  37. case 90:
  38. rotated_x = y;
  39. rotated_y = FRAME_WIDTH - 1 - x;
  40. break;
  41. case 180:
  42. rotated_x = FRAME_WIDTH - 1 - x;
  43. rotated_y = FRAME_HEIGHT - 1 - y;
  44. break;
  45. case 270:
  46. rotated_x = FRAME_HEIGHT - 1 - y;
  47. rotated_y = x;
  48. break;
  49. default:
  50. rotated_x = x;
  51. rotated_y = y;
  52. break;
  53. }
  54. for(uint8_t i = 0; i < 8; ++i) {
  55. if((model->pixels[p] & (1 << i)) != 0) {
  56. // Adjust the coordinates based on the new screen dimensions
  57. uint16_t screen_x, screen_y;
  58. switch(current_instance->rotation_angle) {
  59. case 90:
  60. screen_x = rotated_x;
  61. screen_y = FRAME_HEIGHT - 8 + (rotated_y * 8) + i;
  62. break;
  63. case 180:
  64. screen_x = FRAME_WIDTH - 8 + (rotated_x * 8) + i;
  65. screen_y = FRAME_HEIGHT - 1 - rotated_y;
  66. break;
  67. case 270:
  68. screen_x = FRAME_WIDTH - 1 - rotated_x;
  69. screen_y = rotated_y * 8 + i;
  70. break;
  71. default:
  72. screen_x = rotated_x * 8 + i;
  73. screen_y = rotated_y;
  74. break;
  75. }
  76. canvas_draw_dot(canvas, screen_x, screen_y);
  77. }
  78. }
  79. }
  80. // Draw the guide if the camera is not initialized.
  81. if(!model->initialized) {
  82. canvas_draw_icon(canvas, 74, 16, &I_DolphinCommon_56x48);
  83. canvas_set_font(canvas, FontSecondary);
  84. canvas_draw_str(canvas, 8, 12, "Connect the ESP32-CAM");
  85. canvas_draw_str(canvas, 20, 24, "VCC - 3V3");
  86. canvas_draw_str(canvas, 20, 34, "GND - GND");
  87. canvas_draw_str(canvas, 20, 44, "U0R - TX");
  88. canvas_draw_str(canvas, 20, 54, "U0T - RX");
  89. }
  90. }
  91. static void camera_suite_view_style_1_model_init(UartDumpModel* const model) {
  92. for(size_t i = 0; i < FRAME_BUFFER_LENGTH; i++) {
  93. model->pixels[i] = 0;
  94. }
  95. }
  96. static bool camera_suite_view_style_1_input(InputEvent* event, void* context) {
  97. furi_assert(context);
  98. CameraSuiteViewStyle1* instance = context;
  99. if(event->type == InputTypeRelease) {
  100. uint8_t data[1];
  101. switch(event->key) {
  102. case InputKeyBack:
  103. // Stop the camera stream.
  104. data[0] = 's';
  105. // Go back to the main menu.
  106. with_view_model(
  107. instance->view,
  108. UartDumpModel * model,
  109. {
  110. UNUSED(model);
  111. instance->callback(CameraSuiteCustomEventSceneStyle1Back, instance->context);
  112. },
  113. true);
  114. break;
  115. case InputKeyLeft:
  116. // Camera: Invert.
  117. data[0] = '<';
  118. with_view_model(
  119. instance->view,
  120. UartDumpModel * model,
  121. {
  122. UNUSED(model);
  123. instance->callback(CameraSuiteCustomEventSceneStyle1Left, instance->context);
  124. },
  125. true);
  126. break;
  127. case InputKeyRight:
  128. // Camera: Enable/disable dithering.
  129. data[0] = '>';
  130. with_view_model(
  131. instance->view,
  132. UartDumpModel * model,
  133. {
  134. UNUSED(model);
  135. instance->callback(CameraSuiteCustomEventSceneStyle1Right, instance->context);
  136. },
  137. true);
  138. break;
  139. case InputKeyUp:
  140. // Camera: Increase contrast.
  141. data[0] = 'C';
  142. with_view_model(
  143. instance->view,
  144. UartDumpModel * model,
  145. {
  146. UNUSED(model);
  147. instance->callback(CameraSuiteCustomEventSceneStyle1Up, instance->context);
  148. },
  149. true);
  150. break;
  151. case InputKeyDown:
  152. // Camera: Reduce contrast.
  153. data[0] = 'c';
  154. with_view_model(
  155. instance->view,
  156. UartDumpModel * model,
  157. {
  158. UNUSED(model);
  159. instance->callback(CameraSuiteCustomEventSceneStyle1Down, instance->context);
  160. },
  161. true);
  162. break;
  163. case InputKeyOk:
  164. // Rotate the camera image 90 degrees
  165. instance->rotation_angle += 90;
  166. if(instance->rotation_angle >= 360) {
  167. instance->rotation_angle = 0;
  168. }
  169. with_view_model(
  170. instance->view,
  171. UartDumpModel * model,
  172. {
  173. UNUSED(model);
  174. instance->callback(CameraSuiteCustomEventSceneStyle1Ok, instance->context);
  175. },
  176. true);
  177. break;
  178. case InputKeyMAX:
  179. break;
  180. }
  181. // Send `data` to the ESP32-CAM
  182. furi_hal_uart_tx(FuriHalUartIdUSART1, data, 1);
  183. }
  184. return true;
  185. }
  186. static void camera_suite_view_style_1_exit(void* context) {
  187. furi_assert(context);
  188. }
  189. static void camera_suite_view_style_1_enter(void* context) {
  190. // Check `context` for null. If it is null, abort program, else continue.
  191. furi_assert(context);
  192. // Cast `context` to `CameraSuiteViewStyle1*` and store it in `instance`.
  193. CameraSuiteViewStyle1* instance = (CameraSuiteViewStyle1*)context;
  194. // Assign the current instance to the global variable
  195. current_instance = instance;
  196. uint8_t data[1];
  197. data[0] = 'S'; // Uppercase `S` to start the camera
  198. // Send `data` to the ESP32-CAM
  199. furi_hal_uart_tx(FuriHalUartIdUSART1, data, 1);
  200. with_view_model(
  201. instance->view,
  202. UartDumpModel * model,
  203. { camera_suite_view_style_1_model_init(model); },
  204. true);
  205. }
  206. static void camera_on_irq_cb(UartIrqEvent uartIrqEvent, uint8_t data, void* context) {
  207. // Check `context` for null. If it is null, abort program, else continue.
  208. furi_assert(context);
  209. // Cast `context` to `CameraSuiteViewStyle1*` and store it in `instance`.
  210. CameraSuiteViewStyle1* instance = context;
  211. // If `uartIrqEvent` is `UartIrqEventRXNE`, send the data to the
  212. // `rx_stream` and set the `WorkerEventRx` flag.
  213. if(uartIrqEvent == UartIrqEventRXNE) {
  214. furi_stream_buffer_send(instance->rx_stream, &data, 1, 0);
  215. furi_thread_flags_set(furi_thread_get_id(instance->worker_thread), WorkerEventRx);
  216. }
  217. }
  218. static void process_ringbuffer(UartDumpModel* model, uint8_t byte) {
  219. // First char has to be 'Y' in the buffer.
  220. if(model->ringbuffer_index == 0 && byte != 'Y') {
  221. return;
  222. }
  223. // Second char has to be ':' in the buffer or reset.
  224. if(model->ringbuffer_index == 1 && byte != ':') {
  225. model->ringbuffer_index = 0;
  226. process_ringbuffer(model, byte);
  227. return;
  228. }
  229. // Assign current byte to the ringbuffer.
  230. model->row_ringbuffer[model->ringbuffer_index] = byte;
  231. // Increment the ringbuffer index.
  232. ++model->ringbuffer_index;
  233. // Let's wait 'till the buffer fills.
  234. if(model->ringbuffer_index < RING_BUFFER_LENGTH) {
  235. return;
  236. }
  237. // Flush the ringbuffer to the framebuffer.
  238. model->ringbuffer_index = 0; // Reset the ringbuffer
  239. model->initialized = true; // Established the connection successfully.
  240. size_t row_start_index =
  241. model->row_ringbuffer[2] * ROW_BUFFER_LENGTH; // Third char will determine the row number
  242. if(row_start_index > LAST_ROW_INDEX) { // Failsafe
  243. row_start_index = 0;
  244. }
  245. for(size_t i = 0; i < ROW_BUFFER_LENGTH; ++i) {
  246. model->pixels[row_start_index + i] =
  247. model->row_ringbuffer[i + 3]; // Writing the remaining 16 bytes into the frame buffer
  248. }
  249. }
  250. static int32_t camera_worker(void* context) {
  251. furi_assert(context);
  252. CameraSuiteViewStyle1* instance = context;
  253. while(1) {
  254. uint32_t events =
  255. furi_thread_flags_wait(WORKER_EVENTS_MASK, FuriFlagWaitAny, FuriWaitForever);
  256. furi_check((events & FuriFlagError) == 0);
  257. if(events & WorkerEventStop) {
  258. break;
  259. } else if(events & WorkerEventRx) {
  260. size_t length = 0;
  261. do {
  262. size_t intended_data_size = 64;
  263. uint8_t data[intended_data_size];
  264. length =
  265. furi_stream_buffer_receive(instance->rx_stream, data, intended_data_size, 0);
  266. if(length > 0) {
  267. with_view_model(
  268. instance->view,
  269. UartDumpModel * model,
  270. {
  271. for(size_t i = 0; i < length; i++) {
  272. process_ringbuffer(model, data[i]);
  273. }
  274. },
  275. false);
  276. }
  277. } while(length > 0);
  278. }
  279. }
  280. return 0;
  281. }
  282. CameraSuiteViewStyle1* camera_suite_view_style_1_alloc() {
  283. CameraSuiteViewStyle1* instance = malloc(sizeof(CameraSuiteViewStyle1));
  284. instance->view = view_alloc();
  285. instance->rx_stream = furi_stream_buffer_alloc(2048, 1);
  286. // Set up views
  287. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(UartDumpModel));
  288. view_set_context(instance->view, instance); // furi_assert crashes in events without this
  289. view_set_draw_callback(instance->view, (ViewDrawCallback)camera_suite_view_style_1_draw);
  290. view_set_input_callback(instance->view, camera_suite_view_style_1_input);
  291. view_set_enter_callback(instance->view, camera_suite_view_style_1_enter);
  292. view_set_exit_callback(instance->view, camera_suite_view_style_1_exit);
  293. with_view_model(
  294. instance->view,
  295. UartDumpModel * model,
  296. { camera_suite_view_style_1_model_init(model); },
  297. true);
  298. instance->worker_thread = furi_thread_alloc_ex("UsbUartWorker", 2048, camera_worker, instance);
  299. furi_thread_start(instance->worker_thread);
  300. // Enable uart listener
  301. furi_hal_console_disable();
  302. furi_hal_uart_set_br(FuriHalUartIdUSART1, 230400);
  303. furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, camera_on_irq_cb, instance);
  304. return instance;
  305. }
  306. void camera_suite_view_style_1_free(CameraSuiteViewStyle1* instance) {
  307. furi_assert(instance);
  308. with_view_model(
  309. instance->view, UartDumpModel * model, { UNUSED(model); }, true);
  310. view_free(instance->view);
  311. free(instance);
  312. }
  313. View* camera_suite_view_style_1_get_view(CameraSuiteViewStyle1* instance) {
  314. furi_assert(instance);
  315. return instance->view;
  316. }