Skip to content

Commit 39eb3e8

Browse files
committed
Scan range in spectrum
1 parent 0d66eb8 commit 39eb3e8

File tree

7 files changed

+139
-26
lines changed

7 files changed

+139
-26
lines changed

app/chFrScanner.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ bool gScanPauseMode;
1111

1212
#ifdef ENABLE_SCAN_RANGES
1313
uint32_t gScanRangeStart;
14+
uint32_t gScanRangeStop;
1415
#endif
1516

1617
typedef enum {
@@ -157,9 +158,7 @@ static void NextFreqChannel(void)
157158
{
158159
#ifdef ENABLE_SCAN_RANGES
159160
if(gScanRangeStart) {
160-
uint32_t start = gScanRangeStart;
161-
uint32_t end = gEeprom.VfoInfo[(gEeprom.TX_VFO+1)%2].freq_config_RX.Frequency;
162-
gRxVfo->freq_config_RX.Frequency = APP_SetFreqByStepAndLimits(gRxVfo, gScanStateDir, MIN(start, end), MAX(start, end));
161+
gRxVfo->freq_config_RX.Frequency = APP_SetFreqByStepAndLimits(gRxVfo, gScanStateDir, gScanRangeStart, gScanRangeStop);
163162
}
164163
else
165164
#endif

app/chFrScanner.h

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ extern bool gScanPauseMode;
1212

1313
#ifdef ENABLE_SCAN_RANGES
1414
extern uint32_t gScanRangeStart;
15+
extern uint32_t gScanRangeStop;
1516
#endif
1617

1718
void CHFRSCANNER_Found(void);

app/main.c

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ void toggle_chan_scanlist(void)
5252
if(!IS_MR_CHANNEL(gTxVfo->CHANNEL_SAVE)) {
5353
#ifdef ENABLE_SCAN_RANGES
5454
gScanRangeStart = gScanRangeStart ? 0 : gTxVfo->pRX->Frequency;
55+
gScanRangeStop = gEeprom.VfoInfo[!gEeprom.TX_VFO].freq_config_RX.Frequency;
56+
if(gScanRangeStart > gScanRangeStop)
57+
SWAP(gScanRangeStart, gScanRangeStop);
5558
#endif
5659
return;
5760
}

app/spectrum.c

+125-18
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
#include "app/spectrum.h"
1817
#include "am_fix.h"
1918
#include "audio.h"
19+
20+
#ifdef ENABLE_SCAN_RANGES
21+
#include "chFrScanner.h"
22+
#endif
23+
2024
#include "driver/backlight.h"
2125
#include "frequencies.h"
2226
#include "ui/helper.h"
@@ -53,6 +57,11 @@ PeakInfo peak;
5357
ScanInfo scanInfo;
5458
KeyboardState kbd = {KEY_INVALID, KEY_INVALID, 0};
5559

60+
#ifdef ENABLE_SCAN_RANGES
61+
static uint16_t blacklistFreqs[15];
62+
static uint8_t blacklistFreqsIdx;
63+
#endif
64+
5665
const char *bwOptions[] = {" 25k", "12.5k", "6.25k"};
5766
const uint8_t modulationTypeTuneSteps[] = {100, 50, 10};
5867
const uint8_t modTypeReg47Values[] = {1, 7, 5};
@@ -260,12 +269,24 @@ static void ResetPeak() {
260269
}
261270

262271
bool IsCenterMode() { return settings.scanStepIndex < S_STEP_2_5kHz; }
263-
uint8_t GetStepsCount() { return 128 >> settings.stepsCount; }
272+
// scan step in 0.01khz
264273
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+
265285
uint32_t GetBW() { return GetStepsCount() * GetScanStep(); }
266286
uint32_t GetFStart() {
267287
return IsCenterMode() ? currentFreq - (GetBW() >> 1) : currentFreq;
268288
}
289+
269290
uint32_t GetFEnd() { return currentFreq + GetBW(); }
270291

271292
static void TuneToPeak() {
@@ -352,6 +373,10 @@ static void ResetBlacklist() {
352373
if (rssiHistory[i] == RSSI_MAX_VALUE)
353374
rssiHistory[i] = 0;
354375
}
376+
#ifdef ENABLE_SCAN_RANGES
377+
memset(blacklistFreqs, 0, sizeof(blacklistFreqs));
378+
blacklistFreqsIdx = 0;
379+
#endif
355380
}
356381

357382
static void RelaunchScan() {
@@ -398,7 +423,20 @@ static void UpdatePeakInfo() {
398423
UpdatePeakInfoForce();
399424
}
400425

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+
}
402440

403441
// Update things by keypress
404442

@@ -595,11 +633,24 @@ static void UpdateFreqInput(KEY_Code_t key) {
595633
}
596634

597635
static void Blacklist() {
636+
#ifdef ENABLE_SCAN_RANGES
637+
blacklistFreqs[blacklistFreqsIdx++ % ARRAY_SIZE(blacklistFreqs)] = peak.i;
638+
#endif
598639
rssiHistory[peak.i] = RSSI_MAX_VALUE;
599640
ResetPeak();
600641
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;
602652
}
653+
#endif
603654

604655
// Draw things
605656

@@ -710,9 +761,11 @@ static void DrawRssiTriggerLevel() {
710761
}
711762

712763
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;
716769
uint8_t barValue = 0b00000001;
717770
(f % 10000) < step && (barValue |= 0b00000010);
718771
(f % 50000) < step && (barValue |= 0b00000100);
@@ -764,10 +817,16 @@ static void OnKeyDown(uint8_t key) {
764817
UpdateFreqChangeStep(false);
765818
break;
766819
case KEY_UP:
767-
UpdateCurrentFreq(true);
820+
#ifdef ENABLE_SCAN_RANGES
821+
if(!gScanRangeStart)
822+
#endif
823+
UpdateCurrentFreq(true);
768824
break;
769825
case KEY_DOWN:
770-
UpdateCurrentFreq(false);
826+
#ifdef ENABLE_SCAN_RANGES
827+
if(!gScanRangeStart)
828+
#endif
829+
UpdateCurrentFreq(false);
771830
break;
772831
case KEY_SIDE1:
773832
Blacklist();
@@ -779,7 +838,10 @@ static void OnKeyDown(uint8_t key) {
779838
UpdateRssiTriggerLevel(false);
780839
break;
781840
case KEY_5:
782-
FreqInput();
841+
#ifdef ENABLE_SCAN_RANGES
842+
if(!gScanRangeStart)
843+
#endif
844+
FreqInput();
783845
break;
784846
case KEY_0:
785847
ToggleModulation();
@@ -788,7 +850,10 @@ static void OnKeyDown(uint8_t key) {
788850
ToggleListeningBW();
789851
break;
790852
case KEY_4:
791-
ToggleStepsCount();
853+
#ifdef ENABLE_SCAN_RANGES
854+
if(!gScanRangeStart)
855+
#endif
856+
ToggleStepsCount();
792857
break;
793858
case KEY_SIDE2:
794859
ToggleBacklight();
@@ -932,7 +997,7 @@ static void RenderStatus() {
932997

933998
static void RenderSpectrum() {
934999
DrawTicks();
935-
DrawArrow(peak.i << settings.stepsCount);
1000+
DrawArrow(128u * peak.i / GetStepsCount());
9361001
DrawSpectrum();
9371002
DrawRssiTriggerLevel();
9381003
DrawF(peak.f);
@@ -1049,7 +1114,11 @@ bool HandleUserInput() {
10491114
}
10501115

10511116
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+
) {
10531122
SetF(scanInfo.f);
10541123
Measure();
10551124
UpdateScanInfo();
@@ -1070,6 +1139,10 @@ static void UpdateScan() {
10701139
return;
10711140
}
10721141

1142+
if(scanInfo.measurementsCount < 128)
1143+
memset(&rssiHistory[scanInfo.measurementsCount], 0,
1144+
sizeof(rssiHistory) - scanInfo.measurementsCount*sizeof(rssiHistory[0]));
1145+
10731146
redrawScreen = true;
10741147
preventKeypress = false;
10751148

@@ -1122,7 +1195,7 @@ static void UpdateListening() {
11221195
}
11231196

11241197
ToggleRX(false);
1125-
newScanStart = true;
1198+
ResetScanStats();
11261199
}
11271200

11281201
static void Tick() {
@@ -1135,6 +1208,26 @@ static void Tick() {
11351208
}
11361209
#endif
11371210

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+
11381231
if (!preventKeypress) {
11391232
HandleUserInput();
11401233
}
@@ -1166,18 +1259,32 @@ void APP_RunSpectrum() {
11661259
// TX here coz it always? set to active VFO
11671260
vfo = gEeprom.TX_VFO;
11681261
// 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());
11711277

11721278
BackupRegisters();
11731279

11741280
isListening = true; // to turn off RX later
11751281
redrawStatus = true;
1176-
redrawScreen = false; // we will wait until scan done
1282+
redrawScreen = true;
11771283
newScanStart = true;
11781284

1285+
11791286
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);
11811288

11821289
BK4819_SetFilterBandwidth(settings.listenBw = BK4819_FILTER_BW_WIDE, false);
11831290

app/spectrum.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,17 @@ typedef struct KeyboardState {
138138

139139
typedef struct ScanInfo {
140140
uint16_t rssi, rssiMin, rssiMax;
141-
uint8_t i, iPeak;
141+
uint16_t i, iPeak;
142142
uint32_t f, fPeak;
143143
uint16_t scanStep;
144-
uint8_t measurementsCount;
144+
uint16_t measurementsCount;
145145
} ScanInfo;
146146

147147
typedef struct PeakInfo {
148148
uint16_t t;
149149
uint16_t rssi;
150150
uint32_t f;
151-
uint8_t i;
151+
uint16_t i;
152152
} PeakInfo;
153153

154154
void APP_RunSpectrum(void);

misc.h

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
#define MIN(a, b) ({ __typeof__ (a) _a = (a); __typeof__ (b) _b = (b); _a < _b ? _a : _b; })
3333
#endif
3434

35+
#ifndef SWAP
36+
#define SWAP(a, b) ({ __typeof__ (a) _c = (a); a = b; b = _c; })
37+
#endif
38+
3539
#define IS_MR_CHANNEL(x) ((x) <= MR_CHANNEL_LAST)
3640
#define IS_FREQ_CHANNEL(x) ((x) >= FREQ_CHANNEL_FIRST && (x) <= FREQ_CHANNEL_LAST)
3741
#define IS_VALID_CHANNEL(x) ((x) < LAST_CHANNEL)

ui/main.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,7 @@ void UI_DisplayMain(void)
348348
UI_PrintString("ScnRng", 5, 0, line, 8);
349349
sprintf(String, "%3u.%05u", gScanRangeStart / 100000, gScanRangeStart % 100000);
350350
UI_PrintStringSmall(String, 56, 0, line);
351-
uint32_t frq = gEeprom.VfoInfo[vfo_num].pRX->Frequency;
352-
sprintf(String, "%3u.%05u", frq / 100000, frq % 100000);
351+
sprintf(String, "%3u.%05u", gScanRangeStop / 100000, gScanRangeStop % 100000);
353352
UI_PrintStringSmall(String, 56, 0, line + 1);
354353
continue;
355354
}

0 commit comments

Comments
 (0)