From cb7fe7f041948012a8a8432ee25c9e12476d61e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E7=90=AA?= Date: Tue, 28 Jul 2026 15:43:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8A=A8=E6=80=81=E4=BA=AE?= =?UTF-8?q?=E5=BA=A6=E9=95=BF=E6=8C=89=E5=8F=8A=E6=96=AD=E7=94=B5=E5=88=87?= =?UTF-8?q?=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ble/ble_peripheral/app/src/Rf433.c | 160 ++++++++++-------- .../ble/ble_peripheral/app/src/arch_main.c | 1 + .../ble/ble_peripheral/app/src/fmc_spi.c | 73 +++++++- .../ble/ble_peripheral/app/src/fmc_spi.h | 6 + .../ble/ble_peripheral/app/src/timeslice.c | 4 +- .../example/ble/ble_peripheral/app/src/uart.c | 16 +- .../ble/ble_peripheral/app/src/usr_server.c | 18 +- 7 files changed, 193 insertions(+), 85 deletions(-) diff --git a/project/example/ble/ble_peripheral/app/src/Rf433.c b/project/example/ble/ble_peripheral/app/src/Rf433.c index 6951f29..ed1e550 100644 --- a/project/example/ble/ble_peripheral/app/src/Rf433.c +++ b/project/example/ble/ble_peripheral/app/src/Rf433.c @@ -17,8 +17,6 @@ uint32_t receive_data; uint8_t res_data=255; //433返回的最后一个控制字节(低八位) -static uint16_t long_time=0; //表示长按短按 该值一定要大与68,68是指68ms通过逻辑分析仪测量一帧433的时间 -static uint16_t time_300ms=1; //表示:当长按的时候要300ms执行一次 static uint8_t POWER_flag; uint32_t flag_1h =0; @@ -99,10 +97,19 @@ static uint8_t pair_command_replay; #define PAIR_HOLD_FRAME_COUNT 4U #define PAIR_FRAME_SILENCE_TICKS 100U #define PAIRING_WINDOW_TICKS 50000UL -#define RF433_RELEASE_TIMEOUT_TICKS 1500U #define RF433_MS_TO_SAMPLE_TICKS(ms) ((uint16_t)((ms) * (1000U / RF433_DECODER_SAMPLE_US))) +#define RF433_HOLD_GAP_TICKS ((uint32_t)RF433_MS_TO_SAMPLE_TICKS(250U)) +#define RF433_REPEAT_TICKS ((uint32_t)RF433_MS_TO_SAMPLE_TICKS(500U)) uint8_t long_flag_s=0; +static volatile uint32_t rf_sample_ticks; +static uint16_t rf_dispatch_sequence; +static uint32_t rf_hold_code; +static uint32_t rf_hold_last_frame_tick; +static uint32_t rf_hold_next_repeat_tick; +static uint8_t rf_hold_key; +static uint8_t rf_hold_active; + void _433_fun(void); // Keep paired addresses ordered from oldest to newest. @@ -371,7 +378,6 @@ static void Rf433_Decoder_Frame_Poll(void) res_data = frame.key; rf_frame_sequence++; rf_flag.rf_receive_flag.receive_finish = 1U; - long_time = RF433_RELEASE_TIMEOUT_TICKS; } void Encoder_key(){ @@ -381,54 +387,11 @@ void Encoder_key(){ } -typedef struct { - uint32_t KEY_STATE_Click; // 单击 - uint32_t KEY_STATE_LONG_PRESS; // 长按 -} _Key_TypeDef; -_Key_TypeDef Key_TypeDef={0,0}; -uint32_t KEY_STATE_re=0; // 长按 - - - -//该函数选择那个按键为单击,哪个为长按按键 -static void set_click(){ - if(res_data==Mode_add||res_data==Mode_dow||res_data==F_ON_OFF||res_data==F_Mode_choose||res_data==ON||res_data==OFF){ - Key_TypeDef.KEY_STATE_Click=1; //单击 - }else{ - Key_TypeDef.KEY_STATE_LONG_PRESS=1; //长按 - KEY_STATE_re=1; - } -} - void rf433_receive(void)//RF433 sample, called every 100 us { rf433_decoder_sample((RF_DATA == HIGH_LEVEL) ? 1U : 0U); - - if(long_time > 0U) - { - long_time--; - long_flag_s = 0U; - if(KEY_STATE_re == 1U) - { - if(time_300ms == 0U) - { - time_300ms = RF433_MS_TO_SAMPLE_TICKS(Short_time); - long_flag_s = 1U; - KEY_STATE_re = 0U; - } - else - { - time_300ms--; - } - } - } - else - { - time_300ms = RF433_MS_TO_SAMPLE_TICKS(LongPress_time); - long_flag_s = 0U; - Key_TypeDef.KEY_STATE_Click = 0U; - KEY_STATE_re = 0U; - } + rf_sample_ticks++; + long_flag_s = 0U; if(time_5s <= PAIRING_WINDOW_TICKS) { @@ -451,8 +414,7 @@ void _433_fun(){ pair_deferred_receive_data = receive_data; return; } - if(Key_TypeDef.KEY_STATE_Click==1||KEY_STATE_re==1) return; - set_click(); + sss=1;//记忆标志位 switch(res_data){ //椭圆 @@ -539,28 +501,86 @@ void _433_fun(){ //3748 -void scan_433(){ - - if(power==0){ - if(rf_flag.rf_receive_flag.receive_finish==1){ - if(match_success==1){ - if((adress_H_array[0]==((receive_data >> 16) & 0xff)&&adress_L_array[0]==((receive_data >> 8) & 0xff))|| - (adress_H_array[1]==((receive_data >> 16) & 0xff)&&adress_L_array[1]==((receive_data >> 8) & 0xff))|| - (adress_H_array[2]==((receive_data >> 16) & 0xff)&&adress_L_array[2]==((receive_data >> 8) & 0xff))|| - (adress_H_array[3]==((receive_data >> 16) & 0xff)&&adress_L_array[3]==((receive_data >> 8) & 0xff))|| - (adress_H_array[4]==((receive_data >> 16) & 0xff)&&adress_L_array[4]==((receive_data >> 8) & 0xff)) - ){ - _433_fun(); - } - }else if(match_success==0){ - _433_fun(); - } - } - rf_flag.rf_receive_flag.receive_finish=0; - } +static uint8_t Rf433_Address_Matched(uint32_t code) +{ + uint8_t high = (uint8_t)((code >> 16) & 0xffU); + uint8_t low = (uint8_t)((code >> 8) & 0xffU); + uint8_t i; + + if(match_success == 0U) return 1U; + + for(i = 0U; i < 5U; i++) + { + if(adress_H_array[i] == high && adress_L_array[i] == low) + return 1U; + } + return 0U; } +static uint8_t Rf433_Time_Reached(uint32_t now, uint32_t deadline) +{ + return ((int32_t)(now - deadline) >= 0) ? 1U : 0U; +} +static void Rf433_Run_Command(uint32_t code, uint8_t key) +{ + receive_data = code; + res_data = key; + _433_fun(); +} +void scan_433(){ + uint32_t now = rf_sample_ticks; + if(power != 0U) return; + if(rf_hold_active && + (uint32_t)(now - rf_hold_last_frame_tick) > RF433_HOLD_GAP_TICKS) + { + rf_hold_active = 0U; + } + + if(rf_dispatch_sequence != rf_frame_sequence) + { + uint32_t code = receive_data; + uint8_t key = res_data; + uint32_t frame_gap = (uint32_t)(now - rf_hold_last_frame_tick); + + rf_dispatch_sequence = rf_frame_sequence; + rf_flag.rf_receive_flag.receive_finish = 0U; + + if(!Rf433_Address_Matched(code)) + { + rf_hold_active = 0U; + } + else if(key >= 16U) + { + /* Color-ring frames stay unthrottled for smooth sliding. */ + rf_hold_active = 0U; + Rf433_Run_Command(code, key); + } + else if(!rf_hold_active || rf_hold_code != code || + frame_gap > RF433_HOLD_GAP_TICKS) + { + /* The first valid frame is an immediate short-press action. */ + rf_hold_active = 1U; + rf_hold_code = code; + rf_hold_key = key; + rf_hold_last_frame_tick = now; + rf_hold_next_repeat_tick = now + RF433_REPEAT_TICKS; + Rf433_Run_Command(code, key); + } + else + { + rf_hold_last_frame_tick = now; + } + } + + if(rf_hold_active && + (uint32_t)(now - rf_hold_last_frame_tick) <= RF433_HOLD_GAP_TICKS && + Rf433_Time_Reached(now, rf_hold_next_repeat_tick)) + { + Rf433_Run_Command(rf_hold_code, rf_hold_key); + rf_hold_next_repeat_tick = now + RF433_REPEAT_TICKS; + } +} diff --git a/project/example/ble/ble_peripheral/app/src/arch_main.c b/project/example/ble/ble_peripheral/app/src/arch_main.c index 3cbe852..88ad1df 100644 --- a/project/example/ble/ble_peripheral/app/src/arch_main.c +++ b/project/example/ble/ble_peripheral/app/src/arch_main.c @@ -396,6 +396,7 @@ int main(void) xc_fmc_spi_flash_wake_up(); fmc_spi_read(); + power_restore_apply_on_boot(); timer0_2_init(); bluetooth_init(); 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 ddc31c6..7a42090 100644 --- a/project/example/ble/ble_peripheral/app/src/fmc_spi.c +++ b/project/example/ble/ble_peripheral/app/src/fmc_spi.c @@ -73,6 +73,13 @@ unsigned char get_checksum(unsigned char *ptrdata, unsigned char length) #define FLASH_BRIGHTNESS_PERCENT_MARK 0xA5U #define FLASH_MODE_LAYOUT_INDEX 26U #define FLASH_MODE_LAYOUT_MARK 0xA6U +#define FLASH_POWER_MODE_MARK_INDEX 27U +#define FLASH_POWER_MODE_INDEX 28U +#define FLASH_POWER_CYCLE_INDEX 29U +#define FLASH_POWER_MODE_MARK 0xA7U + +uint8_t power_restore_policy = POWER_RESTORE_CYCLE_STATIC; +static uint8_t power_cycle_next_color; void read_user_data(void) { @@ -141,6 +148,20 @@ void read_user_data(void) } if(choose_mode_falsg > CUSTOM_TEMPERATURE_MODE) choose_mode_falsg = EFFECT_MODE_MIN; + if(r_data[FLASH_POWER_MODE_MARK_INDEX] == FLASH_POWER_MODE_MARK) + { + power_restore_policy = r_data[FLASH_POWER_MODE_INDEX]; + if(power_restore_policy > POWER_RESTORE_CYCLE_STATIC) + power_restore_policy = POWER_RESTORE_CYCLE_STATIC; + power_cycle_next_color = r_data[FLASH_POWER_CYCLE_INDEX]; + if(power_cycle_next_color > 2U) + power_cycle_next_color = 0U; + } + else + { + power_restore_policy = POWER_RESTORE_CYCLE_STATIC; + power_cycle_next_color = 0U; + } //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]; @@ -187,9 +208,9 @@ void wright_user_data(void) app_data[24]=adress_H_array[4]; app_data[25]=adress_L_array[4]; app_data[FLASH_MODE_LAYOUT_INDEX]=FLASH_MODE_LAYOUT_MARK; - app_data[27]=0; - app_data[28]=0; - app_data[29]=0; + app_data[FLASH_POWER_MODE_MARK_INDEX]=FLASH_POWER_MODE_MARK; + app_data[FLASH_POWER_MODE_INDEX]=power_restore_policy; + app_data[FLASH_POWER_CYCLE_INDEX]=power_cycle_next_color; app_data[30]=choose_mode_falsg; app_data[31]=choose_mode_bh; app_data[32]=driveMode; @@ -215,3 +236,49 @@ void fmc_spi_read(void) } +void power_restore_policy_set(uint8_t policy) +{ + if(policy > POWER_RESTORE_CYCLE_STATIC) return; + + power_restore_policy = policy; + if(policy == POWER_RESTORE_CYCLE_STATIC) + power_cycle_next_color = 0U; + sss = 1U; +} + +void power_restore_apply_on_boot(void) +{ + uint8_t color; + + if(power_restore_policy != POWER_RESTORE_CYCLE_STATIC) return; + + color = power_cycle_next_color; + if(color > 2U) color = 0U; + + deviceStatus = POWERON; + Mode = mode0; + choose_mode_falsg = color; + choose_mode_bh = 0U; + + if(color == 0U) + { + W_PWM = 100U; + C_PWM = 0U; + driveMode = 1U; + } + else if(color == 1U) + { + W_PWM = 0U; + C_PWM = 100U; + driveMode = 2U; + } + else + { + W_PWM = 50U; + C_PWM = 50U; + driveMode = 0U; + } + + power_cycle_next_color = (uint8_t)((color + 1U) % 3U); + sss = 1U; +} diff --git a/project/example/ble/ble_peripheral/app/src/fmc_spi.h b/project/example/ble/ble_peripheral/app/src/fmc_spi.h index 1ef1d64..2f45c71 100644 --- a/project/example/ble/ble_peripheral/app/src/fmc_spi.h +++ b/project/example/ble/ble_peripheral/app/src/fmc_spi.h @@ -37,6 +37,9 @@ extern "C" { #include #include +#define POWER_RESTORE_FULL_MEMORY 0U +#define POWER_RESTORE_CYCLE_STATIC 1U + /*------------------------------------------------------------------------------------ Macros -------------------------------------------------------------------------------------*/ @@ -49,12 +52,15 @@ extern "C" { Global Variables -------------------------------------------------------------------------------------*/ extern uint8_t data[]; + extern uint8_t power_restore_policy; /*------------------------------------------------------------------------------------ Exported Functions -------------------------------------------------------------------------------------*/ void fmc_spi_wright(void); void fmc_spi_dma_demo(void); void fmc_spi_read(void); +void power_restore_apply_on_boot(void); +void power_restore_policy_set(uint8_t policy); #ifdef __cplusplus } #endif diff --git a/project/example/ble/ble_peripheral/app/src/timeslice.c b/project/example/ble/ble_peripheral/app/src/timeslice.c index 8404a4d..3130f25 100644 --- a/project/example/ble/ble_peripheral/app/src/timeslice.c +++ b/project/example/ble/ble_peripheral/app/src/timeslice.c @@ -26,8 +26,8 @@ TASK_COMPONENTS TaskComps[] = {0,1,1,choice_mode}, {0,5,5,set_mode}, {0,1,1,Set_timing}, - {0,100,100,scan_433}, - {0, 100,3, Encoder_key}, + {0,10,10,scan_433}, + {0,3,3,Encoder_key}, {0,1,1,My_ADC_Get_Value}, {0,50,50,ble_state_sync_poll}, {0, 311, 1, app_op_flash}, //用户数据持久化 fmc_spi_read9 diff --git a/project/example/ble/ble_peripheral/app/src/uart.c b/project/example/ble/ble_peripheral/app/src/uart.c index 775f6df..bd3fda0 100644 --- a/project/example/ble/ble_peripheral/app/src/uart.c +++ b/project/example/ble/ble_peripheral/app/src/uart.c @@ -37,6 +37,7 @@ #include "timer.h" #include "math.h" #include "timeslice.h" +#include "fmc_spi.h" /*------------------------------------------------------------------------------------ Macros @@ -134,13 +135,11 @@ void scan_uart(uint8_t *arr) break; case 3: if(POWEROFF==deviceStatus)break; - if(Mode==mode0){ - 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]; - } + 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 4: if(POWEROFF==deviceStatus)break; @@ -172,6 +171,9 @@ void scan_uart(uint8_t *arr) flag_1h=flag_1h*60000; //flag_1h=2000; break; + case 12: + power_restore_policy_set(user_rx_buf[1]); + break; case 9: break; diff --git a/project/example/ble/ble_peripheral/app/src/usr_server.c b/project/example/ble/ble_peripheral/app/src/usr_server.c index 7f13c6d..31c2716 100644 --- a/project/example/ble/ble_peripheral/app/src/usr_server.c +++ b/project/example/ble/ble_peripheral/app/src/usr_server.c @@ -14,6 +14,7 @@ #include "usr_server.h" #include "Mode.h" #include "PWM.h" +#include "fmc_spi.h" #if (BLE_APP_PRESENT) uint8_t srv_user_lid = GATT_INVALID_USER_LID; @@ -91,7 +92,8 @@ uint8_t send_flag=1; #define BLE_STATE_DIRTY_BRIGHTNESS 0x04U #define BLE_STATE_DIRTY_SPEED 0x08U #define BLE_STATE_DIRTY_TEMPERATURE 0x10U -#define BLE_STATE_DIRTY_ALL 0x1FU +#define BLE_STATE_DIRTY_POWER_MODE 0x20U +#define BLE_STATE_DIRTY_ALL 0x3FU static uint8_t ble_state_dirty = BLE_STATE_DIRTY_ALL; static uint8_t ble_last_power; @@ -99,6 +101,7 @@ static uint8_t ble_last_mode; static uint8_t ble_last_brightness; static uint8_t ble_last_speed; static uint8_t ble_last_temperature; +static uint8_t ble_last_power_mode; static uint8_t ble_temperature_value(void) { @@ -132,6 +135,8 @@ void ble_state_sync_poll(void) ble_state_dirty |= BLE_STATE_DIRTY_SPEED; if(Mode == mode0 && ble_temperature_value() != ble_last_temperature) ble_state_dirty |= BLE_STATE_DIRTY_TEMPERATURE; + if(power_restore_policy != ble_last_power_mode) + ble_state_dirty |= BLE_STATE_DIRTY_POWER_MODE; if(!send_flag || !ble_state_dirty) return; @@ -159,12 +164,18 @@ void ble_state_sync_poll(void) data[3] = 0x05U; sent_mask = BLE_STATE_DIRTY_SPEED; } - else + else if(ble_state_dirty & BLE_STATE_DIRTY_TEMPERATURE) { value = ble_temperature_value(); data[3] = 0x06U; sent_mask = BLE_STATE_DIRTY_TEMPERATURE; } + else + { + value = power_restore_policy; + data[3] = 0x0CU; + sent_mask = BLE_STATE_DIRTY_POWER_MODE; + } data[0] = value; if(slave_send_data(data, sizeof(data), CUSTOM_SVC_TX_CHAR_VAL) == GATT_NO_ERROR) @@ -173,7 +184,8 @@ void ble_state_sync_poll(void) else if(sent_mask == BLE_STATE_DIRTY_MODE) ble_last_mode = value; else if(sent_mask == BLE_STATE_DIRTY_BRIGHTNESS) ble_last_brightness = value; else if(sent_mask == BLE_STATE_DIRTY_SPEED) ble_last_speed = value; - else ble_last_temperature = value; + else if(sent_mask == BLE_STATE_DIRTY_TEMPERATURE) ble_last_temperature = value; + else ble_last_power_mode = value; ble_state_dirty &= (uint8_t)(~sent_mask); } }