Skip to content

Commit

Permalink
Playing a patch saves its path in settings, after a 60 sec. delay
Browse files Browse the repository at this point in the history
  • Loading branch information
danngreen committed Aug 14, 2024
1 parent 50cd1c2 commit 26e6288
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 1 deletion.
1 change: 1 addition & 0 deletions firmware/src/gui/pages/base.hh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ struct GuiState {
std::optional<Volume> force_refresh_vol{};

bool do_write_settings{};
uint32_t write_settings_after_ms{};

Toggler back_button{};
};
Expand Down
14 changes: 14 additions & 0 deletions firmware/src/gui/pages/page_manager.hh
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,20 @@ public:
pr_info("Wrote settings\n");

gui_state.do_write_settings = false;
gui_state.write_settings_after_ms = 0;
}

if (gui_state.write_settings_after_ms > 0) {
if (get_time() >= gui_state.write_settings_after_ms) {

if (!Settings::write_settings(info.patch_storage, info.settings)) {
pr_err("Failed to write settings file (timer)\n");
} else {
pr_info("Wrote settings (timer)\n");
}

gui_state.write_settings_after_ms = 0;
}
}
}

Expand Down
18 changes: 18 additions & 0 deletions firmware/src/gui/pages/patch_view.hh
Original file line number Diff line number Diff line change
Expand Up @@ -491,10 +491,28 @@ private:
page->redraw_modulename();
}

void save_last_opened_patch_in_settings() {
if (settings.last_patch_vol != patches.get_view_patch_vol() ||
settings.last_patch_opened != patches.get_view_patch_filename())
{
settings.last_patch_vol = patches.get_view_patch_vol();
settings.last_patch_opened = patches.get_view_patch_filename();
pr_dbg("Will set last_patch opened to %s on %d\n",
settings.last_patch_opened.c_str(),
settings.last_patch_vol);

if (gui_state.write_settings_after_ms == 0) {
gui_state.write_settings_after_ms = get_time() + 60 * 1000; //1 minute delay
pr_dbg("Setting timer...\n");
}
}
}

static void playbut_cb(lv_event_t *event) {
auto page = static_cast<PatchViewPage *>(event->user_data);
if (!page->is_patch_playing) {
page->patch_playloader.request_load_view_patch();
page->save_last_opened_patch_in_settings();
} else {
if (page->patch_playloader.is_audio_muted()) {
page->patch_playloader.start_audio();
Expand Down
2 changes: 1 addition & 1 deletion firmware/src/user_settings/settings.hh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct UserSettings {
AudioSettings audio{};
PluginAutoloadSettings plugin_autoload{};
std::string last_patch_opened{};
Volume last_patch_vol{};
Volume last_patch_vol{Volume::NorFlash};
};

} // namespace MetaModule
11 changes: 11 additions & 0 deletions firmware/src/user_settings/settings_parse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ bool parse(std::span<char> yaml, UserSettings *settings) {
read_or_default(node, "module_view", settings, &UserSettings::module_view);
read_or_default(node, "audio", settings, &UserSettings::audio);
read_or_default(node, "plugin_autoload", settings, &UserSettings::plugin_autoload);
read_or_default(node, "last_patch_opened", settings, &UserSettings::last_patch_opened);

// TODO: cleaner way to parse and enum and reject out of range?
if (node.is_map() && node.has_child("last_patch_vol")) {
unsigned t = 0;
node["last_patch_vol"] >> t;
if (t < static_cast<unsigned>(Volume::MaxVolumes))
settings->last_patch_vol = static_cast<Volume>(t);
} else {
settings->last_patch_vol = UserSettings{}.last_patch_vol;
}

return true;
}
Expand Down
2 changes: 2 additions & 0 deletions firmware/src/user_settings/settings_serialize.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ uint32_t serialize(UserSettings const &settings, std::span<char> buffer) {
data["module_view"] << settings.module_view;
data["audio"] << settings.audio;
data["plugin_autoload"] << settings.plugin_autoload;
data["last_patch_opened"] << settings.last_patch_opened;
data["last_patch_vol"] << static_cast<unsigned>(settings.last_patch_vol);

auto res = ryml::emit_yaml(tree, c4::substr(buffer.data(), buffer.size()));
return res.size();
Expand Down
14 changes: 14 additions & 0 deletions firmware/tests/settings_parse_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ TEST_CASE("Parse settings file") {
- Plugin One
- Plugin Two
last_patch_opened: '/somedir/SomePatch.yml'
last_patch_vol: 1
)";
// clang format-on

Expand Down Expand Up @@ -78,6 +81,9 @@ TEST_CASE("Parse settings file") {

CHECK(settings.plugin_autoload.slug.at(0) == "Plugin One");
CHECK(settings.plugin_autoload.slug.at(1) == "Plugin Two");

CHECK(settings.last_patch_opened == "/somedir/SomePatch.yml");
CHECK(settings.last_patch_vol == MetaModule::Volume::SDCard);
}

TEST_CASE("Get default settings if file is missing fields") {
Expand Down Expand Up @@ -166,6 +172,9 @@ TEST_CASE("Get default settings if file is missing fields") {
CHECK(settings.audio.block_size == 64);

CHECK(settings.plugin_autoload.slug.size() == 0);

CHECK(settings.last_patch_opened == "");
CHECK(settings.last_patch_vol == MetaModule::Volume::NorFlash);
}

TEST_CASE("Serialize settings") {
Expand Down Expand Up @@ -200,6 +209,9 @@ TEST_CASE("Serialize settings") {
settings.plugin_autoload.slug.emplace_back("Plugin One");
settings.plugin_autoload.slug.emplace_back("Plugin Two");

settings.last_patch_vol = MetaModule::Volume::SDCard;
settings.last_patch_opened = "SomePatch.yml";

// clang format-off
std::string expected = R"(Settings:
patch_view:
Expand Down Expand Up @@ -234,6 +246,8 @@ TEST_CASE("Serialize settings") {
plugin_autoload:
- Plugin One
- Plugin Two
last_patch_opened: SomePatch.yml
last_patch_vol: 1
)";
// clang format-on

Expand Down

0 comments on commit 26e6288

Please sign in to comment.