From a6ab1504058304012791281f9eb42c262745888f Mon Sep 17 00:00:00 2001 From: jacqueline Date: Fri, 19 May 2023 21:21:27 +1000 Subject: Add tinyfsm, start converting core functions to an FSM-based event loop --- src/system_fsm/running.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/system_fsm/running.cpp (limited to 'src/system_fsm/running.cpp') 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()); + return; + } + sStorage.reset(storage_res.value()); + + auto database_res = database::Database::Open(); + if (database_res.has_error()) { + events::Dispatch( + StorageError()); + return; + } + sDatabase.reset(database_res.value()); + + events::Dispatch( + StorageMounted()); +} + +void Running::exit() { + sDatabase.reset(); + sStorage.reset(); +} + +void Running::react(const StorageUnmountRequested& ev) { + events::Dispatch( + internal::ReadyToUnmount()); +} + +void Running::react(const internal::ReadyToUnmount& ev) { + transit(); +} + +} // namespace states +} // namespace system_fsm -- cgit v1.2.3