From 0add5422ac75db8bdd337c205f2a9de2b6f875ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E7=90=AA?= Date: Mon, 27 Jul 2026 11:30:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=81=A5=E6=8E=A7=E5=AF=B9?= =?UTF-8?q?=E7=A0=81=E5=92=8C=E9=80=9F=E5=BA=A6=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/ble/ble_peripheral/app/src/PWM.h | 3 + .../ble/ble_peripheral/app/src/Rf433.c | 195 +++++++++++++----- .../ble/ble_peripheral/app/src/bridge_pwm.c | 2 +- .../ble/ble_peripheral/app/src/fmc_spi.c | 3 +- .../ble_peripheral/app/src/light_transition.c | 13 +- .../example/ble/ble_peripheral/app/src/uart.c | 2 + 6 files changed, 156 insertions(+), 62 deletions(-) diff --git a/project/example/ble/ble_peripheral/app/src/PWM.h b/project/example/ble/ble_peripheral/app/src/PWM.h index 15a6405..272ba68 100644 --- a/project/example/ble/ble_peripheral/app/src/PWM.h +++ b/project/example/ble/ble_peripheral/app/src/PWM.h @@ -69,6 +69,9 @@ extern uint8_t Brightness; #define BRIGHTNESS_MAX_PERCENT 100U #define BRIGHTNESS_RF_STEP_PERCENT 10U +#define SPEED_MIN_LEVEL 0U +#define SPEED_MAX_LEVEL 9U + #define DEVIATION_FREQ 10 #define DEVIATION_DUTYCYCLE 10 diff --git a/project/example/ble/ble_peripheral/app/src/Rf433.c b/project/example/ble/ble_peripheral/app/src/Rf433.c index 379cbed..59d99c4 100644 --- a/project/example/ble/ble_peripheral/app/src/Rf433.c +++ b/project/example/ble/ble_peripheral/app/src/Rf433.c @@ -4,6 +4,7 @@ #include "xc_drv_uart.h" #include "mode.h" #include "timer.h" +#include "light_transition.h" #include "pwm.h" union rf433_flag rf_flag = {0}; unsigned char high_level_time; @@ -75,6 +76,26 @@ void rf433_gpio_init(){ } static uint8_t ssse=0; +static volatile uint16_t rf_frame_sequence; +static uint16_t pair_last_frame_sequence; +static uint32_t pair_candidate_data; +static uint8_t pair_frame_silence_ticks; + +static uint8_t pairing_feedback_active; +static uint8_t pairing_feedback_on; +static uint8_t pairing_feedback_transitions; +static uint16_t pairing_feedback_elapsed_ms; +static uint16_t pairing_feedback_interval_ms; +static uint8_t pairing_saved_status; +static uint8_t pairing_saved_mode; +static uint8_t pairing_saved_brightness; +static uint16_t pairing_saved_w; +static uint16_t pairing_saved_c; +static uint16_t pairing_saved_w_duty; +static uint16_t pairing_saved_c_duty; + +#define PAIR_HOLD_FRAME_COUNT 8U +#define PAIR_FRAME_SILENCE_TICKS 50U uint8_t long_flag_s=0; // 在固定大小数组中添加新元素(滑动窗口) @@ -89,16 +110,126 @@ void addToFixedArray(uint8_t arr[], uint8_t newValue) { arr[0] = newValue; } +static void Pairing_Feedback_Apply(uint8_t on) +{ + Mode = mode0; + W_PWM = 100U; + C_PWM = 0U; + Brightness = BRIGHTNESS_MAX_PERCENT; + + if(on) + { + Start_PWM(); + W_PWM_duty = 100U; + C_PWM_duty = 0U; + } + else + { + W_PWM_duty = 0U; + C_PWM_duty = 0U; + Stop_PWM(); + Light_Transition_FadeActive = 0U; + Bridge_Off(); + } +} + +static void Pairing_Feedback_Restore(void) +{ + W_PWM = pairing_saved_w; + C_PWM = pairing_saved_c; + Brightness = pairing_saved_brightness; + Mode = pairing_saved_mode; + W_PWM_duty = pairing_saved_w_duty; + C_PWM_duty = pairing_saved_c_duty; + + if(pairing_saved_status == POWERON) + { + Start_PWM(); + } + else + { + Stop_PWM(); + if(W_PWM_duty == 0U && C_PWM_duty == 0U) + { + Light_Transition_FadeActive = 0U; + Bridge_Off(); + } + } +} + +static void Pairing_Feedback_Start(uint16_t interval_ms) +{ + if(pairing_feedback_active) return; + + pairing_saved_status = deviceStatus; + pairing_saved_mode = Mode; + pairing_saved_brightness = Brightness; + pairing_saved_w = W_PWM; + pairing_saved_c = C_PWM; + pairing_saved_w_duty = W_PWM_duty; + pairing_saved_c_duty = C_PWM_duty; + + pairing_feedback_active = 1U; + pairing_feedback_on = 1U; + pairing_feedback_transitions = 6U; + pairing_feedback_elapsed_ms = 0U; + pairing_feedback_interval_ms = interval_ms; + Pairing_Feedback_Apply(1U); +} + +static void Pairing_Feedback_Task(void) +{ + if(!pairing_feedback_active) return; + + pairing_feedback_elapsed_ms += 3U; + if(pairing_feedback_elapsed_ms < pairing_feedback_interval_ms) return; + pairing_feedback_elapsed_ms = 0U; + + if(pairing_feedback_transitions <= 1U) + { + pairing_feedback_active = 0U; + pairing_feedback_transitions = 0U; + Pairing_Feedback_Restore(); + return; + } + + pairing_feedback_transitions--; + pairing_feedback_on = pairing_feedback_on ? 0U : 1U; + Pairing_Feedback_Apply(pairing_feedback_on); +} + static void Encoder_key_NoCode(){ uint8_t i=0,j=0,isok=0,updata=0; uint16_t times_s=0; - if((res_data==Brightness_H||res_data==Brightness_L||res_data==B_Brightness_H||res_data==B_Brightness_L||res_data==F_Brightness_10||res_data==F_Brightness_100)&&time_5s<=40000&&ssse==0){ - long_press_cnt++; - - if(long_press_cnt>200){ - time_5s=40011; + if(rf_frame_sequence == pair_last_frame_sequence){ + if(pair_frame_silence_ticks < 0xffU) pair_frame_silence_ticks++; + if(pair_frame_silence_ticks > PAIR_FRAME_SILENCE_TICKS){ + long_press_cnt = 0U; + pair_candidate_data = 0U; + } + return; + } + + pair_last_frame_sequence = rf_frame_sequence; + pair_frame_silence_ticks = 0U; + + if(!((res_data==Brightness_H||res_data==Brightness_L||res_data==B_Brightness_H||res_data==B_Brightness_L||res_data==F_Brightness_10||res_data==F_Brightness_100)&&time_5s<=50000&&ssse==0)){ + long_press_cnt = 0U; + pair_candidate_data = 0U; + return; + } + + if(pair_candidate_data == receive_data){ + long_press_cnt++; + }else{ + pair_candidate_data = receive_data; + long_press_cnt = 1U; + } + + if(long_press_cnt>=PAIR_HOLD_FRAME_COUNT){ + time_5s=50011; ssse=1; long_press_cnt=0; @@ -146,30 +277,7 @@ static void Encoder_key_NoCode(){ /// EA=0; isok=0; - if(W_PWM==0&&C_PWM==0){ - if(Mode==0){ - W_PWM=100; - C_PWM=0; - (void)0;/* transition */ - }else { - W_PWM=100; - C_PWM=0; - (void)0;/* transition */ - } - } - if(deviceStatus==POWEROFF){ - startTimer(&timer1,1); - Start_PWM(); - } - for (i=0;i<3;i++){ - Start_PWM(); - Delay_ms(times_s); - Stop_PWM(); - Delay_ms(times_s); - } - - Start_PWM(); - startTimer(&timer1,1); + Pairing_Feedback_Start(times_s); } @@ -188,37 +296,18 @@ static void Encoder_key_NoCode(){ times_s=200; match_success = 0; - if(W_PWM==0&&C_PWM==0){ - W_PWM=100; - C_PWM=0; - if(Mode==0){ - (void)0;/* transition */ - }else { - (void)0;/* transition */ - } - } - if(deviceStatus==POWEROFF){ - startTimer(&timer1,1); - Start_PWM(); - } - for (i=0;i<3;i++){ - Start_PWM(); - Delay_ms(times_s); - Stop_PWM(); - Delay_ms(times_s); - } - Start_PWM(); + Pairing_Feedback_Start(times_s); } ssse=1; set_TaskComps_timer(2,2000); } - } } void Encoder_key(){ Encoder_key_NoCode(); //一对一 + Pairing_Feedback_Task(); } @@ -275,6 +364,7 @@ void rf433_receive(void)//rf433 receive_temp = receive_temp << 1; if (receive_data_cnt == 24)//24位数据接收完成 { + rf_frame_sequence++; //进来这个if需要40ms rf_flag.rf_receive_flag.receive_finish = 1;//24位数据接收完成标志 rf_flag.rf_receive_flag.sync_code = 0; @@ -290,6 +380,7 @@ void rf433_receive(void)//rf433 receive_data_cnt++; if (receive_data_cnt == 24) { + rf_frame_sequence++; //进来这个if需要40ms3748f3 rf_flag.rf_receive_flag.receive_finish = 1;//24位数据接收完成标志 rf_flag.rf_receive_flag.sync_code = 0; @@ -386,7 +477,7 @@ void _433_fun(){ Brightness -= BRIGHTNESS_RF_STEP_PERCENT; } }else { - if(Speed<10)Speed++; + if(Speed1)Speed--; + if(Speed>SPEED_MIN_LEVEL)Speed--; } break; default: 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 3c09ec4..002133a 100644 --- a/project/example/ble/ble_peripheral/app/src/bridge_pwm.c +++ b/project/example/ble/ble_peripheral/app/src/bridge_pwm.c @@ -21,7 +21,7 @@ */ #define BRIDGE_PWM_SCALE 1000U #define BRIDGE_PWM_PERIOD 7U -#define BRIDGE_MIX_SCALE 1000U +#define BRIDGE_MIX_SCALE 100U #define BRIDGE_FRAME_US 1000U #define BRIDGE_DEADTIME_US 50U #define BRIDGE_DUAL_ACTIVE_US 900U 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 e871771..6c0d74e 100644 --- a/project/example/ble/ble_peripheral/app/src/fmc_spi.c +++ b/project/example/ble/ble_peripheral/app/src/fmc_spi.c @@ -100,6 +100,8 @@ void read_user_data(void) Brightness = BRIGHTNESS_MAX_PERCENT; Speed = r_data[7]; + if(Speed > SPEED_MAX_LEVEL) + Speed = SPEED_MAX_LEVEL; deviceStatus = r_data[8]; W_PWM = r_data[9]; @@ -203,4 +205,3 @@ void fmc_spi_read(void) } - 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 2a6636a..2e3f595 100644 --- a/project/example/ble/ble_peripheral/app/src/light_transition.c +++ b/project/example/ble/ble_peripheral/app/src/light_transition.c @@ -4,8 +4,7 @@ volatile uint8_t Light_Transition_FadeActive; -#define LIGHT_OUTPUT_SCALE 10U -#define LIGHT_FADE_STEP 10U +#define LIGHT_FADE_STEP 1U /* * Gamma 1.8, mapping APP levels 1..100 to a stable physical range of @@ -62,12 +61,10 @@ void Light_Transition_Task(void) brightness_scale = brightness_gamma_q12[target_brightness - 1U]; } - target_w = (uint16_t) - (((uint32_t)W_PWM * brightness_scale * LIGHT_OUTPUT_SCALE + 2048U) >> - 12); - target_c = (uint16_t) - (((uint32_t)C_PWM * brightness_scale * LIGHT_OUTPUT_SCALE + 2048U) >> - 12); + target_w = + (uint16_t)(((uint32_t)W_PWM * brightness_scale + 2048U) >> 12); + target_c = + (uint16_t)(((uint32_t)C_PWM * brightness_scale + 2048U) >> 12); W_PWM_duty = approach_target(W_PWM_duty, target_w); C_PWM_duty = approach_target(C_PWM_duty, target_c); diff --git a/project/example/ble/ble_peripheral/app/src/uart.c b/project/example/ble/ble_peripheral/app/src/uart.c index 672a1d3..152b30c 100644 --- a/project/example/ble/ble_peripheral/app/src/uart.c +++ b/project/example/ble/ble_peripheral/app/src/uart.c @@ -143,6 +143,8 @@ void scan_uart(uint8_t *arr) break; case 5: if(POWEROFF==deviceStatus)break; + if(user_rx_buf[1] > SPEED_MAX_LEVEL) + user_rx_buf[1] = SPEED_MAX_LEVEL; Speed =user_rx_buf[1]; break; case 6: