优化亮度平滑过渡
This commit is contained in:
@@ -124,11 +124,13 @@ static void Pairing_Feedback_Apply(uint8_t on)
|
|||||||
if(on)
|
if(on)
|
||||||
{
|
{
|
||||||
Start_PWM();
|
Start_PWM();
|
||||||
|
Light_Transition_SyncBrightness(BRIGHTNESS_MAX_PERCENT);
|
||||||
W_PWM_duty = 100U;
|
W_PWM_duty = 100U;
|
||||||
C_PWM_duty = 0U;
|
C_PWM_duty = 0U;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
Light_Transition_SyncBrightness(0U);
|
||||||
W_PWM_duty = 0U;
|
W_PWM_duty = 0U;
|
||||||
C_PWM_duty = 0U;
|
C_PWM_duty = 0U;
|
||||||
Stop_PWM();
|
Stop_PWM();
|
||||||
@@ -143,6 +145,9 @@ static void Pairing_Feedback_Restore(void)
|
|||||||
C_PWM = pairing_saved_c;
|
C_PWM = pairing_saved_c;
|
||||||
Brightness = pairing_saved_brightness;
|
Brightness = pairing_saved_brightness;
|
||||||
Mode = pairing_saved_mode;
|
Mode = pairing_saved_mode;
|
||||||
|
Light_Transition_SyncBrightness(
|
||||||
|
pairing_saved_status == POWERON ? pairing_saved_brightness : 0U);
|
||||||
|
|
||||||
W_PWM_duty = pairing_saved_w_duty;
|
W_PWM_duty = pairing_saved_w_duty;
|
||||||
C_PWM_duty = pairing_saved_c_duty;
|
C_PWM_duty = pairing_saved_c_duty;
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
volatile uint8_t Light_Transition_FadeActive;
|
volatile uint8_t Light_Transition_FadeActive;
|
||||||
|
|
||||||
#define LIGHT_FADE_STEP 1U
|
#define LIGHT_FADE_STEP 1U
|
||||||
|
#define BRIGHTNESS_SMOOTH_SHIFT 4U
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Gamma 1.8, mapping APP levels 1..100 to a stable physical range of
|
* Gamma 1.8, mapping APP levels 1..100 to a stable physical range of
|
||||||
@@ -24,6 +25,36 @@ static const uint16_t brightness_gamma_q12[100] = {
|
|||||||
3515U, 3577U, 3640U, 3704U, 3768U, 3832U, 3897U, 3963U, 4029U, 4096U
|
3515U, 3577U, 3640U, 3704U, 3768U, 3832U, 3897U, 3963U, 4029U, 4096U
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static uint16_t current_brightness_q12;
|
||||||
|
|
||||||
|
static uint16_t brightness_scale_for_level(uint8_t level)
|
||||||
|
{
|
||||||
|
if(level == 0U) return 0U;
|
||||||
|
if(level < BRIGHTNESS_MIN_PERCENT) level = BRIGHTNESS_MIN_PERCENT;
|
||||||
|
if(level > BRIGHTNESS_MAX_PERCENT) level = BRIGHTNESS_MAX_PERCENT;
|
||||||
|
return brightness_gamma_q12[level - 1U];
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint16_t approach_brightness_scale(uint16_t current, uint16_t target)
|
||||||
|
{
|
||||||
|
uint16_t delta;
|
||||||
|
uint16_t step;
|
||||||
|
|
||||||
|
if(current == target) return current;
|
||||||
|
if(current < target)
|
||||||
|
{
|
||||||
|
delta = target - current;
|
||||||
|
step = (uint16_t)((delta + ((1U << BRIGHTNESS_SMOOTH_SHIFT) - 1U)) >>
|
||||||
|
BRIGHTNESS_SMOOTH_SHIFT);
|
||||||
|
return current + step;
|
||||||
|
}
|
||||||
|
|
||||||
|
delta = current - target;
|
||||||
|
step = (uint16_t)((delta + ((1U << BRIGHTNESS_SMOOTH_SHIFT) - 1U)) >>
|
||||||
|
BRIGHTNESS_SMOOTH_SHIFT);
|
||||||
|
return current - step;
|
||||||
|
}
|
||||||
|
|
||||||
static uint16_t approach_target(uint16_t current, uint16_t target)
|
static uint16_t approach_target(uint16_t current, uint16_t target)
|
||||||
{
|
{
|
||||||
if(current < target)
|
if(current < target)
|
||||||
@@ -39,27 +70,25 @@ static uint16_t approach_target(uint16_t current, uint16_t target)
|
|||||||
return current;
|
return current;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Light_Transition_SyncBrightness(uint8_t level)
|
||||||
|
{
|
||||||
|
current_brightness_q12 = brightness_scale_for_level(level);
|
||||||
|
}
|
||||||
|
|
||||||
void Light_Transition_Task(void)
|
void Light_Transition_Task(void)
|
||||||
{
|
{
|
||||||
uint8_t target_brightness = 0U;
|
uint8_t target_brightness = 0U;
|
||||||
uint16_t target_w;
|
uint16_t target_w;
|
||||||
uint16_t target_c;
|
uint16_t target_c;
|
||||||
|
uint16_t target_brightness_scale;
|
||||||
uint16_t brightness_scale;
|
uint16_t brightness_scale;
|
||||||
|
|
||||||
if(deviceStatus != POWEROFF) target_brightness = Brightness;
|
if(deviceStatus != POWEROFF) target_brightness = Brightness;
|
||||||
if(target_brightness > BRIGHTNESS_MAX_PERCENT)
|
target_brightness_scale = brightness_scale_for_level(target_brightness);
|
||||||
target_brightness = BRIGHTNESS_MAX_PERCENT;
|
current_brightness_q12 =
|
||||||
|
approach_brightness_scale(current_brightness_q12,
|
||||||
if(target_brightness == 0U)
|
target_brightness_scale);
|
||||||
{
|
brightness_scale = current_brightness_q12;
|
||||||
brightness_scale = 0U;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if(target_brightness < BRIGHTNESS_MIN_PERCENT)
|
|
||||||
target_brightness = BRIGHTNESS_MIN_PERCENT;
|
|
||||||
brightness_scale = brightness_gamma_q12[target_brightness - 1U];
|
|
||||||
}
|
|
||||||
|
|
||||||
target_w =
|
target_w =
|
||||||
(uint16_t)(((uint32_t)W_PWM * brightness_scale + 2048U) >> 12);
|
(uint16_t)(((uint32_t)W_PWM * brightness_scale + 2048U) >> 12);
|
||||||
|
|||||||
@@ -9,5 +9,6 @@ extern volatile uint8_t Light_Transition_FadeActive;
|
|||||||
#define Light_Transition_RequestOff() (Light_Transition_FadeActive = 1U)
|
#define Light_Transition_RequestOff() (Light_Transition_FadeActive = 1U)
|
||||||
|
|
||||||
void Light_Transition_Task(void);
|
void Light_Transition_Task(void);
|
||||||
|
void Light_Transition_SyncBrightness(uint8_t level);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user