diff options
Diffstat (limited to 'src/system_fsm/running.cpp')
| -rw-r--r-- | src/system_fsm/running.cpp | 76 |
1 files changed, 39 insertions, 37 deletions
diff --git a/src/system_fsm/running.cpp b/src/system_fsm/running.cpp index 9c556e0a..9e10f9ec 100644 --- a/src/system_fsm/running.cpp +++ b/src/system_fsm/running.cpp @@ -25,13 +25,40 @@ namespace states { static database::IFileGatherer* sFileGatherer; -/* - * Ensure the storage and database are both available. If either of these fails - * to open, then we assume it's an issue with the underlying SD card. - */ void Running::entry() { + if (mountStorage()) { + events::Ui().Dispatch(StorageMounted{}); + } +} + +void Running::exit() { + unmountStorage(); +} + +void Running::react(const KeyLockChanged& ev) { + if (IdleCondition()) { + transit<Idle>(); + } +} + +void Running::react(const audio::PlaybackFinished& ev) { + if (IdleCondition()) { + transit<Idle>(); + } +} + +void Running::react(const SdDetectChanged& ev) { + if (ev.has_sd_card) { + if (!sStorage && mountStorage()) { + events::Ui().Dispatch(StorageMounted{}); + } + } else { + unmountStorage(); + } +} + +auto Running::mountStorage() -> bool { ESP_LOGI(kTag, "mounting sd card"); - vTaskDelay(pdMS_TO_TICKS(250)); auto storage_res = drivers::SdStorage::Create(sServices->gpios()); if (storage_res.has_error()) { ESP_LOGW(kTag, "failed to mount!"); @@ -44,12 +71,9 @@ void Running::entry() { sServices->sd(drivers::SdState::kNotPresent); break; } - - events::System().Dispatch(StorageError{}); - events::Audio().Dispatch(StorageError{}); - events::Ui().Dispatch(StorageError{}); - return; + return false; } + sStorage.reset(storage_res.value()); sServices->sd(drivers::SdState::kMounted); @@ -59,43 +83,21 @@ void Running::entry() { database::Database::Open(*sFileGatherer, sServices->tag_parser(), sServices->collator(), sServices->bg_worker()); if (database_res.has_error()) { - ESP_LOGW(kTag, "failed to open!"); - events::System().Dispatch(StorageError{}); - events::Audio().Dispatch(StorageError{}); - events::Ui().Dispatch(StorageError{}); - return; + unmountStorage(); + return false; } + sServices->database( std::unique_ptr<database::Database>{database_res.value()}); ESP_LOGI(kTag, "storage loaded okay"); - StorageMounted ev{}; - events::System().Dispatch(ev); - events::Audio().Dispatch(ev); - events::Ui().Dispatch(ev); + return true; } -void Running::exit() { +auto Running::unmountStorage() -> void { sServices->database({}); sStorage.reset(); } -void Running::react(const KeyLockChanged& ev) { - if (IdleCondition()) { - transit<Idle>(); - } -} - -void Running::react(const audio::PlaybackFinished& ev) { - if (IdleCondition()) { - transit<Idle>(); - } -} - -void Running::react(const StorageError& ev) { - ESP_LOGW(kTag, "error loading storage"); - // TODO. -} - } // namespace states } // namespace system_fsm |
