diff --git a/project/example/ble/ble_peripheral/app/src/Rf433.c b/project/example/ble/ble_peripheral/app/src/Rf433.c index 6902477..41d735e 100644 --- a/project/example/ble/ble_peripheral/app/src/Rf433.c +++ b/project/example/ble/ble_peripheral/app/src/Rf433.c @@ -105,18 +105,58 @@ uint8_t long_flag_s=0; void _433_fun(void); -// 在固定大小数组中添加新元素(滑动窗口) -void addToFixedArray(uint8_t arr[], uint8_t newValue) { - uint8_t i; - // 所有元素向后移动一位 - for( i = 4; i > 0; i--) { - arr[i] = arr[i - 1]; - } - - // 在头部插入新值 - arr[0] = newValue; +// 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 void Pairing_Address_Touch(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(Pairing_Address_IsEmpty(adress_H_array[i], adress_L_array[i])) continue; + if(adress_H_array[i] == high && adress_L_array[i] == low) 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; + } + } +} static void Pairing_Feedback_Apply(uint8_t on) { Light_Transition_SetFeedback(1U, on); @@ -206,7 +246,6 @@ static void Pairing_Candidate_Reset(void) static void Encoder_key_NoCode(){ - uint8_t i=0,j=0,isok=0,updata=0; uint16_t times_s=0; if(pair_frame_silence_ticks < 0xffU) pair_frame_silence_ticks++; @@ -271,52 +310,13 @@ static void Encoder_key_NoCode(){ 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; - // 第一步:检查是否已存在 - for(j = 0; j <5; j++) { - if(adress_H_array[j] == adress_H && adress_L_array[j] == adress_L) { - isok=1; - break; - } - - } - // 如果不存在,找空位添加 - if(j==5) { // 循环完整结束,说明不存在 - // 寻找空位置(值为0的位置) - for(i = 0; i <=4; i++) { - if(adress_H_array[i] == 0 && adress_L_array[i] == 0) { - adress_H_array[i] = adress_H; - adress_L_array[i] = adress_L; - - isok=1; - break; - } - if(i==4){ - for(j = 0; j <5; j++) { - if(adress_H_array[j] == adress_H && adress_L_array[j] == adress_L) { - updata=1; - break; - } - } - if(updata==0){ - addToFixedArray(adress_H_array,adress_H); - addToFixedArray(adress_L_array,adress_L); - } - - } - - } - } - times_s=300; - match_success=1; - - /// EA=0; - isok=0; - Pairing_Feedback_Start(times_s); - - + adress=receive_data >>8; + adress_H = (receive_data >> 16) & 0xff; + adress_L = (receive_data >> 8) & 0xff; + Pairing_Address_Touch(adress_H, adress_L); + 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){ adress_H_array[0] = 0xff;