Skip to content

Commit

Permalink
Scan range in spectrum
Browse files Browse the repository at this point in the history
  • Loading branch information
egzumer committed Dec 19, 2023
1 parent 13b6bb0 commit 0872883
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 23 deletions.
5 changes: 2 additions & 3 deletions app/chFrScanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ bool gScanPauseMode;

#ifdef ENABLE_SCAN_RANGES
uint32_t gScanRangeStart;
uint32_t gScanRangeStop;
#endif

typedef enum {
Expand Down Expand Up @@ -157,9 +158,7 @@ static void NextFreqChannel(void)
{
#ifdef ENABLE_SCAN_RANGES
if(gScanRangeStart) {
uint32_t start = gScanRangeStart;
uint32_t end = gEeprom.VfoInfo[(gEeprom.TX_VFO+1)%2].freq_config_RX.Frequency;
gRxVfo->freq_config_RX.Frequency = APP_SetFreqByStepAndLimits(gRxVfo, gScanStateDir, MIN(start, end), MAX(start, end));
gRxVfo->freq_config_RX.Frequency = APP_SetFreqByStepAndLimits(gRxVfo, gScanStateDir, gScanRangeStart, gScanRangeStop);
}
else
#endif
Expand Down
1 change: 1 addition & 0 deletions app/chFrScanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extern bool gScanPauseMode;

#ifdef ENABLE_SCAN_RANGES
extern uint32_t gScanRangeStart;
extern uint32_t gScanRangeStop;
#endif

void CHFRSCANNER_Found(void);
Expand Down
3 changes: 3 additions & 0 deletions app/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ void toggle_chan_scanlist(void)
if(!IS_MR_CHANNEL(gTxVfo->CHANNEL_SAVE)) {
#ifdef ENABLE_SCAN_RANGES
gScanRangeStart = gScanRangeStart ? 0 : gTxVfo->pRX->Frequency;
gScanRangeStop = gEeprom.VfoInfo[!gEeprom.TX_VFO].freq_config_RX.Frequency;
if(gScanRangeStart > gScanRangeStop)
SWAP(gScanRangeStart, gScanRangeStop);
#endif
return;
}
Expand Down
135 changes: 120 additions & 15 deletions app/spectrum.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "app/spectrum.h"
#include "am_fix.h"
#include "audio.h"

#ifdef ENABLE_SCAN_RANGES
#include "chFrScanner.h"
#endif

#include "driver/backlight.h"
#include "frequencies.h"
#include "ui/helper.h"
Expand Down Expand Up @@ -53,6 +57,11 @@ PeakInfo peak;
ScanInfo scanInfo;
KeyboardState kbd = {KEY_INVALID, KEY_INVALID, 0};

#ifdef ENABLE_SCAN_RANGES
static uint32_t blacklistFreqs[7];
static uint8_t blacklistFreqsIdx;
#endif

const char *bwOptions[] = {" 25k", "12.5k", "6.25k"};
const uint8_t modulationTypeTuneSteps[] = {100, 50, 10};
const uint8_t modTypeReg47Values[] = {1, 7, 5};
Expand Down Expand Up @@ -260,12 +269,24 @@ static void ResetPeak() {
}

bool IsCenterMode() { return settings.scanStepIndex < S_STEP_2_5kHz; }
uint8_t GetStepsCount() { return 128 >> settings.stepsCount; }
// scan step in 0.01khz
uint16_t GetScanStep() { return scanStepValues[settings.scanStepIndex]; }

uint16_t GetStepsCount()
{
#ifdef ENABLE_SCAN_RANGES
if(gScanRangeStart) {
return (gScanRangeStop - gScanRangeStart) / GetScanStep();
}
#endif
return 128 >> settings.stepsCount;
}

uint32_t GetBW() { return GetStepsCount() * GetScanStep(); }
uint32_t GetFStart() {
return IsCenterMode() ? currentFreq - (GetBW() >> 1) : currentFreq;
}

uint32_t GetFEnd() { return currentFreq + GetBW(); }

static void TuneToPeak() {
Expand Down Expand Up @@ -352,6 +373,10 @@ static void ResetBlacklist() {
if (rssiHistory[i] == RSSI_MAX_VALUE)
rssiHistory[i] = 0;
}
#ifdef ENABLE_SCAN_RANGES
memset(blacklistFreqs, 0, ARRAY_SIZE(blacklistFreqs));
blacklistFreqsIdx = 0;
#endif
}

static void RelaunchScan() {
Expand Down Expand Up @@ -398,7 +423,20 @@ static void UpdatePeakInfo() {
UpdatePeakInfoForce();
}

static void Measure() { rssiHistory[scanInfo.i] = scanInfo.rssi = GetRssi(); }
static void Measure()
{
uint16_t rssi = scanInfo.rssi = GetRssi();
#ifdef ENABLE_SCAN_RANGES
if(scanInfo.measurementsCount > 128) {
uint8_t idx = (uint32_t)ARRAY_SIZE(rssiHistory) * 1000 / scanInfo.measurementsCount * scanInfo.i / 1000;
if(rssiHistory[idx] < rssi || isListening)
rssiHistory[idx] = rssi;
rssiHistory[(idx+1)%128] = 0;
return;
}
#endif
rssiHistory[scanInfo.i] = rssi;
}

// Update things by keypress

Expand Down Expand Up @@ -595,11 +633,24 @@ static void UpdateFreqInput(KEY_Code_t key) {
}

static void Blacklist() {
#ifdef ENABLE_SCAN_RANGES
blacklistFreqs[blacklistFreqsIdx++ % ARRAY_SIZE(blacklistFreqs)] = fMeasure;
#endif
rssiHistory[peak.i] = RSSI_MAX_VALUE;
ResetPeak();
ToggleRX(false);
newScanStart = true;
ResetScanStats();
}

#ifdef ENABLE_SCAN_RANGES
static bool IsBlacklisted(uint32_t f)
{
for(uint8_t i = 0; i < ARRAY_SIZE(blacklistFreqs); i++)
if(blacklistFreqs[i] == f)
return true;
return false;
}
#endif

// Draw things

Expand Down Expand Up @@ -764,10 +815,16 @@ static void OnKeyDown(uint8_t key) {
UpdateFreqChangeStep(false);
break;
case KEY_UP:
UpdateCurrentFreq(true);
#ifdef ENABLE_SCAN_RANGES
if(!gScanRangeStart)
#endif
UpdateCurrentFreq(true);
break;
case KEY_DOWN:
UpdateCurrentFreq(false);
#ifdef ENABLE_SCAN_RANGES
if(!gScanRangeStart)
#endif
UpdateCurrentFreq(false);
break;
case KEY_SIDE1:
Blacklist();
Expand All @@ -779,7 +836,10 @@ static void OnKeyDown(uint8_t key) {
UpdateRssiTriggerLevel(false);
break;
case KEY_5:
FreqInput();
#ifdef ENABLE_SCAN_RANGES
if(!gScanRangeStart)
#endif
FreqInput();
break;
case KEY_0:
ToggleModulation();
Expand All @@ -788,7 +848,10 @@ static void OnKeyDown(uint8_t key) {
ToggleListeningBW();
break;
case KEY_4:
ToggleStepsCount();
#ifdef ENABLE_SCAN_RANGES
if(!gScanRangeStart)
#endif
ToggleStepsCount();
break;
case KEY_SIDE2:
ToggleBacklight();
Expand Down Expand Up @@ -932,7 +995,7 @@ static void RenderStatus() {

static void RenderSpectrum() {
DrawTicks();
DrawArrow(peak.i << settings.stepsCount);
DrawArrow(128u * peak.i / GetStepsCount());
DrawSpectrum();
DrawRssiTriggerLevel();
DrawF(peak.f);
Expand Down Expand Up @@ -1049,7 +1112,11 @@ bool HandleUserInput() {
}

static void Scan() {
if (rssiHistory[scanInfo.i] != RSSI_MAX_VALUE) {
if (rssiHistory[scanInfo.i] != RSSI_MAX_VALUE
#ifdef ENABLE_SCAN_RANGES
&& !IsBlacklisted(scanInfo.f)
#endif
) {
SetF(scanInfo.f);
Measure();
UpdateScanInfo();
Expand All @@ -1070,6 +1137,10 @@ static void UpdateScan() {
return;
}

if(scanInfo.measurementsCount < 128)
memset(&rssiHistory[scanInfo.measurementsCount], 0,
sizeof(rssiHistory) - scanInfo.measurementsCount*sizeof(rssiHistory[0]));

redrawScreen = true;
preventKeypress = false;

Expand Down Expand Up @@ -1122,7 +1193,7 @@ static void UpdateListening() {
}

ToggleRX(false);
newScanStart = true;
ResetScanStats();
}

static void Tick() {
Expand All @@ -1135,6 +1206,26 @@ static void Tick() {
}
#endif

#ifdef ENABLE_SCAN_RANGES
if (gNextTimeslice_500ms) {
gNextTimeslice_500ms = false;

// if a lot of steps then it takes long time
// we don't want to wait for whole scan
// listening has it's own timer
if(GetStepsCount()>128 && !isListening) {
UpdatePeakInfo();
if (IsPeakOverLevel()) {
ToggleRX(true);
TuneToPeak();
return;
}
redrawScreen = true;
preventKeypress = false;
}
}
#endif

if (!preventKeypress) {
HandleUserInput();
}
Expand Down Expand Up @@ -1166,18 +1257,32 @@ void APP_RunSpectrum() {
// TX here coz it always? set to active VFO
vfo = gEeprom.TX_VFO;
// set the current frequency in the middle of the display
currentFreq = initialFreq = gEeprom.VfoInfo[vfo].pRX->Frequency -
((GetStepsCount() / 2) * GetScanStep());
#ifdef ENABLE_SCAN_RANGES
if(gScanRangeStart) {
currentFreq = initialFreq = gScanRangeStart;
for(uint8_t i = 0; i < ARRAY_SIZE(scanStepValues); i++) {
if(scanStepValues[i] >= gTxVfo->StepFrequency) {
settings.scanStepIndex = i;
break;
}
}
settings.stepsCount = STEPS_128;
}
else
#endif
currentFreq = initialFreq = gTxVfo->pRX->Frequency -
((GetStepsCount() / 2) * GetScanStep());

BackupRegisters();

isListening = true; // to turn off RX later
redrawStatus = true;
redrawScreen = false; // we will wait until scan done
redrawScreen = true;
newScanStart = true;


ToggleRX(true), ToggleRX(false); // hack to prevent noise when squelch off
RADIO_SetModulation(settings.modulationType = gRxVfo->Modulation);
RADIO_SetModulation(settings.modulationType = gTxVfo->Modulation);

BK4819_SetFilterBandwidth(settings.listenBw = BK4819_FILTER_BW_WIDE, false);

Expand Down
6 changes: 3 additions & 3 deletions app/spectrum.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,17 @@ typedef struct KeyboardState {

typedef struct ScanInfo {
uint16_t rssi, rssiMin, rssiMax;
uint8_t i, iPeak;
uint16_t i, iPeak;
uint32_t f, fPeak;
uint16_t scanStep;
uint8_t measurementsCount;
uint16_t measurementsCount;
} ScanInfo;

typedef struct PeakInfo {
uint16_t t;
uint16_t rssi;
uint32_t f;
uint8_t i;
uint16_t i;
} PeakInfo;

void APP_RunSpectrum(void);
Expand Down
4 changes: 4 additions & 0 deletions misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
#define MIN(a, b) ({ __typeof__ (a) _a = (a); __typeof__ (b) _b = (b); _a < _b ? _a : _b; })
#endif

#ifndef SWAP
#define SWAP(a, b) ({ __typeof__ (a) _c = (a); a = b; b = _c; })
#endif

#define IS_MR_CHANNEL(x) ((x) <= MR_CHANNEL_LAST)
#define IS_FREQ_CHANNEL(x) ((x) >= FREQ_CHANNEL_FIRST && (x) <= FREQ_CHANNEL_LAST)
#define IS_VALID_CHANNEL(x) ((x) < LAST_CHANNEL)
Expand Down
3 changes: 1 addition & 2 deletions ui/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,7 @@ void UI_DisplayMain(void)
UI_PrintString("ScnRng", 5, 0, line, 8);
sprintf(String, "%3u.%05u", gScanRangeStart / 100000, gScanRangeStart % 100000);
UI_PrintStringSmall(String, 56, 0, line);
uint32_t frq = gEeprom.VfoInfo[vfo_num].pRX->Frequency;
sprintf(String, "%3u.%05u", frq / 100000, frq % 100000);
sprintf(String, "%3u.%05u", gScanRangeStop / 100000, gScanRangeStop % 100000);
UI_PrintStringSmall(String, 56, 0, line + 1);
continue;
}
Expand Down

0 comments on commit 0872883

Please sign in to comment.