gpio_scene_usb_uart_config.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include "../usb_uart_bridge.h"
  2. #include "../gpio_app_i.h"
  3. #include "furi_hal.h"
  4. typedef enum {
  5. UsbUartLineIndexVcp,
  6. UsbUartLineIndexBaudrate,
  7. UsbUartLineIndexUart,
  8. UsbUartLineIndexFlow,
  9. } LineIndex;
  10. static UsbUartConfig* cfg_set;
  11. static const char* vcp_ch[] = {"0 (CLI)", "1"};
  12. static const char* uart_ch[] = {"13,14", "15,16"};
  13. static const char* flow_pins[] = {"None", "2,3", "6,7"};
  14. static const char* baudrate_mode[] = {"Host"};
  15. static const uint32_t baudrate_list[] = {
  16. 2400,
  17. 9600,
  18. 19200,
  19. 38400,
  20. 57600,
  21. 115200,
  22. 230400,
  23. 460800,
  24. 921600,
  25. };
  26. bool gpio_scene_usb_uart_cfg_on_event(void* context, SceneManagerEvent event) {
  27. UNUSED(context);
  28. UNUSED(event);
  29. return false;
  30. }
  31. static void line_vcp_cb(VariableItem* item) {
  32. GpioApp* app = variable_item_get_context(item);
  33. uint8_t index = variable_item_get_current_value_index(item);
  34. variable_item_set_current_value_text(item, vcp_ch[index]);
  35. cfg_set->vcp_ch = index;
  36. usb_uart_set_config(app->usb_uart_bridge, cfg_set);
  37. }
  38. static void line_port_cb(VariableItem* item) {
  39. GpioApp* app = variable_item_get_context(item);
  40. uint8_t index = variable_item_get_current_value_index(item);
  41. variable_item_set_current_value_text(item, uart_ch[index]);
  42. if(index == 0)
  43. cfg_set->uart_ch = FuriHalUartIdUSART1;
  44. else if(index == 1)
  45. cfg_set->uart_ch = FuriHalUartIdLPUART1;
  46. usb_uart_set_config(app->usb_uart_bridge, cfg_set);
  47. }
  48. static void line_flow_cb(VariableItem* item) {
  49. GpioApp* app = variable_item_get_context(item);
  50. uint8_t index = variable_item_get_current_value_index(item);
  51. variable_item_set_current_value_text(item, flow_pins[index]);
  52. cfg_set->flow_pins = index;
  53. usb_uart_set_config(app->usb_uart_bridge, cfg_set);
  54. }
  55. static void line_baudrate_cb(VariableItem* item) {
  56. GpioApp* app = variable_item_get_context(item);
  57. uint8_t index = variable_item_get_current_value_index(item);
  58. char br_text[8];
  59. if(index > 0) {
  60. snprintf(br_text, 7, "%lu", baudrate_list[index - 1]);
  61. variable_item_set_current_value_text(item, br_text);
  62. cfg_set->baudrate = baudrate_list[index - 1];
  63. } else {
  64. variable_item_set_current_value_text(item, baudrate_mode[index]);
  65. cfg_set->baudrate = 0;
  66. }
  67. cfg_set->baudrate_mode = index;
  68. usb_uart_set_config(app->usb_uart_bridge, cfg_set);
  69. }
  70. void gpio_scene_usb_uart_cfg_on_enter(void* context) {
  71. GpioApp* app = context;
  72. VariableItemList* var_item_list = app->var_item_list;
  73. cfg_set = malloc(sizeof(UsbUartConfig));
  74. usb_uart_get_config(app->usb_uart_bridge, cfg_set);
  75. VariableItem* item;
  76. char br_text[8];
  77. item = variable_item_list_add(var_item_list, "USB Channel", 2, line_vcp_cb, app);
  78. variable_item_set_current_value_index(item, cfg_set->vcp_ch);
  79. variable_item_set_current_value_text(item, vcp_ch[cfg_set->vcp_ch]);
  80. item = variable_item_list_add(
  81. var_item_list,
  82. "Baudrate",
  83. sizeof(baudrate_list) / sizeof(baudrate_list[0]) + 1,
  84. line_baudrate_cb,
  85. app);
  86. variable_item_set_current_value_index(item, cfg_set->baudrate_mode);
  87. if(cfg_set->baudrate_mode > 0) {
  88. snprintf(br_text, 7, "%lu", baudrate_list[cfg_set->baudrate_mode - 1]);
  89. variable_item_set_current_value_text(item, br_text);
  90. } else {
  91. variable_item_set_current_value_text(item, baudrate_mode[cfg_set->baudrate_mode]);
  92. }
  93. item = variable_item_list_add(var_item_list, "UART Pins", 2, line_port_cb, app);
  94. variable_item_set_current_value_index(item, cfg_set->uart_ch);
  95. variable_item_set_current_value_text(item, uart_ch[cfg_set->uart_ch]);
  96. item = variable_item_list_add(var_item_list, "RTS/DTR Pins", 3, line_flow_cb, app);
  97. variable_item_set_current_value_index(item, cfg_set->flow_pins);
  98. variable_item_set_current_value_text(item, flow_pins[cfg_set->flow_pins]);
  99. variable_item_list_set_selected_item(
  100. var_item_list, scene_manager_get_scene_state(app->scene_manager, GpioAppViewUsbUartCfg));
  101. view_dispatcher_switch_to_view(app->view_dispatcher, GpioAppViewUsbUartCfg);
  102. }
  103. void gpio_scene_usb_uart_cfg_on_exit(void* context) {
  104. GpioApp* app = context;
  105. scene_manager_set_scene_state(
  106. app->scene_manager,
  107. GpioAppViewUsbUartCfg,
  108. variable_item_list_get_selected_item_index(app->var_item_list));
  109. variable_item_list_reset(app->var_item_list);
  110. free(cfg_set);
  111. }