1. 解决频繁频繁掉线的问题:原因是因为ble
sdk需要一开始就初始化,配网的时候只是去控制其广播是否开启。不然会导致一直掉线
This commit is contained in:
@ -28,6 +28,7 @@ static void check_net_cfg_power_on_keep_time(void);
|
||||
static void set_light2breathtimeout(void);
|
||||
static void set_light2net_cfg_done(void);
|
||||
static void start_breath_timer(void);
|
||||
static void report_switch_status(void);
|
||||
void calculate_pwm_duty(void);
|
||||
void update_pwm_output(void);
|
||||
|
||||
@ -35,7 +36,7 @@ void update_pwm_output(void);
|
||||
typedef void (*report_func_t)(void);
|
||||
static void start_report_task(report_func_t report_func);
|
||||
static void report_fade_complete_status(void);
|
||||
static uint32_t calculate_checksum(const device_data_t* data);
|
||||
static uint8_t calculate_checksum(const device_data_t* data);
|
||||
static bool write_device_data_to_addr(uint32_t addr, const device_data_t* data);;
|
||||
static void report_device_online_status(void);
|
||||
|
||||
@ -101,7 +102,7 @@ typedef struct {
|
||||
} print_limiter_t;
|
||||
|
||||
// 渐变控制相关变量
|
||||
static struct {
|
||||
static struct fade_ctx_t {
|
||||
// 渐变控制参数
|
||||
int32_t target_brightness;
|
||||
int32_t target_cct;
|
||||
@ -588,13 +589,13 @@ static void cancel_current_light_fade(void)
|
||||
}
|
||||
|
||||
// 计算校验和
|
||||
static uint32_t calculate_checksum(const device_data_t* data)
|
||||
static uint8_t calculate_checksum(const device_data_t* data)
|
||||
{
|
||||
uint32_t sum = 0;
|
||||
const uint32_t* ptr = (const uint32_t*)(((char*)data) + sizeof(data->checksum));
|
||||
uint8_t sum = 0;
|
||||
const uint8_t* ptr = (const uint8_t*)(((char*)data) + sizeof(data->checksum));
|
||||
size_t len = sizeof(device_data_t) - sizeof(data->checksum); // 减去checksum字段的大小
|
||||
|
||||
for (size_t i = 0; i < len / 4; i++) {
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
sum ^= ptr[i];
|
||||
}
|
||||
return sum;
|
||||
@ -613,7 +614,7 @@ static bool verify_device_data(const device_data_t* data)
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t checksum = calculate_checksum(data);
|
||||
uint8_t checksum = calculate_checksum(data);
|
||||
if (checksum != data->checksum) {
|
||||
e_printf("check sum error:%x\n", data->version);
|
||||
return false;
|
||||
@ -678,9 +679,9 @@ void read_device_data(void)
|
||||
} else if (!main_valid && !backup_valid) { // 如果两个区域都无效,使用默认值
|
||||
e_printf("主数据和备份数据都无效,使用默认值\r\n");
|
||||
// 只打印g_device_control的当前值,不打印data.control
|
||||
// e_printf("读取完成状态: %d\r\n", g_device_control.read_done);
|
||||
e_printf("上电次数: %d\r\n", g_device_control.power_on_cnt);
|
||||
e_printf("配网状态: %d\r\n", g_device_control.is_networked);
|
||||
e_printf("是否是新设备: %d\r\n", !g_device_control.is_net_configured);
|
||||
e_printf("开关状态: %d\r\n", g_device_control.on);
|
||||
e_printf("灯光模式: %d\r\n", g_device_control.elightMode);
|
||||
e_printf("亮度: %d\r\n", g_device_control.brightness_local);
|
||||
@ -692,9 +693,9 @@ void read_device_data(void)
|
||||
}
|
||||
// 更新设备控制状态
|
||||
e_printf("设备状态恢复:\r\n");
|
||||
// e_printf("读取完成状态: %d => %d\r\n", g_device_control.read_done, data.control.read_done);
|
||||
e_printf("上电次数: %d => %d\r\n", g_device_control.power_on_cnt, data.control.power_on_cnt);
|
||||
e_printf("配网状态: %d => %d\r\n", g_device_control.is_networked, data.control.is_networked);
|
||||
e_printf("是否是新设备: %d => %d\r\n", !g_device_control.is_net_configured, !data.control.is_net_configured);
|
||||
e_printf("开关状态: %d => %d\r\n", g_device_control.on, data.control.on);
|
||||
e_printf("灯光模式: %d => %d\r\n", g_device_control.elightMode, data.control.elightMode);
|
||||
e_printf("亮度: %d => %d\r\n", g_device_control.brightness_local, data.control.brightness_local);
|
||||
@ -703,6 +704,7 @@ void read_device_data(void)
|
||||
e_printf("暖白LED: %d => %d\r\n", g_device_control.duty_ww, data.control.duty_ww);
|
||||
e_printf("渐变时间: %d => %d\r\n", g_device_control.fade_time, data.control.fade_time);
|
||||
|
||||
data.control.read_done = false;
|
||||
g_device_control = data.control;
|
||||
lab_exit:
|
||||
#endif
|
||||
@ -725,7 +727,8 @@ void save_device_data(void)
|
||||
last_control.fade_time != g_device_control.fade_time ||
|
||||
last_control.cct_local != g_device_control.cct_local ||
|
||||
last_control.power_on_cnt != g_device_control.power_on_cnt ||
|
||||
last_control.is_networked != g_device_control.is_networked;
|
||||
last_control.is_networked != g_device_control.is_networked ||
|
||||
last_control.is_net_configured != g_device_control.is_net_configured;
|
||||
|
||||
if (!need_save) {
|
||||
e_printf("No changes detected, skip saving\n");
|
||||
@ -834,20 +837,20 @@ static void stop_save_task(void)
|
||||
// 设置灯光状态(统一控制函数)
|
||||
int set_light(int32_t brightness_local, int32_t cct_local)
|
||||
{
|
||||
struct fade_ctx_t tmp_fade_ctx = fade_ctx;
|
||||
// 如果任一参数大于等于0,则更新对应值
|
||||
e_printf("ligMode:%d, local brightness: curr:%d, target:%d, local cct: curr:%d, target:%d\r\n",
|
||||
g_device_control.elightMode, g_device_control.brightness_local, brightness_local, g_device_control.cct_local, cct_local);
|
||||
|
||||
fade_ctx.current_brightness = g_device_control.brightness_local;
|
||||
fade_ctx.current_cct = g_device_control.cct_local;
|
||||
fade_ctx.target_brightness = fade_ctx.current_brightness ;
|
||||
fade_ctx.target_cct = fade_ctx.current_cct;
|
||||
tmp_fade_ctx.current_brightness = g_device_control.brightness_local;
|
||||
tmp_fade_ctx.current_cct = g_device_control.cct_local;
|
||||
tmp_fade_ctx.target_brightness = tmp_fade_ctx.current_brightness ;
|
||||
tmp_fade_ctx.target_cct = tmp_fade_ctx.current_cct;
|
||||
|
||||
if (brightness_local >= 0) {
|
||||
BRIGHTNESS_LITME_RANGE(brightness_local);
|
||||
|
||||
if (g_device_control.elightMode == LIGHT_MODE_CUSTOMER) {
|
||||
fade_ctx.target_brightness = brightness_local;
|
||||
tmp_fade_ctx.target_brightness = brightness_local;
|
||||
} else {
|
||||
g_device_control.brightness_local = brightness_local;
|
||||
}
|
||||
@ -857,7 +860,7 @@ int set_light(int32_t brightness_local, int32_t cct_local)
|
||||
CCT_LITME_RANGE(cct_local);
|
||||
|
||||
if (g_device_control.elightMode == LIGHT_MODE_CUSTOMER) {
|
||||
fade_ctx.target_cct = cct_local;
|
||||
tmp_fade_ctx.target_cct = cct_local;
|
||||
} else {
|
||||
g_device_control.cct_local = cct_local;
|
||||
}
|
||||
@ -867,8 +870,13 @@ int set_light(int32_t brightness_local, int32_t cct_local)
|
||||
{
|
||||
case LIGHT_MODE_CUSTOMER:
|
||||
if (g_device_control.fade_time) {
|
||||
fade_ctx.is_fading = true;
|
||||
fade_ctx.fade_completed = false;
|
||||
tmp_fade_ctx.is_fading = true;
|
||||
tmp_fade_ctx.fade_completed = false;
|
||||
// if(fade_ctx.is_fading) {
|
||||
// e_printf("fade is running, skip set_light\r\n");
|
||||
// return -111;
|
||||
// }
|
||||
fade_ctx = tmp_fade_ctx;
|
||||
// 计算渐变步长
|
||||
calculate_fade_steps();
|
||||
e_printf("start fade\r\n");
|
||||
@ -904,9 +912,6 @@ int set_switch(bool open)
|
||||
calculate_pwm_duty();
|
||||
update_pwm_output();
|
||||
uapi_gpio_set_val(SWITCH_PIN, open ? OPEN_LIGHT : CLOSE_LIGHT);
|
||||
|
||||
// 保存设备状态
|
||||
req_save_device_data();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -937,6 +942,7 @@ static void pwm_init(pin_t pin, pin_t pin1)
|
||||
// uint32_t pwm_base_period_ns = 1000 * 1000 * 1000 / (80 * 1000 * 1000);// 80MHZ
|
||||
// uint32_t pwm_target_period_ns = 1000 * 1000 * 1000 / PWM_FREQUENCY;
|
||||
pwm_period_cnt = ((80 * 1000 * 1000) / PWM_FREQUENCY);
|
||||
cfg_repeat.low_time = pwm_period_cnt;
|
||||
// 设置PWM组
|
||||
channel_id_cw = pin%8;
|
||||
channel_id_ww = pin1%8;
|
||||
@ -960,8 +966,8 @@ static void handle_network_status(void)
|
||||
g_device_control.power_on_cnt++;
|
||||
e_printf("现在是第 %d 次上电\r\n", g_device_control.power_on_cnt);
|
||||
// 检查是否需要进入配网状态
|
||||
if (g_device_control.power_on_cnt >= NET_CFG_ENTRY_CNT) {
|
||||
e_printf("进入配网状态,上电次数:%d\r\n", g_device_control.power_on_cnt);
|
||||
if ((!g_device_control.is_net_configured) || g_device_control.power_on_cnt >= NET_CFG_ENTRY_CNT) {
|
||||
e_printf("进入配网状态,上电次数:%d, is_new_device:%d\r\n", g_device_control.power_on_cnt, !g_device_control.is_net_configured);
|
||||
g_device_control.power_on_cnt = 0;//
|
||||
extern int start_hilink_ble_net_config(int32_t net_cfg_time_s);
|
||||
int ret = start_hilink_ble_net_config(NET_CFG_TOTAL_TIMEOUT/1000);
|
||||
@ -986,24 +992,31 @@ static void handle_network_status(void)
|
||||
// 设备上线时的状态上报
|
||||
static void report_device_online_status(void)
|
||||
{
|
||||
e_printf("[report_task] Starting to report all service status\r\n");
|
||||
e_printf("Starting to report all service status\r\n");
|
||||
fast_report(SVC_ID_SWITCH);
|
||||
fast_report(SVC_ID_BRIGHTNESS);
|
||||
fast_report(SVC_ID_CCT);
|
||||
fast_report(SVC_ID_LIGHT_MODE);
|
||||
fast_report(SVC_ID_FADE_TIME);
|
||||
e_printf("[report_task] Status report completed\r\n");
|
||||
// fast_report(SVC_ID_NET_INFO);
|
||||
e_printf("Status report completed\r\n");
|
||||
}
|
||||
|
||||
// 渐变结束时的状态上报
|
||||
static void report_fade_complete_status(void)
|
||||
{
|
||||
req_save_device_data();
|
||||
e_printf("[report_task] Reporting fade complete status\r\n");
|
||||
e_printf("Reporting fade complete status\r\n");
|
||||
fast_report(SVC_ID_LIGHT_MODE);
|
||||
fast_report(SVC_ID_BRIGHTNESS);
|
||||
fast_report(SVC_ID_CCT);
|
||||
e_printf("[report_task] Fade status report completed\r\n");
|
||||
e_printf("Fade status report completed\r\n");
|
||||
}
|
||||
// 渐变结束时的状态上报
|
||||
static void report_switch_status(void)
|
||||
{
|
||||
req_save_device_data();
|
||||
fast_report(SVC_ID_SWITCH);
|
||||
}
|
||||
|
||||
// 状态上报任务函数
|
||||
@ -1032,6 +1045,7 @@ static void start_report_task(report_func_t report_func)
|
||||
e_printf("[report_task] Previous task is still running, skip this report\r\n");
|
||||
return;
|
||||
}
|
||||
report_task_running = true; // 设置任务运行标志
|
||||
if (report_task_handle) {
|
||||
osal_kthread_destroy(report_task_handle, 0);
|
||||
report_task_handle = NULL;
|
||||
@ -1042,7 +1056,6 @@ static void start_report_task(report_func_t report_func)
|
||||
report_task_handle = osal_kthread_create((osal_kthread_handler)report_task, (void*)report_func, "ReportStaTask",
|
||||
REPORT_TASK_STACK_SIZE);
|
||||
if (report_task_handle != NULL) {
|
||||
report_task_running = true; // 设置任务运行标志
|
||||
osal_kthread_set_priority(report_task_handle, REPORT_TASK_PRIO);
|
||||
}
|
||||
osal_kthread_unlock();
|
||||
@ -1057,6 +1070,7 @@ void handle_device_online(void)
|
||||
return;
|
||||
}
|
||||
e_printf("device online!\r\n");
|
||||
g_device_control.is_net_configured = true;//设备上线后,认为设备已经配网成功,就取消标记为新设备,终生只存在这一次标记
|
||||
if (!g_device_control.is_networked) {
|
||||
e_printf("设备首次上线,记录配网状态\r\n");
|
||||
g_device_control.is_networked = true;
|
||||
@ -1081,6 +1095,7 @@ void handle_device_offline(void)
|
||||
device_online = 0;
|
||||
e_printf("device offline!\r\n");
|
||||
}
|
||||
bool g_reset_factory_flag = false;
|
||||
// 处理设备解绑
|
||||
void handle_device_unbind(void)
|
||||
{
|
||||
@ -1090,6 +1105,9 @@ void handle_device_unbind(void)
|
||||
g_device_control.power_on_cnt = 0; // 重置上电计数
|
||||
stop_spotlight_main_task();
|
||||
device_control_t tmp = DEFAULT_DEVICE_DATA;//恢复默认
|
||||
if (!g_reset_factory_flag) {
|
||||
tmp.is_net_configured = g_device_control.is_net_configured;
|
||||
}
|
||||
g_device_control = tmp;
|
||||
save_device_data();
|
||||
}
|
||||
|
Reference in New Issue
Block a user