Skip to content

Commit

Permalink
Merge pull request #301 from bmorcelli/main
Browse files Browse the repository at this point in the history
Enhanced CC1101 Freq Scan using rssi
  • Loading branch information
pr3y authored Oct 3, 2024
2 parents 44824b6 + 9dd3d3d commit ff23891
Showing 1 changed file with 73 additions and 25 deletions.
98 changes: 73 additions & 25 deletions src/modules/rf/rf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,11 @@ static const float subghz_frequency_list[] = {
906.400f, 915.000f, 925.000f, 928.000f
};

struct FreqFound {
float freq;
int rssi;
};

void rf_scan_copy() {
if (!initRfModule("rx")) {
return;
Expand Down Expand Up @@ -1180,15 +1185,18 @@ void rf_scan_copy() {
{ 48, 56 }, // 779-928 MHz
{ 0, sizeof(subghz_frequency_list) / sizeof(subghz_frequency_list[0]) - 1} // All ranges
};

#define _MAX_TRIES 3
uint8_t _try=0;
RestartScan:
drawMainBorder();
tft.setCursor(10, 28);
tft.setTextSize(FP);
tft.println("Waiting for signal.");
tft.setCursor(10, tft.getCursorY());
if (RfFxdFreq) {
if(_try>=_MAX_TRIES) tft.setTextColor(getColorVariation(FGCOLOR), BGCOLOR);
tft.println("Freq: " + String(RfFreq) + " MHz");
if(_try>=_MAX_TRIES) tft.setTextColor(FGCOLOR, BGCOLOR);
}
else {
tft.println("Range: " + String(sz_range[RfScanRange]));
Expand All @@ -1200,20 +1208,56 @@ void rf_scan_copy() {
int idx = range_limits[RfScanRange][0];

float found_freq = 0.f;

float frequency;
if(RfFxdFreq) {
frequency = RfFreq;
ELECHOUSE_cc1101.setMHZ(frequency);
delay(50);
}
int rssiThreshold = -60;
int rssi;
FreqFound _freqs[_MAX_TRIES]; // get the best RSSI out of 3 tries
for (;;) {
FastScan:
if (idx < range_limits[RfScanRange][0] || idx > range_limits[RfScanRange][1]) {
idx = range_limits[RfScanRange][0];
}

if (checkEscPress()) {
break;
}
if(!RfFxdFreq) { // Try FastScan
frequency = subghz_frequency_list[idx];
ELECHOUSE_cc1101.setMHZ(frequency);
delay(5); // delay to let CC1101 set the frequency
rssi = ELECHOUSE_cc1101.getRssi();
if(checkSelPress()) Serial.println("Frequency: " + String(frequency) + " - rssi: " + String(rssi));

if(rssi>rssiThreshold) {
_freqs[_try].freq=frequency;
_freqs[_try].rssi=rssi;
_try++;
if(_try>=_MAX_TRIES) {
int max_index=0;
for (int i = 1; i < _MAX_TRIES; i++) {
if (_freqs[i].rssi > _freqs[max_index].rssi) max_index = i;
}
RfFreq=_freqs[max_index].freq;
frequency = _freqs[max_index].freq;
RfFxdFreq=true;
Serial.println("Frequency Found: " + String(frequency));
goto RestartScan;
} else ++idx;
}
else {
++idx;
if(checkNextPress()) goto Menu;
goto FastScan;
}

}

float frequency = RfFxdFreq ? RfFreq : subghz_frequency_list[idx];

ELECHOUSE_cc1101.setMHZ(frequency);
delay(50);
//frequency = RfFxdFreq ? RfFreq : subghz_frequency_list[idx];

if (rcswitch.available()) {
unsigned long value = rcswitch.getReceivedValue();
Expand Down Expand Up @@ -1268,6 +1312,7 @@ void rf_scan_copy() {
}

if (checkNextPress()) {
Menu:
int option = 0;
if (found_freq) {
options = {
Expand All @@ -1283,25 +1328,28 @@ void rf_scan_copy() {
}

if (option == 1) {
options = {
{ String("Fxd [" + String(RfFreq) + "]").c_str(), [&]() { RfFxdFreq = 1; } },
{ sz_range[0], [&]() { RfScanRange = 0; RfFxdFreq = 0; } },
{ sz_range[1], [&]() { RfScanRange = 1; RfFxdFreq = 0; } },
{ sz_range[2], [&]() { RfScanRange = 2; RfFxdFreq = 0; } },
{ sz_range[3], [&]() { RfScanRange = 3; RfFxdFreq = 0; } },
};

delay(200);
loopOptions(options);

if (RfFxdFreq) {
displayRedStripe("Scan freq set to " + String(RfFreq), TFT_WHITE, FGCOLOR);
}
else {
displayRedStripe("Range set to " + String(sz_range[RfScanRange]), TFT_WHITE, FGCOLOR);
}

saveConfigs();
if(RfModule) {
options = {
{ String("Fxd [" + String(RfFreq) + "]").c_str(), [&]() { RfFxdFreq = 1; ELECHOUSE_cc1101.setMHZ(RfFreq); } },
{ sz_range[0], [&]() { RfScanRange = 0; RfFxdFreq = 0; } },
{ sz_range[1], [&]() { RfScanRange = 1; RfFxdFreq = 0; } },
{ sz_range[2], [&]() { RfScanRange = 2; RfFxdFreq = 0; } },
{ sz_range[3], [&]() { RfScanRange = 3; RfFxdFreq = 0; } },
};

delay(200);
loopOptions(options);

if (RfFxdFreq) {
displayRedStripe("Scan freq set to " + String(RfFreq), TFT_WHITE, FGCOLOR);
}
else {
displayRedStripe("Range set to " + String(sz_range[RfScanRange]), TFT_WHITE, FGCOLOR);
}
saveConfigs();
delay(1500);
} else displayWarning("Need CC1101");
goto RestartScan;
}
else if (option == 2) {
option = 0;
Expand Down

0 comments on commit ff23891

Please sign in to comment.