gpio_scene_usb_uart.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include "../gpio_app_i.h"
  2. #include "../usb_uart_bridge.h"
  3. typedef struct {
  4. UsbUartConfig cfg;
  5. UsbUartState state;
  6. } SceneUsbUartBridge;
  7. static SceneUsbUartBridge* scene_usb_uart;
  8. void gpio_scene_usb_uart_callback(GpioCustomEvent event, void* context) {
  9. furi_assert(context);
  10. GpioApp* app = context;
  11. view_dispatcher_send_custom_event(app->view_dispatcher, event);
  12. }
  13. void gpio_scene_usb_uart_on_enter(void* context) {
  14. GpioApp* app = context;
  15. uint32_t prev_state = scene_manager_get_scene_state(app->scene_manager, GpioAppViewUsbUart);
  16. if(prev_state == 0) {
  17. scene_usb_uart = furi_alloc(sizeof(SceneUsbUartBridge));
  18. scene_usb_uart->cfg.vcp_ch = 0; // TODO: settings load
  19. scene_usb_uart->cfg.uart_ch = 0;
  20. scene_usb_uart->cfg.flow_pins = 0;
  21. scene_usb_uart->cfg.baudrate_mode = 0;
  22. scene_usb_uart->cfg.baudrate = 0;
  23. app->usb_uart_bridge = usb_uart_enable(&scene_usb_uart->cfg);
  24. }
  25. usb_uart_get_config(app->usb_uart_bridge, &scene_usb_uart->cfg);
  26. usb_uart_get_state(app->usb_uart_bridge, &scene_usb_uart->state);
  27. gpio_usb_uart_set_callback(app->gpio_usb_uart, gpio_scene_usb_uart_callback, app);
  28. scene_manager_set_scene_state(app->scene_manager, GpioAppViewUsbUart, 0);
  29. view_dispatcher_switch_to_view(app->view_dispatcher, GpioAppViewUsbUart);
  30. }
  31. bool gpio_scene_usb_uart_on_event(void* context, SceneManagerEvent event) {
  32. GpioApp* app = context;
  33. if(event.type == SceneManagerEventTypeCustom) {
  34. scene_manager_set_scene_state(app->scene_manager, GpioAppViewUsbUart, 1);
  35. scene_manager_next_scene(app->scene_manager, GpioAppViewUsbUartCfg);
  36. return true;
  37. } else if(event.type == SceneManagerEventTypeTick) {
  38. uint32_t tx_cnt_last = scene_usb_uart->state.tx_cnt;
  39. uint32_t rx_cnt_last = scene_usb_uart->state.rx_cnt;
  40. usb_uart_get_state(app->usb_uart_bridge, &scene_usb_uart->state);
  41. gpio_usb_uart_update_state(
  42. app->gpio_usb_uart, &scene_usb_uart->cfg, &scene_usb_uart->state);
  43. if(tx_cnt_last != scene_usb_uart->state.tx_cnt)
  44. notification_message(app->notifications, &sequence_blink_blue_10);
  45. if(rx_cnt_last != scene_usb_uart->state.rx_cnt)
  46. notification_message(app->notifications, &sequence_blink_green_10);
  47. }
  48. return false;
  49. }
  50. void gpio_scene_usb_uart_on_exit(void* context) {
  51. GpioApp* app = context;
  52. uint32_t prev_state = scene_manager_get_scene_state(app->scene_manager, GpioAppViewUsbUart);
  53. if(prev_state == 0) {
  54. usb_uart_disable(app->usb_uart_bridge);
  55. free(scene_usb_uart);
  56. }
  57. }