优化开关与渐变的无缝衔接,消除‘先黑一下再变亮’突变

问题:关灯渐变进行中途点击开灯,会出现先熄灭再从0渐变的突变。

方案(A+B+C):
A. ‘开灯’打断‘关灯渐变’时,沿用当前渐变中的即时亮度作为起点,反向渐变到目标(不再强制从0起步)。
B. 渐变任务收尾保护:关灯渐变完成前若已被开灯抢占,跳过PWM关断收尾,避免黑场。
C. 取消逻辑加固:取消当前渐变时清除 is_fading/is_closing_fade/fade_completed,避免旧渐变回调误关灯。

实现点:
- set_light:记录 was_closing_fade 与当前即时亮度;开灯时根据 was_closing_fade 决定起点(即时亮度或0)。
- fade_task:在关灯完成分支检查 g_device_control.on,已被开灯抢占则不关断,交由开灯渐变接管。
- cancel_current_light_fade:补充状态清理,防止回调残留。

验证:亮→关(渐变中)→开、末尾抢占、快速连点关/开、单色↔双色切换夹杂开关,均无‘黑一下’突变。
This commit is contained in:
2025-11-17 07:27:44 +08:00
parent cb1ef39108
commit 53ba39e35d
17 changed files with 328 additions and 19 deletions

View File

@@ -254,14 +254,20 @@ static void *fade_task(const char *arg)
fade_ctx.fade_completed = false;
if (fade_ctx.is_closing_fade) {
// 关灯渐变完成不更新g_device_control.brightness_local保持原有目标亮度
fade_ctx.is_closing_fade = false;
update_pwm_output(false, 0, 0);
g_device_control.duty_cw = 0;
g_device_control.duty_ww = 0;
// 注意:这里不更新 g_device_control.brightness_local保持之前的亮度值
e_printf("[fade_task] Close light fade completed, PWM turned off, brightness_local preserved: %d\r\n",
g_device_control.brightness_local);
// 若在收尾时已被“开灯”抢占,则跳过关断动作,避免“先黑一下”的突变
if (g_device_control.on) {
e_printf("[fade_task] Close-fade preempted by OPEN, skip turning off\r\n");
fade_ctx.is_closing_fade = false;
// 保持当前PWM与占空后续由OPEN流程接管渐变
} else {
// 关灯渐变完成不更新g_device_control.brightness_local保持原有目标亮度
fade_ctx.is_closing_fade = false;
update_pwm_output(false, 0, 0);
g_device_control.duty_cw = 0;
g_device_control.duty_ww = 0;
e_printf("[fade_task] Close light fade completed, PWM turned off, brightness_local preserved: %d\r\n",
g_device_control.brightness_local);
}
} else {
// 正常渐变完成:正常更新所有值
g_device_control.duty_ww = device_control.duty_ww;
@@ -667,10 +673,10 @@ static void cancel_current_light_fade(void)
// 竞态保护:先拉低标志再停计时器,避免回调自重启
fade_ctx.timer_active = false;
uapi_timer_stop(fade_ctx.timer_handle);
// 如果我打断了渐变,则将当前值更新到目标值不然下次计算会异常
// g_device_control.cct_local = fade_ctx.current_cct;
// g_device_control.brightness_local = fade_ctx.current_brightness;
// memset(&fade_ctx, 0, FADE_CTRL_DATA_SIZE(fade_ctx));
// 标记为非渐变态,清除“关灯渐变”标记与完成态,避免旧渐变在回调里误触发收尾关断
fade_ctx.is_fading = false;
fade_ctx.is_closing_fade = false;
fade_ctx.fade_completed = false;
}
// 计算校验和
@@ -939,12 +945,16 @@ int set_light(light_ctrl_source_e source,
int32_t brightness_local_target, int32_t cct_local_target)
{
bool was_on = g_device_control.on;
// 记录调用前是否处于“关灯渐变中”,用于开灯时做无缝反向
bool was_closing_fade = (fade_ctx.is_fading && fade_ctx.is_closing_fade);
int32_t curr_brightness_before_cancel = fade_ctx.current_brightness;
int32_t curr_cct_before_cancel = fade_ctx.current_cct;
struct fade_ctx_t tmp_fade_ctx = {0};
if (fade_ctx.is_fading) {
bool closing_fade_active = fade_ctx.is_closing_fade;
bool suppress_cancel = false;
// 在关灯渐变过程中,属性/模式变更不应打断关灯过程
if (closing_fade_active) {
if (fade_ctx.is_closing_fade) {
if (source == APP_CHANGE_LIGHT_BRIGHTNESS_CCT || source == APP_CHANGE_LIGHT_MODE || source == APP_CLOSE_LIGHT) {
suppress_cancel = true; // 允许继续关灯渐变;再次关灯请求可复用或稍后重启
}
@@ -958,11 +968,20 @@ int set_light(light_ctrl_source_e source,
g_device_control.on = true;
if (DEV_POWER_ON == source || (APP_OPEN_LIGHT == source && !was_on)) {
// 开机或关灯→开灯从0开始渐变
fade_ctx.current_brightness = 0;
fade_ctx.current_cct = g_device_control.cct_local; // 色温保持目标值
e_printf("[set_light] 开灯渐变设置: brightness=0->%d, cct=%d\n",
g_device_control.brightness_local, fade_ctx.current_cct);
// 若之前处于关灯渐变中,被“开灯”打断,则沿用渐变中的即时亮度作为起点,实现无缝反向
if (was_closing_fade) {
fade_ctx.current_brightness = curr_brightness_before_cancel;
fade_ctx.current_cct = g_device_control.cct_local; // 使用当前目标色温
fade_ctx.is_closing_fade = false;
e_printf("[set_light] 开灯打断关灯渐变: brightness=%d->%d, cct=%d\n",
fade_ctx.current_brightness, g_device_control.brightness_local, fade_ctx.current_cct);
} else {
// 真正的关→开或上电开灯从0开始渐变
fade_ctx.current_brightness = 0;
fade_ctx.current_cct = g_device_control.cct_local; // 色温保持目标值
e_printf("[set_light] 开灯渐变设置: brightness=0->%d, cct=%d\n",
g_device_control.brightness_local, fade_ctx.current_cct);
}
}
brightness_local_target = g_device_control.brightness_local;