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/drivers/display.cpp | 4 ++-- src/drivers/i2s_dac.cpp | 16 ++++++++-------- src/drivers/include/display.hpp | 6 +++--- src/drivers/include/i2s_dac.hpp | 6 +++--- src/drivers/include/relative_wheel.hpp | 8 ++------ src/drivers/include/storage.hpp | 6 +++--- src/drivers/relative_wheel.cpp | 6 +++--- src/drivers/storage.cpp | 14 +++++++------- 8 files changed, 31 insertions(+), 35 deletions(-) (limited to 'src/drivers') diff --git a/src/drivers/display.cpp b/src/drivers/display.cpp index 2d480aa6..e04de477 100644 --- a/src/drivers/display.cpp +++ b/src/drivers/display.cpp @@ -85,7 +85,7 @@ extern "C" void FlushDataCallback(lv_disp_drv_t* disp_drv, instance->OnLvglFlush(disp_drv, area, color_map); } -auto Display::Create(IGpios* expander, +auto Display::Create(IGpios& expander, const displays::InitialisationData& init_data) -> Display* { ESP_LOGI(kTag, "Init I/O pins"); @@ -182,7 +182,7 @@ auto Display::Create(IGpios* expander, return display.release(); } -Display::Display(IGpios* gpio, spi_device_handle_t handle) +Display::Display(IGpios& gpio, spi_device_handle_t handle) : gpio_(gpio), handle_(handle), worker_task_(tasks::Worker::Start()), diff --git a/src/drivers/i2s_dac.cpp b/src/drivers/i2s_dac.cpp index af2125a6..bedf7ebf 100644 --- a/src/drivers/i2s_dac.cpp +++ b/src/drivers/i2s_dac.cpp @@ -39,7 +39,7 @@ namespace drivers { static const char* kTag = "i2s_dac"; static const i2s_port_t kI2SPort = I2S_NUM_0; -auto I2SDac::create(IGpios* expander) -> std::optional { +auto I2SDac::create(IGpios& expander) -> std::optional { i2s_chan_handle_t i2s_handle; i2s_chan_config_t channel_config = I2S_CHANNEL_DEFAULT_CONFIG(kI2SPort, I2S_ROLE_MASTER); @@ -77,7 +77,7 @@ auto I2SDac::create(IGpios* expander) -> std::optional { return dac.release(); } -I2SDac::I2SDac(IGpios* gpio, i2s_chan_handle_t i2s_handle) +I2SDac::I2SDac(IGpios& gpio, i2s_chan_handle_t i2s_handle) : gpio_(gpio), i2s_handle_(i2s_handle), i2s_active_(false), @@ -87,7 +87,7 @@ I2SDac::I2SDac(IGpios* gpio, i2s_chan_handle_t i2s_handle) clock_config_.clk_src = I2S_CLK_SRC_APLL; // Keep the 5V circuity off until it's needed. - gpio_->WriteSync(IGpios::Pin::kAmplifierEnable, false); + gpio_.WriteSync(IGpios::Pin::kAmplifierEnable, false); // Reset all registers back to their default values. wm8523::WriteRegister(wm8523::Register::kReset, 1); @@ -103,9 +103,9 @@ I2SDac::~I2SDac() { auto I2SDac::Start() -> void { std::lock_guard lock(configure_mutex_); - gpio_->WriteSync(IGpios::Pin::kAmplifierUnmute, false); + gpio_.WriteSync(IGpios::Pin::kAmplifierUnmute, false); // Ramp up the amplifier power supply. - gpio_->WriteSync(IGpios::Pin::kAmplifierEnable, true); + gpio_.WriteSync(IGpios::Pin::kAmplifierEnable, true); // Wait for voltage to stabilise vTaskDelay(pdMS_TO_TICKS(5)); @@ -124,7 +124,7 @@ auto I2SDac::Start() -> void { wm8523::WriteRegister(wm8523::Register::kPsCtrl, 0b11); vTaskDelay(pdMS_TO_TICKS(5)); - gpio_->WriteSync(IGpios::Pin::kAmplifierUnmute, true); + gpio_.WriteSync(IGpios::Pin::kAmplifierUnmute, true); } auto I2SDac::Stop() -> void { @@ -134,7 +134,7 @@ auto I2SDac::Stop() -> void { wm8523::WriteRegister(wm8523::Register::kPsCtrl, 0b10); vTaskDelay(pdMS_TO_TICKS(5)); // Silence the output. - gpio_->WriteSync(IGpios::Pin::kAmplifierUnmute, false); + gpio_.WriteSync(IGpios::Pin::kAmplifierUnmute, false); vTaskDelay(pdMS_TO_TICKS(5)); @@ -143,7 +143,7 @@ auto I2SDac::Stop() -> void { i2s_channel_disable(i2s_handle_); i2s_active_ = false; - gpio_->WriteSync(IGpios::Pin::kAmplifierEnable, false); + gpio_.WriteSync(IGpios::Pin::kAmplifierEnable, false); } auto I2SDac::Reconfigure(Channels ch, BitsPerSample bps, SampleRate rate) diff --git a/src/drivers/include/display.hpp b/src/drivers/include/display.hpp index 77165c99..766fc4ea 100644 --- a/src/drivers/include/display.hpp +++ b/src/drivers/include/display.hpp @@ -30,10 +30,10 @@ class Display { * over SPI. This never fails, since unfortunately these display don't give * us back any kind of signal to tell us we're actually using them correctly. */ - static auto Create(IGpios* expander, + static auto Create(IGpios& expander, const displays::InitialisationData& init_data) -> Display*; - Display(IGpios* gpio, spi_device_handle_t handle); + Display(IGpios& gpio, spi_device_handle_t handle); ~Display(); auto SetDisplayOn(bool) -> void; @@ -49,7 +49,7 @@ class Display { Display& operator=(const Display&) = delete; private: - IGpios* gpio_; + IGpios& gpio_; spi_device_handle_t handle_; std::unique_ptr worker_task_; diff --git a/src/drivers/include/i2s_dac.hpp b/src/drivers/include/i2s_dac.hpp index c7faed2f..6bc5b6a4 100644 --- a/src/drivers/include/i2s_dac.hpp +++ b/src/drivers/include/i2s_dac.hpp @@ -33,9 +33,9 @@ namespace drivers { */ class I2SDac { public: - static auto create(IGpios* expander) -> std::optional; + static auto create(IGpios& expander) -> std::optional; - I2SDac(IGpios* gpio, i2s_chan_handle_t i2s_handle); + I2SDac(IGpios& gpio, i2s_chan_handle_t i2s_handle); ~I2SDac(); auto Start() -> void; @@ -69,7 +69,7 @@ class I2SDac { I2SDac& operator=(const I2SDac&) = delete; private: - IGpios* gpio_; + IGpios& gpio_; i2s_chan_handle_t i2s_handle_; bool i2s_active_; StreamBufferHandle_t buffer_; diff --git a/src/drivers/include/relative_wheel.hpp b/src/drivers/include/relative_wheel.hpp index 5e801aba..b5532a4c 100644 --- a/src/drivers/include/relative_wheel.hpp +++ b/src/drivers/include/relative_wheel.hpp @@ -20,11 +20,7 @@ namespace drivers { class RelativeWheel { public: - static auto Create(TouchWheel* touch) -> RelativeWheel* { - return new RelativeWheel(touch); - } - - explicit RelativeWheel(TouchWheel* touch); + explicit RelativeWheel(TouchWheel& touch); auto Update() -> void; auto SetEnabled(bool) -> void; @@ -37,7 +33,7 @@ class RelativeWheel { RelativeWheel& operator=(const RelativeWheel&) = delete; private: - TouchWheel* touch_; + TouchWheel& touch_; bool is_enabled_; diff --git a/src/drivers/include/storage.hpp b/src/drivers/include/storage.hpp index 65be75f1..0b0cb494 100644 --- a/src/drivers/include/storage.hpp +++ b/src/drivers/include/storage.hpp @@ -31,9 +31,9 @@ class SdStorage { FAILED_TO_MOUNT, }; - static auto Create(IGpios* gpio) -> cpp::result; + static auto Create(IGpios& gpio) -> cpp::result; - SdStorage(IGpios* gpio, + SdStorage(IGpios& gpio, sdspi_dev_handle_t handle_, std::unique_ptr host_, std::unique_ptr card_, @@ -50,7 +50,7 @@ class SdStorage { SdStorage& operator=(const SdStorage&) = delete; private: - IGpios* gpio_; + IGpios& gpio_; // SPI and SD driver info sdspi_dev_handle_t handle_; diff --git a/src/drivers/relative_wheel.cpp b/src/drivers/relative_wheel.cpp index 75b62ae7..c014ab5e 100644 --- a/src/drivers/relative_wheel.cpp +++ b/src/drivers/relative_wheel.cpp @@ -13,7 +13,7 @@ namespace drivers { -RelativeWheel::RelativeWheel(TouchWheel* touch) +RelativeWheel::RelativeWheel(TouchWheel& touch) : touch_(touch), is_enabled_(true), is_clicking_(false), @@ -23,8 +23,8 @@ RelativeWheel::RelativeWheel(TouchWheel* touch) last_angle_(0) {} auto RelativeWheel::Update() -> void { - touch_->Update(); - TouchWheelData d = touch_->GetTouchWheelData(); + touch_.Update(); + TouchWheelData d = touch_.GetTouchWheelData(); is_clicking_ = d.is_button_touched; diff --git a/src/drivers/storage.cpp b/src/drivers/storage.cpp index f253a79a..6acb6870 100644 --- a/src/drivers/storage.cpp +++ b/src/drivers/storage.cpp @@ -32,10 +32,10 @@ namespace drivers { const char* kStoragePath = "/sdcard"; -auto SdStorage::Create(IGpios* gpio) -> cpp::result { - gpio->WriteSync(IGpios::Pin::kSdPowerEnable, 1); - gpio->WriteSync(IGpios::Pin::kSdMuxSwitch, IGpios::SD_MUX_ESP); - gpio->WriteSync(IGpios::Pin::kSdMuxDisable, 0); +auto SdStorage::Create(IGpios& gpio) -> cpp::result { + gpio.WriteSync(IGpios::Pin::kSdPowerEnable, 1); + gpio.WriteSync(IGpios::Pin::kSdMuxSwitch, IGpios::SD_MUX_ESP); + gpio.WriteSync(IGpios::Pin::kSdMuxDisable, 0); sdspi_dev_handle_t handle; FATFS* fs = nullptr; @@ -95,7 +95,7 @@ auto SdStorage::Create(IGpios* gpio) -> cpp::result { return new SdStorage(gpio, handle, std::move(host), std::move(card), fs); } -SdStorage::SdStorage(IGpios* gpio, +SdStorage::SdStorage(IGpios& gpio, sdspi_dev_handle_t handle, std::unique_ptr host, std::unique_ptr card, @@ -117,8 +117,8 @@ SdStorage::~SdStorage() { sdspi_host_remove_device(this->handle_); sdspi_host_deinit(); - gpio_->WriteSync(IGpios::Pin::kSdPowerEnable, 1); - gpio_->WriteSync(IGpios::Pin::kSdMuxDisable, 1); + gpio_.WriteSync(IGpios::Pin::kSdPowerEnable, 1); + gpio_.WriteSync(IGpios::Pin::kSdMuxDisable, 1); } auto SdStorage::GetFs() -> FATFS* { -- cgit v1.2.3