From e66570c005043330747ef593dd3a76c92bb1486f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E7=90=AA?= Date: Tue, 28 Jul 2026 13:14:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=A9=E5=B1=95=E5=8D=81=E4=B8=89=E7=A7=8D?= =?UTF-8?q?=E7=81=AF=E5=85=89=E6=95=88=E6=9E=9C=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/ble/ble_peripheral/app/src/Mode.c | 390 ++++++++---------- .../example/ble/ble_peripheral/app/src/Mode.h | 4 + .../ble/ble_peripheral/app/src/Rf433.c | 4 +- .../ble/ble_peripheral/app/src/bridge_pwm.c | 3 +- .../ble/ble_peripheral/app/src/fmc_spi.c | 2 + .../ble_peripheral/app/src/light_transition.c | 6 +- .../example/ble/ble_peripheral/app/src/uart.c | 5 +- 7 files changed, 185 insertions(+), 229 deletions(-) diff --git a/project/example/ble/ble_peripheral/app/src/Mode.c b/project/example/ble/ble_peripheral/app/src/Mode.c index b68fbfe..b7bd741 100644 --- a/project/example/ble/ble_peripheral/app/src/Mode.c +++ b/project/example/ble/ble_peripheral/app/src/Mode.c @@ -66,88 +66,52 @@ void set_mode(){ if(choose_mode_bh==0){ choose_mode_bh=1; switch(choose_mode_falsg){ - // case 0: - //暖光 - W_PWM=Color_table_static[0][0]; - C_PWM=Color_table_static[0][1]; - driveMode=1;//切换为暖光的底层输出 - Mode=mode0; + W_PWM=100U; C_PWM=0U; driveMode=1U; Mode=mode0; break; case 1: - //冷光 - W_PWM=Color_table_static[2][0]; - C_PWM=Color_table_static[2][1]; - driveMode=2;//切换为冷光的底层输出 - Mode=mode0; + W_PWM=0U; C_PWM=100U; driveMode=2U; Mode=mode0; break; case 2: - //中性光 - W_PWM=Color_table_static[1][0]; - C_PWM=Color_table_static[1][1]; - driveMode=0;//切换为中性光的底层输出 - Mode=mode0; + W_PWM=50U; C_PWM=50U; driveMode=0U; Mode=mode0; break; case 3: - //暖光呼吸 - W_PWM=Color_table[0][0]; - C_PWM=Color_table[0][1]; - driveMode=1;//切换为暖光的底层输出 - direction=1;//呼吸方向恢复默认值 - Mode=mode1; + W_PWM=100U; C_PWM=0U; driveMode=1U; direction=1U; Mode=mode1; break; - case 4: - //冷光呼吸 - W_PWM=Color_table[2][0]; - C_PWM=Color_table[2][1]; - driveMode=2;//切换为中性光的底层输出 - direction=1;//呼吸方向恢复默认值 - Mode=mode1; + case 4: + W_PWM=0U; C_PWM=100U; driveMode=2U; direction=1U; Mode=mode1; break; - case 5: - //三个色温轮询呼吸//先从暖光开始呼吸 - W_PWM=Color_table[0][0]; - C_PWM=Color_table[0][1]; - driveMode=1;//切换为暖光的底层输出 - direction=1;//呼吸方向恢复默认值 - - Mode=mode1; + case 5: + W_PWM=68U; C_PWM=68U; driveMode=0U; direction=1U; Mode=mode1; break; - case 6: - //暖光跳变 - W_PWM=Color_table[0][0]; - C_PWM=Color_table[0][1]; - driveMode=1;//切换为暖光的底层输出 - direction=1;//呼吸方向恢复默认值 - Mode=mode2; + case 6: + W_PWM=100U; C_PWM=0U; driveMode=1U; direction=1U; Mode=mode1; break; - case 7: - //中性光跳变 - W_PWM=Color_table[1][0]; - C_PWM=Color_table[1][1]; - driveMode=0;//切换为中性光的底层输出 - Mode=mode2; + case 7: + W_PWM=100U; C_PWM=0U; driveMode=1U; direction=1U; Mode=mode3; break; - case 8: - //冷光跳变 - W_PWM=Color_table[2][0]; - C_PWM=Color_table[2][1]; - driveMode=2;//切换为中性光的底层输出 - direction=1;//呼吸方向恢复默认值 - Mode=mode2; + case 8: + W_PWM=100U; C_PWM=0U; driveMode=1U; direction=1U; Mode=mode2; break; - case 9: - //暖光中性光冷交替跳变,先从暖光跳变 - W_PWM=Color_table[0][0]; - C_PWM=Color_table[0][1]; - driveMode=1;//切换为暖光的底层输出 - direction=1;//呼吸方向恢复默认值 - Mode=mode2; + case 9: + W_PWM=0U; C_PWM=100U; driveMode=2U; direction=1U; Mode=mode2; + break; + case 10: + W_PWM=50U; C_PWM=50U; driveMode=0U; direction=1U; Mode=mode2; + break; + case 11: + W_PWM=100U; C_PWM=0U; driveMode=1U; direction=2U; Mode=mode2; + break; + case 12: + W_PWM=100U; C_PWM=0U; driveMode=1U; direction=2U; Mode=mode2; + break; + case CUSTOM_TEMPERATURE_MODE: + Mode=mode0; + break; + default: + choose_mode_falsg=0U; + W_PWM=100U; C_PWM=0U; driveMode=1U; Mode=mode0; break; - - case 10://圆环模式 - Mode=mode0; - break; } if(mode_entry_initialized) { @@ -214,112 +178,85 @@ static void breathe_fun(uint16_t *PWM_1, uint16_t *PWM_2, uint8_t flag) { // } } } -void Mode1(){ //呼吸 - if(choose_mode_falsg==3){ //暖光呼吸 - breathe_fun(&C_PWM,&W_PWM,0); - }else if(choose_mode_falsg==4){//冷光呼吸 - breathe_fun(&W_PWM,&C_PWM,0); - }else if(choose_mode_falsg==5){ - switch (direction) { - case 0: - breathespeed_flag=0; - W_PWM += 1; - C_PWM = 0; - - if (W_PWM >= 100) { - direction = 1; - } - - break; - case 1: - breathespeed_flag=0; - W_PWM -= 1; - C_PWM = 0; - if (W_PWM <= 1) { - direction = THREE_COLOR_WAIT_NEUTRAL; - W_PWM = 0; - C_PWM = 0; - } - break; - case 2: - breathespeed_flag=1; - W_PWM += 1; - C_PWM += 1; - if (W_PWM >= 68) { - W_PWM=68; - C_PWM=68; - direction = 3; - } - break; - case 3: - breathespeed_flag=1; - W_PWM -= 1; - C_PWM -= 1; - if (W_PWM <=1) { - direction = THREE_COLOR_WAIT_COLD; - W_PWM = 0; - C_PWM = 0; - } - break; - case 4: - breathespeed_flag=0; - W_PWM = 0; - C_PWM += 1; - - if (C_PWM >= 100) { - direction = 5; - } - break; - - case 5: - breathespeed_flag=0; - W_PWM = 0; - C_PWM -= 1; - if (C_PWM <=1){ - direction = THREE_COLOR_WAIT_WARM; - W_PWM = 0; - C_PWM = 0; - } - - break; - case THREE_COLOR_WAIT_NEUTRAL: - breathespeed_flag=1; - W_PWM = 0; - C_PWM = 0; - if(W_PWM_duty == 0U && C_PWM_duty == 0U){ - driveMode=0; - W_PWM = 1; - C_PWM = 1; - direction = 2; - } - break; - case THREE_COLOR_WAIT_COLD: - breathespeed_flag=0; - W_PWM = 0; - C_PWM = 0; - if(W_PWM_duty == 0U && C_PWM_duty == 0U){ - driveMode=2; - C_PWM = 1; - direction = 4; - } - break; - case THREE_COLOR_WAIT_WARM: - breathespeed_flag=0; - W_PWM = 0; - C_PWM = 0; - if(W_PWM_duty == 0U && C_PWM_duty == 0U){ - driveMode=1; - W_PWM = 1; - direction = 0; - } - break; - } +static void neutral_breathe_fun(void) +{ + breathespeed_flag=1U; + if(direction==1U){ + if(W_PWM>1U){ W_PWM--; C_PWM--; } + if(W_PWM<=1U){ W_PWM=1U; C_PWM=1U; direction=0U; } + }else{ + if(W_PWM<68U){ W_PWM++; C_PWM++; } + if(W_PWM>=68U){ W_PWM=68U; C_PWM=68U; direction=1U; } } - //(void)0;/* transition */ - (void)0;/* transition */ } - +void Mode1(void) +{ + if(choose_mode_falsg==3U){ + breathespeed_flag=0U; + breathe_fun(&C_PWM,&W_PWM,0U); + }else if(choose_mode_falsg==4U){ + breathespeed_flag=0U; + breathe_fun(&W_PWM,&C_PWM,0U); + }else if(choose_mode_falsg==5U){ + neutral_breathe_fun(); + }else if(choose_mode_falsg==6U){ + switch(direction){ + case 0U: + breathespeed_flag=0U; + W_PWM++; C_PWM=0U; + if(W_PWM>=100U){ W_PWM=100U; direction=1U; } + break; + case 1U: + breathespeed_flag=0U; + if(W_PWM>0U) W_PWM--; + C_PWM=0U; + if(W_PWM<=1U){ W_PWM=0U; direction=THREE_COLOR_WAIT_COLD; } + break; + case 4U: + breathespeed_flag=0U; + W_PWM=0U; C_PWM++; + if(C_PWM>=100U){ C_PWM=100U; direction=5U; } + break; + case 5U: + breathespeed_flag=0U; + W_PWM=0U; + if(C_PWM>0U) C_PWM--; + if(C_PWM<=1U){ C_PWM=0U; direction=THREE_COLOR_WAIT_NEUTRAL; } + break; + case 2U: + breathespeed_flag=1U; + W_PWM++; C_PWM++; + if(W_PWM>=68U){ W_PWM=68U; C_PWM=68U; direction=3U; } + break; + case 3U: + breathespeed_flag=1U; + if(W_PWM>0U) W_PWM--; + if(C_PWM>0U) C_PWM--; + if(W_PWM<=1U){ W_PWM=0U; C_PWM=0U; direction=THREE_COLOR_WAIT_WARM; } + break; + case THREE_COLOR_WAIT_COLD: + breathespeed_flag=0U; W_PWM=0U; C_PWM=0U; + if(W_PWM_duty==0U && C_PWM_duty==0U){ + driveMode=2U; C_PWM=1U; direction=4U; + } + break; + case THREE_COLOR_WAIT_NEUTRAL: + breathespeed_flag=1U; W_PWM=0U; C_PWM=0U; + if(W_PWM_duty==0U && C_PWM_duty==0U){ + driveMode=0U; W_PWM=1U; C_PWM=1U; direction=2U; + } + break; + case THREE_COLOR_WAIT_WARM: + default: + breathespeed_flag=0U; W_PWM=0U; C_PWM=0U; + if(W_PWM_duty==0U && C_PWM_duty==0U){ + driveMode=1U; W_PWM=1U; direction=0U; + } + break; + } + } +} void jump_fun(uint16_t *PWM_1, uint16_t *PWM_2, uint8_t flag){ if(flag==0){ *PWM_1 = 0; @@ -332,8 +269,8 @@ void jump_fun(uint16_t *PWM_1, uint16_t *PWM_2, uint8_t flag){ } }else if(flag==1){ if (!direction){ - *PWM_1 = 40; - *PWM_2 = 40; + *PWM_1 = 50; + *PWM_2 = 50; direction = 1; } else { *PWM_1 = 0; @@ -343,58 +280,65 @@ void jump_fun(uint16_t *PWM_1, uint16_t *PWM_2, uint8_t flag){ } } -void Mode2(){ //跳变 - //printf("dd=%d--%d\r\n",W_PWM,W_PWM); - if(choose_mode_falsg==6){ - jump_fun(&C_PWM,&W_PWM,0); //暖光跳变 - }else if(choose_mode_falsg==7){ - jump_fun(&W_PWM,&C_PWM,1); //中光跳变 - }else if(choose_mode_falsg==8){ - jump_fun(&W_PWM,&C_PWM,0); //冷光跳变 - }else if(choose_mode_falsg==9){ - switch (direction) { - case 1: - W_PWM =100; - C_PWM = 0; - direction = 2; - break; - case 2: - W_PWM = 0; - C_PWM = 0; - direction = 3; - driveMode=0; - break; - case 3: - W_PWM =40; - C_PWM =40; - direction = 4; - break; - case 4: - W_PWM =0; - C_PWM =0; - direction = 5; - driveMode=2; - break; - case 5: - W_PWM = 0; - C_PWM = 100; - direction = 6; - break; - - case 6: - W_PWM = 0; - C_PWM = 0; - direction = 1; - driveMode=1; - break; - } +void Mode2(void) +{ + if(choose_mode_falsg==8U){ + jump_fun(&C_PWM,&W_PWM,0U); + }else if(choose_mode_falsg==9U){ + jump_fun(&W_PWM,&C_PWM,0U); + }else if(choose_mode_falsg==10U){ + jump_fun(&W_PWM,&C_PWM,1U); + }else if(choose_mode_falsg==11U){ + switch(direction){ + case 1U: + W_PWM=100U; C_PWM=0U; driveMode=1U; direction=2U; + break; + case 2U: + W_PWM=0U; C_PWM=100U; driveMode=2U; direction=3U; + break; + default: + W_PWM=50U; C_PWM=50U; driveMode=0U; direction=1U; + break; } - (void)0;/* transition */ + }else if(choose_mode_falsg==12U){ + switch(direction){ + case 1U: + W_PWM=100U; C_PWM=0U; driveMode=1U; direction=2U; + break; + case 2U: + W_PWM=0U; C_PWM=0U; driveMode=2U; direction=3U; + break; + case 3U: + W_PWM=0U; C_PWM=100U; direction=4U; + break; + case 4U: + W_PWM=0U; C_PWM=0U; driveMode=0U; direction=5U; + break; + case 5U: + W_PWM=50U; C_PWM=50U; direction=6U; + break; + default: + W_PWM=0U; C_PWM=0U; driveMode=1U; direction=1U; + break; + } + } } -void Mode3(){ //爆闪 //无 - - (void)0;/* transition */ +void Mode3(void) +{ + if(choose_mode_falsg!=7U) return; + if(direction==1U){ + if(C_PWM<100U) C_PWM++; + W_PWM=100U-C_PWM; + if(C_PWM>=100U){ C_PWM=100U; W_PWM=0U; direction=0U; } + }else{ + if(C_PWM>0U) C_PWM--; + W_PWM=100U-C_PWM; + if(C_PWM==0U){ W_PWM=100U; direction=1U; } + } + if(C_PWM==0U) driveMode=1U; + else if(W_PWM==0U) driveMode=2U; + else driveMode=0U; } pfunc modefunctin[] = { //模式切换,一共15个模式 Mode0, @@ -417,7 +361,7 @@ void choice_mode(){ }else if(Mode==mode2){ startTimer(&timer1,time_2[Speed]); }else if(Mode==mode3){ - startTimer(&timer1,time_3[Speed]);// + startTimer(&timer1,time_1[Speed]);// } modefunctin[Mode](); diff --git a/project/example/ble/ble_peripheral/app/src/Mode.h b/project/example/ble/ble_peripheral/app/src/Mode.h index 79db50d..ae91f73 100644 --- a/project/example/ble/ble_peripheral/app/src/Mode.h +++ b/project/example/ble/ble_peripheral/app/src/Mode.h @@ -13,6 +13,10 @@ extern "C" { #include typedef void (*pfunc)(void); +#define EFFECT_MODE_MIN 0U +#define EFFECT_MODE_MAX 12U +#define CUSTOM_TEMPERATURE_MODE 13U + extern uint8_t deviceStatus; //开机标志位 extern uint8_t Mode; //mode0 1 2 3 4 diff --git a/project/example/ble/ble_peripheral/app/src/Rf433.c b/project/example/ble/ble_peripheral/app/src/Rf433.c index 219bad4..6951f29 100644 --- a/project/example/ble/ble_peripheral/app/src/Rf433.c +++ b/project/example/ble/ble_peripheral/app/src/Rf433.c @@ -438,7 +438,7 @@ void rf433_receive(void)//RF433 sample, called every 100 us uint16_t spee_dealy_time=0; //速度缓慢变换 #define REMOTE_MODE_MIN 0U -#define REMOTE_MODE_MAX 9U +#define REMOTE_MODE_MAX EFFECT_MODE_MAX void _433_fun(){ if(Light_Transition_FeedbackActive()) return; @@ -527,7 +527,7 @@ void _433_fun(){ if(res_data>=16&&res_data<=255){ Mode=mode0; choose_mode_bh=0; - choose_mode_falsg=10;//圆环模式 + choose_mode_falsg=CUSTOM_TEMPERATURE_MODE;//圆环模式 driveMode=0; temperature((uint8_t)(((uint16_t)(255U - res_data) * 255U + 119U) / 239U)); startTimer(&timer1,1); diff --git a/project/example/ble/ble_peripheral/app/src/bridge_pwm.c b/project/example/ble/ble_peripheral/app/src/bridge_pwm.c index 51b6e96..ac46d1f 100644 --- a/project/example/ble/ble_peripheral/app/src/bridge_pwm.c +++ b/project/example/ble/ble_peripheral/app/src/bridge_pwm.c @@ -403,7 +403,8 @@ __RAM_CODE void Bridge_Service_1ms(void) * PWM all the way down to zero. */ if (deviceStatus != POWEROFF && - Mode == mode1 && choose_mode_falsg == 5U && + Mode == mode1 && + (choose_mode_falsg == 5U || choose_mode_falsg == 6U) && w_mix != 0U && c_mix != 0U) { pdm_w_step = BRIDGE_DUAL_ACTIVE_US * ((uint32_t)w_mix << 12); diff --git a/project/example/ble/ble_peripheral/app/src/fmc_spi.c b/project/example/ble/ble_peripheral/app/src/fmc_spi.c index 6c0d74e..326b05f 100644 --- a/project/example/ble/ble_peripheral/app/src/fmc_spi.c +++ b/project/example/ble/ble_peripheral/app/src/fmc_spi.c @@ -131,6 +131,8 @@ void read_user_data(void) adress_H_array[4]=r_data[24]; adress_L_array[4]=r_data[25]; choose_mode_falsg=r_data[30]; + if(choose_mode_falsg > CUSTOM_TEMPERATURE_MODE) + choose_mode_falsg = EFFECT_MODE_MIN; //choose_mode_bh=r_data[31]; driveMode=r_data[32]; flag_1h = (r_data[36] << 24) | (r_data[35] << 16) | (r_data[34] << 8) | r_data[33]; diff --git a/project/example/ble/ble_peripheral/app/src/light_transition.c b/project/example/ble/ble_peripheral/app/src/light_transition.c index cbee8f6..36fe976 100644 --- a/project/example/ble/ble_peripheral/app/src/light_transition.c +++ b/project/example/ble/ble_peripheral/app/src/light_transition.c @@ -103,6 +103,9 @@ static uint16_t output_brightness_scale_for_level(uint8_t level) if(level < BRIGHTNESS_MIN_PERCENT) level = BRIGHTNESS_MIN_PERCENT; if(level > BRIGHTNESS_MAX_PERCENT) level = BRIGHTNESS_MAX_PERCENT; + if(Mode == mode3 && choose_mode_falsg == 7U) + return brightness_scale_for_level(level); + if(W_PWM != 0U && C_PWM != 0U) return mixed_brightness_gamma_q12[level - 1U]; @@ -360,7 +363,8 @@ void Light_Transition_Task(void) * brightness is treated as single color. */ if(W_PWM != 0U && C_PWM != 0U && - !(Mode == mode1 && choose_mode_falsg == 5U)) + !(Mode == mode1 && + (choose_mode_falsg == 5U || choose_mode_falsg == 6U))) { mix_total = (uint16_t)(W_PWM + C_PWM); weak_mix = (W_PWM < C_PWM) ? W_PWM : C_PWM; diff --git a/project/example/ble/ble_peripheral/app/src/uart.c b/project/example/ble/ble_peripheral/app/src/uart.c index 152b30c..3b58b0c 100644 --- a/project/example/ble/ble_peripheral/app/src/uart.c +++ b/project/example/ble/ble_peripheral/app/src/uart.c @@ -124,8 +124,9 @@ void scan_uart(uint8_t *arr) break; case 2: if(POWEROFF==deviceStatus)break; + if(user_rx_buf[1] < 1U || user_rx_buf[1] > (EFFECT_MODE_MAX + 1U)) break; choose_mode_bh=0; - choose_mode_falsg=user_rx_buf[1]-1; + choose_mode_falsg=user_rx_buf[1]-1U; startTimer(&timer1,1); break; case 3: @@ -153,7 +154,7 @@ void scan_uart(uint8_t *arr) temperature(user_rx_buf[1]); Mode=mode0; choose_mode_bh=0; - choose_mode_falsg=10;//圆环模式 + choose_mode_falsg=CUSTOM_TEMPERATURE_MODE;//圆环模式 startTimer(&timer1,1); break; case 10: