1
1
2
+ #include <stdlib.h> // abs()
3
+
2
4
#if defined(ENABLE_UART ) && defined(ENABLE_UART_DEBUG )
3
5
#include "driver/uart.h"
4
6
#endif
5
7
#include "freq_ignore.h"
6
8
#include "misc.h"
7
9
10
+ //#define FI_CLOSE_ENOUGH_HZ 300
11
+
8
12
// a list of frequencies to ignore/skip when scanning
9
13
uint32_t ignore_frequencies [64 ];
10
14
int ignore_frequencies_count = 0 ;
@@ -21,53 +25,70 @@ void FI_clear_freq_ignored(void)
21
25
int FI_freq_ignored (const uint32_t frequency )
22
26
{ // return index of the ignored frequency
23
27
24
- if (frequency == 0 || frequency == 0xffffffff || ignore_frequencies_count <= 0 )
25
- return -1 ;
28
+ #ifdef FI_CLOSE_ENOUGH_HZ
29
+ if (frequency <= FI_CLOSE_ENOUGH_HZ || frequency >= (0xffffffff - FI_CLOSE_ENOUGH_HZ ) || ignore_frequencies_count <= 0 )
30
+ return -1 ; // invalid frequency
31
+ #else
32
+ if (frequency == 0 || frequency == 0xffffffff || ignore_frequencies_count <= 0 )
33
+ return -1 ; // invalid frequency
34
+ #endif
26
35
27
- if (ignore_frequencies_count >= 8 )
28
- { // binary search .. becomes much faster than sequencial search when the list is bigger
36
+ if (ignore_frequencies_count >= 20 )
37
+ { // binary search becomes faster than sequencial as the list grows beyound a certain size
29
38
int low = 0 ;
30
39
int high = ignore_frequencies_count ;
40
+
31
41
while (low < high )
32
42
{
33
43
register int mid = (low + high ) / 2 ;
34
44
register uint32_t freq = ignore_frequencies [mid ];
35
- if ( freq > frequency )
36
- high = mid ;
37
- else
38
- if ( freq < frequency )
39
- low = mid + 1 ;
40
- else
41
- {
45
+
46
+ #ifdef FI_CLOSE_ENOUGH_HZ
47
+ if ( abs (( int32_t ) frequency - ( int32_t ) freq ) <= FI_CLOSE_ENOUGH_HZ )
48
+ #else
49
+ if ( frequency == freq )
50
+ #endif
51
+ { // found it
42
52
#if defined(ENABLE_UART ) && defined(ENABLE_UART_DEBUG )
43
53
UART_printf ("ignored bin %u %u\r\n" , frequency , mid );
44
54
#endif
45
55
return mid ;
46
56
}
57
+
58
+ if (freq > frequency )
59
+ high = mid ;
60
+ else
61
+ low = mid + 1 ;
47
62
}
48
63
}
49
64
else
50
65
{ // sequencial search
51
- int i ;
66
+ register int i ;
52
67
for (i = 0 ; i < ignore_frequencies_count ; i ++ )
53
68
{
54
69
register uint32_t freq = ignore_frequencies [i ];
55
- if (frequency == freq )
70
+
71
+ #ifdef FI_CLOSE_ENOUGH_HZ
72
+ if (abs ((int32_t )frequency - (int32_t )freq ) <= FI_CLOSE_ENOUGH_HZ )
73
+ #else
74
+ if (frequency == freq )
75
+ #endif
56
76
{ // found it
57
77
#if defined(ENABLE_UART ) && defined(ENABLE_UART_DEBUG )
58
78
UART_printf ("ignored seq %u %u\r\n" , frequency , i );
59
79
#endif
60
80
return i ;
61
81
}
82
+
62
83
if (frequency < freq )
63
- return -1 ; // can exit loop early as the list is sorted by frequency
84
+ return -1 ; // exit loop early as the list is sorted by frequency
64
85
}
65
86
}
66
87
67
88
return -1 ; // not found
68
89
}
69
90
70
- void FI_add_freq_ignored (const uint32_t frequency )
91
+ bool FI_add_freq_ignored (const uint32_t frequency )
71
92
{ // add a new frequency to the ignore list
72
93
73
94
int i ;
@@ -76,31 +97,45 @@ void FI_add_freq_ignored(const uint32_t frequency)
76
97
UART_printf ("ignore add %u\r\n" , frequency );
77
98
#endif
78
99
79
- if (frequency == 0 || frequency == 0xffffffff )
80
- return ;
100
+ #ifdef FI_CLOSE_ENOUGH_HZ
101
+ if (frequency <= FI_CLOSE_ENOUGH_HZ || frequency >= (0xffffffff - FI_CLOSE_ENOUGH_HZ ))
102
+ return false; // invalid frequency
103
+ #else
104
+ if (frequency == 0 || frequency == 0xffffffff )
105
+ return false; // invalid frequency
106
+ #endif
81
107
82
108
if (ignore_frequencies_count >= (int )ARRAY_SIZE (ignore_frequencies ))
83
109
{ // the list is full
84
110
#if defined(ENABLE_UART ) && defined(ENABLE_UART_DEBUG )
85
111
UART_SendText ("ignore add full\r\n" );
86
112
#endif
87
- return ;
113
+ return false; // failed
88
114
}
89
115
90
116
for (i = 0 ; i < ignore_frequencies_count ; i ++ )
91
117
{
92
118
register uint32_t freq = ignore_frequencies [i ];
93
119
94
- if (frequency == freq )
120
+ #ifdef FI_CLOSE_ENOUGH_HZ
121
+ if (abs ((int32_t )frequency - (int32_t )freq ) <= FI_CLOSE_ENOUGH_HZ )
122
+ #else
123
+ if (frequency == freq )
124
+ #endif
95
125
{ // already in the list
96
126
#if defined(ENABLE_UART ) && defined(ENABLE_UART_DEBUG )
97
127
UART_SendText ("ignore add already\r\n" );
98
128
#endif
99
- return ;
129
+ return true ;
100
130
}
101
131
102
- if (frequency < freq )
103
- break ;
132
+ #ifdef FI_CLOSE_ENOUGH_HZ
133
+ if (frequency < (freq + FI_CLOSE_ENOUGH_HZ ))
134
+ break ; // exit loop early as the list is sorted by frequency
135
+ #else
136
+ if (frequency < freq )
137
+ break ; // exit loop early as the list is sorted by frequency
138
+ #endif
104
139
}
105
140
106
141
// found the location to store the new frequency - the list is kept sorted by frequency
@@ -117,6 +152,8 @@ void FI_add_freq_ignored(const uint32_t frequency)
117
152
for (i = 0 ; i < ignore_frequencies_count ; i ++ )
118
153
UART_printf ("%2u %10u\r\n" , i , ignore_frequencies [i ]);
119
154
#endif
155
+
156
+ return true;
120
157
}
121
158
122
159
void FI_sub_freq_ignored (const uint32_t frequency )
@@ -126,9 +163,6 @@ void FI_sub_freq_ignored(const uint32_t frequency)
126
163
UART_printf ("ignore sub %u\r\n" , frequency );
127
164
#endif
128
165
129
- if (frequency == 0 || frequency == 0xffffffff )
130
- return ;
131
-
132
166
int index = FI_freq_ignored (frequency );
133
167
if (index < 0 )
134
168
return ;
0 commit comments