567 lines
14 KiB
C
567 lines
14 KiB
C
#include "RF433.h" // Device header
|
|
#include "xc_drv_gpio.h"
|
|
#include "uart.h"
|
|
#include "xc_drv_uart.h"
|
|
#include "mode.h"
|
|
#include "timer.h"
|
|
#include "light_transition.h"
|
|
#include "pwm.h"
|
|
#include "rf433_decoder.h"
|
|
union rf433_flag rf_flag = {0};
|
|
unsigned char high_level_time;
|
|
unsigned char low_level_time;
|
|
unsigned char receive_data_cnt;
|
|
uint32_t receive_temp;
|
|
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;
|
|
uint8_t is_time=0;
|
|
|
|
|
|
uint32_t adress=0;
|
|
uint32_t time_5s=1;
|
|
uint16_t long_press_cnt=0;
|
|
|
|
|
|
uint8_t adress_H=0;
|
|
uint8_t adress_L=0;
|
|
uint8_t adress_H_array[5]={0,0,0,0,0};
|
|
uint8_t adress_L_array[5]={0,0,0,0,0};
|
|
uint8_t match_success=0;
|
|
|
|
|
|
void Delay_50us(unsigned int n)
|
|
{
|
|
unsigned char i;
|
|
while(n--)
|
|
{
|
|
for(i=0;i<104;i++);
|
|
}
|
|
}
|
|
void Delay_ms(unsigned int n)
|
|
{
|
|
while(n--)
|
|
{
|
|
Delay_50us(20);
|
|
}
|
|
}
|
|
|
|
static uint8_t power=0;
|
|
void Lock_Pwm7xd(void){
|
|
power=1;
|
|
}
|
|
|
|
void rf433_gpio_init(){
|
|
GPIO_InitCfg_t GPIO_InitCfg = { 0 }; // 清零初始化配置结构体
|
|
GPIO_InitCfg.Mux = GPIO_Mux0; // 选择功能复用器0
|
|
GPIO_InitCfg.FunSel = GPIO_Dx; // 选择GPIO功能(非特殊功能)
|
|
GPIO_InitCfg.Pull = GPIO_PULLUP; // 上拉电阻使能
|
|
|
|
GPIO_InitCfg.Dir = GPIO_DIR_INPUT; // 输出方向
|
|
//GPIO_InitCfg.Dir =GPIO_DIR_OUTPUT;
|
|
GPIO_InitCfg.Int = NOT_INT; // 不使能中断
|
|
GPIO_InitCfg.Pin = RF433_PIN; // 设置LED1引脚
|
|
xc_gpio_init(&GPIO_InitCfg); // 初始化GPIO
|
|
|
|
// GPIO_InitCfg.Dir = GPIO_DIR_OUTPUT; // 输出方向
|
|
// GPIO_InitCfg.Int = NOT_INT; // 不使能中断
|
|
// GPIO_InitCfg.Pin = RF_dd; // 设置LED1引脚
|
|
// xc_gpio_init(&GPIO_InitCfg); // 初始化GPIO
|
|
}
|
|
|
|
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 uint32_t pair_alternate_data;
|
|
static uint8_t pair_candidate_valid;
|
|
static uint8_t pair_alternate_count;
|
|
|
|
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 pair_deferred_command;
|
|
static uint8_t pair_deferred_res_data;
|
|
static uint32_t pair_deferred_receive_data;
|
|
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)))
|
|
uint8_t long_flag_s=0;
|
|
|
|
void _433_fun(void);
|
|
|
|
// Keep paired addresses ordered from oldest to newest.
|
|
static uint8_t Pairing_Address_IsEmpty(uint8_t high, uint8_t low)
|
|
{
|
|
return ((high == 0U && low == 0U) ||
|
|
(high == 0xffU && low == 0xffU));
|
|
}
|
|
|
|
static uint8_t Pairing_Address_AddNew(uint8_t high, uint8_t low)
|
|
{
|
|
uint8_t next_h[5];
|
|
uint8_t next_l[5];
|
|
uint8_t count = 0U;
|
|
uint8_t i;
|
|
|
|
for(i = 0U; i < 5U; i++)
|
|
{
|
|
if(adress_H_array[i] == high && adress_L_array[i] == low)
|
|
{
|
|
return 0U;
|
|
}
|
|
}
|
|
|
|
for(i = 0U; i < 5U; i++)
|
|
{
|
|
if(Pairing_Address_IsEmpty(adress_H_array[i], adress_L_array[i])) continue;
|
|
|
|
next_h[count] = adress_H_array[i];
|
|
next_l[count] = adress_L_array[i];
|
|
count++;
|
|
}
|
|
|
|
if(count >= 5U)
|
|
{
|
|
for(i = 0U; i < 4U; i++)
|
|
{
|
|
next_h[i] = next_h[i + 1U];
|
|
next_l[i] = next_l[i + 1U];
|
|
}
|
|
count = 4U;
|
|
}
|
|
|
|
next_h[count] = high;
|
|
next_l[count] = low;
|
|
count++;
|
|
|
|
for(i = 0U; i < 5U; i++)
|
|
{
|
|
if(i < count)
|
|
{
|
|
adress_H_array[i] = next_h[i];
|
|
adress_L_array[i] = next_l[i];
|
|
}
|
|
else
|
|
{
|
|
adress_H_array[i] = 0xffU;
|
|
adress_L_array[i] = 0xffU;
|
|
}
|
|
}
|
|
|
|
return 1U;
|
|
}
|
|
static void Pairing_Feedback_Apply(uint8_t on)
|
|
{
|
|
Light_Transition_SetFeedback(1U, on);
|
|
}
|
|
|
|
static void Pairing_Feedback_Restore(void)
|
|
{
|
|
Light_Transition_SetFeedback(0U, 0U);
|
|
if(deviceStatus == POWERON)
|
|
{
|
|
Light_Transition_BeginModeChange();
|
|
startTimer(&timer1, 1U);
|
|
}
|
|
}
|
|
|
|
static void Pairing_Feedback_Start(uint16_t interval_ms)
|
|
{
|
|
if(pairing_feedback_active) return;
|
|
|
|
pair_deferred_command = 0U;
|
|
|
|
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 uint8_t Pairing_IsTriggerCommand(uint8_t command)
|
|
{
|
|
return command == Brightness_H || command == Brightness_L ||
|
|
command == B_Brightness_H || command == B_Brightness_L ||
|
|
command == F_Brightness_10 || command == F_Brightness_100;
|
|
}
|
|
|
|
static void Pairing_Replay_Deferred(void)
|
|
{
|
|
uint8_t current_res_data;
|
|
uint32_t current_receive_data;
|
|
|
|
if(!pair_deferred_command) return;
|
|
|
|
current_res_data = res_data;
|
|
current_receive_data = receive_data;
|
|
res_data = pair_deferred_res_data;
|
|
receive_data = pair_deferred_receive_data;
|
|
pair_deferred_command = 0U;
|
|
pair_command_replay = 1U;
|
|
_433_fun();
|
|
pair_command_replay = 0U;
|
|
res_data = current_res_data;
|
|
receive_data = current_receive_data;
|
|
}
|
|
|
|
static void Pairing_Candidate_Reset(void)
|
|
{
|
|
long_press_cnt = 0U;
|
|
pair_candidate_data = 0U;
|
|
pair_alternate_data = 0U;
|
|
pair_candidate_valid = 0U;
|
|
pair_alternate_count = 0U;
|
|
pair_frame_silence_ticks = 0U;
|
|
pair_deferred_command = 0U;
|
|
}
|
|
|
|
|
|
|
|
static void Encoder_key_NoCode(){
|
|
uint16_t times_s=0;
|
|
uint8_t pairing_data_changed=0U;
|
|
|
|
if(pair_frame_silence_ticks < 0xffU) pair_frame_silence_ticks++;
|
|
if(pair_frame_silence_ticks > PAIR_FRAME_SILENCE_TICKS)
|
|
{
|
|
if(pair_candidate_valid &&
|
|
long_press_cnt < PAIR_HOLD_FRAME_COUNT)
|
|
Pairing_Replay_Deferred();
|
|
Pairing_Candidate_Reset();
|
|
}
|
|
|
|
if(rf_frame_sequence == pair_last_frame_sequence) return;
|
|
pair_last_frame_sequence = rf_frame_sequence;
|
|
|
|
if(ssse != 0U || time_5s > PAIRING_WINDOW_TICKS) return;
|
|
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))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if(!pair_candidate_valid)
|
|
{
|
|
pair_candidate_data = receive_data;
|
|
pair_candidate_valid = 1U;
|
|
long_press_cnt = 1U;
|
|
pair_alternate_count = 0U;
|
|
pair_frame_silence_ticks = 0U;
|
|
}
|
|
else if(pair_candidate_data == receive_data)
|
|
{
|
|
long_press_cnt++;
|
|
pair_alternate_count = 0U;
|
|
pair_frame_silence_ticks = 0U;
|
|
}
|
|
else
|
|
{
|
|
if(pair_alternate_data == receive_data)
|
|
{
|
|
pair_alternate_count++;
|
|
}
|
|
else
|
|
{
|
|
pair_alternate_data = receive_data;
|
|
pair_alternate_count = 1U;
|
|
}
|
|
|
|
if(pair_alternate_count >= 2U)
|
|
{
|
|
pair_candidate_data = pair_alternate_data;
|
|
long_press_cnt = pair_alternate_count;
|
|
pair_alternate_count = 0U;
|
|
pair_frame_silence_ticks = 0U;
|
|
}
|
|
}
|
|
|
|
if(long_press_cnt>=PAIR_HOLD_FRAME_COUNT){
|
|
time_5s=PAIRING_WINDOW_TICKS + 1U;
|
|
ssse=1;
|
|
long_press_cnt=0;
|
|
pair_deferred_command=0U;
|
|
|
|
if(res_data==Brightness_H||res_data==B_Brightness_H||res_data==F_Brightness_10){
|
|
adress=receive_data >>8;
|
|
adress_H = (receive_data >> 16) & 0xff;
|
|
adress_L = (receive_data >> 8) & 0xff;
|
|
if(Pairing_Address_AddNew(adress_H, adress_L))
|
|
{
|
|
pairing_data_changed=1U;
|
|
}
|
|
else if(match_success==0U)
|
|
{
|
|
pairing_data_changed=1U;
|
|
}
|
|
times_s=300;
|
|
match_success=1;
|
|
Pairing_Feedback_Start(times_s);
|
|
}
|
|
else if(res_data==Brightness_L||res_data==B_Brightness_L||res_data==F_Brightness_100){
|
|
pairing_data_changed=1U;
|
|
adress_H_array[0] = 0xff;
|
|
adress_L_array[0] = 0xff;
|
|
adress_H_array[1] = 0xff;
|
|
adress_L_array[1] = 0xff;
|
|
adress_H_array[2] = 0xff;
|
|
adress_L_array[2] = 0xff;
|
|
adress_H_array[3] = 0xff;
|
|
adress_L_array[3] = 0xff;
|
|
adress_H_array[4] = 0xff;
|
|
adress_L_array[4] = 0xff;
|
|
match_success = 0;
|
|
times_s=200;
|
|
match_success = 0;
|
|
|
|
Pairing_Feedback_Start(times_s);
|
|
}
|
|
|
|
ssse=1;
|
|
if(pairing_data_changed) set_TaskComps_timer(2,2000);
|
|
}
|
|
}
|
|
|
|
|
|
static void Rf433_Decoder_Frame_Poll(void)
|
|
{
|
|
Rf433DecoderFrame_t frame;
|
|
|
|
if(!rf433_decoder_get_frame(&frame)) return;
|
|
|
|
receive_data = frame.code;
|
|
receive_temp = frame.code;
|
|
res_data = frame.key;
|
|
rf_frame_sequence++;
|
|
rf_flag.rf_receive_flag.receive_finish = 1U;
|
|
long_time = RF433_RELEASE_TIMEOUT_TICKS;
|
|
}
|
|
|
|
void Encoder_key(){
|
|
Rf433_Decoder_Frame_Poll();
|
|
Encoder_key_NoCode(); //一对一
|
|
Pairing_Feedback_Task();
|
|
}
|
|
|
|
|
|
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;
|
|
}
|
|
|
|
if(time_5s <= PAIRING_WINDOW_TICKS)
|
|
{
|
|
time_5s++;
|
|
}
|
|
}
|
|
uint16_t spee_dealy_time=0; //速度缓慢变换
|
|
|
|
#define REMOTE_MODE_MIN 0U
|
|
#define REMOTE_MODE_MAX EFFECT_MODE_MAX
|
|
|
|
void _433_fun(){
|
|
if(Light_Transition_FeedbackActive()) return;
|
|
if(!pair_command_replay && ssse == 0U &&
|
|
time_5s <= PAIRING_WINDOW_TICKS &&
|
|
Pairing_IsTriggerCommand(res_data))
|
|
{
|
|
pair_deferred_command = 1U;
|
|
pair_deferred_res_data = res_data;
|
|
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){
|
|
//椭圆
|
|
case ON://单击
|
|
if(deviceStatus==POWEROFF){
|
|
Start_PWM();
|
|
}
|
|
break;
|
|
case OFF:
|
|
if(deviceStatus==POWERON){
|
|
Stop_PWM();
|
|
}
|
|
break;
|
|
case Mode_add:
|
|
if(POWEROFF==deviceStatus)break;
|
|
if(choose_mode_falsg>REMOTE_MODE_MAX){
|
|
choose_mode_falsg=REMOTE_MODE_MIN;
|
|
}else if(choose_mode_falsg<REMOTE_MODE_MAX){
|
|
choose_mode_falsg++;
|
|
}else{
|
|
break;
|
|
}
|
|
choose_mode_bh=0;
|
|
startTimer(&timer1,1);
|
|
break;
|
|
case Mode_dow:
|
|
if(POWEROFF==deviceStatus)break;
|
|
if(choose_mode_falsg>REMOTE_MODE_MAX){
|
|
choose_mode_falsg=REMOTE_MODE_MIN;
|
|
}else if(choose_mode_falsg>REMOTE_MODE_MIN){
|
|
choose_mode_falsg--;
|
|
}else{
|
|
break;
|
|
}
|
|
choose_mode_bh=0;
|
|
startTimer(&timer1,1);
|
|
break;
|
|
case Brightness_L:
|
|
if(POWEROFF==deviceStatus)break;
|
|
if(Mode==mode0){
|
|
if(Brightness <=
|
|
(BRIGHTNESS_MIN_PERCENT + BRIGHTNESS_RF_STEP_PERCENT))
|
|
{
|
|
Brightness = BRIGHTNESS_MIN_PERCENT;
|
|
}
|
|
else
|
|
{
|
|
Brightness -= BRIGHTNESS_RF_STEP_PERCENT;
|
|
}
|
|
}else {
|
|
if(Speed<SPEED_MAX_LEVEL)Speed++;
|
|
}
|
|
break;
|
|
case Brightness_H:
|
|
if(POWEROFF==deviceStatus)break;
|
|
if(Mode==mode0){
|
|
if(Brightness >=
|
|
(BRIGHTNESS_MAX_PERCENT - BRIGHTNESS_RF_STEP_PERCENT))
|
|
{
|
|
Brightness = BRIGHTNESS_MAX_PERCENT;
|
|
}
|
|
else
|
|
{
|
|
Brightness += BRIGHTNESS_RF_STEP_PERCENT;
|
|
}
|
|
}else{
|
|
if(Speed>SPEED_MIN_LEVEL)Speed--;
|
|
}
|
|
break;
|
|
default:
|
|
if(POWEROFF==deviceStatus)break;
|
|
if(res_data>=16&&res_data<=255){
|
|
Mode=mode0;
|
|
choose_mode_bh=0;
|
|
choose_mode_falsg=CUSTOM_TEMPERATURE_MODE;//圆环模式
|
|
driveMode=0;
|
|
temperature((uint8_t)(((uint16_t)(255U - res_data) * 255U + 119U) / 239U));
|
|
startTimer(&timer1,1);
|
|
}
|
|
break;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
//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;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|