13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
-
17
16
#include "app/spectrum.h"
18
17
#include "am_fix.h"
19
18
#include "audio.h"
19
+
20
+ #ifdef ENABLE_SCAN_RANGES
21
+ #include "chFrScanner.h"
22
+ #endif
23
+
20
24
#include "driver/backlight.h"
21
25
#include "frequencies.h"
22
26
#include "ui/helper.h"
@@ -53,6 +57,11 @@ PeakInfo peak;
53
57
ScanInfo scanInfo ;
54
58
KeyboardState kbd = {KEY_INVALID , KEY_INVALID , 0 };
55
59
60
+ #ifdef ENABLE_SCAN_RANGES
61
+ static uint16_t blacklistFreqs [15 ];
62
+ static uint8_t blacklistFreqsIdx ;
63
+ #endif
64
+
56
65
const char * bwOptions [] = {" 25k" , "12.5k" , "6.25k" };
57
66
const uint8_t modulationTypeTuneSteps [] = {100 , 50 , 10 };
58
67
const uint8_t modTypeReg47Values [] = {1 , 7 , 5 };
@@ -260,12 +269,24 @@ static void ResetPeak() {
260
269
}
261
270
262
271
bool IsCenterMode () { return settings .scanStepIndex < S_STEP_2_5kHz ; }
263
- uint8_t GetStepsCount () { return 128 >> settings . stepsCount ; }
272
+ // scan step in 0.01khz
264
273
uint16_t GetScanStep () { return scanStepValues [settings .scanStepIndex ]; }
274
+
275
+ uint16_t GetStepsCount ()
276
+ {
277
+ #ifdef ENABLE_SCAN_RANGES
278
+ if (gScanRangeStart ) {
279
+ return (gScanRangeStop - gScanRangeStart ) / GetScanStep ();
280
+ }
281
+ #endif
282
+ return 128 >> settings .stepsCount ;
283
+ }
284
+
265
285
uint32_t GetBW () { return GetStepsCount () * GetScanStep (); }
266
286
uint32_t GetFStart () {
267
287
return IsCenterMode () ? currentFreq - (GetBW () >> 1 ) : currentFreq ;
268
288
}
289
+
269
290
uint32_t GetFEnd () { return currentFreq + GetBW (); }
270
291
271
292
static void TuneToPeak () {
@@ -352,6 +373,10 @@ static void ResetBlacklist() {
352
373
if (rssiHistory [i ] == RSSI_MAX_VALUE )
353
374
rssiHistory [i ] = 0 ;
354
375
}
376
+ #ifdef ENABLE_SCAN_RANGES
377
+ memset (blacklistFreqs , 0 , sizeof (blacklistFreqs ));
378
+ blacklistFreqsIdx = 0 ;
379
+ #endif
355
380
}
356
381
357
382
static void RelaunchScan () {
@@ -398,7 +423,20 @@ static void UpdatePeakInfo() {
398
423
UpdatePeakInfoForce ();
399
424
}
400
425
401
- static void Measure () { rssiHistory [scanInfo .i ] = scanInfo .rssi = GetRssi (); }
426
+ static void Measure ()
427
+ {
428
+ uint16_t rssi = scanInfo .rssi = GetRssi ();
429
+ #ifdef ENABLE_SCAN_RANGES
430
+ if (scanInfo .measurementsCount > 128 ) {
431
+ uint8_t idx = (uint32_t )ARRAY_SIZE (rssiHistory ) * 1000 / scanInfo .measurementsCount * scanInfo .i / 1000 ;
432
+ if (rssiHistory [idx ] < rssi || isListening )
433
+ rssiHistory [idx ] = rssi ;
434
+ rssiHistory [(idx + 1 )%128 ] = 0 ;
435
+ return ;
436
+ }
437
+ #endif
438
+ rssiHistory [scanInfo .i ] = rssi ;
439
+ }
402
440
403
441
// Update things by keypress
404
442
@@ -595,11 +633,24 @@ static void UpdateFreqInput(KEY_Code_t key) {
595
633
}
596
634
597
635
static void Blacklist () {
636
+ #ifdef ENABLE_SCAN_RANGES
637
+ blacklistFreqs [blacklistFreqsIdx ++ % ARRAY_SIZE (blacklistFreqs )] = peak .i ;
638
+ #endif
598
639
rssiHistory [peak .i ] = RSSI_MAX_VALUE ;
599
640
ResetPeak ();
600
641
ToggleRX (false);
601
- newScanStart = true;
642
+ ResetScanStats ();
643
+ }
644
+
645
+ #ifdef ENABLE_SCAN_RANGES
646
+ static bool IsBlacklisted (uint16_t idx )
647
+ {
648
+ for (uint8_t i = 0 ; i < ARRAY_SIZE (blacklistFreqs ); i ++ )
649
+ if (blacklistFreqs [i ] == idx )
650
+ return true;
651
+ return false;
602
652
}
653
+ #endif
603
654
604
655
// Draw things
605
656
@@ -710,9 +761,11 @@ static void DrawRssiTriggerLevel() {
710
761
}
711
762
712
763
static void DrawTicks () {
713
- uint32_t f = GetFStart () % 100000 ;
714
- uint32_t step = GetScanStep ();
715
- for (uint8_t i = 0 ; i < 128 ; i += (1 << settings .stepsCount ), f += step ) {
764
+ uint32_t f = GetFStart ();
765
+ uint32_t span = GetFEnd () - GetFStart ();
766
+ uint32_t step = span / 128 ;
767
+ for (uint8_t i = 0 ; i < 128 ; i += (1 << settings .stepsCount )) {
768
+ f = GetFStart () + span * i / 128 ;
716
769
uint8_t barValue = 0b00000001 ;
717
770
(f % 10000 ) < step && (barValue |= 0b00000010 );
718
771
(f % 50000 ) < step && (barValue |= 0b00000100 );
@@ -764,10 +817,16 @@ static void OnKeyDown(uint8_t key) {
764
817
UpdateFreqChangeStep (false);
765
818
break ;
766
819
case KEY_UP :
767
- UpdateCurrentFreq (true);
820
+ #ifdef ENABLE_SCAN_RANGES
821
+ if (!gScanRangeStart )
822
+ #endif
823
+ UpdateCurrentFreq (true);
768
824
break ;
769
825
case KEY_DOWN :
770
- UpdateCurrentFreq (false);
826
+ #ifdef ENABLE_SCAN_RANGES
827
+ if (!gScanRangeStart )
828
+ #endif
829
+ UpdateCurrentFreq (false);
771
830
break ;
772
831
case KEY_SIDE1 :
773
832
Blacklist ();
@@ -779,7 +838,10 @@ static void OnKeyDown(uint8_t key) {
779
838
UpdateRssiTriggerLevel (false);
780
839
break ;
781
840
case KEY_5 :
782
- FreqInput ();
841
+ #ifdef ENABLE_SCAN_RANGES
842
+ if (!gScanRangeStart )
843
+ #endif
844
+ FreqInput ();
783
845
break ;
784
846
case KEY_0 :
785
847
ToggleModulation ();
@@ -788,7 +850,10 @@ static void OnKeyDown(uint8_t key) {
788
850
ToggleListeningBW ();
789
851
break ;
790
852
case KEY_4 :
791
- ToggleStepsCount ();
853
+ #ifdef ENABLE_SCAN_RANGES
854
+ if (!gScanRangeStart )
855
+ #endif
856
+ ToggleStepsCount ();
792
857
break ;
793
858
case KEY_SIDE2 :
794
859
ToggleBacklight ();
@@ -932,7 +997,7 @@ static void RenderStatus() {
932
997
933
998
static void RenderSpectrum () {
934
999
DrawTicks ();
935
- DrawArrow (peak .i << settings . stepsCount );
1000
+ DrawArrow (128u * peak .i / GetStepsCount () );
936
1001
DrawSpectrum ();
937
1002
DrawRssiTriggerLevel ();
938
1003
DrawF (peak .f );
@@ -1049,7 +1114,11 @@ bool HandleUserInput() {
1049
1114
}
1050
1115
1051
1116
static void Scan () {
1052
- if (rssiHistory [scanInfo .i ] != RSSI_MAX_VALUE ) {
1117
+ if (rssiHistory [scanInfo .i ] != RSSI_MAX_VALUE
1118
+ #ifdef ENABLE_SCAN_RANGES
1119
+ && !IsBlacklisted (scanInfo .i )
1120
+ #endif
1121
+ ) {
1053
1122
SetF (scanInfo .f );
1054
1123
Measure ();
1055
1124
UpdateScanInfo ();
@@ -1070,6 +1139,10 @@ static void UpdateScan() {
1070
1139
return ;
1071
1140
}
1072
1141
1142
+ if (scanInfo .measurementsCount < 128 )
1143
+ memset (& rssiHistory [scanInfo .measurementsCount ], 0 ,
1144
+ sizeof (rssiHistory ) - scanInfo .measurementsCount * sizeof (rssiHistory [0 ]));
1145
+
1073
1146
redrawScreen = true;
1074
1147
preventKeypress = false;
1075
1148
@@ -1122,7 +1195,7 @@ static void UpdateListening() {
1122
1195
}
1123
1196
1124
1197
ToggleRX (false);
1125
- newScanStart = true ;
1198
+ ResetScanStats () ;
1126
1199
}
1127
1200
1128
1201
static void Tick () {
@@ -1135,6 +1208,26 @@ static void Tick() {
1135
1208
}
1136
1209
#endif
1137
1210
1211
+ #ifdef ENABLE_SCAN_RANGES
1212
+ if (gNextTimeslice_500ms ) {
1213
+ gNextTimeslice_500ms = false;
1214
+
1215
+ // if a lot of steps then it takes long time
1216
+ // we don't want to wait for whole scan
1217
+ // listening has it's own timer
1218
+ if (GetStepsCount ()> 128 && !isListening ) {
1219
+ UpdatePeakInfo ();
1220
+ if (IsPeakOverLevel ()) {
1221
+ ToggleRX (true);
1222
+ TuneToPeak ();
1223
+ return ;
1224
+ }
1225
+ redrawScreen = true;
1226
+ preventKeypress = false;
1227
+ }
1228
+ }
1229
+ #endif
1230
+
1138
1231
if (!preventKeypress ) {
1139
1232
HandleUserInput ();
1140
1233
}
@@ -1166,18 +1259,32 @@ void APP_RunSpectrum() {
1166
1259
// TX here coz it always? set to active VFO
1167
1260
vfo = gEeprom .TX_VFO ;
1168
1261
// set the current frequency in the middle of the display
1169
- currentFreq = initialFreq = gEeprom .VfoInfo [vfo ].pRX -> Frequency -
1170
- ((GetStepsCount () / 2 ) * GetScanStep ());
1262
+ #ifdef ENABLE_SCAN_RANGES
1263
+ if (gScanRangeStart ) {
1264
+ currentFreq = initialFreq = gScanRangeStart ;
1265
+ for (uint8_t i = 0 ; i < ARRAY_SIZE (scanStepValues ); i ++ ) {
1266
+ if (scanStepValues [i ] >= gTxVfo -> StepFrequency ) {
1267
+ settings .scanStepIndex = i ;
1268
+ break ;
1269
+ }
1270
+ }
1271
+ settings .stepsCount = STEPS_128 ;
1272
+ }
1273
+ else
1274
+ #endif
1275
+ currentFreq = initialFreq = gTxVfo -> pRX -> Frequency -
1276
+ ((GetStepsCount () / 2 ) * GetScanStep ());
1171
1277
1172
1278
BackupRegisters ();
1173
1279
1174
1280
isListening = true; // to turn off RX later
1175
1281
redrawStatus = true;
1176
- redrawScreen = false; // we will wait until scan done
1282
+ redrawScreen = true;
1177
1283
newScanStart = true;
1178
1284
1285
+
1179
1286
ToggleRX (true), ToggleRX (false); // hack to prevent noise when squelch off
1180
- RADIO_SetModulation (settings .modulationType = gRxVfo -> Modulation );
1287
+ RADIO_SetModulation (settings .modulationType = gTxVfo -> Modulation );
1181
1288
1182
1289
BK4819_SetFilterBandwidth (settings .listenBw = BK4819_FILTER_BW_WIDE , false);
1183
1290
0 commit comments