添加STAwifi模式以及修改灯状态

This commit is contained in:
OPTOC
2025-08-28 16:04:01 +08:00
parent 2ab5a97ac3
commit 56c1716726
8 changed files with 230 additions and 54 deletions

View File

@@ -511,7 +511,7 @@ static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_
size_t len = os_pipe_fifo_fill(&port_data->pipe_obj, p_data->write.value, p_data->write.len); size_t len = os_pipe_fifo_fill(&port_data->pipe_obj, p_data->write.value, p_data->write.len);
if (len < p_data->write.len) if (len < p_data->write.len)
{ {
SYS_LOG_WRN("Pipeline cache exceeded %d bytes", p_data->write.len - len); SYS_LOG_WRN("read Pipeline cache exceeded %d bytes", p_data->write.len - len);
} }
} }
else else
@@ -559,7 +559,7 @@ static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_
size_t len = os_pipe_fifo_fill(&port_data->pipe_obj, fifo->data, fifo->len); size_t len = os_pipe_fifo_fill(&port_data->pipe_obj, fifo->data, fifo->len);
if (len < p_data->write.len) if (len < p_data->write.len)
{ {
SYS_LOG_WRN("Pipeline cache exceeded %d bytes", p_data->write.len - len); SYS_LOG_WRN("write Pipeline cache exceeded %d bytes", p_data->write.len - len);
} }
os_fifo_free(fifo); os_fifo_free(fifo);

View File

