2022-09-27 22:01:09 +00:00
|
|
|
#include "subghz_last_settings.h"
|
2022-10-01 04:39:51 +00:00
|
|
|
#include "subghz_i.h"
|
2022-10-02 10:50:29 +00:00
|
|
|
#ifdef SUBGHZ_SAVE_DETECT_RAW_SETTING
|
2022-10-01 04:39:51 +00:00
|
|
|
#include <lib/subghz/protocols/raw.h>
|
2022-10-02 10:50:29 +00:00
|
|
|
#endif
|
2022-09-27 22:01:09 +00:00
|
|
|
|
|
|
|
#define TAG "SubGhzLastSettings"
|
|
|
|
|
2022-09-28 01:49:06 +00:00
|
|
|
#define SUBGHZ_LAST_SETTING_FILE_TYPE "Flipper SubGhz Last Setting File"
|
|
|
|
#define SUBGHZ_LAST_SETTING_FILE_VERSION 1
|
2022-09-28 03:46:45 +00:00
|
|
|
#define SUBGHZ_LAST_SETTINGS_PATH EXT_PATH("subghz/assets/last_subghz.settings")
|
2022-09-27 22:01:09 +00:00
|
|
|
// 1 = "AM650"
|
|
|
|
// "AM270", "AM650", "FM238", "FM476",
|
|
|
|
#define SUBGHZ_LAST_SETTING_DEFAULT_PRESET 1
|
|
|
|
#define SUBGHZ_LAST_SETTING_DEFAULT_FREQUENCY 433920000
|
2022-10-11 14:45:46 +00:00
|
|
|
#define SUBGHZ_LAST_SETTING_FREQUENCY_ANALYZER_FEEDBACK_LEVEL 2
|
2022-11-23 16:46:31 +00:00
|
|
|
#define SUBGHZ_LAST_SETTING_FREQUENCY_ANALYZER_TRIGGER -93.0f
|
2022-10-01 04:39:51 +00:00
|
|
|
|
2022-10-02 10:50:29 +00:00
|
|
|
#ifdef SUBGHZ_SAVE_DETECT_RAW_SETTING
|
2022-10-01 04:39:51 +00:00
|
|
|
#define SUBGHZ_LAST_SETTING_DEFAULT_READ_RAW 0
|
2022-10-02 12:50:03 +00:00
|
|
|
#define SUBGHZ_LAST_SETTING_FIELD_DETECT_RAW "DetectRaw"
|
2022-10-02 10:50:29 +00:00
|
|
|
#endif
|
2022-10-01 04:39:51 +00:00
|
|
|
|
|
|
|
#define SUBGHZ_LAST_SETTING_FIELD_FREQUENCY "Frequency"
|
2022-10-26 23:56:21 +00:00
|
|
|
//#define SUBGHZ_LAST_SETTING_FIELD_PRESET "Preset"
|
2022-10-11 14:45:46 +00:00
|
|
|
#define SUBGHZ_LAST_SETTING_FIELD_FREQUENCY_ANALYZER_FEEDBACK_LEVEL "FeedbackLevel"
|
2022-11-16 21:37:35 +00:00
|
|
|
#define SUBGHZ_LAST_SETTING_FIELD_FREQUENCY_ANALYZER_TRIGGER "FATrigger"
|
2022-09-27 22:01:09 +00:00
|
|
|
|
2022-09-28 01:49:06 +00:00
|
|
|
SubGhzLastSettings* subghz_last_settings_alloc(void) {
|
|
|
|
SubGhzLastSettings* instance = malloc(sizeof(SubGhzLastSettings));
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
void subghz_last_settings_free(SubGhzLastSettings* instance) {
|
|
|
|
furi_assert(instance);
|
|
|
|
free(instance);
|
|
|
|
}
|
|
|
|
|
|
|
|
void subghz_last_settings_load(SubGhzLastSettings* instance, size_t preset_count) {
|
2022-10-26 23:56:21 +00:00
|
|
|
UNUSED(preset_count);
|
2022-09-28 01:49:06 +00:00
|
|
|
furi_assert(instance);
|
2022-10-01 23:18:30 +00:00
|
|
|
#ifdef FURI_DEBUG
|
2022-09-28 01:49:06 +00:00
|
|
|
FURI_LOG_I(TAG, "subghz_last_settings_load");
|
|
|
|
#endif
|
|
|
|
|
2022-09-27 22:01:09 +00:00
|
|
|
Storage* storage = furi_record_open(RECORD_STORAGE);
|
2022-09-28 01:49:06 +00:00
|
|
|
FlipperFormat* fff_data_file = flipper_format_file_alloc(storage);
|
|
|
|
|
|
|
|
uint32_t temp_frequency = 0;
|
2022-10-11 14:45:46 +00:00
|
|
|
uint32_t temp_frequency_analyzer_feedback_level = 0;
|
2022-11-16 21:37:35 +00:00
|
|
|
float temp_frequency_analyzer_trigger = 0;
|
2022-10-26 23:56:21 +00:00
|
|
|
//int32_t temp_preset = 0;
|
2022-10-11 14:45:46 +00:00
|
|
|
bool frequency_analyzer_feedback_level_was_read = false;
|
2022-11-16 21:37:35 +00:00
|
|
|
bool frequency_analyzer_trigger_was_read = false;
|
2022-10-02 10:50:29 +00:00
|
|
|
#ifdef SUBGHZ_SAVE_DETECT_RAW_SETTING
|
2022-10-01 04:39:51 +00:00
|
|
|
uint32_t temp_read_raw = 0;
|
2022-10-02 10:50:29 +00:00
|
|
|
#endif
|
2022-09-28 01:49:06 +00:00
|
|
|
|
|
|
|
if(FSE_OK == storage_sd_status(storage) && SUBGHZ_LAST_SETTINGS_PATH &&
|
|
|
|
flipper_format_file_open_existing(fff_data_file, SUBGHZ_LAST_SETTINGS_PATH)) {
|
2022-10-26 23:56:21 +00:00
|
|
|
/*
|
2022-10-01 04:39:51 +00:00
|
|
|
flipper_format_read_int32(
|
2022-10-26 23:56:21 +00:00
|
|
|
fff_data_file, SUBGHZ_LAST_SETTING_FIELD_PRESET, (int32_t*)&temp_preset, 1);*/
|
2022-10-01 04:39:51 +00:00
|
|
|
flipper_format_read_uint32(
|
|
|
|
fff_data_file, SUBGHZ_LAST_SETTING_FIELD_FREQUENCY, (uint32_t*)&temp_frequency, 1);
|
2022-10-11 14:45:46 +00:00
|
|
|
frequency_analyzer_feedback_level_was_read = flipper_format_read_uint32(
|
|
|
|
fff_data_file,
|
|
|
|
SUBGHZ_LAST_SETTING_FIELD_FREQUENCY_ANALYZER_FEEDBACK_LEVEL,
|
|
|
|
(uint32_t*)&temp_frequency_analyzer_feedback_level,
|
|
|
|
1);
|
2022-11-16 21:37:35 +00:00
|
|
|
frequency_analyzer_trigger_was_read = flipper_format_read_float(
|
|
|
|
fff_data_file,
|
|
|
|
SUBGHZ_LAST_SETTING_FIELD_FREQUENCY_ANALYZER_TRIGGER,
|
|
|
|
(float*)&temp_frequency_analyzer_trigger,
|
|
|
|
1);
|
2022-10-02 10:50:29 +00:00
|
|
|
#ifdef SUBGHZ_SAVE_DETECT_RAW_SETTING
|
2022-10-01 04:39:51 +00:00
|
|
|
flipper_format_read_uint32(
|
|
|
|
fff_data_file, SUBGHZ_LAST_SETTING_FIELD_DETECT_RAW, (uint32_t*)&temp_read_raw, 1);
|
2022-10-02 10:50:29 +00:00
|
|
|
#endif
|
2022-09-28 01:49:06 +00:00
|
|
|
} else {
|
|
|
|
FURI_LOG_E(TAG, "Error open file %s", SUBGHZ_LAST_SETTINGS_PATH);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(temp_frequency == 0 || !furi_hal_subghz_is_tx_allowed(temp_frequency)) {
|
|
|
|
FURI_LOG_W(TAG, "Last used frequency not found or can't be used!");
|
2022-09-27 22:01:09 +00:00
|
|
|
instance->frequency = SUBGHZ_LAST_SETTING_DEFAULT_FREQUENCY;
|
|
|
|
instance->preset = SUBGHZ_LAST_SETTING_DEFAULT_PRESET;
|
2022-10-11 14:45:46 +00:00
|
|
|
instance->frequency_analyzer_feedback_level =
|
|
|
|
SUBGHZ_LAST_SETTING_FREQUENCY_ANALYZER_FEEDBACK_LEVEL;
|
2022-11-16 21:37:35 +00:00
|
|
|
instance->frequency_analyzer_trigger = SUBGHZ_LAST_SETTING_FREQUENCY_ANALYZER_TRIGGER;
|
2022-10-02 10:50:29 +00:00
|
|
|
#ifdef SUBGHZ_SAVE_DETECT_RAW_SETTING
|
2022-10-01 04:39:51 +00:00
|
|
|
instance->detect_raw = SUBGHZ_LAST_SETTING_DEFAULT_READ_RAW;
|
2022-10-02 10:50:29 +00:00
|
|
|
#endif
|
2022-09-28 01:49:06 +00:00
|
|
|
} else {
|
|
|
|
instance->frequency = temp_frequency;
|
2022-10-11 14:45:46 +00:00
|
|
|
instance->frequency_analyzer_feedback_level =
|
|
|
|
frequency_analyzer_feedback_level_was_read ?
|
|
|
|
temp_frequency_analyzer_feedback_level :
|
|
|
|
SUBGHZ_LAST_SETTING_FREQUENCY_ANALYZER_FEEDBACK_LEVEL;
|
2022-11-16 21:37:35 +00:00
|
|
|
|
|
|
|
instance->frequency_analyzer_trigger = frequency_analyzer_trigger_was_read ?
|
|
|
|
temp_frequency_analyzer_trigger :
|
|
|
|
SUBGHZ_LAST_SETTING_FREQUENCY_ANALYZER_TRIGGER;
|
2022-10-02 10:50:29 +00:00
|
|
|
#ifdef SUBGHZ_SAVE_DETECT_RAW_SETTING
|
2022-10-01 04:39:51 +00:00
|
|
|
instance->detect_raw = temp_read_raw;
|
2022-10-02 10:50:29 +00:00
|
|
|
#endif
|
2022-09-28 01:49:06 +00:00
|
|
|
|
2022-10-26 23:56:21 +00:00
|
|
|
/*if(temp_preset > (int32_t)preset_count - 1 || temp_preset < 0) {
|
|
|
|
FURI_LOG_W(TAG, "Last used preset no found");*/
|
|
|
|
instance->preset = SUBGHZ_LAST_SETTING_DEFAULT_PRESET;
|
|
|
|
/*/} else {
|
2022-09-28 01:49:06 +00:00
|
|
|
instance->preset = temp_preset;
|
2022-10-26 23:56:21 +00:00
|
|
|
}*/
|
2022-09-27 22:01:09 +00:00
|
|
|
}
|
2022-09-28 01:49:06 +00:00
|
|
|
|
2022-09-28 03:51:13 +00:00
|
|
|
flipper_format_file_close(fff_data_file);
|
2022-09-28 01:49:06 +00:00
|
|
|
flipper_format_free(fff_data_file);
|
2022-09-27 22:01:09 +00:00
|
|
|
furi_record_close(RECORD_STORAGE);
|
2022-09-28 01:49:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool subghz_last_settings_save(SubGhzLastSettings* instance) {
|
|
|
|
furi_assert(instance);
|
2022-10-01 23:18:30 +00:00
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
FURI_LOG_I(TAG, "last_settings_save");
|
2022-09-28 01:49:06 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
bool saved = false;
|
|
|
|
Storage* storage = furi_record_open(RECORD_STORAGE);
|
|
|
|
FlipperFormat* file = flipper_format_file_alloc(storage);
|
|
|
|
|
|
|
|
do {
|
|
|
|
if(FSE_OK != storage_sd_status(storage)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open file
|
|
|
|
if(!flipper_format_file_open_always(file, SUBGHZ_LAST_SETTINGS_PATH)) break;
|
|
|
|
|
|
|
|
// Write header
|
|
|
|
if(!flipper_format_write_header_cstr(
|
|
|
|
file, SUBGHZ_LAST_SETTING_FILE_TYPE, SUBGHZ_LAST_SETTING_FILE_VERSION))
|
|
|
|
break;
|
|
|
|
|
2022-10-26 23:56:21 +00:00
|
|
|
/*
|
2022-10-01 04:39:51 +00:00
|
|
|
if(!flipper_format_insert_or_update_int32(
|
|
|
|
file, SUBGHZ_LAST_SETTING_FIELD_PRESET, &instance->preset, 1)) {
|
|
|
|
break;
|
2022-10-26 23:56:21 +00:00
|
|
|
}*/
|
2022-10-01 04:39:51 +00:00
|
|
|
if(!flipper_format_insert_or_update_uint32(
|
|
|
|
file, SUBGHZ_LAST_SETTING_FIELD_FREQUENCY, &instance->frequency, 1)) {
|
2022-09-28 01:49:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-10-11 14:45:46 +00:00
|
|
|
if(!flipper_format_insert_or_update_uint32(
|
2022-10-12 02:15:45 +00:00
|
|
|
file,
|
|
|
|
SUBGHZ_LAST_SETTING_FIELD_FREQUENCY_ANALYZER_FEEDBACK_LEVEL,
|
|
|
|
&instance->frequency_analyzer_feedback_level,
|
|
|
|
1)) {
|
2022-10-11 14:45:46 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-11-16 21:37:35 +00:00
|
|
|
if(!flipper_format_insert_or_update_float(
|
|
|
|
file,
|
|
|
|
SUBGHZ_LAST_SETTING_FIELD_FREQUENCY_ANALYZER_TRIGGER,
|
|
|
|
&instance->frequency_analyzer_trigger,
|
|
|
|
1)) {
|
|
|
|
break;
|
|
|
|
}
|
2022-10-02 10:50:29 +00:00
|
|
|
#ifdef SUBGHZ_SAVE_DETECT_RAW_SETTING
|
2022-10-01 04:39:51 +00:00
|
|
|
if(!flipper_format_insert_or_update_uint32(
|
|
|
|
file, SUBGHZ_LAST_SETTING_FIELD_DETECT_RAW, &instance->detect_raw, 1)) {
|
2022-09-28 01:49:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-10-02 10:50:29 +00:00
|
|
|
#endif
|
2022-09-28 01:49:06 +00:00
|
|
|
saved = true;
|
|
|
|
} while(0);
|
|
|
|
|
|
|
|
if(!saved) {
|
|
|
|
FURI_LOG_E(TAG, "Error save file %s", SUBGHZ_LAST_SETTINGS_PATH);
|
|
|
|
}
|
|
|
|
|
2022-09-28 03:51:13 +00:00
|
|
|
flipper_format_file_close(file);
|
2022-09-28 01:49:06 +00:00
|
|
|
flipper_format_free(file);
|
|
|
|
furi_record_close(RECORD_STORAGE);
|
|
|
|
|
|
|
|
return saved;
|
2022-10-01 04:39:51 +00:00
|
|
|
}
|
|
|
|
|
2022-10-02 10:50:29 +00:00
|
|
|
#ifdef SUBGHZ_SAVE_DETECT_RAW_SETTING
|
2022-10-01 04:39:51 +00:00
|
|
|
void subghz_last_settings_set_detect_raw_values(void* context) {
|
|
|
|
furi_assert(context);
|
|
|
|
SubGhz* instance = (SubGhz*)context;
|
|
|
|
bool is_detect_raw = instance->last_settings->detect_raw > 0;
|
|
|
|
subghz_receiver_set_filter(
|
|
|
|
instance->txrx->receiver, is_detect_raw ? DETECT_RAW_TRUE : DETECT_RAW_FALSE);
|
|
|
|
subghz_protocol_decoder_raw_set_auto_mode(
|
|
|
|
subghz_receiver_search_decoder_base_by_name(
|
|
|
|
instance->txrx->receiver, SUBGHZ_PROTOCOL_RAW_NAME),
|
|
|
|
is_detect_raw);
|
2022-10-02 10:50:29 +00:00
|
|
|
}
|
|
|
|
#endif
|