camera_suite_view_style_1.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. with_view_model(
  197. instance->view,
  198. UartDumpModel * model,
  199. { camera_suite_view_style_1_model_init(model); },
  200. true);
  201. }
  202. static void camera_on_irq_cb(UartIrqEvent uartIrqEvent, uint8_t data, void* context) {
  203. // Check `context` for null. If it is null, abort program, else continue.
  204. furi_assert(context);
  205. // Cast `context` to `CameraSuiteViewStyle1*` and store it in `instance`.
  206. CameraSuiteViewStyle1* instance = context;
  207. // If `uartIrqEvent` is `UartIrqEventRXNE`, send the data to the
  208. // `rx_stream` and set the `WorkerEventRx` flag.
  209. if(uartIrqEvent == UartIrqEventRXNE) {
  210. furi_stream_buffer_send(instance->rx_stream, &data, 1, 0);
  211. furi_thread_flags_set(furi_thread_get_id(instance->worker_thread), WorkerEventRx);
  212. }
  213. }
  214. static void process_ringbuffer(UartDumpModel* model, uint8_t byte) {
  215. // First char has to be 'Y' in the buffer.
  216. if(model->ringbuffer_index == 0 && byte != 'Y') {
  217. return;
  218. }
  219. // Second char has to be ':' in the buffer or reset.
  220. if(model->ringbuffer_index == 1 && byte != ':') {
  221. model->ringbuffer_index = 0;
  222. process_ringbuffer(model, byte);
  223. return;
  224. }
  225. // Assign current byte to the ringbuffer.
  226. model->row_ringbuffer[model->ringbuffer_index] = byte;
  227. // Increment the ringbuffer index.
  228. ++model->ringbuffer_index;
  229. // Let's wait 'till the buffer fills.
  230. if(model->ringbuffer_index < RING_BUFFER_LENGTH) {
  231. return;
  232. }
  233. // Flush the ringbuffer to the framebuffer.
  234. model->ringbuffer_index = 0; // Reset the ringbuffer
  235. model->initialized = true; // Established the connection successfully.
  236. size_t row_start_index =
  237. model->row_ringbuffer[2] * ROW_BUFFER_LENGTH; // Third char will determine the row number
  238. if(row_start_index > LAST_ROW_INDEX) { // Failsafe
  239. row_start_index = 0;
  240. }
  241. for(size_t i = 0; i < ROW_BUFFER_LENGTH; ++i) {
  242. model->pixels[row_start_index + i] =
  243. model->row_ringbuffer[i + 3]; // Writing the remaining 16 bytes into the frame buffer
  244. }
  245. }
  246. static int32_t camera_worker(void* context) {
  247. furi_assert(context);
  248. CameraSuiteViewStyle1* instance = context;
  249. while(1) {
  250. uint32_t events =
  251. furi_thread_flags_wait(WORKER_EVENTS_MASK, FuriFlagWaitAny, FuriWaitForever);
  252. furi_check((events & FuriFlagError) == 0);
  253. if(events & WorkerEventStop) {
  254. break;
  255. } else if(events & WorkerEventRx) {
  256. size_t length = 0;
  257. do {
  258. size_t intended_data_size = 64;
  259. uint8_t data[intended_data_size];
  260. length =
  261. furi_stream_buffer_receive(instance->rx_stream, data, intended_data_size, 0);
  262. if(length > 0) {
  263. with_view_model(
  264. instance->view,
  265. UartDumpModel * model,
  266. {
  267. for(size_t i = 0; i < length; i++) {
  268. process_ringbuffer(model, data[i]);
  269. }
  270. },
  271. false);
  272. }
  273. } while(length > 0);
  274. }
  275. }
  276. return 0;
  277. }
  278. CameraSuiteViewStyle1* camera_suite_view_style_1_alloc() {
  279. CameraSuiteViewStyle1* instance = malloc(sizeof(CameraSuiteViewStyle1));
  280. instance->view = view_alloc();
  281. instance->rx_stream = furi_stream_buffer_alloc(2048, 1);
  282. // Set up views
  283. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(UartDumpModel));
  284. view_set_context(instance->view, instance); // furi_assert crashes in events without this
  285. view_set_draw_callback(instance->view, (ViewDrawCallback)camera_suite_view_style_1_draw);
  286. view_set_input_callback(instance->view, camera_suite_view_style_1_input);
  287. view_set_enter_callback(instance->view, camera_suite_view_style_1_enter);
  288. view_set_exit_callback(instance->view, camera_suite_view_style_1_exit);
  289. with_view_model(
  290. instance->view,
  291. UartDumpModel * model,
  292. { camera_suite_view_style_1_model_init(model); },
  293. true);
  294. instance->worker_thread = furi_thread_alloc_ex("UsbUartWorker", 2048, camera_worker, instance);
  295. furi_thread_start(instance->worker_thread);
  296. // Enable uart listener
  297. furi_hal_console_disable();
  298. furi_hal_uart_set_br(FuriHalUartIdUSART1, 230400);
  299. furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, camera_on_irq_cb, instance);
  300. return instance;
  301. }
  302. void camera_suite_view_style_1_free(CameraSuiteViewStyle1* instance) {
  303. furi_assert(instance);
  304. with_view_model(
  305. instance->view, UartDumpModel * model, { UNUSED(model); }, true);
  306. view_free(instance->view);
  307. free(instance);
  308. }
  309. View* camera_suite_view_style_1_get_view(CameraSuiteViewStyle1* instance) {
  310. furi_assert(instance);
  311. return instance->view;
  312. }