@@ -11,10 +11,12 @@ uint16_t toggle_cycle = 1000;
static uint8_t rgb_color_index = RGB_COLOR_RAD; static uint8_t rgb_color_index = RGB_COLOR_RAD;
rgb_color_t rgb_color_rad = {0,255,0,0,0}; rgb_color_t rgb_color_rad = {0,255,0,0,0};
rgb_color_t rgb_color_orange = {0,255,80,0,0};
rgb_color_t rgb_color_green = {0,0,255,0,0}; rgb_color_t rgb_color_green = {0,0,255,0,0};
rgb_color_t rgb_color_blue = {0,0,0,255,0}; rgb_color_t rgb_color_blue = {0,0,0,255,0};
rgb_color_t rgb_color_cyan = {0,0,255,255,0};
rgb_color_t rgb_color_purple = {0,255,0,255,0};
rgb_color_t rgb_color_white = {0,255,255,255,0};
void led_strip_init(void) void led_strip_init(void)
{ {
@@ -71,9 +73,21 @@ void _work_rgb_led(void *arg)
case RGB_COLOR_RAD: case RGB_COLOR_RAD:
expression = &rgb_color_rad; expression = &rgb_color_rad;
break; break;
case RGB_COLOR_ORANGE:
expression = &rgb_color_orange;
break;
case RGB_COLOR_GREEN: case RGB_COLOR_GREEN:
expression = &rgb_color_green; expression = &rgb_color_green;
break; break;
case RGB_COLOR_WHITE:
expression = &rgb_color_white;
break;
case RGB_COLOR_PURPLE:
expression = &rgb_color_purple;
break;
case RGB_COLOR_CYAN:
expression = &rgb_color_cyan;
break;
case RGB_COLOR_BLUE: case RGB_COLOR_BLUE:
expression = &rgb_color_blue; expression = &rgb_color_blue;
break; break;

View File

@@ -35,7 +35,11 @@ typedef struct rgb_color_s
typedef enum typedef enum
{ {
RGB_COLOR_RAD = 0, RGB_COLOR_RAD = 0,
RGB_COLOR_ORANGE,
RGB_COLOR_GREEN, RGB_COLOR_GREEN,
RGB_COLOR_WHITE,
RGB_COLOR_PURPLE,
RGB_COLOR_CYAN,
RGB_COLOR_BLUE, RGB_COLOR_BLUE,
}RGB_COLOR; }RGB_COLOR;

View File

@@ -40,19 +40,19 @@ uint8_t device_init(device_t *port)
{ {
SYS_LOG_ERR("embedded device choice error"); SYS_LOG_ERR("embedded device choice error");
} }
//默认蓝牙 val //默认BLE模式
res = pc_device_choice(port, DATA_PORT_TYPE_WIFI); res = pc_device_choice_inside(port, DATA_PORT_TYPE_BLE_VAL, DISCONNECT);
res = pc_device_choice_inside(port, DATA_PORT_TYPE_BLE_VAL, 0);
if(res == DEVICE_PC_ERROR) if(res == DEVICE_PC_ERROR)
{ {
SYS_LOG_ERR("pc device choice error"); SYS_LOG_ERR("pc device choice error");
} }
SYS_LOG_INF("device init success");
return DEVICE_OK; return DEVICE_OK;
} }
uint8_t uart_init(init_device_t *port) uint8_t uart_init(init_device_t *port)
{ {
// port->uart_port = sb_uart_port_bind(g_cfg_board->uart_fc.id, g_cfg_board->uart_fc.br, g_cfg_board->uart_fc.pin_txd.pin, g_cfg_board->uart_fc.pin_rxd.pin, g_cfg_board->uart_fc.irq_prior, 1024, 0, NULL); // port->uart_port = sb_uart_port_bind(g_cfg_board->uart_fc.id, g_cfg_board->uart_fc.br, g_cfg_board->uart_fc.pin_txd.pin, g_cfg_board->uart_fc.pin_rxd.pin, g_cfg_board->uart_fc.irq_prior, 1024, 0, NULL);
port->uart_port = sb_uart_port_bind(1, 115200, 0, 10, 21, 1024, 0, NULL); port->uart_port = sb_uart_port_bind(1, 115200, 21, 20, 21, 1024, 0, NULL);
if(port->uart_port == NULL) if(port->uart_port == NULL)
{ {
@@ -76,7 +76,7 @@ static void ble_server_connect_handler(ble_server_status_t status)
} }
SYS_LOG_INF("ble Connected"); SYS_LOG_INF("ble Connected");
} else if(status == BLE_SERVER_STATUS_DISCONNECTED){ } else if(status == BLE_SERVER_STATUS_DISCONNECTED){
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_BLE_VAL, 0); uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_BLE_VAL, DISCONNECT);
if(res == DEVICE_PC_ERROR){ if(res == DEVICE_PC_ERROR){
} }
SYS_LOG_INF("ble dis Connected"); SYS_LOG_INF("ble dis Connected");
@@ -122,14 +122,54 @@ static void wifi_event_handler(bool is_connect, sb_data_port_t *port)
{ {
//实现自动切换发送对象 //实现自动切换发送对象
if(is_connect){ if(is_connect){
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_WIFI, CONNECT_WIFI); uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_WIFI_TCP, CONNECT_WIFI_TCP);
if(res == DEVICE_PC_ERROR) if(res == DEVICE_PC_ERROR)
{ {
SYS_LOG_ERR("wifi pc device choice error"); SYS_LOG_ERR("wifi pc device choice error");
} }
SYS_LOG_INF("wifi connect"); SYS_LOG_INF("wifi connect");
}else{ }else{
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_WIFI, 0); uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_WIFI_UDP, CONNECT_WIFI_UDP);
if(res == DEVICE_PC_ERROR)
{
SYS_LOG_ERR("wifi pc device choice error");
}
SYS_LOG_INF("wifi disconnect");
}
}
static void wifi_ap_connect_handler(wifi_ap_connect_status_t status, uint8_t connect_cnt)
{
SYS_LOG_WRN("wifi ap connect status %d %d", (int)status, (int)connect_cnt);
//实现自动切换发送对象
if((connect_cnt == 1 && status == WIFI_AP_CONNECT_GOT_STA)){
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_WIFI_UDP, CONNECT_WIFI_UDP);
if(res == DEVICE_PC_ERROR)
{
SYS_LOG_ERR("wifi pc device choice error");
}
SYS_LOG_INF("wifi connect");
}else{
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_WIFI_UDP, DISCONNECT);
if(res == DEVICE_PC_ERROR)
{
SYS_LOG_ERR("wifi pc device choice error");
}
SYS_LOG_INF("wifi disconnect");
}
}
static void wifi_sta_connect_handler(wifi_sta_connect_status_t status, uint8_t ip_v4[4])
{
SYS_LOG_WRN("wifi ap connect status %d", (int)status);
//实现自动切换发送对象
if(status == WIFI_STA_CONNECT_GOT_IP){
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_WIFI_UDP, CONNECT_WIFI_UDP);
if(res == DEVICE_PC_ERROR)
{
SYS_LOG_ERR("wifi pc device choice error");
}
SYS_LOG_INF("wifi connect");
}else{
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_WIFI_UDP, DISCONNECT);
if(res == DEVICE_PC_ERROR) if(res == DEVICE_PC_ERROR)
{ {
SYS_LOG_ERR("wifi pc device choice error"); SYS_LOG_ERR("wifi pc device choice error");
@@ -148,22 +188,23 @@ uint8_t wifi_init(init_device_t *port)
.gw_v4 = {192, 168, 1, 1}, .gw_v4 = {192, 168, 1, 1},
.mask_v4 = {255, 255, 255, 0}, .mask_v4 = {255, 255, 255, 0},
.max_connection = 1, .max_connection = 1,
.connect_cb = NULL, .connect_cb = wifi_ap_connect_handler,
}, },
.sta = { .sta = {
#if 0 #if 0
.ssid = "KFXJ", .ssid = "KFXJ",
.password = "Kfdx201*", .password = "Kfdx201*",
#else #else
.ssid = "SDWAN", .ssid = "Sertorf_sta",
.password = "Dxltkj201", .password = "12345678",
#endif #endif
.connect_cb = NULL, .connect_cb = wifi_sta_connect_handler,
}, },
}; };
wifi_netif_init(&init_struct); wifi_netif_init(&init_struct);
wifi_set_mode(WIFI_NETIF_MODE_AP); wifi_set_mode(WIFI_NETIF_MODE_AP);
// wifi_set_mode(WIFI_NETIF_MODE_STA);
// 初始化socket // 初始化socket
socket_inet_init(); socket_inet_init();
@@ -176,17 +217,51 @@ uint8_t wifi_init(init_device_t *port)
.rx_resume_work = NULL, .rx_resume_work = NULL,
}; };
port->wifi = socket_inet_server_bind_tcp(port->tcp_listen, &bind_param); port->wifi_tcp = socket_inet_server_bind_tcp(port->tcp_listen, &bind_param);
if (port->wifi) port->wifi_udp = socket_inet_server_bind_udp(14550, &bind_param);
if (port->wifi_tcp)
{ {
sb_data_port_start(port->wifi); sb_data_port_start(port->wifi_tcp);
} }
if (port->wifi_udp)
{
sb_data_port_start(port->wifi_udp);
}
wifi_mode_switch(port);
wifi_start(); wifi_start();
return DEVICE_OK; return DEVICE_OK;
} }
void wifi_mode_switch(init_device_t *port)
{
static init_device_t* port_lat = NULL;
static bool wifi_mode_flag = false;
if(port_lat == NULL && port != NULL)
{
port_lat = port;
return;
}
if (wifi_mode_flag)
{
wifi_mode_flag = false;
wifi_set_mode(WIFI_NETIF_MODE_AP);
port_lat->wifi_mode = WIFI_NETIF_MODE_AP;
SYS_LOG_INF("wifi mode switch to ap");
}
else
{
wifi_mode_flag = true;
wifi_set_mode(WIFI_NETIF_MODE_STA);
port_lat->wifi_mode = WIFI_NETIF_MODE_STA;
SYS_LOG_INF("wifi mode switch to sta");
}
wifi_start();
}
uint8_t embedded_device_choice(device_t *port, uint8_t type) uint8_t embedded_device_choice(device_t *port, uint8_t type)
{ {
//端口与PC一致 //端口与PC一致
@@ -202,8 +277,11 @@ uint8_t embedded_device_choice(device_t *port, uint8_t type)
case DATA_PORT_TYPE_UART: case DATA_PORT_TYPE_UART:
port->embedded_device = port->init_device.uart_port; port->embedded_device = port->init_device.uart_port;
break; break;
case DATA_PORT_TYPE_WIFI: case DATA_PORT_TYPE_WIFI_TCP:
port->embedded_device = port->init_device.wifi; port->embedded_device = port->init_device.wifi_tcp;
break;
case DATA_PORT_TYPE_WIFI_UDP:
port->embedded_device = port->init_device.wifi_udp;
break; break;
case DATA_PORT_TYPE_BLE_CMD: case DATA_PORT_TYPE_BLE_CMD:
port->embedded_device = port->init_device.ble_spp_server_cmd; port->embedded_device = port->init_device.ble_spp_server_cmd;
@@ -241,17 +319,15 @@ uint32_t embedded_device_get_rx_length(device_t *port)
static uint8_t pc_device_choice_inside(device_t *port, uint8_t type, uint8_t connect) static uint8_t pc_device_choice_inside(device_t *port, uint8_t type, uint8_t connect)
{ {
static device_t* port_device_inside = NULL; static device_t* port_device_inside = NULL;
// if(port_device_inside == NULL && port == NULL) SYS_LOG_WRN("pc_device_choice_inside type %d connect %d", type, connect);
// { if(port != NULL && port_device_inside == NULL){
// SYS_LOG_WRN("pc_device_choice_inside error port is null"); SYS_LOG_INF("pc_device_choice_inside");
// return DEVICE_PC_ERROR; pc_device_choice(port, type);
// } port_device_inside = port;
if(port == NULL && port_device_inside != NULL){ port_device_inside->connect_pc = connect;
}else if(port == NULL && port_device_inside != NULL && (port_device_inside->pc_device_type == type || connect != DISCONNECT)){
pc_device_choice(port_device_inside, type); pc_device_choice(port_device_inside, type);
port_device_inside->connect_pc = connect; port_device_inside->connect_pc = connect;
}else{
SYS_LOG_INF("pc_device_choice_inside");
port_device_inside = port;
} }
return DEVICE_OK; return DEVICE_OK;
} }
@@ -270,8 +346,11 @@ uint8_t pc_device_choice(device_t *port, uint8_t type)
case DATA_PORT_TYPE_UART: case DATA_PORT_TYPE_UART:
port->pc_device = port->init_device.uart_port; port->pc_device = port->init_device.uart_port;
break; break;
case DATA_PORT_TYPE_WIFI: case DATA_PORT_TYPE_WIFI_TCP:
port->pc_device = port->init_device.wifi; port->pc_device = port->init_device.wifi_tcp;
break;
case DATA_PORT_TYPE_WIFI_UDP:
port->pc_device = port->init_device.wifi_udp;
break; break;
case DATA_PORT_TYPE_BLE_CMD: case DATA_PORT_TYPE_BLE_CMD:
port->pc_device = port->init_device.ble_spp_server_cmd; port->pc_device = port->init_device.ble_spp_server_cmd;
@@ -285,7 +364,7 @@ uint8_t pc_device_choice(device_t *port, uint8_t type)
if(port->pc_device == NULL) if(port->pc_device == NULL)
{ {
SYS_LOG_WRN("embedded device choice error"); SYS_LOG_WRN("pc device choice error");
return DEVICE_PC_ERROR; return DEVICE_PC_ERROR;
} }

View File

@@ -28,8 +28,10 @@ enum DEVICE_ERROR
}; };
enum DATA_PORT_TYPE enum DATA_PORT_TYPE
{ {
DATA_PORT_TYPE_UART = 1, DATA_PORT_TYPE_NONE = 0,
DATA_PORT_TYPE_WIFI, DATA_PORT_TYPE_UART,
DATA_PORT_TYPE_WIFI_TCP,
DATA_PORT_TYPE_WIFI_UDP,
DATA_PORT_TYPE_BLE_CMD, DATA_PORT_TYPE_BLE_CMD,
DATA_PORT_TYPE_BLE_VAL DATA_PORT_TYPE_BLE_VAL
}; };
@@ -38,7 +40,15 @@ enum CONNECT_TYPE
DISCONNECT = 0, DISCONNECT = 0,
CONNECT_UART, CONNECT_UART,
CONNECT_BLE, CONNECT_BLE,
CONNECT_WIFI CONNECT_WIFI_TCP,
CONNECT_WIFI_UDP,
};
enum WIFI_MODE
{
AP_TCP,
STA_TCP,
AP_UDP,
STA_UDP
}; };
typedef struct typedef struct
{ {
@@ -47,7 +57,9 @@ typedef struct
sb_data_port_t* ble_spp_server_val; sb_data_port_t* ble_spp_server_val;
sb_data_port_t* ble_spp_client_cmd; sb_data_port_t* ble_spp_client_cmd;
sb_data_port_t* ble_spp_client_val; sb_data_port_t* ble_spp_client_val;
sb_data_port_t* wifi; sb_data_port_t* wifi_tcp;
sb_data_port_t* wifi_udp;
uint8_t wifi_mode;
socket_listen_tcp_t tcp_listen; socket_listen_tcp_t tcp_listen;
}init_device_t; }init_device_t;
@@ -96,6 +108,10 @@ uint8_t ble_init(init_device_t *port);
* @retval 0 成功 * @retval 0 成功
*/ */
uint8_t wifi_init(init_device_t *port); uint8_t wifi_init(init_device_t *port);
/**
* @brief WIFI 模式切换
*/
void wifi_mode_switch(init_device_t *port);
/** /**
* @brief 嵌入式设备数据接口 * @brief 嵌入式设备数据接口
* *

View File

@@ -12,6 +12,9 @@ void sertrf_init(void)
} }
//RGB灯 //RGB灯
work_rgb_led_start(); work_rgb_led_start();
//按键初始化
button_work_init();
//线程启动
sertrf_start(); sertrf_start();
} }
void sertrf_start(void) void sertrf_start(void)
@@ -35,37 +38,90 @@ void embedded_thread(void* arg)
// SYS_LOG_INF("data : %s", data); // SYS_LOG_INF("data : %s", data);
pc_device_write(&sertrf.device, data, embedded_size); pc_device_write(&sertrf.device, data, embedded_size);
} }
embedded_size = pc_device_get_rx_length(&sertrf.device);
if(embedded_size > 0) uint32_t pc_size = pc_device_get_rx_length(&sertrf.device);
if(pc_size > 0)
{ {
uint8_t data[embedded_size]; uint8_t data[pc_size];
pc_device_read(&sertrf.device, data, embedded_size); pc_device_read(&sertrf.device, data, pc_size);
// SYS_LOG_INF("data : %s", data); // SYS_LOG_INF("data : %s", data);
embedded_device_write(&sertrf.device, data, embedded_size); embedded_device_write(&sertrf.device, data, pc_size);
} }
pc_link_rgb_color(sertrf.device.connect_pc); // printf_chill_time(10,1000);
pc_link_rgb_color(&sertrf.device);
//需要添加一些延时,否则会卡死,导致看门狗复位 //需要添加一些延时,否则会卡死,导致看门狗复位
// vTaskDelay(( TickType_t ) 1000 / configTICK_RATE_HZ); // vTaskDelay(( TickType_t ) 1000 / configTICK_RATE_HZ);
os_thread_sleep(1); os_thread_sleep(1);
} }
} }
void pc_link_rgb_color(uint8_t connect) void pc_link_rgb_color(device_t* device)
{ {
static uint8_t last_connect = 0; static uint8_t last_connect = 0, last_wifi_mode = 0;
if(connect != last_connect) if(device->connect_pc != last_connect || last_wifi_mode != device->init_device.wifi_mode)
{ {
switch (connect) switch (device->connect_pc)
{ {
case DISCONNECT: case DISCONNECT:
rgb_color_change(0, RGB_COLOR_RAD); {
if(device->init_device.wifi_mode == WIFI_NETIF_MODE_AP)
rgb_color_change(0, RGB_COLOR_BLUE);
else
rgb_color_change(0, RGB_COLOR_ORANGE);
break; break;
case CONNECT_WIFI: }
case CONNECT_WIFI_TCP:
{
if(device->init_device.wifi_mode == WIFI_NETIF_MODE_AP)
rgb_color_change(0, RGB_COLOR_WHITE);
else
rgb_color_change(0, RGB_COLOR_PURPLE);
break;
}
case CONNECT_WIFI_UDP:
{
if(device->init_device.wifi_mode == WIFI_NETIF_MODE_AP)
rgb_color_change(0, RGB_COLOR_WHITE);
else
rgb_color_change(0, RGB_COLOR_PURPLE);
break;
}
case CONNECT_BLE:
rgb_color_change(0, RGB_COLOR_GREEN); rgb_color_change(0, RGB_COLOR_GREEN);
break; break;
case CONNECT_BLE:
rgb_color_change(0, RGB_COLOR_BLUE);
break;
} }
last_connect = connect; last_connect = device->connect_pc;
last_wifi_mode = device->init_device.wifi_mode;
}
if(device->connect_pc){
rgb_update_cyle(0);
}else{
rgb_update_cyle(500);
} }
} }
void printf_chill_time(uint8_t chill_time, uint16_t type)
{
static size_t last_time[24] = {0};
static uint16_t cnt[24] = {0};
static uint32_t type_cnt_time[24] = {0};
size_t now_time = os_get_sys_time();
cnt[chill_time]++;
type_cnt_time[chill_time] += now_time - last_time[chill_time];
if(cnt[chill_time] % type == 0 && type_cnt_time[chill_time] / type >= now_time - last_time[chill_time] - 1)
{
SYS_LOG_INF("TIME_CHILL%d : %d : %d",chill_time, now_time - last_time[chill_time], type_cnt_time[chill_time]);
cnt[chill_time] = 0;
type_cnt_time[chill_time] = 0;
}
else if(cnt[chill_time] % type == 0)
{
SYS_LOG_WRN("TIME_CHILL%d : %d : %d",chill_time, now_time - last_time[chill_time], type_cnt_time[chill_time]);
cnt[chill_time] = 0;
type_cnt_time[chill_time] = 0;
}
last_time[chill_time] = now_time;
}

View File

@@ -2,7 +2,7 @@
#include "device.h" #include "device.h"
#include "../led_strip/led_strip.h" #include "../led_strip/led_strip.h"
#include "key.h"
typedef struct typedef struct
{ {
device_t device; device_t device;
@@ -36,6 +36,12 @@ void sertrf_status(void);
void embedded_thread(void* arg); void embedded_thread(void* arg);
/** /**
* @brief 根据连接状态显示不同的颜色 * @brief 根据连接状态显示不同的颜色
*
* @param connect 连接状态
*/ */
void pc_link_rgb_color(uint8_t connect); void pc_link_rgb_color(device_t* device);
/**
* @brief 打印时间间隔
*/
void printf_chill_time(uint8_t chill_time, uint16_t type);

View File

@@ -6,6 +6,7 @@ CONFIG_BOOTLOADER_LOG_LEVEL_WARN=y
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
CONFIG_PARTITION_TABLE_CUSTOM=y CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_BT_ENABLED=y CONFIG_BT_ENABLED=y
CONFIG_ESP_WIFI_ENABLED=y
CONFIG_BT_BLE_42_FEATURES_SUPPORTED=y CONFIG_BT_BLE_42_FEATURES_SUPPORTED=y
CONFIG_ESP_HTTP_CLIENT_ENABLE_DIGEST_AUTH=y CONFIG_ESP_HTTP_CLIENT_ENABLE_DIGEST_AUTH=y
CONFIG_ESP_SLEEP_POWER_DOWN_FLASH=y CONFIG_ESP_SLEEP_POWER_DOWN_FLASH=y