Skip to content

Commit

Permalink
Merge pull request #87 from kamilsss655/rc19.8
Browse files Browse the repository at this point in the history
Rc19.8
  • Loading branch information
kamilsss655 authored Jan 20, 2024
2 parents 07f191e + fb28775 commit 12bf25a
Showing 1 changed file with 82 additions and 40 deletions.
122 changes: 82 additions & 40 deletions app/spectrum.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,26 @@ struct FrequencyBandInfo {
};

bool gTailFound;
bool isBlacklistApplied;

#define F_MAX frequencyBandTable[ARRAY_SIZE(frequencyBandTable) - 1].upper

#ifdef ENABLE_SPECTRUM_CHANNEL_SCAN
Mode appMode;
//Idea - make this user adjustable to compensate for different antennas, frontends, conditions
#define UHF_NOISE_FLOOR 40
bool scanPassComplete;
uint16_t rssiNormalization[128];
#define MAX_ATTENUATION 160
#define ATTENUATE_STEP 10
bool isNormalizationApplied;
bool isAttenuationApplied;
uint8_t gainOffset[128];
uint8_t attenuationOffset[128];
uint8_t scanChannel[MR_CHANNEL_LAST+3];
uint8_t scanChannelsCount;
void ToggleScanList();
void AutoAdjustResolution();
void ToggleNormalizeRssi();
void ToggleNormalizeRssi(bool on);
void Attenuate(uint8_t amount);
#endif

const uint16_t RSSI_MAX_VALUE = 65535;
Expand Down Expand Up @@ -389,8 +395,10 @@ uint16_t GetRssi() {
rssi+=UHF_NOISE_FLOOR;
}

if (appMode==CHANNEL_MODE)
rssi+=rssiNormalization[scanInfo.i];
if (appMode==CHANNEL_MODE) {
rssi+=gainOffset[scanInfo.i];
rssi-=attenuationOffset[scanInfo.i];
}

#endif
return rssi;
Expand Down Expand Up @@ -467,9 +475,11 @@ static void ResetBlacklist() {
if(appMode==CHANNEL_MODE){
LoadValidMemoryChannels();
AutoAdjustResolution();
memset(rssiNormalization, 0, sizeof(rssiNormalization));
ToggleNormalizeRssi(false);
memset(attenuationOffset, 0, sizeof(attenuationOffset));
isAttenuationApplied = false;
}

isBlacklistApplied = false;
RelaunchScan();
}

Expand All @@ -490,8 +500,9 @@ static void UpdateScanInfo() {
scanInfo.fPeak = scanInfo.f;
scanInfo.iPeak = scanInfo.i;
}

