diff options
| author | jacqueline <me@jacqueline.id.au> | 2023-05-19 21:21:27 +1000 |
|---|---|---|
| committer | jacqueline <me@jacqueline.id.au> | 2023-05-19 21:21:27 +1000 |
| commit | a6ab1504058304012791281f9eb42c262745888f (patch) | |
| tree | f82379cd1e66a8ae2f1afbae5cf083a8ab7acc53 /src/system_fsm/running.cpp | |
| parent | b320a6a863cf1c10dc79254af41f573730935564 (diff) | |
| download | tangara-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.cpp | 54 |
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 |
