summaryrefslogtreecommitdiff
path: root/src/system_fsm/include
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2023-08-30 16:48:10 +1000
committerjacqueline <me@jacqueline.id.au>2023-08-30 16:48:10 +1000
commit320fdeb9d8355d3c361d5c6d60de8afc64501af9 (patch)
treef0d5a2ab82199c78ad6768c6b18ba1239a0b7ee4 /src/system_fsm/include
parent4247c9fe7d25c921fbfc73fc50e849c8780e7ad6 (diff)
downloadtangara-fw-320fdeb9d8355d3c361d5c6d60de8afc64501af9.tar.gz
Use a service locator instead of passing around subsets of drivers between FSMs
Diffstat (limited to 'src/system_fsm/include')
-rw-r--r--src/system_fsm/include/service_locator.hpp113
-rw-r--r--src/system_fsm/include/system_events.hpp9
-rw-r--r--src/system_fsm/include/system_fsm.hpp19
3 files changed, 121 insertions, 20 deletions
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 <me@jacqueline.id.au>
+ *
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+#pragma once
+
+#include <memory>
+
+#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<drivers::Gpios> i) { gpios_ = std::move(i); }
+
+ auto samd() -> drivers::Samd& {
+ assert(samd_ != nullptr);
+ return *samd_;
+ }
+
+ auto samd(std::unique_ptr<drivers::Samd> i) { samd_ = std::move(i); }
+
+ auto nvs() -> drivers::NvsStorage& {
+ assert(nvs_ != nullptr);
+ return *nvs_;
+ }
+
+ auto nvs(std::unique_ptr<drivers::NvsStorage> i) { nvs_ = std::move(i); }
+
+ auto bluetooth() -> drivers::Bluetooth& {
+ assert(bluetooth_ != nullptr);
+ return *bluetooth_;
+ }
+
+ auto bluetooth(std::unique_ptr<drivers::Bluetooth> i) {
+ bluetooth_ = std::move(i);
+ }
+
+ auto battery() -> battery::Battery& {
+ assert(battery_ != nullptr);
+ return *battery_;
+ }
+
+ auto battery(std::unique_ptr<battery::Battery> i) { battery_ = std::move(i); }
+
+ auto touchwheel() -> std::optional<drivers::TouchWheel*> {
+ if (!touchwheel_) {
+ return {};
+ }
+ return touchwheel_.get();
+ }
+
+ auto touchwheel(std::unique_ptr<drivers::TouchWheel> i) {
+ touchwheel_ = std::move(i);
+ }
+
+ auto database() -> std::weak_ptr<database::Database> { return database_; }
+
+ auto database(std::unique_ptr<database::Database> i) {
+ database_ = std::move(i);
+ }
+
+ auto tag_parser() -> database::ITagParser& {
+ assert(tag_parser_ != nullptr);
+ return *tag_parser_;
+ }
+
+ auto tag_parser(std::unique_ptr<database::ITagParser> i) {
+ tag_parser_ = std::move(i);
+ }
+
+ auto track_queue() -> audio::TrackQueue& {
+ assert(queue_ != nullptr);
+ return *queue_;
+ }
+
+ auto track_queue(std::unique_ptr<audio::TrackQueue> i) {
+ queue_ = std::move(i);
+ }
+
+ private:
+ std::unique_ptr<drivers::Gpios> gpios_;
+ std::unique_ptr<drivers::Samd> samd_;
+ std::unique_ptr<drivers::NvsStorage> nvs_;
+ std::unique_ptr<drivers::TouchWheel> touchwheel_;
+ std::unique_ptr<drivers::Bluetooth> bluetooth_;
+
+ std::unique_ptr<audio::TrackQueue> queue_;
+ std::unique_ptr<battery::Battery> battery_;
+
+ std::shared_ptr<database::Database> database_;
+ std::unique_ptr<database::ITagParser> 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 <memory>
#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<ServiceLocator> 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<database::Database> 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<SystemState> {
protected:
auto IdleCondition() -> bool;
- static std::shared_ptr<drivers::Gpios> sGpios;
- static std::shared_ptr<drivers::Samd> sSamd;
- static std::shared_ptr<drivers::NvsStorage> sNvs;
-
- static std::shared_ptr<drivers::TouchWheel> sTouch;
- static std::shared_ptr<drivers::RelativeWheel> sRelativeTouch;
- static std::shared_ptr<drivers::AdcBattery> sAdc;
- static std::shared_ptr<battery::Battery> sBattery;
- static std::shared_ptr<drivers::SdStorage> sStorage;
- static std::shared_ptr<drivers::Display> sDisplay;
- static std::shared_ptr<drivers::Bluetooth> sBluetooth;
-
- static std::shared_ptr<database::Database> sDatabase;
- static std::shared_ptr<database::TagParserImpl> sTagParser;
-
- static std::shared_ptr<audio::TrackQueue> sTrackQueue;
+ static std::shared_ptr<ServiceLocator> sServices;
+ static std::unique_ptr<drivers::SdStorage> sStorage;
static console::AppConsole* sAppConsole;
};