From 320fdeb9d8355d3c361d5c6d60de8afc64501af9 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Wed, 30 Aug 2023 16:48:10 +1000 Subject: Use a service locator instead of passing around subsets of drivers between FSMs --- src/system_fsm/include/service_locator.hpp | 113 +++++++++++++++++++++++++++++ src/system_fsm/include/system_events.hpp | 9 ++- src/system_fsm/include/system_fsm.hpp | 19 +---- 3 files changed, 121 insertions(+), 20 deletions(-) create mode 100644 src/system_fsm/include/service_locator.hpp (limited to 'src/system_fsm/include') diff --git a/src/system_fsm/include/service_locator.hpp b/src/system_fsm/include/service_locator.hpp new file mode 100644 index 00000000..00285ed5 --- /dev/null +++ b/src/system_fsm/include/service_locator.hpp @@ -0,0 +1,113 @@ +/* + * Copyright 2023 jacqueline + * + * SPDX-License-Identifier: GPL-3.0-only + */ + +#pragma once + +#include + +#include "battery.hpp" +#include "bluetooth.hpp" +#include "database.hpp" +#include "gpios.hpp" +#include "nvs.hpp" +#include "samd.hpp" +#include "tag_parser.hpp" +#include "touchwheel.hpp" +#include "track_queue.hpp" + +namespace system_fsm { + +class ServiceLocator { + public: + static auto instance() -> ServiceLocator&; + + auto gpios() -> drivers::Gpios& { + assert(gpios_ != nullptr); + return *gpios_; + } + + auto gpios(std::unique_ptr i) { gpios_ = std::move(i); } + + auto samd() -> drivers::Samd& { + assert(samd_ != nullptr); + return *samd_; + } + + auto samd(std::unique_ptr i) { samd_ = std::move(i); } + + auto nvs() -> drivers::NvsStorage& { + assert(nvs_ != nullptr); + return *nvs_; + } + + auto nvs(std::unique_ptr i) { nvs_ = std::move(i); } + + auto bluetooth() -> drivers::Bluetooth& { + assert(bluetooth_ != nullptr); + return *bluetooth_; + } + + auto bluetooth(std::unique_ptr i) { + bluetooth_ = std::move(i); + } + + auto battery() -> battery::Battery& { + assert(battery_ != nullptr); + return *battery_; + } + + auto battery(std::unique_ptr i) { battery_ = std::move(i); } + + auto touchwheel() -> std::optional { + if (!touchwheel_) { + return {}; + } + return touchwheel_.get(); + } + + auto touchwheel(std::unique_ptr i) { + touchwheel_ = std::move(i); + } + + auto database() -> std::weak_ptr { return database_; } + + auto database(std::unique_ptr i) { + database_ = std::move(i); + } + + auto tag_parser() -> database::ITagParser& { + assert(tag_parser_ != nullptr); + return *tag_parser_; + } + + auto tag_parser(std::unique_ptr i) { + tag_parser_ = std::move(i); + } + + auto track_queue() -> audio::TrackQueue& { + assert(queue_ != nullptr); + return *queue_; + } + + auto track_queue(std::unique_ptr i) { + queue_ = std::move(i); + } + + private: + std::unique_ptr gpios_; + std::unique_ptr samd_; + std::unique_ptr nvs_; + std::unique_ptr touchwheel_; + std::unique_ptr bluetooth_; + + std::unique_ptr queue_; + std::unique_ptr battery_; + + std::shared_ptr database_; + std::unique_ptr tag_parser_; +}; + +} // namespace system_fsm diff --git a/src/system_fsm/include/system_events.hpp b/src/system_fsm/include/system_events.hpp index 64cbd393..e22fe2ae 100644 --- a/src/system_fsm/include/system_events.hpp +++ b/src/system_fsm/include/system_events.hpp @@ -9,6 +9,7 @@ #include #include "database.hpp" +#include "service_locator.hpp" #include "tinyfsm.hpp" namespace system_fsm { @@ -19,7 +20,9 @@ struct DisplayReady : tinyfsm::Event {}; * Sent by SysState when the system has finished with its boot and self-test, * and is now ready to run normally. */ -struct BootComplete : tinyfsm::Event {}; +struct BootComplete : tinyfsm::Event { + std::shared_ptr services; +}; /* * May be sent by any component to indicate that the system has experienced an @@ -33,9 +36,7 @@ struct OnIdle : tinyfsm::Event {}; /* * Sent by SysState when the system storage has been successfully mounted. */ -struct StorageMounted : tinyfsm::Event { - std::weak_ptr db; -}; +struct StorageMounted : tinyfsm::Event {}; struct StorageError : tinyfsm::Event {}; diff --git a/src/system_fsm/include/system_fsm.hpp b/src/system_fsm/include/system_fsm.hpp index 371e5527..28448e5a 100644 --- a/src/system_fsm/include/system_fsm.hpp +++ b/src/system_fsm/include/system_fsm.hpp @@ -18,6 +18,7 @@ #include "nvs.hpp" #include "relative_wheel.hpp" #include "samd.hpp" +#include "service_locator.hpp" #include "storage.hpp" #include "tag_parser.hpp" #include "tinyfsm.hpp" @@ -60,22 +61,8 @@ class SystemState : public tinyfsm::Fsm { protected: auto IdleCondition() -> bool; - static std::shared_ptr sGpios; - static std::shared_ptr sSamd; - static std::shared_ptr sNvs; - - static std::shared_ptr sTouch; - static std::shared_ptr sRelativeTouch; - static std::shared_ptr sAdc; - static std::shared_ptr sBattery; - static std::shared_ptr sStorage; - static std::shared_ptr sDisplay; - static std::shared_ptr sBluetooth; - - static std::shared_ptr sDatabase; - static std::shared_ptr sTagParser; - - static std::shared_ptr sTrackQueue; + static std::shared_ptr sServices; + static std::unique_ptr sStorage; static console::AppConsole* sAppConsole; }; -- cgit v1.2.3