2021-10-10 17:35:10 +03:00
|
|
|
#include "subghz_frequency_analyzer_worker.h"
|
2022-05-19 20:05:01 +03:00
|
|
|
#include <lib/drivers/cc1101_regs.h>
|
2021-10-10 17:35:10 +03:00
|
|
|
|
|
|
|
#include <furi.h>
|
|
|
|
|
|
|
|
#include "../subghz_i.h"
|
|
|
|
|
2021-10-13 22:35:33 +03:00
|
|
|
static const uint8_t subghz_preset_ook_58khz[][2] = {
|
2022-05-19 20:04:52 +03:00
|
|
|
{CC1101_FIFOTHR, 0x47}, // The only important bit is ADC_RETENTION, FIFO Tx=33 Rx=32
|
|
|
|
{CC1101_MDMCFG4, 0xF5}, // Rx BW filter is 58.035714kHz
|
|
|
|
{CC1101_TEST2, 0x81}, // FIFOTHR ADC_RETENTION=1 matched value
|
|
|
|
{CC1101_TEST1, 0x35}, // FIFOTHR ADC_RETENTION=1 matched value
|
2021-10-13 22:35:33 +03:00
|
|
|
/* End */
|
|
|
|
{0, 0},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const uint8_t subghz_preset_ook_650khz[][2] = {
|
2022-05-19 20:04:52 +03:00
|
|
|
{CC1101_FIFOTHR, 0x07}, // The only important bit is ADC_RETENTION
|
|
|
|
{CC1101_MDMCFG4, 0x17}, // Rx BW filter is 650.000kHz
|
|
|
|
{CC1101_TEST2, 0x88},
|
|
|
|
{CC1101_TEST1, 0x31},
|
2021-10-13 22:35:33 +03:00
|
|
|
/* End */
|
|
|
|
{0, 0},
|
|
|
|
};
|
|
|
|
|
2021-10-10 17:35:10 +03:00
|
|
|
struct SubGhzFrequencyAnalyzerWorker {
|
|
|
|
FuriThread* thread;
|
|
|
|
|
|
|
|
volatile bool worker_running;
|
2022-05-19 20:04:52 +03:00
|
|
|
uint8_t count_repet;
|
2021-10-10 17:35:10 +03:00
|
|
|
FrequencyRSSI frequency_rssi_buf;
|
2022-04-14 17:05:40 +03:00
|
|
|
SubGhzSetting* setting;
|
2021-10-10 17:35:10 +03:00
|
|
|
|
|
|
|
float filVal;
|
|
|
|
|
|
|
|
SubGhzFrequencyAnalyzerWorkerPairCallback pair_callback;
|
|
|
|
void* context;
|
|
|
|
};
|
|
|
|
|
|
|
|
// running average with adaptive coefficient
|
|
|
|
static uint32_t subghz_frequency_analyzer_worker_expRunningAverageAdaptive(
|
|
|
|
SubGhzFrequencyAnalyzerWorker* instance,
|
|
|
|
uint32_t newVal) {
|
|
|
|
float k;
|
|
|
|
float newValFloat = newVal;
|
|
|
|
// the sharpness of the filter depends on the absolute value of the difference
|
2022-05-06 16:37:10 +03:00
|
|
|
if(fabs(newValFloat - instance->filVal) > 500000)
|
2021-10-10 17:35:10 +03:00
|
|
|
k = 0.9;
|
|
|
|
else
|
|
|
|
k = 0.03;
|
|
|
|
|
|
|
|
instance->filVal += (newValFloat - instance->filVal) * k;
|
|
|
|
return (uint32_t)instance->filVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Worker thread
|
|
|
|
*
|
|
|
|
* @param context
|
|
|
|
* @return exit code
|
|
|
|
*/
|
|
|
|
static int32_t subghz_frequency_analyzer_worker_thread(void* context) {
|
|
|
|
SubGhzFrequencyAnalyzerWorker* instance = context;
|
|
|
|
|
2021-10-13 18:39:37 +03:00
|
|
|
FrequencyRSSI frequency_rssi = {.frequency = 0, .rssi = 0};
|
2022-05-19 20:04:39 +03:00
|
|
|
float rssi;
|
|
|
|
uint32_t frequency;
|
|
|
|
uint32_t frequency_start;
|
2021-10-10 17:35:10 +03:00
|
|
|
|
|
|
|
//Start CC1101
|
|
|
|
furi_hal_subghz_reset();
|
2022-05-19 20:05:01 +03:00
|
|
|
furi_hal_subghz_load_preset(FuriHalSubGhzPresetOok650Async);
|
|
|
|
furi_hal_subghz_set_frequency(433920000);
|
|
|
|
furi_hal_subghz_flush_rx();
|
2022-05-05 01:34:44 +03:00
|
|
|
furi_hal_subghz_set_path(FuriHalSubGhzPathIsolate);
|
2022-05-19 20:05:01 +03:00
|
|
|
furi_hal_subghz_rx();
|
2021-10-10 17:35:10 +03:00
|
|
|
|
|
|
|
while(instance->worker_running) {
|
|
|
|
osDelay(10);
|
|
|
|
frequency_rssi.rssi = -127.0f;
|
2021-10-13 22:35:33 +03:00
|
|
|
furi_hal_subghz_idle();
|
2022-05-19 20:05:01 +03:00
|
|
|
furi_hal_subghz_load_registers(subghz_preset_ook_650khz);
|
2022-04-14 17:05:40 +03:00
|
|
|
for(size_t i = 0; i < subghz_setting_get_frequency_count(instance->setting); i++) {
|
|
|
|
if(furi_hal_subghz_is_frequency_valid(
|
|
|
|
subghz_setting_get_frequency(instance->setting, i))) {
|
2022-05-19 20:05:01 +03:00
|
|
|
furi_hal_subghz_idle();
|
|
|
|
frequency = furi_hal_subghz_set_frequency(
|
2022-04-14 17:05:40 +03:00
|
|
|
subghz_setting_get_frequency(instance->setting, i));
|
2022-05-19 20:05:01 +03:00
|
|
|
furi_hal_subghz_rx();
|
2022-05-19 20:04:39 +03:00
|
|
|
osDelay(3);
|
2021-10-10 17:35:10 +03:00
|
|
|
rssi = furi_hal_subghz_get_rssi();
|
|
|
|
if(frequency_rssi.rssi < rssi) {
|
|
|
|
frequency_rssi.rssi = rssi;
|
|
|
|
frequency_rssi.frequency = frequency;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-19 20:05:01 +03:00
|
|
|
if(frequency_rssi.rssi > -90.0) {
|
2022-05-19 20:04:39 +03:00
|
|
|
// -0.5 ... 433.92 ... +0.5
|
2022-05-19 20:05:01 +03:00
|
|
|
frequency_start = frequency_rssi.frequency - 250000;
|
|
|
|
//step 10KHz
|
2022-05-19 20:04:39 +03:00
|
|
|
frequency_rssi.rssi = -127.0;
|
|
|
|
furi_hal_subghz_idle();
|
2022-05-19 20:05:01 +03:00
|
|
|
furi_hal_subghz_load_registers(subghz_preset_ook_58khz);
|
2022-05-19 20:04:39 +03:00
|
|
|
for(uint32_t i = frequency_start; i < frequency_start + 500000; i += 10000) {
|
2021-10-10 17:35:10 +03:00
|
|
|
if(furi_hal_subghz_is_frequency_valid(i)) {
|
2022-05-19 20:05:01 +03:00
|
|
|
furi_hal_subghz_idle();
|
|
|
|
frequency = furi_hal_subghz_set_frequency(i);
|
|
|
|
furi_hal_subghz_rx();
|
|
|
|
osDelay(3);
|
2021-10-10 17:35:10 +03:00
|
|
|
rssi = furi_hal_subghz_get_rssi();
|
|
|
|
if(frequency_rssi.rssi < rssi) {
|
|
|
|
frequency_rssi.rssi = rssi;
|
|
|
|
frequency_rssi.frequency = frequency;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-19 20:05:01 +03:00
|
|
|
if(frequency_rssi.rssi > -90.0) {
|
2022-05-19 20:04:39 +03:00
|
|
|
instance->count_repet = 20;
|
2021-10-10 17:35:10 +03:00
|
|
|
if(instance->filVal) {
|
|
|
|
frequency_rssi.frequency =
|
|
|
|
subghz_frequency_analyzer_worker_expRunningAverageAdaptive(
|
|
|
|
instance, frequency_rssi.frequency);
|
|
|
|
}
|
2022-05-19 20:04:52 +03:00
|
|
|
if(instance->pair_callback)
|
2021-10-10 17:35:10 +03:00
|
|
|
instance->pair_callback(
|
|
|
|
instance->context, frequency_rssi.frequency, frequency_rssi.rssi);
|
2022-05-19 20:04:52 +03:00
|
|
|
|
2021-10-10 17:35:10 +03:00
|
|
|
} else {
|
2022-05-19 20:04:52 +03:00
|
|
|
if(instance->count_repet > 0) {
|
|
|
|
instance->count_repet--;
|
2021-10-10 17:35:10 +03:00
|
|
|
} else {
|
|
|
|
instance->filVal = 0;
|
|
|
|
if(instance->pair_callback) instance->pair_callback(instance->context, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Stop CC1101
|
|
|
|
furi_hal_subghz_idle();
|
|
|
|
furi_hal_subghz_sleep();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SubGhzFrequencyAnalyzerWorker* subghz_frequency_analyzer_worker_alloc() {
|
2022-02-18 22:53:46 +03:00
|
|
|
SubGhzFrequencyAnalyzerWorker* instance = malloc(sizeof(SubGhzFrequencyAnalyzerWorker));
|
2021-10-10 17:35:10 +03:00
|
|
|
|
|
|
|
instance->thread = furi_thread_alloc();
|
2022-03-03 12:48:56 +03:00
|
|
|
furi_thread_set_name(instance->thread, "SubGhzFAWorker");
|
2021-10-10 17:35:10 +03:00
|
|
|
furi_thread_set_stack_size(instance->thread, 2048);
|
|
|
|
furi_thread_set_context(instance->thread, instance);
|
|
|
|
furi_thread_set_callback(instance->thread, subghz_frequency_analyzer_worker_thread);
|
|
|
|
|
2022-04-14 17:05:40 +03:00
|
|
|
instance->setting = subghz_setting_alloc();
|
|
|
|
subghz_setting_load(instance->setting, "/ext/subghz/assets/setting_frequency_analyzer_user");
|
2021-10-10 17:35:10 +03:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
void subghz_frequency_analyzer_worker_free(SubGhzFrequencyAnalyzerWorker* instance) {
|
|
|
|
furi_assert(instance);
|
|
|
|
|
|
|
|
furi_thread_free(instance->thread);
|
2022-04-14 17:05:40 +03:00
|
|
|
subghz_setting_free(instance->setting);
|
2021-10-10 17:35:10 +03:00
|
|
|
free(instance);
|
|
|
|
}
|
|
|
|
|
|
|
|
void subghz_frequency_analyzer_worker_set_pair_callback(
|
|
|
|
SubGhzFrequencyAnalyzerWorker* instance,
|
|
|
|
SubGhzFrequencyAnalyzerWorkerPairCallback callback,
|
|
|
|
void* context) {
|
|
|
|
furi_assert(instance);
|
|
|
|
furi_assert(context);
|
|
|
|
instance->pair_callback = callback;
|
|
|
|
instance->context = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
void subghz_frequency_analyzer_worker_start(SubGhzFrequencyAnalyzerWorker* instance) {
|
|
|
|
furi_assert(instance);
|
|
|
|
furi_assert(!instance->worker_running);
|
|
|
|
|
|
|
|
instance->worker_running = true;
|
|
|
|
|
|
|
|
furi_thread_start(instance->thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
void subghz_frequency_analyzer_worker_stop(SubGhzFrequencyAnalyzerWorker* instance) {
|
|
|
|
furi_assert(instance);
|
|
|
|
furi_assert(instance->worker_running);
|
|
|
|
|
|
|
|
instance->worker_running = false;
|
|
|
|
|
|
|
|
furi_thread_join(instance->thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool subghz_frequency_analyzer_worker_is_running(SubGhzFrequencyAnalyzerWorker* instance) {
|
|
|
|
furi_assert(instance);
|
|
|
|
return instance->worker_running;
|
2022-05-19 20:04:23 +03:00
|
|
|
}
|