camera_suite_view_style_1.c 12 KB

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