diff options
| author | jacqueline <me@jacqueline.id.au> | 2024-02-05 11:02:45 +1100 |
|---|---|---|
| committer | jacqueline <me@jacqueline.id.au> | 2024-02-05 11:03:52 +1100 |
| commit | 299f3cc48f683d3e6dec1efb4957fdb49b4de2c3 (patch) | |
| tree | 6eb0445c300492e53cdc048f12cc6780a12c55eb /src/system_fsm/running.cpp | |
| parent | 811c335c2ac425320a1949ab23378172e86ae60a (diff) | |
| download | tangara-fw-299f3cc48f683d3e6dec1efb4957fdb49b4de2c3.tar.gz | |
Preserve the queue when going into standby
Diffstat (limited to 'src/system_fsm/running.cpp')
| -rw-r--r-- | src/system_fsm/running.cpp | 49 |
1 files changed, 33 insertions, 16 deletions
diff --git a/src/system_fsm/running.cpp b/src/system_fsm/running.cpp index ec146029..d1d02fab 100644 --- a/src/system_fsm/running.cpp +++ b/src/system_fsm/running.cpp @@ -9,6 +9,7 @@ #include "database.hpp" #include "db_events.hpp" #include "file_gatherer.hpp" +#include "freertos/portmacro.h" #include "freertos/projdefs.h" #include "gpios.hpp" #include "result.hpp" @@ -25,12 +26,22 @@ namespace states { [[maybe_unused]] static const char kTag[] = "RUN"; +static const TickType_t kTicksBeforeUnmount = pdMS_TO_TICKS(10000); + +static TimerHandle_t sUnmountTimer = nullptr; + +static void timer_callback(TimerHandle_t timer) { + events::System().Dispatch(internal::UnmountTimeout{}); +} + static database::IFileGatherer* sFileGatherer; void Running::entry() { - if (mountStorage()) { - events::Ui().Dispatch(StorageMounted{}); + if (!sUnmountTimer) { + sUnmountTimer = xTimerCreate("unmount_timeout", kTicksBeforeUnmount, false, + NULL, timer_callback); } + mountStorage(); } void Running::exit() { @@ -38,18 +49,18 @@ void Running::exit() { } void Running::react(const KeyLockChanged& ev) { - if (IdleCondition()) { - transit<Idle>(); - } + checkIdle(); } -void Running::react(const audio::PlaybackFinished& ev) { - if (IdleCondition()) { - transit<Idle>(); - } +void Running::react(const audio::PlaybackStopped& ev) { + checkIdle(); } void Running::react(const database::event::UpdateFinished&) { + checkIdle(); +} + +void Running::react(const internal::UnmountTimeout&) { if (IdleCondition()) { transit<Idle>(); } @@ -61,10 +72,8 @@ void Running::react(const SdDetectChanged& ev) { return; } - if (ev.has_sd_card) { - if (!sStorage && mountStorage()) { - events::Ui().Dispatch(StorageMounted{}); - } + if (ev.has_sd_card && !sStorage) { + mountStorage(); } // Don't automatically unmount, since this event seems to occasionally happen // supriously. FIXME: Why? @@ -102,9 +111,14 @@ void Running::react(const SamdUsbMscChanged& ev) { gpios.WriteSync(drivers::IGpios::Pin::kSdPowerEnable, 0); // Now it's ready for us. - if (mountStorage()) { - events::Ui().Dispatch(StorageMounted{}); - } + mountStorage(); + } +} + +auto Running::checkIdle() -> void { + xTimerStop(sUnmountTimer, portMAX_DELAY); + if (IdleCondition()) { + xTimerStart(sUnmountTimer, portMAX_DELAY); } } @@ -142,6 +156,9 @@ auto Running::mountStorage() -> bool { std::unique_ptr<database::Database>{database_res.value()}); ESP_LOGI(kTag, "storage loaded okay"); + events::Ui().Dispatch(StorageMounted{}); + events::Audio().Dispatch(StorageMounted{}); + events::System().Dispatch(StorageMounted{}); // Tell the database to refresh so that we pick up any changes from the newly // mounted card. |
