Skip to content

Commit

Permalink
SubGhz: frequency analyzer combined frequency detection method (#1321)
Browse files Browse the repository at this point in the history
Co-authored-by: あく <alleteam@gmail.com>
  • Loading branch information
2 people authored and hedger committed Jun 21, 2022
1 parent 94a18a4 commit b25cdd1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 25 deletions.
68 changes: 45 additions & 23 deletions applications/subghz/helpers/subghz_frequency_analyzer_worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ static uint32_t subghz_frequency_analyzer_worker_expRunningAverageAdaptive(
static int32_t subghz_frequency_analyzer_worker_thread(void* context) {
SubGhzFrequencyAnalyzerWorker* instance = context;

FrequencyRSSI frequency_rssi = {.frequency = 0, .rssi = 0};
FrequencyRSSI frequency_rssi = {
.frequency_coarse = 0, .rssi_coarse = 0, .frequency_fine = 0, .rssi_fine = 0};
float rssi = 0;
uint32_t frequency = 0;
CC1101Status status;
Expand Down Expand Up @@ -105,7 +106,8 @@ static int32_t subghz_frequency_analyzer_worker_thread(void* context) {
float rssi_avg = 0;
size_t rssi_avg_samples = 0;

frequency_rssi.rssi = -127.0f;
frequency_rssi.rssi_coarse = -127.0f;
frequency_rssi.rssi_fine = -127.0f;
furi_hal_subghz_idle();
subghz_frequency_analyzer_worker_load_registers(subghz_preset_ook_650khz);

Expand Down Expand Up @@ -137,9 +139,9 @@ static int32_t subghz_frequency_analyzer_worker_thread(void* context) {

if(rssi < rssi_min) rssi_min = rssi;

if(frequency_rssi.rssi < rssi) {
frequency_rssi.rssi = rssi;
frequency_rssi.frequency = frequency;
if(frequency_rssi.rssi_coarse < rssi) {
frequency_rssi.rssi_coarse = rssi;
frequency_rssi.frequency_coarse = frequency;
}
}
}
Expand All @@ -148,20 +150,17 @@ static int32_t subghz_frequency_analyzer_worker_thread(void* context) {
TAG,
"RSSI: avg %f, max %f at %u, min %f",
(double)(rssi_avg / rssi_avg_samples),
(double)frequency_rssi.rssi,
frequency_rssi.frequency,
(double)frequency_rssi.rssi_coarse,
frequency_rssi.frequency_coarse,
(double)rssi_min);

// Second stage: fine scan
if(frequency_rssi.rssi > SUBGHZ_FREQUENCY_ANALYZER_THRESHOLD) {
FURI_LOG_D(TAG, "~:%u:%f", frequency_rssi.frequency, (double)frequency_rssi.rssi);

frequency_rssi.rssi = -127.0;
if(frequency_rssi.rssi_coarse > SUBGHZ_FREQUENCY_ANALYZER_THRESHOLD) {
furi_hal_subghz_idle();
subghz_frequency_analyzer_worker_load_registers(subghz_preset_ook_58khz);
//-0.3 ... 433.92 ... +0.3 step 10KHz
for(uint32_t i = frequency_rssi.frequency - 300000;
i < frequency_rssi.frequency + 300000;
//for example -0.3 ... 433.92 ... +0.3 step 20KHz
for(uint32_t i = frequency_rssi.frequency_coarse - 300000;
i < frequency_rssi.frequency_coarse + 300000;
i += 20000) {
if(furi_hal_subghz_is_frequency_valid(i)) {
furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
Expand All @@ -183,28 +182,51 @@ static int32_t subghz_frequency_analyzer_worker_thread(void* context) {

FURI_LOG_T(TAG, "#:%u:%f", frequency, (double)rssi);

if(frequency_rssi.rssi < rssi) {
frequency_rssi.rssi = rssi;
frequency_rssi.frequency = frequency;
if(frequency_rssi.rssi_fine < rssi) {
frequency_rssi.rssi_fine = rssi;
frequency_rssi.frequency_fine = frequency;
}
}
}
}

// Deliver results
if(frequency_rssi.rssi > SUBGHZ_FREQUENCY_ANALYZER_THRESHOLD) {
FURI_LOG_D(TAG, "=:%u:%f", frequency_rssi.frequency, (double)frequency_rssi.rssi);
// Deliver results fine
if(frequency_rssi.rssi_fine > SUBGHZ_FREQUENCY_ANALYZER_THRESHOLD) {
FURI_LOG_D(
TAG, "=:%u:%f", frequency_rssi.frequency_fine, (double)frequency_rssi.rssi_fine);

instance->sample_hold_counter = 20;
if(instance->filVal) {
frequency_rssi.frequency_fine =
subghz_frequency_analyzer_worker_expRunningAverageAdaptive(
instance, frequency_rssi.frequency_fine);
}
// Deliver callback
if(instance->pair_callback) {
instance->pair_callback(
instance->context, frequency_rssi.frequency_fine, frequency_rssi.rssi_fine);
}
} else if( // Deliver results coarse
(frequency_rssi.rssi_coarse > SUBGHZ_FREQUENCY_ANALYZER_THRESHOLD) &&
(instance->sample_hold_counter < 10)) {
FURI_LOG_D(
TAG,
"~:%u:%f",
frequency_rssi.frequency_coarse,
(double)frequency_rssi.rssi_coarse);

instance->sample_hold_counter = 20;
if(instance->filVal) {
frequency_rssi.frequency =
frequency_rssi.frequency_coarse =
subghz_frequency_analyzer_worker_expRunningAverageAdaptive(
instance, frequency_rssi.frequency);
instance, frequency_rssi.frequency_coarse);
}
// Deliver callback
if(instance->pair_callback) {
instance->pair_callback(
instance->context, frequency_rssi.frequency, frequency_rssi.rssi);
instance->context,
frequency_rssi.frequency_coarse,
frequency_rssi.rssi_coarse);
}
} else {
if(instance->sample_hold_counter > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ typedef void (
*SubGhzFrequencyAnalyzerWorkerPairCallback)(void* context, uint32_t frequency, float rssi);

typedef struct {
uint32_t frequency;
float rssi;
uint32_t frequency_coarse;
float rssi_coarse;
uint32_t frequency_fine;
float rssi_fine;
} FrequencyRSSI;

/** Allocate SubGhzFrequencyAnalyzerWorker
Expand Down

0 comments on commit b25cdd1

Please sign in to comment.