增加动态亮度长按及断电切换

This commit is contained in:
2026-07-28 15:43:13 +08:00
parent 4aa84e178e
commit cb7fe7f041
7 changed files with 193 additions and 85 deletions
@@ -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;
}
}