summaryrefslogtreecommitdiff
path: root/src/system_fsm/running.cpp
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2024-01-30 09:55:31 +1100
committerjacqueline <me@jacqueline.id.au>2024-01-30 09:55:31 +1100
commitc399199bfccb5298fe4b0cf566d8e69729596ba4 (patch)
tree224655d23c9e2db64c1f4fb90ed91154475f56ea /src/system_fsm/running.cpp
parent9039a97ab47c280555ba891f2aaf34119776d695 (diff)
downloadtangara-fw-c399199bfccb5298fe4b0cf566d8e69729596ba4.tar.gz
Improve handling of sd card changes during runtime
- mount if an sd card is inserted - unmount if it's removed
Diffstat (limited to 'src/system_fsm/running.cpp')
-rw-r--r--src/system_fsm/running.cpp76
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