Skip to content

Commit

Permalink
Merge pull request #77 from derskythe/subghz-save-settings-v2
Browse files Browse the repository at this point in the history
SubGHz save settings version 2
  • Loading branch information
xMasterX committed Sep 28, 2022
2 parents 4271246 + b934c41 commit 9c62e1f
Show file tree
Hide file tree
Showing 13 changed files with 328 additions and 42 deletions.
15 changes: 12 additions & 3 deletions applications/main/subghz/scenes/subghz_scene_frequency_analyzer.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "../subghz_i.h"
#include "../views/subghz_frequency_analyzer.h"
#include <dolphin/dolphin.h>

void subghz_scene_frequency_analyzer_callback(SubGhzCustomEvent event, void* context) {
Expand All @@ -17,8 +16,18 @@ void subghz_scene_frequency_analyzer_on_enter(void* context) {
}

bool subghz_scene_frequency_analyzer_on_event(void* context, SceneManagerEvent event) {
UNUSED(context);
UNUSED(event);
SubGhz* subghz = context;
if(event.type == SceneManagerEventTypeCustom &&
event.event == SubGhzCustomEventViewReceiverOK) {
uint32_t frequency =
subghz_frequency_analyzer_get_frequency_to_save(subghz->subghz_frequency_analyzer);
if(frequency > 0) {
subghz->last_settings->frequency = frequency;
subghz_last_settings_save(subghz->last_settings);
}

return true;
}
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions applications/main/subghz/scenes/subghz_scene_need_saving.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ bool subghz_scene_need_saving_on_event(void* context, SceneManagerEvent event) {
subghz->txrx->rx_key_state = SubGhzRxKeyStateIDLE;
subghz_preset_init(
subghz,
"AM650",
subghz_setting_get_default_frequency(subghz->setting),
subghz_setting_get_preset_name(subghz->setting, subghz->last_settings->preset),
subghz->last_settings->frequency,
NULL,
0);
scene_manager_search_and_switch_to_previous_scene(
Expand Down
4 changes: 2 additions & 2 deletions applications/main/subghz/scenes/subghz_scene_read_raw.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ bool subghz_scene_read_raw_on_event(void* context, SceneManagerEvent event) {
//Restore default setting
subghz_preset_init(
subghz,
"AM650",
subghz_setting_get_default_frequency(subghz->setting),
subghz_setting_get_preset_name(subghz->setting, subghz->last_settings->preset),
subghz->last_settings->frequency,
NULL,
0);
if(!scene_manager_search_and_switch_to_previous_scene(
Expand Down
14 changes: 11 additions & 3 deletions applications/main/subghz/scenes/subghz_scene_receiver.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "../subghz_i.h"
#include "../views/receiver.h"

#define TAG "SubGhzSceneReceiver"

const NotificationSequence subghz_sequence_rx = {
&message_green_255,

Expand Down Expand Up @@ -103,7 +105,11 @@ void subghz_scene_receiver_on_enter(void* context) {

if(subghz->txrx->rx_key_state == SubGhzRxKeyStateIDLE) {
subghz_preset_init(
subghz, "AM650", subghz_setting_get_default_frequency(subghz->setting), NULL, 0);
subghz,
subghz_setting_get_preset_name(subghz->setting, subghz->last_settings->preset),
subghz->last_settings->frequency,
NULL,
0);
subghz_history_reset(subghz->txrx->history);
subghz->txrx->rx_key_state = SubGhzRxKeyStateStart;
}
Expand Down Expand Up @@ -169,8 +175,8 @@ bool subghz_scene_receiver_on_event(void* context, SceneManagerEvent event) {
subghz->txrx->rx_key_state = SubGhzRxKeyStateIDLE;
subghz_preset_init(
subghz,
"AM650",
subghz_setting_get_default_frequency(subghz->setting),
subghz_setting_get_preset_name(subghz->setting, subghz->last_settings->preset),
subghz->last_settings->frequency,
NULL,
0);
scene_manager_search_and_switch_to_previous_scene(
Expand All @@ -188,6 +194,8 @@ bool subghz_scene_receiver_on_event(void* context, SceneManagerEvent event) {
subghz->state_notifications = SubGhzNotificationStateIDLE;
subghz->txrx->idx_menu_chosen =
subghz_view_receiver_get_idx_menu(subghz->subghz_receiver);
scene_manager_set_scene_state(
subghz->scene_manager, SubGhzViewIdReceiver, SubGhzCustomEventManagerSet);
scene_manager_next_scene(subghz->scene_manager, SubGhzSceneReceiverConfig);
consumed = true;
break;
Expand Down
55 changes: 32 additions & 23 deletions applications/main/subghz/scenes/subghz_scene_receiver_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <lib/subghz/protocols/raw.h>

#define TAG "SubGhzSceneReceiverConfig"

enum SubGhzSettingIndex {
SubGhzSettingIndexFrequency,
SubGhzSettingIndexHopping,
Expand Down Expand Up @@ -145,6 +147,8 @@ static void subghz_scene_receiver_config_set_frequency(VariableItem* item) {
(subghz_setting_get_frequency(subghz->setting, index) % 1000000) / 10000);
variable_item_set_current_value_text(item, text_buf);
subghz->txrx->preset->frequency = subghz_setting_get_frequency(subghz->setting, index);
subghz->last_settings->frequency = subghz->txrx->preset->frequency;
subghz_setting_set_default_frequency(subghz->setting, subghz->txrx->preset->frequency);
} else {
variable_item_set_current_value_index(
item, subghz_setting_get_frequency_default_index(subghz->setting));
Expand All @@ -154,11 +158,13 @@ static void subghz_scene_receiver_config_set_frequency(VariableItem* item) {
static void subghz_scene_receiver_config_set_preset(VariableItem* item) {
SubGhz* subghz = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);
variable_item_set_current_value_text(
item, subghz_setting_get_preset_name(subghz->setting, index));
const char* preset_name = subghz_setting_get_preset_name(subghz->setting, index);
variable_item_set_current_value_text(item, preset_name);
subghz->last_settings->preset = index;

subghz_preset_init(
subghz,
subghz_setting_get_preset_name(subghz->setting, index),
preset_name,
subghz->txrx->preset->frequency,
subghz_setting_get_preset_data(subghz->setting, index),
subghz_setting_get_preset_data_size(subghz->setting, index));
Expand Down Expand Up @@ -238,6 +244,13 @@ void subghz_scene_receiver_config_on_enter(void* context) {
VariableItem* item;
uint8_t value_index;

#if FURI_DEBUG
FURI_LOG_D(
TAG,
"last frequency: %d, preset: %d",
subghz->last_settings->frequency,
subghz->last_settings->preset);
#endif
item = variable_item_list_add(
subghz->variable_item_list,
"Frequency:",
Expand All @@ -258,20 +271,6 @@ void subghz_scene_receiver_config_on_enter(void* context) {
(subghz_setting_get_frequency(subghz->setting, value_index) % 1000000) / 10000);
variable_item_set_current_value_text(item, text_buf);

if(scene_manager_get_scene_state(subghz->scene_manager, SubGhzSceneReadRAW) !=
SubGhzCustomEventManagerSet) {
item = variable_item_list_add(
subghz->variable_item_list,
"Hopping:",
HOPPING_COUNT,
subghz_scene_receiver_config_set_hopping_running,
subghz);
value_index = subghz_scene_receiver_config_hopper_value_index(
subghz->txrx->hopper_state, hopping_value, HOPPING_COUNT, subghz);
variable_item_set_current_value_index(item, value_index);
variable_item_set_current_value_text(item, hopping_text[value_index]);
}

item = variable_item_list_add(
subghz->variable_item_list,
"Modulation:",
Expand All @@ -286,6 +285,19 @@ void subghz_scene_receiver_config_on_enter(void* context) {

if(scene_manager_get_scene_state(subghz->scene_manager, SubGhzSceneReadRAW) !=
SubGhzCustomEventManagerSet) {
// Hopping
item = variable_item_list_add(
subghz->variable_item_list,
"Hopping:",
HOPPING_COUNT,
subghz_scene_receiver_config_set_hopping_running,
subghz);
value_index = subghz_scene_receiver_config_hopper_value_index(
subghz->txrx->hopper_state, hopping_value, HOPPING_COUNT, subghz);
variable_item_set_current_value_index(item, value_index);
variable_item_set_current_value_text(item, hopping_text[value_index]);

// Detect Raw
item = variable_item_list_add(
subghz->variable_item_list,
"Detect Raw:",
Expand All @@ -298,10 +310,8 @@ void subghz_scene_receiver_config_on_enter(void* context) {
DETECT_RAW_COUNT);
variable_item_set_current_value_index(item, value_index);
variable_item_set_current_value_text(item, detect_raw_text[value_index]);
}

if(scene_manager_get_scene_state(subghz->scene_manager, SubGhzSceneReadRAW) !=
SubGhzCustomEventManagerSet) {
// RSSI
item = variable_item_list_add(
subghz->variable_item_list,
"RSSI for Raw:",
Expand All @@ -315,10 +325,8 @@ void subghz_scene_receiver_config_on_enter(void* context) {
RSSI_THRESHOLD_COUNT);
variable_item_set_current_value_index(item, value_index);
variable_item_set_current_value_text(item, rssi_threshold_text[value_index]);
}

if(scene_manager_get_scene_state(subghz->scene_manager, SubGhzSceneReadRAW) !=
SubGhzCustomEventManagerSet) {
// Lock keyboard
variable_item_list_add(subghz->variable_item_list, "Lock Keyboard", 1, NULL, NULL);
variable_item_list_set_enter_callback(
subghz->variable_item_list,
Expand Down Expand Up @@ -347,6 +355,7 @@ void subghz_scene_receiver_config_on_exit(void* context) {
SubGhz* subghz = context;
variable_item_list_set_selected_item(subghz->variable_item_list, 0);
variable_item_list_reset(subghz->variable_item_list);
subghz_last_settings_save(subghz->last_settings);
scene_manager_set_scene_state(
subghz->scene_manager, SubGhzSceneReadRAW, SubGhzCustomEventManagerNoSet);
}
22 changes: 21 additions & 1 deletion applications/main/subghz/subghz.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <lib/toolbox/path.h>
#include "subghz_i.h"

#define TAG "SubGhzApp"

bool subghz_custom_event_callback(void* context, uint32_t event) {
furi_assert(context);
SubGhz* subghz = context;
Expand Down Expand Up @@ -174,13 +176,30 @@ SubGhz* subghz_alloc() {
subghz->setting = subghz_setting_alloc();
subghz_setting_load(subghz->setting, EXT_PATH("subghz/assets/setting_user"));

// Load last used values for Read, Read RAW, etc. or default
subghz->last_settings = subghz_last_settings_alloc();
subghz_last_settings_load(
subghz->last_settings, subghz_setting_get_preset_count(subghz->setting));
#if FURI_DEBUG
FURI_LOG_D(
TAG,
"last frequency: %d, preset: %d",
subghz->last_settings->frequency,
subghz->last_settings->preset);
#endif
subghz_setting_set_default_frequency(subghz->setting, subghz->last_settings->frequency);

//init Worker & Protocol & History & KeyBoard
subghz->lock = SubGhzLockOff;
subghz->txrx = malloc(sizeof(SubGhzTxRx));
subghz->txrx->preset = malloc(sizeof(SubGhzPresetDefinition));
string_init(subghz->txrx->preset->name);
subghz_preset_init(
subghz, "AM650", subghz_setting_get_default_frequency(subghz->setting), NULL, 0);
subghz,
subghz_setting_get_preset_name(subghz->setting, subghz->last_settings->preset),
subghz->last_settings->frequency,
NULL,
0);

subghz->txrx->txrx_state = SubGhzTxRxStateSleep;
subghz->txrx->hopper_state = SubGhzHopperStateOFF;
Expand Down Expand Up @@ -287,6 +306,7 @@ void subghz_free(SubGhz* subghz) {

//setting
subghz_setting_free(subghz->setting);
subghz_last_settings_free(subghz->last_settings);

//Worker & Protocol & History
subghz_receiver_free(subghz->txrx->receiver);
Expand Down
3 changes: 2 additions & 1 deletion applications/main/subghz/subghz_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

#include "subghz_history.h"
#include "subghz_setting.h"
#include "subghz_last_settings.h"

#include <gui/modules/variable_item_list.h>
#include <lib/toolbox/path.h>
Expand Down Expand Up @@ -100,10 +101,10 @@ struct SubGhz {
SubGhzTestPacket* subghz_test_packet;
string_t error_str;
SubGhzSetting* setting;
SubGhzLastSettings* last_settings;
SubGhzLock lock;

bool in_decoder_scene;

void* rpc_ctx;
};

Expand Down
105 changes: 105 additions & 0 deletions applications/main/subghz/subghz_last_settings.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include "subghz_last_settings.h"
#include <lib/flipper_format/flipper_format.h>

#define TAG "SubGhzLastSettings"

#define SUBGHZ_LAST_SETTING_FILE_TYPE "Flipper SubGhz Last Setting File"
#define SUBGHZ_LAST_SETTING_FILE_VERSION 1
#define SUBGHZ_LAST_SETTINGS_PATH EXT_PATH("subghz/assets/last_subghz.settings")
// 1 = "AM650"
// "AM270", "AM650", "FM238", "FM476",
#define SUBGHZ_LAST_SETTING_DEFAULT_PRESET 1
#define SUBGHZ_LAST_SETTING_DEFAULT_FREQUENCY 433920000

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) {
furi_assert(instance);
#if FURI_DEBUG
FURI_LOG_I(TAG, "subghz_last_settings_load");
#endif

Storage* storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* fff_data_file = flipper_format_file_alloc(storage);

uint32_t temp_frequency = 0;
int32_t temp_preset = 0;

if(FSE_OK == storage_sd_status(storage) && SUBGHZ_LAST_SETTINGS_PATH &&
flipper_format_file_open_existing(fff_data_file, SUBGHZ_LAST_SETTINGS_PATH)) {
flipper_format_read_int32(fff_data_file, "Preset", (int32_t*)&temp_preset, 1);
flipper_format_read_uint32(fff_data_file, "Frequency", (uint32_t*)&temp_frequency, 1);
} 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!");
instance->frequency = SUBGHZ_LAST_SETTING_DEFAULT_FREQUENCY;
instance->preset = SUBGHZ_LAST_SETTING_DEFAULT_PRESET;
} else {
instance->frequency = temp_frequency;

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 {
instance->preset = temp_preset;
}
}

flipper_format_file_close(fff_data_file);
flipper_format_free(fff_data_file);
furi_record_close(RECORD_STORAGE);
}

bool subghz_last_settings_save(SubGhzLastSettings* instance) {
furi_assert(instance);
#if FURI_DEBUG
FURI_LOG_I(TAG, "subghz_last_settings_save");
#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;

if(!flipper_format_insert_or_update_int32(file, "Preset", &instance->preset, 1)) {
break;
}
if(!flipper_format_insert_or_update_uint32(file, "Frequency", &instance->frequency, 1)) {
break;
}
saved = true;
} while(0);

if(!saved) {
FURI_LOG_E(TAG, "Error save file %s", SUBGHZ_LAST_SETTINGS_PATH);
}

flipper_format_file_close(file);
flipper_format_free(file);
furi_record_close(RECORD_STORAGE);

return saved;
}

0 comments on commit 9c62e1f

Please sign in to comment.