diff --git a/project/example/ble/ble_peripheral/app/src/RF433.h b/project/example/ble/ble_peripheral/app/src/RF433.h index 4b6edbd..543bcd8 100644 --- a/project/example/ble/ble_peripheral/app/src/RF433.h +++ b/project/example/ble/ble_peripheral/app/src/RF433.h @@ -100,7 +100,7 @@ #define RF_DATA xc_gpio_read_pin(RF433_PIN) //RF433接收引脚,根据具体单片机定义引脚 -#define RF433_PIN GPIO_5 //RF433接收引脚,根据具体单片机定义引脚 +#define RF433_PIN GPIO_18 //RF433接收引脚,与DWM11硬件配置一致 #define RF_dd GPIO_0 // @@ -188,7 +188,7 @@ void scan_433(); void Lock_Pwm7xd(); void Delay_50us(); void Encoder_key(); +void rf433_clear_code(void); #endif - diff --git a/project/example/ble/ble_peripheral/app/src/Rf433.c b/project/example/ble/ble_peripheral/app/src/Rf433.c index 25888eb..54e1e66 100644 --- a/project/example/ble/ble_peripheral/app/src/Rf433.c +++ b/project/example/ble/ble_peripheral/app/src/Rf433.c @@ -253,6 +253,27 @@ static void Pairing_Candidate_Reset(void) pair_deferred_command = 0U; } +void rf433_clear_code(void) +{ + uint8_t i; + + for(i = 0U; i < 5U; i++) + { + adress_H_array[i] = 0xffU; + adress_L_array[i] = 0xffU; + } + + adress = 0U; + adress_H = 0U; + adress_L = 0U; + match_success = 0U; + ssse = 1U; + time_5s = PAIRING_WINDOW_TICKS + 1U; + Pairing_Candidate_Reset(); + Pairing_Feedback_Start(200U); + set_TaskComps_timer(2U, 2000U); +} + static void Encoder_key_NoCode(){ @@ -564,4 +585,3 @@ void scan_433(){ - 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 88ad1df..77d407c 100644 --- a/project/example/ble/ble_peripheral/app/src/arch_main.c +++ b/project/example/ble/ble_peripheral/app/src/arch_main.c @@ -57,6 +57,7 @@ #include "mode.h" #include "RF433.h" #include "rf433_decoder.h" +#include "external_key.h" #include "rgblight.h" #include "timer.h" #include "fmc_spi.h" @@ -450,8 +451,11 @@ int main(void) #endif // gpio_pullup_input_inter_test(); adc_Init(); - rf433_decoder_init(); + external_key_init(); + /* Keep GPIO18 RF433 initialization last, after debug UART setup. */ rf433_gpio_init(); + /* Clear UART-to-GPIO transition pulses after GPIO18 is stable. */ + rf433_decoder_init(); while (1){ //DEBUG("222\r\n"); //printf("ddd=%d---%d---%d\r\n",W_PWM_duty,C_PWM_duty,(2*deadTime)); diff --git a/project/example/ble/ble_peripheral/app/src/external_key.c b/project/example/ble/ble_peripheral/app/src/external_key.c new file mode 100644 index 0000000..724e539 --- /dev/null +++ b/project/example/ble/ble_peripheral/app/src/external_key.c @@ -0,0 +1,278 @@ +#include "external_key.h" +#include "xc_drv_gpio.h" +#include "Mode.h" +#include "PWM.h" +#include "RF433.h" +#include "timer.h" +#include "timeslice.h" + +#define EXTERNAL_KEY_PIN GPIO_5 +#define EXTERNAL_KEY_PRESSED_LEVEL GPIO_PIN_RESET + +/* external_key_scan is called every 10 ms. */ +#define EXT_KEY_DEBOUNCE_TICKS 2U +#define EXT_KEY_DOUBLE_TICKS 15U +#define EXT_KEY_LONG_TICKS 40U +#define EXT_KEY_BRIGHTNESS_STEP_TICKS 5U +#define EXT_KEY_SPEED_STEP_TICKS 15U +#define EXT_KEY_CLEAR_CODE_TICKS 3000U + +#define EXT_KEY_BRIGHTNESS_STEP 3U +#define EXT_KEY_SPEED_STEP 1U +#define EXT_KEY_SAVE_DELAY_MS 1000U + +typedef enum +{ + EXT_KEY_IDLE = 0, + EXT_KEY_DOWN, + EXT_KEY_WAIT_SECOND, + EXT_KEY_SECOND_DOWN, + EXT_KEY_LONG +} EXT_KEY_STATE; + +static EXT_KEY_STATE ext_key_state = EXT_KEY_IDLE; +static uint16_t ext_key_press_ticks; +static uint16_t ext_key_wait_ticks; +static uint16_t ext_key_step_ticks; +static uint8_t ext_key_direction_up = 1U; +static uint8_t ext_key_adjust_speed; +static uint8_t ext_key_clear_triggered; + +static uint8_t external_key_is_pressed(void) +{ + return xc_gpio_read_pin(EXTERNAL_KEY_PIN) == + EXTERNAL_KEY_PRESSED_LEVEL; +} + +static void external_key_save_later(void) +{ + set_TaskComps_timer(2U, EXT_KEY_SAVE_DELAY_MS); +} + +static void external_key_power_toggle(void) +{ + if(deviceStatus == POWEROFF) + Start_PWM(); + else + Stop_PWM(); + + external_key_save_later(); +} + +static void external_key_mode_next(void) +{ + if(deviceStatus == POWEROFF) + Start_PWM(); + + if(choose_mode_falsg >= EFFECT_MODE_MAX) + choose_mode_falsg = EFFECT_MODE_MIN; + else + choose_mode_falsg++; + + choose_mode_bh = 0U; + startTimer(&timer1, 1U); + external_key_save_later(); +} + +static void external_key_brightness_step(void) +{ + if(ext_key_direction_up) + { + if(Brightness >= + (BRIGHTNESS_MAX_PERCENT - EXT_KEY_BRIGHTNESS_STEP)) + Brightness = BRIGHTNESS_MAX_PERCENT; + else + Brightness += EXT_KEY_BRIGHTNESS_STEP; + } + else + { + if(Brightness <= + (BRIGHTNESS_MIN_PERCENT + EXT_KEY_BRIGHTNESS_STEP)) + Brightness = BRIGHTNESS_MIN_PERCENT; + else + Brightness -= EXT_KEY_BRIGHTNESS_STEP; + } +} + +static void external_key_speed_step(void) +{ + /* + * A smaller Speed value is faster in the current effect tables. + * Keep the same direction convention used by DWM11. + */ + if(ext_key_direction_up) + { + if(Speed > SPEED_MIN_LEVEL) + Speed -= EXT_KEY_SPEED_STEP; + } + else + { + if(Speed < SPEED_MAX_LEVEL) + Speed += EXT_KEY_SPEED_STEP; + } +} + +static void external_key_adjust_step(void) +{ + if(deviceStatus == POWEROFF) + Start_PWM(); + + if(ext_key_adjust_speed) + external_key_speed_step(); + else + external_key_brightness_step(); +} + +static void external_key_long_start(void) +{ + ext_key_adjust_speed = (Mode == mode0) ? 0U : 1U; + ext_key_step_ticks = 0U; + external_key_adjust_step(); +} + +static void external_key_long_stop(void) +{ + if(ext_key_clear_triggered) + { + ext_key_clear_triggered = 0U; + ext_key_adjust_speed = 0U; + return; + } + + external_key_save_later(); + ext_key_direction_up = ext_key_direction_up ? 0U : 1U; + ext_key_adjust_speed = 0U; +} + +void external_key_init(void) +{ + GPIO_InitCfg_t gpio_cfg = {0}; + + gpio_cfg.Mux = GPIO_Mux0; + gpio_cfg.FunSel = GPIO_Dx; + gpio_cfg.Pull = GPIO_PULLUP; + gpio_cfg.Dir = GPIO_DIR_INPUT; + gpio_cfg.Int = NOT_INT; + gpio_cfg.Pin = EXTERNAL_KEY_PIN; + xc_gpio_init(&gpio_cfg); +} + +void external_key_scan(void) +{ + uint8_t pressed = external_key_is_pressed(); + uint16_t step_period; + + switch(ext_key_state) + { + case EXT_KEY_IDLE: + if(pressed) + { + ext_key_press_ticks = 1U; + ext_key_step_ticks = 0U; + ext_key_clear_triggered = 0U; + ext_key_state = EXT_KEY_DOWN; + } + break; + + case EXT_KEY_DOWN: + if(pressed) + { + if(ext_key_press_ticks < 0xFFFFU) + ext_key_press_ticks++; + + if(ext_key_press_ticks >= EXT_KEY_LONG_TICKS) + { + external_key_long_start(); + ext_key_state = EXT_KEY_LONG; + } + } + else + { + if(ext_key_press_ticks >= EXT_KEY_DEBOUNCE_TICKS) + { + ext_key_wait_ticks = 0U; + ext_key_state = EXT_KEY_WAIT_SECOND; + } + else + { + ext_key_state = EXT_KEY_IDLE; + } + } + break; + + case EXT_KEY_WAIT_SECOND: + if(pressed) + { + ext_key_press_ticks = 1U; + ext_key_step_ticks = 0U; + ext_key_clear_triggered = 0U; + ext_key_state = EXT_KEY_SECOND_DOWN; + } + else + { + ext_key_wait_ticks++; + if(ext_key_wait_ticks >= EXT_KEY_DOUBLE_TICKS) + { + external_key_power_toggle(); + ext_key_state = EXT_KEY_IDLE; + } + } + break; + + case EXT_KEY_SECOND_DOWN: + if(pressed) + { + if(ext_key_press_ticks < 0xFFFFU) + ext_key_press_ticks++; + + if(ext_key_press_ticks >= EXT_KEY_LONG_TICKS) + { + external_key_long_start(); + ext_key_state = EXT_KEY_LONG; + } + } + else + { + if(ext_key_press_ticks >= EXT_KEY_DEBOUNCE_TICKS) + external_key_mode_next(); + ext_key_state = EXT_KEY_IDLE; + } + break; + + case EXT_KEY_LONG: + if(pressed) + { + if(ext_key_press_ticks < 0xFFFFU) + ext_key_press_ticks++; + + if(!ext_key_clear_triggered && + ext_key_press_ticks >= EXT_KEY_CLEAR_CODE_TICKS) + { + ext_key_clear_triggered = 1U; + rf433_clear_code(); + } + else if(!ext_key_clear_triggered) + { + step_period = ext_key_adjust_speed ? + EXT_KEY_SPEED_STEP_TICKS : + EXT_KEY_BRIGHTNESS_STEP_TICKS; + ext_key_step_ticks++; + if(ext_key_step_ticks >= step_period) + { + ext_key_step_ticks = 0U; + external_key_adjust_step(); + } + } + } + else + { + external_key_long_stop(); + ext_key_state = EXT_KEY_IDLE; + } + break; + + default: + ext_key_state = EXT_KEY_IDLE; + break; + } +} diff --git a/project/example/ble/ble_peripheral/app/src/external_key.h b/project/example/ble/ble_peripheral/app/src/external_key.h new file mode 100644 index 0000000..b978997 --- /dev/null +++ b/project/example/ble/ble_peripheral/app/src/external_key.h @@ -0,0 +1,7 @@ +#ifndef _EXTERNAL_KEY_H_ +#define _EXTERNAL_KEY_H_ + +void external_key_init(void); +void external_key_scan(void); + +#endif diff --git a/project/example/ble/ble_peripheral/app/src/rf433_decoder.c b/project/example/ble/ble_peripheral/app/src/rf433_decoder.c index 6c420d7..79b90f0 100644 --- a/project/example/ble/ble_peripheral/app/src/rf433_decoder.c +++ b/project/example/ble/ble_peripheral/app/src/rf433_decoder.c @@ -1,657 +1,179 @@ #include "rf433_decoder.h" #include "rf433_decoder_port.h" -#define RF433_FIFO_SIZE 256u +/* + * DWM11 fixed-width 24-bit RF433 decoder. + * + * The input is sampled every 100 us. A complete pulse pair is evaluated on + * each rising edge, exactly like the proven DWM11 implementation. + */ +#define RF433_SYNC_HIGH_MIN_EXCLUSIVE 1u +#define RF433_SYNC_HIGH_MAX_EXCLUSIVE 7u +#define RF433_SYNC_LOW_MIN_EXCLUSIVE 115u +#define RF433_SYNC_LOW_MAX_EXCLUSIVE 130u -#define RF433_SYNC_LOW_MIN 35u //45鏀35 -#define RF433_SYNC_LOW_MAX 200u //180鏀200 -#define RF433_SYNC_HIGH_MIN 1u -#define RF433_SYNC_HIGH_MAX 15u //12鏀15 +#define RF433_ZERO_HIGH_MIN 1u +#define RF433_ZERO_HIGH_MAX 7u +#define RF433_ZERO_LOW_MIN 7u +#define RF433_ZERO_LOW_MAX 16u -#define RF433_BIT_LEN 24u -#define RF433_DIRECT_CONFIRM 1u -#define RF433_WEAK_CONFIRM 2u +#define RF433_ONE_HIGH_MIN 7u +#define RF433_ONE_HIGH_MAX 16u +#define RF433_ONE_LOW_MIN 2u +#define RF433_ONE_LOW_MAX 7u -#define RF433_LEVEL_TIMEOUT 300u //300 - -#define RF433_BIT_TOTAL_MIN 3u //4鏀3 -#define RF433_BIT_TOTAL_MAX 40u //35鏀40 - -#define RF433_PROCESS_MAX_ONCE 32u - -#define RF433_FRAME_SCORE_DIRECT_MIN 50u -#define RF433_FRAME_SCORE_WEAK_MIN 35u -#define RF433_BIT_SCORE_GOOD 4u -#define RF433_BIT_SCORE_NORMAL 3u -#define RF433_BIT_SCORE_POOR 1u - -#define RF433_WINDOW_TIGHT 0u -#define RF433_WINDOW_NORMAL 1u -#define RF433_WINDOW_LOOSE 2u - - -// 鏂板锛氳繛鍙戝睆钄介棿闅旓紙鍗曚綅锛100us閲囨牱tick锛200=20ms鍐呯浉鍚岀爜鍙緭鍑轰竴娆★級 -#define RF433_ANTI_REPEAT_TICK 200u - - -#if RF433_DECODER_DEBUG_DETAIL -#define RF433_DETAIL_LOG(fmt, ...) RF433_DECODER_LOG(fmt, ##__VA_ARGS__) -#else -#define RF433_DETAIL_LOG(fmt, ...) -#endif - -typedef enum -{ - RF433_STATE_IDLE = 0, - RF433_STATE_DATA, -} Rf433State_t; +#define RF433_BIT_LEN 24u typedef struct { - uint8_t level; - uint16_t width; -} Rf433Pulse_t; - -typedef struct -{ - volatile Rf433Pulse_t fifo[RF433_FIFO_SIZE]; - volatile uint16_t w; - volatile uint16_t r; - volatile uint16_t lost; - uint8_t last_level; - uint16_t width; - uint8_t inited; -} Rf433Fifo_t; - -typedef struct -{ - Rf433State_t state; - uint16_t last_high; - uint16_t last_low; - uint16_t t; - uint16_t short_min; - uint16_t short_max; - uint16_t long_min; - uint16_t long_max; - uint32_t data; - uint8_t bit_cnt; - uint8_t bit_score_sum; - uint8_t frame_score; - uint8_t ppl_locked; - uint8_t window_mode; - uint32_t last_code; - uint8_t same_cnt; + volatile uint16_t high_ticks; + volatile uint16_t low_ticks; + volatile uint8_t current_level; + volatile uint8_t sync_found; + volatile uint8_t bit_count; + volatile uint32_t data; + volatile uint8_t frame_ready; Rf433DecoderFrame_t frame; - bool frame_ready; - - -} Rf433Decode_t; +} Rf433Dwm11Decoder_t; -static Rf433Fifo_t s_fifo; -static Rf433Decode_t s_dec; +static Rf433Dwm11Decoder_t s_dec; -static uint16_t rf433_limit_t(uint16_t t) +static void rf433_increment_saturated(volatile uint16_t *value) { - if (t < 2u) + if (*value < 0xffffu) { - t = 2u; - } - - if (t > 12u) - { - t = 12u; - } - - return t; -} - -static void rf433_update_range_by_t(uint16_t t) -{ - t = rf433_limit_t(t); - s_dec.t = t; - - if (s_dec.window_mode == RF433_WINDOW_TIGHT) - { - s_dec.short_min = 1u; - s_dec.short_max = (uint16_t)(t + t / 2u + 2u); - s_dec.long_min = (uint16_t)(t * 2u); - s_dec.long_max = (uint16_t)(t * 4u + 2u); - } - else if (s_dec.window_mode == RF433_WINDOW_LOOSE) - { - s_dec.short_min = 1u; - s_dec.short_max = (uint16_t)(t * 2u + 4u); - s_dec.long_min = (uint16_t)(t + t / 2u); - if (s_dec.long_min < 3u) - { - s_dec.long_min = 3u; - } - s_dec.long_max = (uint16_t)(t * 7u + 4u); - } - else - { - s_dec.short_min = 1u; - s_dec.short_max = (uint16_t)(t * 2u + 3u); - s_dec.long_min = (uint16_t)(t + t / 2u); - if (s_dec.long_min < 3u) - { - s_dec.long_min = 3u; - } - s_dec.long_max = (uint16_t)(t * 7u + 3u); + (*value)++; } } -static void rf433_decode_reset(void) +static uint8_t rf433_in_range(uint16_t value, uint16_t min, uint16_t max) { - s_dec.state = RF433_STATE_IDLE; - s_dec.last_high = 0; - s_dec.last_low = 0; - s_dec.data = 0; - s_dec.bit_cnt = 0; - s_dec.bit_score_sum = 0; - s_dec.frame_score = 0; - s_dec.window_mode = RF433_WINDOW_NORMAL; - rf433_update_range_by_t(4u); + return (value >= min && value <= max) ? 1u : 0u; } -static void rf433_debug_reset(const char *reason, uint16_t high, uint16_t low) +static uint8_t rf433_is_sync(uint16_t high, uint16_t low) { - RF433_DETAIL_LOG("[RF433 RESET] %s bits=%d high=%d low=%d data=0x%06X T=%d win=%d lost=%d\r\n", - reason, - s_dec.bit_cnt, - high, - low, - (unsigned int)s_dec.data, - s_dec.t, - s_dec.window_mode, - s_fifo.lost); + return (high > RF433_SYNC_HIGH_MIN_EXCLUSIVE && + high < RF433_SYNC_HIGH_MAX_EXCLUSIVE && + low > RF433_SYNC_LOW_MIN_EXCLUSIVE && + low < RF433_SYNC_LOW_MAX_EXCLUSIVE) ? 1u : 0u; +} + +static void rf433_publish_frame(uint32_t code) +{ + s_dec.frame.code = code; + s_dec.frame.bit_len = RF433_BIT_LEN; + s_dec.frame.addr_h = (uint8_t)((code >> 16) & 0xffu); + s_dec.frame.addr_l = (uint8_t)((code >> 8) & 0xffu); + s_dec.frame.key = (uint8_t)(code & 0xffu); + s_dec.frame.score = 100u; + s_dec.frame_ready = 1u; + + RF433_DECODER_LOG("[RF433 DWM11 OK] code=0x%06X key=0x%02X\r\n", + (unsigned int)code, + s_dec.frame.key); +} + +static void rf433_decode_bit(uint8_t bit) +{ + s_dec.data <<= 1; + if (bit != 0u) + { + s_dec.data |= 1u; + } + + s_dec.bit_count++; + if (s_dec.bit_count >= RF433_BIT_LEN) + { + rf433_publish_frame(s_dec.data); + s_dec.sync_found = 0u; + s_dec.bit_count = 0u; + } } void rf433_decoder_init(void) { - s_fifo.w = 0; - s_fifo.r = 0; - s_fifo.lost = 0; - s_fifo.last_level = 0; - s_fifo.width = 0; - s_fifo.inited = 0; - - s_dec.last_code = 0; - s_dec.same_cnt = 0; - s_dec.frame_ready = false; - s_dec.ppl_locked = 0; - s_dec.window_mode = RF433_WINDOW_NORMAL; - - - rf433_decode_reset(); + s_dec.high_ticks = 0u; + s_dec.low_ticks = 0u; + s_dec.current_level = 0u; + s_dec.sync_found = 0u; + s_dec.bit_count = 0u; + s_dec.data = 0u; + s_dec.frame_ready = 0u; } void rf433_decoder_sample(uint8_t level) { - uint16_t next; - Rf433Pulse_t pulse; + uint16_t high; + uint16_t low; level = level ? 1u : 0u; - if (!s_fifo.inited) + if (level == 0u) { - s_fifo.last_level = level; - s_fifo.width = 1; - s_fifo.inited = 1; + rf433_increment_saturated(&s_dec.low_ticks); + s_dec.current_level = 0u; return; } - if (level == s_fifo.last_level) + rf433_increment_saturated(&s_dec.high_ticks); + + if (s_dec.current_level == 0u) { - if (s_fifo.width < 0xFFFFu) + high = s_dec.high_ticks; + low = s_dec.low_ticks; + + if (rf433_is_sync(high, low)) { - s_fifo.width++; + s_dec.sync_found = 1u; + s_dec.bit_count = 0u; + s_dec.data = 0u; } - return; - } - - pulse.level = s_fifo.last_level; - pulse.width = s_fifo.width; - s_fifo.last_level = level; - s_fifo.width = 1; - - next = (uint16_t)((s_fifo.w + 1u) % RF433_FIFO_SIZE); - if (next != s_fifo.r) - { - s_fifo.fifo[s_fifo.w] = pulse; - s_fifo.w = next; - } - else if (s_fifo.lost < 0xFFFFu) - { - s_fifo.lost++; - } -} - -static bool rf433_fifo_pop(Rf433Pulse_t *pulse) -{ - if (pulse == 0 || s_fifo.r == s_fifo.w) - { - return false; - } - - *pulse = s_fifo.fifo[s_fifo.r]; - s_fifo.r = (uint16_t)((s_fifo.r + 1u) % RF433_FIFO_SIZE); - return true; -} - -static bool rf433_is_sync(uint16_t high, uint16_t low) -{ - if (high == 0u || low == 0u) - { - return false; - } - - if (high < RF433_SYNC_HIGH_MIN || high > RF433_SYNC_HIGH_MAX) - { - return false; - } - - if (low < RF433_SYNC_LOW_MIN || low > RF433_SYNC_LOW_MAX) - { - return false; - } - - if (low < (uint16_t)(high * 10u)) //12 - { - return false; - } - - return true; -} - -static void rf433_ppl_update_by_sync(uint16_t sync_low) -{ - uint16_t t_new = rf433_limit_t((uint16_t)(sync_low / 31u)); - uint16_t t_filter; - - if (s_dec.ppl_locked) - { - t_filter = (uint16_t)((s_dec.t * 7u + t_new) / 8u); - } - else - { - t_filter = (uint16_t)((s_dec.t * 3u + t_new) / 4u); - } - - rf433_update_range_by_t(t_filter); -} - -static void rf433_update_t_by_bit(uint16_t high, uint16_t low, uint8_t bit) -{ - uint16_t t_new; - uint16_t t_filter; - - if (bit == 0u) - { - t_new = (uint16_t)((high + low / 3u) / 2u); - } - else - { - t_new = (uint16_t)((high / 3u + low) / 2u); - } - - t_new = rf433_limit_t(t_new); - - if (s_dec.ppl_locked) - { - t_filter = (uint16_t)((s_dec.t * 7u + t_new) / 8u); - } - else - { - t_filter = (uint16_t)((s_dec.t * 3u + t_new) / 4u); - } - - rf433_update_range_by_t(t_filter); -} - -static bool rf433_is_short(uint16_t width) -{ - return ((width >= s_dec.short_min) && (width <= s_dec.short_max)); -} - -static bool rf433_is_long(uint16_t width) -{ - return ((width >= s_dec.long_min) && (width <= s_dec.long_max)); -} - -static bool rf433_total_valid(uint16_t high, uint16_t low) -{ - uint16_t total = (uint16_t)(high + low); - return ((total >= RF433_BIT_TOTAL_MIN) && (total <= RF433_BIT_TOTAL_MAX)); -} - -static bool rf433_decode_bit(uint16_t high, uint16_t low, uint8_t *bit) -{ - if (bit == 0 || !rf433_total_valid(high, low)) - { - return false; - } - - if (rf433_is_short(high) && rf433_is_long(low)) - { - *bit = 0; - return true; - } - - if (rf433_is_long(high) && rf433_is_short(low)) - { - *bit = 1; - return true; - } - - return false; -} - -#if RF433_DECODER_DEBUG_DETAIL -static const char *rf433_bit_fail_reason(uint16_t high, uint16_t low) -{ - if (!rf433_total_valid(high, low)) - { - return "total"; - } - - if (!rf433_is_short(high) && !rf433_is_long(high)) - { - return "high"; - } - - if (!rf433_is_short(low) && !rf433_is_long(low)) - { - return "low"; - } - - return "pair"; -} -#endif - -static uint16_t rf433_abs_diff_u16(uint16_t a, uint16_t b) -{ - return (a > b) ? (uint16_t)(a - b) : (uint16_t)(b - a); -} - -static uint8_t rf433_score_bit(uint16_t high, uint16_t low, uint8_t bit) -{ - uint16_t ideal_high; - uint16_t ideal_low; - uint16_t err; - - if (bit == 0u) - { - ideal_high = s_dec.t; - ideal_low = (uint16_t)(s_dec.t * 3u); - } - else - { - ideal_high = (uint16_t)(s_dec.t * 3u); - ideal_low = s_dec.t; - } - - err = (uint16_t)(rf433_abs_diff_u16(high, ideal_high) + - rf433_abs_diff_u16(low, ideal_low)); - - if (err <= 2u) - { - return RF433_BIT_SCORE_GOOD; - } - - if (err <= 5u) - { - return RF433_BIT_SCORE_NORMAL; - } - - return RF433_BIT_SCORE_POOR; -} - -static uint8_t rf433_calc_frame_score(void) -{ - uint16_t score; - - if (s_dec.bit_cnt == 0u) - { - return 0u; - } - - score = (uint16_t)s_dec.bit_score_sum * 100u; - score = score / (uint16_t)(RF433_BIT_LEN * RF433_BIT_SCORE_GOOD); - return (score > 100u) ? 100u : (uint8_t)score; -} - -static void rf433_update_window_by_score(uint8_t score) -{ - if (score >= 90u) - { - s_dec.window_mode = RF433_WINDOW_TIGHT; - s_dec.ppl_locked = 1u; - } - else if (score >= 60u) - { - s_dec.window_mode = RF433_WINDOW_NORMAL; - } - else - { - s_dec.window_mode = RF433_WINDOW_LOOSE; - s_dec.ppl_locked = 0u; - } - - rf433_update_range_by_t(s_dec.t); -} - -static void rf433_output_code(uint32_t code, uint8_t required_count) -{ - if (code == s_dec.last_code) - { - if (s_dec.same_cnt < 255u) + else if (s_dec.sync_found != 0u) { - s_dec.same_cnt++; - } - } - else - { - s_dec.last_code = code; - s_dec.same_cnt = 1; - - - } - - RF433_DETAIL_LOG("[RF433 REPEAT] code=0x%06X same=%d/%d\r\n", - (unsigned int)code, - s_dec.same_cnt, - required_count); - - if (s_dec.same_cnt >= required_count) - { - s_dec.frame.code = code; - s_dec.frame.bit_len = RF433_BIT_LEN; - s_dec.frame.addr_h = (uint8_t)((code >> 16) & 0xFFu); - s_dec.frame.addr_l = (uint8_t)((code >> 8) & 0xFFu); - s_dec.frame.key = (uint8_t)(code & 0xFFu); - s_dec.frame.score = s_dec.frame_score; - s_dec.frame_ready = true; - - - - s_dec.same_cnt = 0; - - RF433_DECODER_LOG("[RF433 OK] code=0x%06X addr_h=0x%02X addr_l=0x%02X key=0x%02X score=%d\r\n", - (unsigned int)s_dec.frame.code, - s_dec.frame.addr_h, - s_dec.frame.addr_l, - s_dec.frame.key, - s_dec.frame.score); - } -} - -static void rf433_process_clean_pulse(Rf433Pulse_t pulse) -{ - uint8_t bit; - uint8_t bit_score; - - if (pulse.width == 0u) - { - return; - } - - if (pulse.width > RF433_LEVEL_TIMEOUT) - { - rf433_debug_reset("timeout", s_dec.last_high, pulse.width); - rf433_decode_reset(); - return; - } - - if (pulse.level) - { - s_dec.last_high = pulse.width; - return; - } - - s_dec.last_low = pulse.width; - - if (rf433_is_sync(s_dec.last_high, s_dec.last_low)) - { - s_dec.window_mode = s_dec.ppl_locked ? RF433_WINDOW_TIGHT : RF433_WINDOW_NORMAL; - rf433_ppl_update_by_sync(s_dec.last_low); - - RF433_DETAIL_LOG("[RF433 SYNC] high=%d low=%d T=%d short=%d-%d long=%d-%d win=%d lock=%d\r\n", - s_dec.last_high, - s_dec.last_low, - s_dec.t, - s_dec.short_min, - s_dec.short_max, - s_dec.long_min, - s_dec.long_max, - s_dec.window_mode, - s_dec.ppl_locked); - - s_dec.state = RF433_STATE_DATA; - s_dec.data = 0; - s_dec.bit_cnt = 0; - s_dec.bit_score_sum = 0; - s_dec.frame_score = 0; - s_dec.last_high = 0; - s_dec.last_low = 0; - return; - } - - if (s_dec.state != RF433_STATE_DATA || - s_dec.last_high == 0u || - s_dec.last_low == 0u) - { - return; - } - - if (rf433_decode_bit(s_dec.last_high, s_dec.last_low, &bit)) - { - bit_score = rf433_score_bit(s_dec.last_high, s_dec.last_low, bit); - if (s_dec.bit_score_sum <= (uint8_t)(255u - bit_score)) - { - s_dec.bit_score_sum += bit_score; - } - - rf433_update_t_by_bit(s_dec.last_high, s_dec.last_low, bit); - s_dec.data <<= 1; - if (bit) - { - s_dec.data |= 1u; - } - s_dec.bit_cnt++; - - RF433_DETAIL_LOG("[RF433 BIT] idx=%02d bit=%d high=%d low=%d score=%d T=%d data=0x%06X\r\n", - s_dec.bit_cnt, - bit, - s_dec.last_high, - s_dec.last_low, - bit_score, - s_dec.t, - (unsigned int)s_dec.data); - - if (s_dec.bit_cnt >= RF433_BIT_LEN) - { - s_dec.frame_score = rf433_calc_frame_score(); - rf433_update_window_by_score(s_dec.frame_score); - - RF433_DECODER_LOG("[RF433 FRAME] score=%3d T=%2d win=%d lock=%d bits=%2d code=0x%06X addr_h=0x%02X addr_l=0x%02X key=0x%02X\r\n", - s_dec.frame_score, - s_dec.t, - s_dec.window_mode, - s_dec.ppl_locked, - s_dec.bit_cnt, - (unsigned int)s_dec.data, - (unsigned int)((s_dec.data >> 16) & 0xFFu), - (unsigned int)((s_dec.data >> 8) & 0xFFu), - (unsigned int)(s_dec.data & 0xFFu)); - - if (s_dec.frame_score >= RF433_FRAME_SCORE_DIRECT_MIN) + if (rf433_in_range(high, RF433_ZERO_HIGH_MIN, RF433_ZERO_HIGH_MAX) && + rf433_in_range(low, RF433_ZERO_LOW_MIN, RF433_ZERO_LOW_MAX)) { - rf433_output_code(s_dec.data, RF433_DIRECT_CONFIRM); + rf433_decode_bit(0u); } - else if (s_dec.frame_score >= RF433_FRAME_SCORE_WEAK_MIN) + else if (rf433_in_range(high, RF433_ONE_HIGH_MIN, RF433_ONE_HIGH_MAX) && + rf433_in_range(low, RF433_ONE_LOW_MIN, RF433_ONE_LOW_MAX)) { - rf433_output_code(s_dec.data, RF433_WEAK_CONFIRM); + rf433_decode_bit(1u); } else { - RF433_DECODER_LOG("[RF433 BAD] score=%d code=0x%06X\r\n", - s_dec.frame_score, - (unsigned int)s_dec.data); + s_dec.sync_found = 0u; + s_dec.bit_count = 0u; + s_dec.data = 0u; } - - rf433_decode_reset(); } + + s_dec.low_ticks = 0u; + s_dec.high_ticks = 1u; } - else - { -#if RF433_DECODER_DEBUG_DETAIL - RF433_DETAIL_LOG("[RF433 BIT_ERR] reason=%s idx=%02d high=%d low=%d total=%d T=%d short=%d-%d long=%d-%d data=0x%06X\r\n", - rf433_bit_fail_reason(s_dec.last_high, s_dec.last_low), - (uint8_t)(s_dec.bit_cnt + 1u), - s_dec.last_high, - s_dec.last_low, - (uint16_t)(s_dec.last_high + s_dec.last_low), - s_dec.t, - s_dec.short_min, - s_dec.short_max, - s_dec.long_min, - s_dec.long_max, - (unsigned int)s_dec.data); -#endif - rf433_debug_reset("bit", s_dec.last_high, s_dec.last_low); - rf433_decode_reset(); - } + + s_dec.current_level = 1u; } void rf433_decoder_process(void) { - Rf433Pulse_t pulse; - uint8_t cnt = 0; - - - - - while ((cnt < RF433_PROCESS_MAX_ONCE) && rf433_fifo_pop(&pulse)) - { - rf433_process_clean_pulse(pulse); - cnt++; - } + /* DWM11 decodes directly in the 100 us sampling callback. */ } bool rf433_decoder_get_frame(Rf433DecoderFrame_t *frame) { - if (frame == 0 || !s_dec.frame_ready) + if (frame == 0 || s_dec.frame_ready == 0u) { return false; } *frame = s_dec.frame; - s_dec.frame_ready = false; + s_dec.frame_ready = 0u; return true; } uint16_t rf433_decoder_get_lost_count(void) { - return s_fifo.lost; + return 0u; } diff --git a/project/example/ble/ble_peripheral/app/src/timeslice.c b/project/example/ble/ble_peripheral/app/src/timeslice.c index 8404a4d..12ace5a 100644 --- a/project/example/ble/ble_peripheral/app/src/timeslice.c +++ b/project/example/ble/ble_peripheral/app/src/timeslice.c @@ -13,8 +13,9 @@ #include "ota_flash_interface.h" #include "ota_protocol.h" #include "pwm.h" -#include "light_transition.h" +#include "light_transition.h" #include "usr_server.h" +#include "external_key.h" /*********************************************************************************************************************/ void rwip_schedule(void); @@ -33,6 +34,7 @@ TASK_COMPONENTS TaskComps[] = {0, 311, 1, app_op_flash}, //用户数据持久化 fmc_spi_read9 {0, 311, 1, xc_ota_schedule}, //保存flash数据任务 8 //最后调用的 {0, 1, 1, rf433_decoder_process}, + {0, 10, 10, external_key_scan}, //GPIO5 external key scan }; /************************************************************************************** diff --git a/project/example/ble/ble_peripheral/mdk/ble_peripheral.uvprojx b/project/example/ble/ble_peripheral/mdk/ble_peripheral.uvprojx index 011f4f2..f47d3a3 100644 --- a/project/example/ble/ble_peripheral/mdk/ble_peripheral.uvprojx +++ b/project/example/ble/ble_peripheral/mdk/ble_peripheral.uvprojx @@ -749,6 +749,11 @@ 1 ..\app\src\light_transition.c + + external_key.c + 1 + ..\app\src\external_key.c + fmc_spi.c 1 @@ -1552,6 +1557,11 @@ 1 ..\app\src\light_transition.c + + external_key.c + 1 + ..\app\src\external_key.c + fmc_spi.c 1