提升亮度调节精度至百分比

This commit is contained in:
2026-07-26 23:20:44 +08:00
parent 9321568920
commit e5e899a8f1
43 changed files with 1914 additions and 1961 deletions
@@ -41,7 +41,7 @@ uint16_t deadTime_W = _deadTime;
uint8_t driveMode=1;
uint8_t PWMMAX=100; //最大占空比
uint8_t Brightness=10;
uint8_t Brightness=BRIGHTNESS_MAX_PERCENT;
// 切换 PWM 输出管脚,蓝牙调用此方法,切换设备输出线序
uint16_t W_PWM_duty;//初始化暖光
@@ -93,8 +93,8 @@ void PWM_SetDuty(uint8_t PWMX ,uint8_t duty) {
static uint16_t scale_display_value(uint16_t value)
{
if(value > 100U) value = 100U;
if(Brightness > 10U) return value;
return (uint16_t)(((uint32_t)value * Brightness + 5U) / 10U);
if(Brightness >= BRIGHTNESS_MAX_PERCENT) return value;
return (uint16_t)(((uint32_t)value * Brightness + 50U) / 100U);
}
void UpdateDisplay(void) // static update with global brightness
@@ -65,6 +65,10 @@ extern uint8_t driveMode;
extern uint8_t PWMMAX;
extern uint8_t Brightness;
#define BRIGHTNESS_MIN_PERCENT 10U
#define BRIGHTNESS_MAX_PERCENT 100U
#define BRIGHTNESS_RF_STEP_PERCENT 10U
#define DEVIATION_FREQ 10
#define DEVIATION_DUTYCYCLE 10
@@ -376,8 +376,15 @@ void _433_fun(){
case Brightness_L:
if(POWEROFF==deviceStatus)break;
if(Mode==mode0){
Brightness--;
if(Brightness<=2){Brightness=2;}
if(Brightness <=
(BRIGHTNESS_MIN_PERCENT + BRIGHTNESS_RF_STEP_PERCENT))
{
Brightness = BRIGHTNESS_MIN_PERCENT;
}
else
{
Brightness -= BRIGHTNESS_RF_STEP_PERCENT;
}
}else {
if(Speed<10)Speed++;
}
@@ -385,8 +392,15 @@ void _433_fun(){
case Brightness_H:
if(POWEROFF==deviceStatus)break;
if(Mode==mode0){
Brightness++;
if(Brightness>=10){Brightness=10;}
if(Brightness >=
(BRIGHTNESS_MAX_PERCENT - BRIGHTNESS_RF_STEP_PERCENT))
{
Brightness = BRIGHTNESS_MAX_PERCENT;
}
else
{
Brightness += BRIGHTNESS_RF_STEP_PERCENT;
}
}else{
if(Speed>1)Speed--;
}
@@ -69,6 +69,9 @@ unsigned char get_checksum(unsigned char *ptrdata, unsigned char length)
return CheckSum;
}
#define FLASH_BRIGHTNESS_FORMAT_INDEX 14U
#define FLASH_BRIGHTNESS_PERCENT_MARK 0xA5U
void read_user_data(void)
{
@@ -78,6 +81,24 @@ void read_user_data(void)
Mode = r_data[2];
Brightness = r_data[3];
/*
* Firmware before the percent-brightness update stored 2..10.
* Byte 14 was unused, so use it as an explicit format marker.
*/
if(r_data[FLASH_BRIGHTNESS_FORMAT_INDEX] !=
FLASH_BRIGHTNESS_PERCENT_MARK)
{
if(Brightness >= 2U && Brightness <= 10U)
Brightness = (uint8_t)(Brightness * 10U);
else
Brightness = BRIGHTNESS_MAX_PERCENT;
}
if(Brightness < BRIGHTNESS_MIN_PERCENT)
Brightness = BRIGHTNESS_MIN_PERCENT;
else if(Brightness > BRIGHTNESS_MAX_PERCENT)
Brightness = BRIGHTNESS_MAX_PERCENT;
Speed = r_data[7];
deviceStatus = r_data[8];
@@ -138,6 +159,7 @@ void wright_user_data(void)
app_data[11] = 0;
app_data[12] = is_time;
app_data[13] = 0;
app_data[FLASH_BRIGHTNESS_FORMAT_INDEX] = FLASH_BRIGHTNESS_PERCENT_MARK;
app_data[15] = match_success;
@@ -182,4 +204,3 @@ void fmc_spi_read(void)
@@ -14,14 +14,19 @@ static uint8_t approach_target(uint8_t current, uint8_t target)
void Light_Transition_Task(void)
{
uint8_t target_brightness = 0U;
uint8_t target_w;
uint8_t target_c;
uint16_t brightness_scale;
if(deviceStatus != POWEROFF) target_brightness = Brightness;
brightness_scale = target_brightness * 205U;
W_PWM_duty = approach_target(
(uint8_t)W_PWM_duty, (uint8_t)(W_PWM * brightness_scale >> 11));
C_PWM_duty = approach_target(
(uint8_t)C_PWM_duty, (uint8_t)(C_PWM * brightness_scale >> 11));
/* 41 / 4096 is approximately 1 / 100, avoiding division in this task. */
brightness_scale = (uint16_t)target_brightness * 41U;
target_w =
(uint8_t)(((uint32_t)W_PWM * brightness_scale + 2048U) >> 12);
target_c =
(uint8_t)(((uint32_t)C_PWM * brightness_scale + 2048U) >> 12);
W_PWM_duty = approach_target((uint8_t)W_PWM_duty, target_w);
C_PWM_duty = approach_target((uint8_t)C_PWM_duty, target_c);
Light_Transition_FadeActive =
deviceStatus == POWEROFF && (W_PWM_duty != 0U || C_PWM_duty != 0U);
@@ -134,8 +134,11 @@ void scan_uart(uint8_t *arr)
case 4:
if(POWEROFF==deviceStatus)break;
if(Mode==mode0){
if(user_rx_buf[1]<=20)user_rx_buf[1]=20;
Brightness=user_rx_buf[1]/10;
if(user_rx_buf[1] < BRIGHTNESS_MIN_PERCENT)
user_rx_buf[1] = BRIGHTNESS_MIN_PERCENT;
else if(user_rx_buf[1] > BRIGHTNESS_MAX_PERCENT)
user_rx_buf[1] = BRIGHTNESS_MAX_PERCENT;
Brightness = user_rx_buf[1];
}
break;
case 5:
@@ -126,7 +126,7 @@ void ble_state_sync_poll(void)
ble_state_dirty |= BLE_STATE_DIRTY_POWER;
if((uint8_t)(choose_mode_falsg + 1U) != ble_last_mode)
ble_state_dirty |= BLE_STATE_DIRTY_MODE;
if((uint8_t)(Brightness * 10U) != ble_last_brightness)
if(Brightness != ble_last_brightness)
ble_state_dirty |= BLE_STATE_DIRTY_BRIGHTNESS;
if(Speed != ble_last_speed)
ble_state_dirty |= BLE_STATE_DIRTY_SPEED;
@@ -149,7 +149,7 @@ void ble_state_sync_poll(void)
}
else if(ble_state_dirty & BLE_STATE_DIRTY_BRIGHTNESS)
{
value = Brightness * 10U;
value = Brightness;
data[3] = 0x04U;
sent_mask = BLE_STATE_DIRTY_BRIGHTNESS;
}