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 9973aa1..6c420d7 100644 --- a/project/example/ble/ble_peripheral/app/src/rf433_decoder.c +++ b/project/example/ble/ble_peripheral/app/src/rf433_decoder.c @@ -3,16 +3,14 @@ #define RF433_FIFO_SIZE 256u -#define RF433_GLITCH_DYNAMIC_MIN 2u -#define RF433_GLITCH_DYNAMIC_MAX 4u - #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_BIT_LEN 24u -#define RF433_REPEAT_CONFIRM 1u //2改1 +#define RF433_DIRECT_CONFIRM 1u +#define RF433_WEAK_CONFIRM 2u #define RF433_LEVEL_TIMEOUT 300u //300 @@ -21,7 +19,8 @@ #define RF433_PROCESS_MAX_ONCE 32u -#define RF433_FRAME_SCORE_MIN 50u //70改50 +#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 @@ -35,12 +34,6 @@ #define RF433_ANTI_REPEAT_TICK 200u -#if RF433_DECODER_DEBUG_PULSE -#define RF433_PULSE_LOG(fmt, ...) RF433_DECODER_LOG(fmt, ##__VA_ARGS__) -#else -#define RF433_PULSE_LOG(fmt, ...) -#endif - #if RF433_DECODER_DEBUG_DETAIL #define RF433_DETAIL_LOG(fmt, ...) RF433_DECODER_LOG(fmt, ##__VA_ARGS__) #else @@ -94,17 +87,8 @@ typedef struct } Rf433Decode_t; -typedef struct -{ - Rf433Pulse_t p0; - Rf433Pulse_t p1; - Rf433Pulse_t p2; - uint8_t cnt; -} Rf433Repair_t; - static Rf433Fifo_t s_fifo; static Rf433Decode_t s_dec; -static Rf433Repair_t s_repair; static uint16_t rf433_limit_t(uint16_t t) { @@ -170,17 +154,6 @@ static void rf433_decode_reset(void) rf433_update_range_by_t(4u); } -static void rf433_repair_reset(void) -{ - s_repair.cnt = 0; - s_repair.p0.level = 0; - s_repair.p0.width = 0; - s_repair.p1.level = 0; - s_repair.p1.width = 0; - s_repair.p2.level = 0; - s_repair.p2.width = 0; -} - static void rf433_debug_reset(const char *reason, 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", @@ -211,7 +184,6 @@ void rf433_decoder_init(void) rf433_decode_reset(); - rf433_repair_reset(); } void rf433_decoder_sample(uint8_t level) @@ -337,23 +309,6 @@ static void rf433_update_t_by_bit(uint16_t high, uint16_t low, uint8_t bit) rf433_update_range_by_t(t_filter); } -static uint16_t rf433_get_glitch_max(void) -{ - uint16_t v = s_dec.t / 2u; - - if (v < RF433_GLITCH_DYNAMIC_MIN) - { - v = RF433_GLITCH_DYNAMIC_MIN; - } - - if (v > RF433_GLITCH_DYNAMIC_MAX) - { - v = RF433_GLITCH_DYNAMIC_MAX; - } - - return v; -} - static bool rf433_is_short(uint16_t width) { return ((width >= s_dec.short_min) && (width <= s_dec.short_max)); @@ -486,7 +441,7 @@ static void rf433_update_window_by_score(uint8_t score) rf433_update_range_by_t(s_dec.t); } -static void rf433_output_code(uint32_t code) +static void rf433_output_code(uint32_t code, uint8_t required_count) { if (code == s_dec.last_code) { @@ -506,9 +461,9 @@ static void rf433_output_code(uint32_t code) RF433_DETAIL_LOG("[RF433 REPEAT] code=0x%06X same=%d/%d\r\n", (unsigned int)code, s_dec.same_cnt, - RF433_REPEAT_CONFIRM); + required_count); - if (s_dec.same_cnt >= RF433_REPEAT_CONFIRM) + if (s_dec.same_cnt >= required_count) { s_dec.frame.code = code; s_dec.frame.bit_len = RF433_BIT_LEN; @@ -545,7 +500,6 @@ static void rf433_process_clean_pulse(Rf433Pulse_t pulse) { rf433_debug_reset("timeout", s_dec.last_high, pulse.width); rf433_decode_reset(); - rf433_repair_reset(); return; } @@ -631,9 +585,13 @@ static void rf433_process_clean_pulse(Rf433Pulse_t pulse) (unsigned int)((s_dec.data >> 8) & 0xFFu), (unsigned int)(s_dec.data & 0xFFu)); - if (s_dec.frame_score >= RF433_FRAME_SCORE_MIN) + if (s_dec.frame_score >= RF433_FRAME_SCORE_DIRECT_MIN) { - rf433_output_code(s_dec.data); + rf433_output_code(s_dec.data, RF433_DIRECT_CONFIRM); + } + else if (s_dec.frame_score >= RF433_FRAME_SCORE_WEAK_MIN) + { + rf433_output_code(s_dec.data, RF433_WEAK_CONFIRM); } else { @@ -643,7 +601,6 @@ static void rf433_process_clean_pulse(Rf433Pulse_t pulse) } rf433_decode_reset(); - rf433_repair_reset(); } } else @@ -664,96 +621,9 @@ static void rf433_process_clean_pulse(Rf433Pulse_t pulse) #endif rf433_debug_reset("bit", s_dec.last_high, s_dec.last_low); rf433_decode_reset(); - rf433_repair_reset(); } } -static bool rf433_can_merge_glitch(Rf433Pulse_t a, Rf433Pulse_t b, Rf433Pulse_t c) -{ - if (a.level != c.level) - { - return false; - } - - if (b.width > rf433_get_glitch_max()) - { - return false; - } - - if (a.width == 0u || c.width == 0u) - { - return false; - } - - return true; -} - -static void rf433_repair_input_pulse(Rf433Pulse_t pulse) -{ - Rf433Pulse_t merged; - Rf433Pulse_t out; - - RF433_PULSE_LOG("[RF433 RAW] L=%d W=%d\r\n", pulse.level, pulse.width); - - if (pulse.width == 0u) - { - return; - } - - if (pulse.width > RF433_LEVEL_TIMEOUT) - { - rf433_debug_reset("timeout", s_dec.last_high, pulse.width); - rf433_decode_reset(); - rf433_repair_reset(); - return; - } - - if (s_repair.cnt == 0u) - { - s_repair.p0 = pulse; - s_repair.cnt = 1u; - return; - } - - if (s_repair.cnt == 1u) - { - s_repair.p1 = pulse; - s_repair.cnt = 2u; - return; - } - - s_repair.p2 = pulse; - if (rf433_can_merge_glitch(s_repair.p0, s_repair.p1, s_repair.p2)) - { - merged.level = s_repair.p0.level; - merged.width = (uint16_t)(s_repair.p0.width + - s_repair.p1.width + - s_repair.p2.width); - - RF433_PULSE_LOG("[RF433 MERGE] %d:%d + %d:%d + %d:%d => %d:%d\r\n", - s_repair.p0.level, - s_repair.p0.width, - s_repair.p1.level, - s_repair.p1.width, - s_repair.p2.level, - s_repair.p2.width, - merged.level, - merged.width); - - s_repair.p0 = merged; - s_repair.cnt = 1u; - return; - } - - out = s_repair.p0; - RF433_PULSE_LOG("[RF433 CLEAN] L=%d W=%d\r\n", out.level, out.width); - rf433_process_clean_pulse(out); - - s_repair.p0 = s_repair.p1; - s_repair.p1 = s_repair.p2; - s_repair.cnt = 2u; -} - void rf433_decoder_process(void) { Rf433Pulse_t pulse; @@ -764,7 +634,7 @@ void rf433_decoder_process(void) while ((cnt < RF433_PROCESS_MAX_ONCE) && rf433_fifo_pop(&pulse)) { - rf433_repair_input_pulse(pulse); + rf433_process_clean_pulse(pulse); cnt++; } }