Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SubGhz: frequency analyzer combined frequency detection method #1321

Merged
merged 3 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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