From 7e8a1397322b499e5bd52c8753584e43fe65adf3 Mon Sep 17 00:00:00 2001 From: gullradriel <3157857+gullradriel@users.noreply.github.com> Date: Sun, 4 Jun 2023 21:25:25 +0200 Subject: [PATCH] moved pmem setting restore before top bar is loaded (#1105) * moved pmem setting restore before top bar is loaded * added a check pmem from sdcard function, replaced occurences, added a save to file on back button * Added defines for flag file and dump file, changed test func name to should_use_sdcard_for_pmem, used file_exists, removed save and load pmem parameter * changed a text to a more comprehesible one --- firmware/application/apps/ui_settings.cpp | 23 +++++-------------- firmware/application/apps/ui_settings.hpp | 4 ++-- firmware/application/ui_navigation.cpp | 14 +++++------ .../common/portapack_persistent_memory.cpp | 10 ++++++-- .../common/portapack_persistent_memory.hpp | 10 ++++++-- 5 files changed, 31 insertions(+), 30 deletions(-) diff --git a/firmware/application/apps/ui_settings.cpp b/firmware/application/apps/ui_settings.cpp index b699c5d9..eb0a0c95 100644 --- a/firmware/application/apps/ui_settings.cpp +++ b/firmware/application/apps/ui_settings.cpp @@ -446,27 +446,16 @@ SetPersistentMemoryView::SetPersistentMemoryView(NavigationView& nav) { add_children({&text_pmem_about, &text_pmem_informations, &text_pmem_status, - &check_load_mem_at_startup, + &check_use_sdcard_for_pmem, &button_save_mem_to_file, &button_load_mem_from_file, &button_load_mem_defaults, &button_return}); - bool load_mem_at_startup = false; - File pmem_flag_file_handle; - - std::string folder = "SETTINGS"; - make_new_directory(folder); - - std::string pmem_flag_file = "/SETTINGS/PMEM_FILEFLAG"; - auto result = pmem_flag_file_handle.open(pmem_flag_file); - if (!result.is_valid()) { - load_mem_at_startup = true; - } - check_load_mem_at_startup.set_value(load_mem_at_startup); - check_load_mem_at_startup.on_select = [this](Checkbox&, bool v) { + check_use_sdcard_for_pmem.set_value(portapack::persistent_memory::should_use_sdcard_for_pmem()); + check_use_sdcard_for_pmem.on_select = [this](Checkbox&, bool v) { File pmem_flag_file_handle; - std::string pmem_flag_file = "/SETTINGS/PMEM_FILEFLAG"; + std::string pmem_flag_file = PMEM_FILEFLAG; if (v) { auto result = pmem_flag_file_handle.open(pmem_flag_file); if (result.is_valid()) { @@ -490,7 +479,7 @@ SetPersistentMemoryView::SetPersistentMemoryView(NavigationView& nav) { }; button_save_mem_to_file.on_select = [&nav, this](Button&) { - if (!portapack::persistent_memory::save_persistent_settings_to_file("SETTINGS/pmem_settings")) { + if (!portapack::persistent_memory::save_persistent_settings_to_file()) { text_pmem_status.set("!problem saving settings!"); } else { text_pmem_status.set("settings saved"); @@ -498,7 +487,7 @@ SetPersistentMemoryView::SetPersistentMemoryView(NavigationView& nav) { }; button_load_mem_from_file.on_select = [&nav, this](Button&) { - if (!portapack::persistent_memory::load_persistent_settings_from_file("SETTINGS/pmem_settings")) { + if (!portapack::persistent_memory::load_persistent_settings_from_file()) { text_pmem_status.set("!problem loading settings!"); } else { text_pmem_status.set("settings loaded"); diff --git a/firmware/application/apps/ui_settings.hpp b/firmware/application/apps/ui_settings.hpp index f4fb79e1..bf90be60 100644 --- a/firmware/application/apps/ui_settings.hpp +++ b/firmware/application/apps/ui_settings.hpp @@ -485,10 +485,10 @@ class SetPersistentMemoryView : public View { {0, 3 * 16, 240, 16}, ""}; - Checkbox check_load_mem_at_startup{ + Checkbox check_use_sdcard_for_pmem{ {18, 6 * 16}, 19, - "load from sd at startup"}; + "use sdcard for p.mem"}; Button button_save_mem_to_file{ {0, 8 * 16, 240, 32}, diff --git a/firmware/application/ui_navigation.cpp b/firmware/application/ui_navigation.cpp index cd2fc758..365c7a31 100644 --- a/firmware/application/ui_navigation.cpp +++ b/firmware/application/ui_navigation.cpp @@ -126,6 +126,10 @@ SystemStatusView::SystemStatusView( &sd_card_status_view, }); + if (portapack::persistent_memory::should_use_sdcard_for_pmem()) { + portapack::persistent_memory::load_persistent_settings_from_file(); + } + if (portapack::persistent_memory::config_speaker()) button_speaker.hidden(false); else @@ -156,6 +160,9 @@ SystemStatusView::SystemStatusView( refresh(); button_back.on_select = [this](ImageButton&) { + if (portapack::persistent_memory::should_use_sdcard_for_pmem()) { + portapack::persistent_memory::save_persistent_settings_to_file(); + } if (this->on_back) this->on_back(); }; @@ -692,13 +699,6 @@ SystemView::SystemView( navigation_view.push(); - File pmem_flag_file_handle; - std::string pmem_flag_file = "/SETTINGS/PMEM_FILEFLAG"; - auto result = pmem_flag_file_handle.open(pmem_flag_file); - if (!result.is_valid()) { - portapack::persistent_memory::load_persistent_settings_from_file("SETTINGS/pmem_settings"); - } - if (portapack::persistent_memory::config_splash()) { navigation_view.push(); } diff --git a/firmware/common/portapack_persistent_memory.cpp b/firmware/common/portapack_persistent_memory.cpp index af8bcfcd..b7445adb 100644 --- a/firmware/common/portapack_persistent_memory.cpp +++ b/firmware/common/portapack_persistent_memory.cpp @@ -820,8 +820,13 @@ void set_encoder_dial_sensitivity(uint8_t v) { data->encoder_dial_sensitivity = v; } +bool should_use_sdcard_for_pmem() { + return std::filesystem::file_exists(PMEM_FILEFLAG); +} + // sd persisting settings -int save_persistent_settings_to_file(std::string filename) { +int save_persistent_settings_to_file() { + std::string filename = PMEM_SETTING_FILE; delete_file(filename); File outfile; auto result = outfile.create(filename); @@ -832,7 +837,8 @@ int save_persistent_settings_to_file(std::string filename) { return true; } -int load_persistent_settings_from_file(std::string filename) { +int load_persistent_settings_from_file() { + std::string filename = PMEM_SETTING_FILE; File infile; auto result = infile.open(filename); if (!result.is_valid()) { diff --git a/firmware/common/portapack_persistent_memory.hpp b/firmware/common/portapack_persistent_memory.hpp index 101512b4..911fed7d 100644 --- a/firmware/common/portapack_persistent_memory.hpp +++ b/firmware/common/portapack_persistent_memory.hpp @@ -32,6 +32,11 @@ #include "modems.hpp" #include "serializer.hpp" +// persistant memory from/to sdcard flag file +#define PMEM_FILEFLAG "/SETTINGS/PMEM_FILEFLAG" +// persistant memory from/to sdcard flag file +#define PMEM_SETTING_FILE "/SETTINGS/pmem_settings" + using namespace modems; using namespace serializer; @@ -244,8 +249,9 @@ void set_recon_load_hamradios(const bool v); void set_recon_match_mode(const bool v); // sd persisting settings -int save_persistent_settings_to_file(std::string filename); -int load_persistent_settings_from_file(std::string filename); +bool should_use_sdcard_for_pmem(); +int save_persistent_settings_to_file(); +int load_persistent_settings_from_file(); } /* namespace persistent_memory */