if (scanInfo.rssi < scanInfo.rssiMin) {
// add attenuation offset to prevent noise floor lowering when attenuated rx is over
// essentially we measure non-attenuated lowest rssi
if (scanInfo.rssi+attenuationOffset[scanInfo.i] < scanInfo.rssiMin) {
scanInfo.rssiMin = scanInfo.rssi;
settings.dbMin = Rssi2DBm(scanInfo.rssiMin);
redrawStatus = true;
Expand Down Expand Up @@ -727,6 +738,7 @@ static void Blacklist() {
rssiHistory[ScanRangeidx()] = RSSI_MAX_VALUE;
#endif
rssiHistory[peak.i] = RSSI_MAX_VALUE;
isBlacklistApplied = true;
ResetPeak();
ToggleRX(false);
ResetScanStats();
Expand Down Expand Up @@ -857,8 +869,14 @@ static void DrawF(uint32_t f) {
static void DrawNums() {

if (currentState == SPECTRUM) {
sprintf(String, "%ux", GetStepsCount());
if(isNormalizationApplied){
sprintf(String, "N(%ux)", GetStepsCount());
}
else {
sprintf(String, "%ux", GetStepsCount());
}
GUI_DisplaySmallest(String, 0, 1, false, true);

if (appMode==CHANNEL_MODE)
{
sprintf(String, "%s", scanListOptions[settings.scanList]);
Expand All @@ -883,6 +901,16 @@ static void DrawNums() {
sprintf(String, "M:%d", scanChannel[0]+1);
GUI_DisplaySmallest(String, 0, 49, false, true);

if(isAttenuationApplied){
sprintf(String, "ATT");
GUI_DisplaySmallest(String, 48, 49, false, true);
}

if(isBlacklistApplied){
sprintf(String, "BL");
GUI_DisplaySmallest(String, 63, 49, false, true);
}

sprintf(String, "M:%d", scanChannel[GetStepsCount()-1]+1);
GUI_DisplaySmallest(String, 108, 49, false, true);
}
Expand Down Expand Up @@ -968,10 +996,20 @@ static void OnKeyDown(uint8_t key) {
}
break;
case KEY_2:
UpdateFreqChangeStep(true);
if(appMode==CHANNEL_MODE){
ToggleNormalizeRssi(!isNormalizationApplied);
}
else {
UpdateFreqChangeStep(true);
}
break;
case KEY_8:
UpdateFreqChangeStep(false);
if(appMode==CHANNEL_MODE){
ToggleBacklight();
}
else{
UpdateFreqChangeStep(false);
}
break;
case KEY_UP:
#ifdef ENABLE_SCAN_RANGES
Expand Down Expand Up @@ -1029,16 +1067,13 @@ static void OnKeyDown(uint8_t key) {
}
break;
case KEY_SIDE2:
#ifdef ENABLE_SPECTRUM_CHANNEL_SCAN
if(appMode==CHANNEL_MODE){
ToggleNormalizeRssi();
}
else {
ToggleBacklight();
}
#elif
if(appMode==CHANNEL_MODE) {
Attenuate(ATTENUATE_STEP);
}
else {
ToggleBacklight();
#endif
}

break;
case KEY_PTT:
#ifdef ENABLE_SPECTRUM_COPY_VFO
Expand Down Expand Up @@ -1363,12 +1398,6 @@ static void UpdateScan() {
NextScanStep();
return;
}
#ifdef ENABLE_SPECTRUM_CHANNEL_SCAN
else {
if(scanPassComplete == false)
scanPassComplete = true;
}
#endif

if(scanInfo.measurementsCount < 128)
memset(&rssiHistory[scanInfo.measurementsCount], 0,
Expand Down Expand Up @@ -1598,26 +1627,39 @@ void APP_RunSpectrum() {
}
// 2024 by kamilsss655 -> https://github.com/kamilsss655
// flattens spectrum by bringing all the rssi readings to the peak value
void ToggleNormalizeRssi()
void ToggleNormalizeRssi(bool on)
{
// we don't want to normalize when there is already active signal RX
if(IsPeakOverLevel())
if(IsPeakOverLevel() && on)
return;

memset(rssiNormalization, 0, sizeof(rssiNormalization));

// we should do a full scan without correction, and only then apply correction
if(scanPassComplete == false){
newScanStart = true;
return;
}
else
{
if(on) {
for(uint8_t i = 0; i < ARRAY_SIZE(rssiHistory); i++)
{
rssiNormalization[i] = peak.rssi - rssiHistory[i];
gainOffset[i] = peak.rssi - rssiHistory[i];
}
scanPassComplete = false;
isNormalizationApplied = true;
}
else {
memset(gainOffset, 0, sizeof(gainOffset));
isNormalizationApplied = false;
}
RelaunchScan();
}

void Attenuate(uint8_t amount)
{
// we don't want to attenuate random scan measurments, only active rx measurment
if(!IsPeakOverLevel())
return;
// we don't want to lower the whole scan minimum rssi, otherwise the screen re-renders to show new lowest signal
if((GetRssi()-amount) <= scanInfo.rssiMin)
return;
// idea: consider amount to be 10% of rssiMax-rssiMin
if(attenuationOffset[scanInfo.i] < MAX_ATTENUATION){
attenuationOffset[scanInfo.i]+=amount;
isAttenuationApplied = true;
}

}
#endif

0 comments on commit 12bf25a

Please sign in to comment.