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/ui/include/lvgl_task.hpp | 27 +++++++++++++++++++++++++-- src/ui/include/screen_playing.hpp | 4 ++-- src/ui/include/screen_settings.hpp | 6 +++--- src/ui/include/ui_fsm.hpp | 25 +++++++++++-------------- src/ui/include/wheel_encoder.hpp | 4 ++-- 5 files changed, 43 insertions(+), 23 deletions(-) (limited to 'src/ui/include') diff --git a/src/ui/include/lvgl_task.hpp b/src/ui/include/lvgl_task.hpp index 7e60c4b4..6b7e446e 100644 --- a/src/ui/include/lvgl_task.hpp +++ b/src/ui/include/lvgl_task.hpp @@ -12,15 +12,38 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" +#include "freertos/timers.h" #include "display.hpp" #include "relative_wheel.hpp" +#include "screen.hpp" #include "themes.hpp" #include "touchwheel.hpp" +#include "wheel_encoder.hpp" namespace ui { -auto StartLvgl(std::weak_ptr touch_wheel, - std::weak_ptr display) -> void; +class UiTask { + public: + static auto Start() -> UiTask*; + + ~UiTask(); + + // FIXME: Once we have more input devices, this function should accept a more + // generic interface. + auto SetInputDevice(std::shared_ptr dev) -> void; + + private: + UiTask(); + + auto Main() -> void; + + std::shared_ptr input_device_; + std::shared_ptr current_screen_; + + std::atomic quit_; + SemaphoreHandle_t frame_semaphore_; + TimerHandle_t frame_timer_; +}; } // namespace ui diff --git a/src/ui/include/screen_playing.hpp b/src/ui/include/screen_playing.hpp index c684ddff..f2998c88 100644 --- a/src/ui/include/screen_playing.hpp +++ b/src/ui/include/screen_playing.hpp @@ -29,7 +29,7 @@ namespace screens { class Playing : public Screen { public: explicit Playing(std::weak_ptr db, - audio::TrackQueue* queue); + audio::TrackQueue& queue); ~Playing(); auto Tick() -> void override; @@ -51,7 +51,7 @@ class Playing : public Screen { auto ApplyNextUp(const std::vector& tracks) -> void; std::weak_ptr db_; - audio::TrackQueue* queue_; + audio::TrackQueue& queue_; std::optional track_; std::vector next_tracks_; diff --git a/src/ui/include/screen_settings.hpp b/src/ui/include/screen_settings.hpp index 61375fa9..0ec96d26 100644 --- a/src/ui/include/screen_settings.hpp +++ b/src/ui/include/screen_settings.hpp @@ -37,14 +37,14 @@ class Headphones : public MenuScreen { class Appearance : public MenuScreen { public: - Appearance(drivers::NvsStorage* nvs, drivers::Display* display); + Appearance(drivers::NvsStorage& nvs, drivers::Display& display); auto ChangeBrightness(uint_fast8_t) -> void; auto CommitBrightness() -> void; private: - drivers::NvsStorage* nvs_; - drivers::Display* display_; + drivers::NvsStorage& nvs_; + drivers::Display& display_; lv_obj_t* current_brightness_label_; uint_fast8_t current_brightness_; diff --git a/src/ui/include/ui_fsm.hpp b/src/ui/include/ui_fsm.hpp index 1fa6bf26..12fe5c69 100644 --- a/src/ui/include/ui_fsm.hpp +++ b/src/ui/include/ui_fsm.hpp @@ -11,9 +11,12 @@ #include "audio_events.hpp" #include "battery.hpp" +#include "gpios.hpp" +#include "lvgl_task.hpp" #include "nvs.hpp" #include "relative_wheel.hpp" #include "screen_playing.hpp" +#include "service_locator.hpp" #include "tinyfsm.hpp" #include "display.hpp" @@ -24,15 +27,13 @@ #include "touchwheel.hpp" #include "track_queue.hpp" #include "ui_events.hpp" +#include "wheel_encoder.hpp" namespace ui { class UiState : public tinyfsm::Fsm { public: - static auto Init(drivers::IGpios*, - std::shared_ptr, - audio::TrackQueue*, - std::shared_ptr) -> bool; + static auto InitBootSplash(drivers::IGpios&) -> bool; virtual ~UiState() {} @@ -46,7 +47,7 @@ class UiState : public tinyfsm::Fsm { /* Fallback event handler. Does nothing. */ void react(const tinyfsm::Event& ev) {} - void react(const system_fsm::BatteryStateChanged&); + virtual void react(const system_fsm::BatteryStateChanged&); virtual void react(const audio::PlaybackStarted&) {} virtual void react(const audio::PlaybackUpdate&) {} @@ -76,15 +77,10 @@ class UiState : public tinyfsm::Fsm { void PopScreen(); void UpdateTopBar(); - static drivers::IGpios* sIGpios; - static audio::TrackQueue* sQueue; - - static std::shared_ptr sTouchWheel; - static std::shared_ptr sRelativeWheel; - static std::shared_ptr sDisplay; - static std::shared_ptr sBattery; - static std::shared_ptr sNvs; - static std::weak_ptr sDb; + static std::unique_ptr sTask; + static std::shared_ptr sServices; + static std::unique_ptr sDisplay; + static std::shared_ptr sEncoder; static std::stack> sScreens; static std::shared_ptr sCurrentScreen; @@ -97,6 +93,7 @@ class Splash : public UiState { public: void exit() override; void react(const system_fsm::BootComplete&) override; + void react(const system_fsm::BatteryStateChanged&) override{}; using UiState::react; }; diff --git a/src/ui/include/wheel_encoder.hpp b/src/ui/include/wheel_encoder.hpp index c49e5929..fcac5edd 100644 --- a/src/ui/include/wheel_encoder.hpp +++ b/src/ui/include/wheel_encoder.hpp @@ -17,7 +17,7 @@ namespace ui { class TouchWheelEncoder { public: - explicit TouchWheelEncoder(std::weak_ptr wheel); + explicit TouchWheelEncoder(std::unique_ptr wheel); auto Read(lv_indev_data_t* data) -> void; auto registration() -> lv_indev_t* { return registration_; } @@ -27,7 +27,7 @@ class TouchWheelEncoder { lv_indev_t* registration_; lv_key_t last_key_; - std::weak_ptr wheel_; + std::unique_ptr wheel_; }; } // namespace ui -- cgit v1.2.3