summaryrefslogtreecommitdiff
path: root/src/system_fsm/running.cpp
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2023-05-19 21:21:27 +1000
committerjacqueline <me@jacqueline.id.au>2023-05-19 21:21:27 +1000
commita6ab1504058304012791281f9eb42c262745888f (patch)
treef82379cd1e66a8ae2f1afbae5cf083a8ab7acc53 /src/system_fsm/running.cpp
parentb320a6a863cf1c10dc79254af41f573730935564 (diff)
downloadtangara-fw-a6ab1504058304012791281f9eb42c262745888f.tar.gz
Add tinyfsm, start converting core functions to an FSM-based event loop
Diffstat (limited to 'src/system_fsm/running.cpp')
-rw-r--r--src/system_fsm/running.cpp54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/system_fsm/running.cpp b/src/system_fsm/running.cpp
new file mode 100644
index 00000000..e55989f1
--- /dev/null
+++ b/src/system_fsm/running.cpp
@@ -0,0 +1,54 @@
+
+#include "result.hpp"
+
+#include "audio_fsm.hpp"
+#include "event_queue.hpp"
+#include "storage.hpp"
+#include "system_events.hpp"
+#include "system_fsm.hpp"
+#include "ui_fsm.hpp"
+
+namespace system_fsm {
+namespace states {
+
+/*
+ * 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() {
+ auto storage_res = drivers::SdStorage::Create(sGpioExpander.get());
+ if (storage_res.has_error()) {
+ events::Dispatch<StorageError, SystemState, audio::AudioState, ui::UiState>(
+ StorageError());
+ return;
+ }
+ sStorage.reset(storage_res.value());
+
+ auto database_res = database::Database::Open();
+ if (database_res.has_error()) {
+ events::Dispatch<StorageError, SystemState, audio::AudioState, ui::UiState>(
+ StorageError());
+ return;
+ }
+ sDatabase.reset(database_res.value());
+
+ events::Dispatch<StorageMounted, SystemState, audio::AudioState, ui::UiState>(
+ StorageMounted());
+}
+
+void Running::exit() {
+ sDatabase.reset();
+ sStorage.reset();
+}
+
+void Running::react(const StorageUnmountRequested& ev) {
+ events::Dispatch<internal::ReadyToUnmount, SystemState>(
+ internal::ReadyToUnmount());
+}
+
+void Running::react(const internal::ReadyToUnmount& ev) {
+ transit<Unmounted>();
+}
+
+} // namespace states
+} // namespace system_fsm