添加RF、wifi、ble功能开关,以及在连接ble关闭wifi、连接wifi关闭ble的功能

This commit is contained in:
OPTOC
2025-10-20 12:06:29 +08:00
parent ce9fac6b32
commit c026c49fac
5 changed files with 201 additions and 59 deletions

View File

@@ -10,6 +10,10 @@
*/
static uint8_t pc_device_choice_inside(device_t *port, uint8_t type, uint8_t connect);
// 内部全局变量
static uint8_t* rf_flag_off_overall = NULL;
static device_t *device_inside;
uint8_t device_init(device_t *port)
{
uint8_t res = DEVICE_OK;
@@ -53,6 +57,11 @@ uint8_t device_init(device_t *port)
SYS_LOG_ERR("app device choice error");
}
SYS_LOG_INF("device init success");
// 赋值全局变量 RF开关
rf_flag_off_overall = &port->rf_flag_off;
// 赋值设备
device_inside = port;
return DEVICE_OK;
}
uint8_t uart_init(init_device_t *port)
@@ -75,21 +84,26 @@ uint8_t uart_init(init_device_t *port)
// 蓝牙连接回调函数
static void ble_server_connect_handler(ble_server_status_t status)
{
// 处理连接状态,例如:
if (status == BLE_SERVER_STATUS_CONNECTED) {
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_BLE_VAL, CONNECT_BLE);
if(res == DEVICE_PC_ERROR){
if(rf_flag_off_overall != NULL && *rf_flag_off_overall == 0){
// 处理连接状态,例如:
if (status == BLE_SERVER_STATUS_CONNECTED) {
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_BLE_VAL, CONNECT_BLE);
if(res == DEVICE_PC_ERROR){
}
device_wifi_stop();
SYS_LOG_INF("ble Connected");
} else if(status == BLE_SERVER_STATUS_DISCONNECTED){
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_BLE_VAL, DISCONNECT);
if(res == DEVICE_PC_ERROR){
}
device_wifi_start();
SYS_LOG_INF("ble dis Connected");
// disconnected / other
} else {
SYS_LOG_INF("ble stop");
}
SYS_LOG_INF("ble Connected");
} else if(status == BLE_SERVER_STATUS_DISCONNECTED){
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_BLE_VAL, DISCONNECT);
if(res == DEVICE_PC_ERROR){
}
SYS_LOG_INF("ble dis Connected");
// disconnected / other
} else {
SYS_LOG_INF("ble stop");
}
}
static void _sb_manufacturer_encode(uint8_t manufacturer_data[20])
{
@@ -155,42 +169,50 @@ static void wifi_event_handler(bool is_connect, sb_data_port_t *port)
}
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");
if(rf_flag_off_overall != NULL && *rf_flag_off_overall == 0)
{
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");
}
device_ble_stop();
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");
}
device_ble_start();
SYS_LOG_INF("wifi disconnect");
}
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");
if(rf_flag_off_overall != NULL && *rf_flag_off_overall == 0)
{
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)
{
SYS_LOG_ERR("wifi pc device choice error");
}
SYS_LOG_INF("wifi disconnect");
}
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");
}
}
uint8_t wifi_init(init_device_t *port)
@@ -283,6 +305,51 @@ void wifi_mode_switch(init_device_t *port)
}
wifi_start();
}
void device_wifi_start(void)
{
device_inside->wifi_flag_off = 0;
sb_data_port_start(device_inside->init_device.wifi_tcp);
sb_data_port_start(device_inside->init_device.wifi_udp);
}
void device_wifi_stop(void)
{
device_inside->wifi_flag_off = 1;
sb_data_port_stop(device_inside->init_device.wifi_tcp);
sb_data_port_stop(device_inside->init_device.wifi_udp);
}
void device_ble_start(void)
{
device_inside->ble_flag_off = 0;
sb_data_port_start(device_inside->init_device.ble_spp_server_cmd);
sb_data_port_start(device_inside->init_device.ble_spp_server_val);
}
void device_ble_stop(void)
{
device_inside->ble_flag_off = 1;
sb_data_port_stop(device_inside->init_device.ble_spp_server_cmd);
sb_data_port_stop(device_inside->init_device.ble_spp_server_val);
}
void rf_start(device_t *port)
{
port->rf_flag_off = 0;
sb_data_port_start(port->init_device.wifi_tcp);
sb_data_port_start(port->init_device.wifi_udp);
sb_data_port_start(port->init_device.ble_spp_server_cmd);
sb_data_port_start(port->init_device.ble_spp_server_val);
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_BLE_VAL, DISCONNECT);
}
void rf_stop(device_t *port)
{
port->rf_flag_off = 1;
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_RF_OFF, CONNECT_RF_OFF);
sb_data_port_stop(port->init_device.wifi_tcp);
sb_data_port_stop(port->init_device.wifi_udp);
sb_data_port_stop(port->init_device.ble_spp_server_cmd);
sb_data_port_stop(port->init_device.ble_spp_server_val);
}
uint8_t embedded_device_choice(device_t *port, uint8_t type)
{
//端口与PC一致
@@ -352,13 +419,12 @@ 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 device_t* port_device_inside = NULL;
SYS_LOG_WRN("pc_device_choice_inside type %d connect %d", type, connect);
if(port != NULL && port_device_inside == NULL){
SYS_LOG_INF("pc_device_choice_inside");
pc_device_choice(port, type);
port_device_inside = port;
port_device_inside->connect_pc = connect;
}else if(port == NULL && port_device_inside != NULL && (port_device_inside->pc_device_type == type || connect != DISCONNECT)){
}else if(port == NULL && port_device_inside != NULL && (port_device_inside->pc_device_type == type || connect != DISCONNECT || port_device_inside->pc_device_type == DATA_PORT_TYPE_RF_OFF)){
pc_device_choice(port_device_inside, type);
port_device_inside->connect_pc = connect;
}
@@ -410,6 +476,9 @@ int pc_device_read(device_t *port, void *buffer, uint32_t length)
{
if (port->pc_device == NULL)
return -1;
//判断是否开启射频
if(port->rf_flag_off && port->pc_device_type > 1)
return -2;
return sb_data_port_read(port->pc_device, buffer, length, 0);
}
@@ -418,6 +487,9 @@ int pc_device_write(device_t *port, void *buffer, uint32_t length)
{
if(port->pc_device == NULL)
return -1;
//判断是否开启射频
if(port->rf_flag_off && port->pc_device_type > 1)
return -2;
if(port->connect_pc)
return sb_data_port_write(port->pc_device, buffer, length, 0);
@@ -428,7 +500,9 @@ uint32_t pc_device_get_rx_length(device_t *port)
{
if(port->pc_device == NULL)
return 0;
//判断是否开启射频
if(port->rf_flag_off && port->pc_device_type > 1)
return 0;
return sb_data_port_get_rx_length(port->pc_device);
}
@@ -479,6 +553,10 @@ int app_device_read(device_t *port, void *buffer, uint32_t length, uint32_t time
if(port->app_device == NULL)
return -1;
//判断是否开启射频
if((port->rf_flag_off || port->ble_flag_off) && port->app_device_type > 1)
return -2;
return sb_data_port_read(port->app_device, buffer, length, timeout);
}
@@ -487,6 +565,10 @@ int app_device_write(device_t *port, void *buffer, uint32_t length)
if(port->app_device == NULL)
return -1;
//判断是否开启射频
if((port->rf_flag_off || port->ble_flag_off) && port->app_device_type > 1)
return -2;
return sb_data_port_write(port->app_device, buffer, length, 0);
}
uint32_t app_device_get_rx_length(device_t *port)
@@ -494,5 +576,9 @@ uint32_t app_device_get_rx_length(device_t *port)
if(port->app_device == NULL)
return 0;
//判断是否开启射频
if((port->rf_flag_off || port->ble_flag_off) && port->app_device_type > 1)
return -2;
return sb_data_port_get_rx_length(port->app_device);
}

