2021-10-26 21:41:56 +03:00
|
|
|
#include "../usb_uart_bridge.h"
|
2021-10-21 21:12:20 +03:00
|
|
|
#include "../gpio_app_i.h"
|
|
|
|
#include "furi-hal.h"
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
UsbUartLineIndexVcp,
|
|
|
|
UsbUartLineIndexUart,
|
|
|
|
UsbUartLineIndexBaudrate,
|
|
|
|
UsbUartLineIndexEnable,
|
|
|
|
UsbUartLineIndexDisable,
|
|
|
|
} LineIndex;
|
|
|
|
|
2021-10-26 21:41:56 +03:00
|
|
|
static UsbUartConfig* cfg_set;
|
2021-10-21 21:12:20 +03:00
|
|
|
|
|
|
|
static const char* vcp_ch[] = {"0 (CLI)", "1"};
|
|
|
|
static const char* uart_ch[] = {"USART1", "LPUART1"};
|
|
|
|
static const char* baudrate_mode[] = {"Host"};
|
|
|
|
static const uint32_t baudrate_list[] = {
|
|
|
|
2400,
|
|
|
|
9600,
|
|
|
|
19200,
|
|
|
|
38400,
|
|
|
|
57600,
|
|
|
|
115200,
|
|
|
|
230400,
|
|
|
|
460800,
|
|
|
|
921600,
|
|
|
|
};
|
|
|
|
|
|
|
|
bool gpio_scene_usb_uart_on_event(void* context, SceneManagerEvent event) {
|
|
|
|
//GpioApp* app = context;
|
|
|
|
bool consumed = false;
|
|
|
|
|
|
|
|
if(event.type == SceneManagerEventTypeCustom) {
|
2021-10-26 21:41:56 +03:00
|
|
|
if(event.event == GPIO_SCENE_USB_UART_CUSTOM_EVENT_ENABLE) {
|
|
|
|
usb_uart_enable(cfg_set);
|
|
|
|
} else if(event.event == GPIO_SCENE_USB_UART_CUSTOM_EVENT_DISABLE) {
|
2021-10-21 21:12:20 +03:00
|
|
|
usb_uart_disable();
|
|
|
|
}
|
|
|
|
consumed = true;
|
|
|
|
}
|
|
|
|
return consumed;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void line_vcp_cb(VariableItem* item) {
|
|
|
|
//GpioApp* app = variable_item_get_context(item);
|
|
|
|
uint8_t index = variable_item_get_current_value_index(item);
|
|
|
|
|
|
|
|
variable_item_set_current_value_text(item, vcp_ch[index]);
|
|
|
|
|
2021-10-26 21:41:56 +03:00
|
|
|
cfg_set->vcp_ch = index;
|
2021-10-21 21:12:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void line_port_cb(VariableItem* item) {
|
|
|
|
//GpioApp* app = variable_item_get_context(item);
|
|
|
|
uint8_t index = variable_item_get_current_value_index(item);
|
|
|
|
|
|
|
|
variable_item_set_current_value_text(item, uart_ch[index]);
|
|
|
|
|
2021-10-26 21:41:56 +03:00
|
|
|
if(index == 0)
|
|
|
|
cfg_set->uart_ch = FuriHalUartIdUSART1;
|
|
|
|
else if(index == 1)
|
|
|
|
cfg_set->uart_ch = FuriHalUartIdLPUART1;
|
2021-10-21 21:12:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void line_baudrate_cb(VariableItem* item) {
|
|
|
|
//GpioApp* app = variable_item_get_context(item);
|
|
|
|
uint8_t index = variable_item_get_current_value_index(item);
|
|
|
|
|
2021-10-26 21:41:56 +03:00
|
|
|
char br_text[8];
|
|
|
|
|
2021-10-21 21:12:20 +03:00
|
|
|
if(index > 0) {
|
2021-10-26 21:41:56 +03:00
|
|
|
snprintf(br_text, 7, "%lu", baudrate_list[index - 1]);
|
|
|
|
variable_item_set_current_value_text(item, br_text);
|
|
|
|
cfg_set->baudrate = baudrate_list[index - 1];
|
2021-10-21 21:12:20 +03:00
|
|
|
} else {
|
|
|
|
variable_item_set_current_value_text(item, baudrate_mode[index]);
|
2021-10-26 21:41:56 +03:00
|
|
|
cfg_set->baudrate = 0;
|
2021-10-21 21:12:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gpio_scene_usb_uart_enter_callback(void* context, uint32_t index) {
|
|
|
|
furi_assert(context);
|
|
|
|
GpioApp* app = context;
|
2021-10-26 21:41:56 +03:00
|
|
|
if(index == UsbUartLineIndexEnable)
|
|
|
|
view_dispatcher_send_custom_event(
|
|
|
|
app->view_dispatcher, GPIO_SCENE_USB_UART_CUSTOM_EVENT_ENABLE);
|
|
|
|
else if(index == UsbUartLineIndexDisable)
|
|
|
|
view_dispatcher_send_custom_event(
|
|
|
|
app->view_dispatcher, GPIO_SCENE_USB_UART_CUSTOM_EVENT_DISABLE);
|
2021-10-21 21:12:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void gpio_scene_usb_uart_on_enter(void* context) {
|
|
|
|
GpioApp* app = context;
|
|
|
|
VariableItemList* var_item_list = app->var_item_list;
|
|
|
|
|
2021-10-26 21:41:56 +03:00
|
|
|
cfg_set = furi_alloc(sizeof(UsbUartConfig));
|
2021-10-21 21:12:20 +03:00
|
|
|
|
|
|
|
VariableItem* item;
|
|
|
|
|
|
|
|
variable_item_list_set_enter_callback(var_item_list, gpio_scene_usb_uart_enter_callback, app);
|
|
|
|
|
|
|
|
item = variable_item_list_add(var_item_list, "VCP Channel", 2, line_vcp_cb, app);
|
|
|
|
variable_item_set_current_value_index(item, 0);
|
|
|
|
variable_item_set_current_value_text(item, vcp_ch[0]);
|
|
|
|
|
|
|
|
item = variable_item_list_add(var_item_list, "UART Port", 2, line_port_cb, app);
|
|
|
|
variable_item_set_current_value_index(item, 0);
|
|
|
|
variable_item_set_current_value_text(item, uart_ch[0]);
|
|
|
|
|
|
|
|
item = variable_item_list_add(
|
|
|
|
var_item_list,
|
|
|
|
"Baudrate",
|
|
|
|
sizeof(baudrate_list) / sizeof(baudrate_list[0]) + 1,
|
|
|
|
line_baudrate_cb,
|
|
|
|
app);
|
|
|
|
variable_item_set_current_value_index(item, 0);
|
|
|
|
variable_item_set_current_value_text(item, baudrate_mode[0]);
|
|
|
|
|
|
|
|
item = variable_item_list_add(var_item_list, "Enable", 0, NULL, NULL);
|
|
|
|
item = variable_item_list_add(var_item_list, "Disable", 0, NULL, NULL);
|
|
|
|
|
2021-11-04 22:33:28 +03:00
|
|
|
variable_item_list_set_selected_item(
|
|
|
|
var_item_list, scene_manager_get_scene_state(app->scene_manager, GpioSceneUsbUart));
|
|
|
|
|
2021-10-21 21:12:20 +03:00
|
|
|
view_dispatcher_switch_to_view(app->view_dispatcher, GpioAppViewUsbUart);
|
|
|
|
}
|
|
|
|
|
|
|
|
void gpio_scene_usb_uart_on_exit(void* context) {
|
|
|
|
GpioApp* app = context;
|
|
|
|
usb_uart_disable();
|
2021-11-04 22:33:28 +03:00
|
|
|
scene_manager_set_scene_state(
|
|
|
|
app->scene_manager,
|
|
|
|
GpioSceneUsbUart,
|
|
|
|
variable_item_list_get_selected_item_index(app->var_item_list));
|
2021-10-21 21:12:20 +03:00
|
|
|
variable_item_list_clean(app->var_item_list);
|
2021-10-26 21:41:56 +03:00
|
|
|
free(cfg_set);
|
|
|
|
}
|