Files
ESPC3-wireless/app/drivers/sertrf/sertrf.c
2025-09-09 18:16:48 +08:00

260 lines
7.7 KiB
C

#include "sertrf.h"
sertrf_t sertrf;
void sertrf_init(void)
{
uint8_t res = 0;
res = device_init(&sertrf.device);
if(res)
{
SYS_LOG_WRN("device init error");
}
// 协议初始化
Protocol_init(PORT_LINUX_SBDATA, sertrf.device.embedded_device);
//RGB灯
work_rgb_led_start();
//按键初始化
button_work_init();
// 协议初始化
sertrf.resend_read_mutex = xSemaphoreCreateMutex();
resend_init(&sertrf.resend_device, resend_send, resend_recv, resend_get_length,resend_user_parse);
//线程启动
sertrf_start();
}
void sertrf_start(void)
{
os_thread_create(&sertrf.embedded_thread,
"embedded_thread",
embedded_thread,
NULL,
4096,
20);
os_thread_create(&sertrf.pc_thread,
"pc_thread",
pc_thread,
NULL,
4096,
20);
os_thread_create(&sertrf.task_thread,
"task_thread",
task_thread,
NULL,
2048,
10);
}
//因为 串口
void embedded_thread(void* arg)
{
while(true)
{
uint32_t embedded_size = embedded_device_get_rx_length(&sertrf.device);
if(embedded_size > 0)
{
uint8_t data[embedded_size ];
// data[embedded_size] = '\0';
embedded_device_read(&sertrf.device, data, embedded_size,0);
Protocol_buf_decode(data, embedded_size);
// SYS_LOG_INF("data : %s", data);
pc_device_write(&sertrf.device, data, embedded_size);
}
printf_chill_time(10,1000);
//需要添加一些延时,否则会卡死,导致看门狗复位
// vTaskDelay(( TickType_t ) 1000 / configTICK_RATE_HZ);
os_thread_sleep(1);
}
}
void pc_thread(void* arg)
{
while(true)
{
uint32_t pc_size = pc_device_get_rx_length(&sertrf.device);
if(pc_size > 0)
{
uint8_t data[pc_size];
pc_device_read(&sertrf.device, data, pc_size);
// SYS_LOG_INF("data : %s", data);
embedded_device_write(&sertrf.device, data, pc_size);
}
// printf_chill_time(10,1000);
//需要添加一些延时,否则会卡死,导致看门狗复位
// vTaskDelay(( TickType_t ) 1000 / configTICK_RATE_HZ);
os_thread_sleep(1);
}
}
void task_thread(void* arg)
{
while(true)
{
size_t start_time = os_get_sys_time();
pc_link_rgb_color(&sertrf.device);
if(sertrf.device.connect_pc > DISCONNECT)
{
static size_t end_time = 0;
if(start_time - end_time > 2000)
{
switch(get_protocol_status())
{
case PROTOCOL_STATUS_OK:
rgb_color_change(0, sertrf.device.last_color);
break;
case PROTOCOL_STATUS_NO_DATA:
rgb_color_change(0, RGB_COLOR_RAD);
break;
case PROTOCOL_STATUS_TYPE_IDLE:
rgb_color_change(0, RGB_COLOR_RAD);
break;
case PROTOCOL_STATUS_ANALYSIS_ERROR:
rgb_color_change(0, RGB_COLOR_RAD);
break;
default:
break;
}
end_time = os_get_sys_time();
}
}
os_thread_sleep(100);
}
}
void pc_link_rgb_color(device_t* device)
{
static uint8_t last_connect = 255, last_wifi_mode = 0;
uint8_t new_color = 0;
if(device->connect_pc != last_connect || last_wifi_mode != device->init_device.wifi_mode)
{
switch (device->connect_pc)
{
case DISCONNECT:
{
if(device->init_device.wifi_mode == WIFI_NETIF_MODE_AP)
new_color = RGB_COLOR_GREEN_WHITE;
else
new_color = RGB_COLOR_GREEN_PURPLE;
break;
}
case CONNECT_WIFI_TCP:
{
if(device->init_device.wifi_mode == WIFI_NETIF_MODE_AP)
new_color = RGB_COLOR_WHITE;
else
new_color = RGB_COLOR_PURPLE;
break;
}
case CONNECT_WIFI_UDP:
{
if(device->init_device.wifi_mode == WIFI_NETIF_MODE_AP)
new_color = RGB_COLOR_WHITE;
else
new_color = RGB_COLOR_PURPLE;
break;
}
case CONNECT_BLE:
new_color = RGB_COLOR_GREEN;
break;
}
if(device->last_color != new_color)
rgb_color_change(0,new_color);
device->last_color = new_color;
last_connect = device->connect_pc;
last_wifi_mode = device->init_device.wifi_mode;
}
if(device->connect_pc == DISCONNECT)
{
rgb_update_cyle(50);
}
else if(device->connect_pc){
rgb_update_cyle(888);
}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;
}
int resend_send(void* data, uint16_t len, int timeout)
{
return embedded_device_write(&sertrf.device, data, len);
}
int resend_recv(void* data, uint16_t len, int timeout)
{
int ret = 0;
if (xSemaphoreTake(sertrf.resend_read_mutex, portMAX_DELAY) == pdTRUE)
{
ret = embedded_device_read(&sertrf.device, data, len,timeout);
xSemaphoreGive(sertrf.resend_read_mutex);
}
return ret;
}
int resend_get_length(void)
{
return embedded_device_get_rx_length(&sertrf.device);
}
void resend_user_parse(void *resend_device)
{
resend_device_t* resend_parse = (resend_device_t*)resend_device;
switch(resend_parse->rx_frame.cmd)
{
case RESEND_CMD_OTA_START:
{
if(!sertrf.resend_device.status.resend_flag)
otau_init(&sertrf.otau);
resend_send_cmd(resend_device, RESEND_CMD_ACK, 0);
}
break;
case RESEND_CMD_OTA_DATA:
{
if(!sertrf.resend_device.status.resend_flag)
{
SYS_LOG_DBG("KUYI_CMD_OTA_DATA");
otau_write(&sertrf.otau, sertrf.resend_device.rx_frame.payload, sertrf.resend_device.rx_frame.len);
}
resend_send_cmd(resend_device, RESEND_CMD_ACK, 0);
}
break;
case RESEND_CMD_OTA_END:
{
if(!sertrf.resend_device.status.resend_flag)
otau_end(&sertrf.otau);
resend_send_cmd(resend_device, RESEND_CMD_ACK, 0);
os_thread_sleep(2);
esp_restart();
}
break;
}
}