View File

@@ -39,7 +39,9 @@ enum DATA_PORT_TYPE
DATA_PORT_TYPE_WIFI_TCP,
DATA_PORT_TYPE_WIFI_UDP,
DATA_PORT_TYPE_BLE_CMD,
DATA_PORT_TYPE_BLE_VAL
DATA_PORT_TYPE_BLE_VAL,
DATA_PORT_TYPE_RF_OFF = 25,
};
enum CONNECT_TYPE
{
@@ -48,6 +50,8 @@ enum CONNECT_TYPE
CONNECT_BLE,
CONNECT_WIFI_TCP,
CONNECT_WIFI_UDP,
CONNECT_RF_OFF = 25,
};
enum WIFI_MODE
{
@@ -84,6 +88,10 @@ typedef struct
uint8_t connect_embedded;
uint8_t connect_pc;
//射频信号是否开启 0开启 1关闭
uint8_t rf_flag_off;
uint8_t wifi_flag_off;
uint8_t ble_flag_off;
// int (*embedded_read)(device_t *port, void *buffer, uint32_t length);
// int (*embedded_write)(device_t *port, const void *data, uint32_t size);
uint8_t last_color;
@@ -122,6 +130,35 @@ uint8_t wifi_init(init_device_t *port);
* @brief WIFI 模式切换
*/
void wifi_mode_switch(init_device_t *port);
/**
* @brief
*/
void device_wifi_start(void);
/**
* @brief
*/
void device_wifi_stop(void);
/**
* @brief
*/
void device_ble_start(void);
/**
* @brief
*/
void device_ble_stop(void);
/**
* @brief 开启所有射频信号
*/
void rf_start(device_t *port);
/**
* @brief 关闭所有射频信号
*/
void rf_stop(device_t *port);
/**
* @brief 嵌入式设备数据接口
*

View File

@@ -1,5 +1,5 @@
#include "key.h"
#include "sertrf.h"
button_t btn;
cfg_board_pin_io_t key_switch = {
@@ -34,11 +34,13 @@ static void my_button_handler(button_event_t evt) {
break;
case EVT_DOUBLE_CLICK: //双击
// SYS_LOG_INF("[Event] DOUBLE_CLICK");
sertrf_rf_switch(1);
break;
case EVT_LONG_PRESS: //长按
SYS_LOG_INF("[Event] LONG_PRESS");
Examples_run();
rgb_Indicator_light_off(0);
// Examples_run();
rgb_Indicator_light_off(0); //如果处理有变色,需要该函数先调用
sertrf_rf_switch(0);
break;
case EVT_SINGLE_LONG_PRESS: //单击后长按
rgb_Indicator_light_off(0);

View File

@@ -65,6 +65,18 @@ void sertrf_start(void)
2048,
10);
}
void sertrf_rf_switch(uint8_t on)
{
switch(on)
{
case 1:
rf_start(&sertrf.device);
break;
case 0:
rf_stop(&sertrf.device);
break;
}
}
//因为 串口
void embedded_thread(void* arg)
{
@@ -233,7 +245,14 @@ void pc_link_rgb_color(device_t* device)
break;
}
case CONNECT_BLE:
{
new_color = RGB_COLOR_GREEN;
}
break;
case CONNECT_RF_OFF:
{
new_color = RGB_COLOR_BLUE;
}
break;
}

View File

@@ -53,6 +53,11 @@ void sertrf_start(void);
*/
void sertrf_stop(void);
/**
* @brief 射频模块开关
*/
void sertrf_rf_switch(uint8_t on);
/**
* @brief 模块数据查看
*/
@@ -81,12 +86,5 @@ void task_thread(void* arg);
* @param connect 连接状态
*/
void pc_link_rgb_color(device_t* device);
/**
* @brief 打印时间间隔
*/
void printf_chill_time(uint8_t chill_time, uint16_t type);
/**
* @brief 字符串转十进制
*/
uint32_t parse_hex_or_dec(const char *s);