From 371f0a20cad4dfcb3237db6f72a7e35403950938 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Fri, 30 Jun 2023 20:48:40 +1000 Subject: Clean up gpios interface --- src/drivers/include/digital_pot.hpp | 49 ----------- src/drivers/include/display.hpp | 8 +- src/drivers/include/gpio_expander.hpp | 148 --------------------------------- src/drivers/include/i2s_dac.hpp | 8 +- src/drivers/include/relative_wheel.hpp | 2 +- src/drivers/include/storage.hpp | 8 +- src/drivers/include/touchwheel.hpp | 2 +- 7 files changed, 14 insertions(+), 211 deletions(-) delete mode 100644 src/drivers/include/digital_pot.hpp delete mode 100644 src/drivers/include/gpio_expander.hpp (limited to 'src/drivers/include') diff --git a/src/drivers/include/digital_pot.hpp b/src/drivers/include/digital_pot.hpp deleted file mode 100644 index e2ca00b1..00000000 --- a/src/drivers/include/digital_pot.hpp +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright 2023 jacqueline - * - * SPDX-License-Identifier: GPL-3.0-only - */ - -#pragma once - -#include -#include - -#include "esp_err.h" -#include "result.hpp" - -#include "gpio_expander.hpp" - -namespace drivers { - -/* - * Driver for a two-channel digital potentiometer, with steps measured in - * decibels. - */ -class DigitalPot { - public: - explicit DigitalPot(GpioExpander* gpios); - ~DigitalPot() {} - - // Not copyable or movable. - DigitalPot(const DigitalPot&) = delete; - DigitalPot& operator=(const DigitalPot&) = delete; - - enum class Channel { - kLeft, - kRight, - }; - - auto SetRelative(int_fast8_t change) -> void; - auto SetRelative(Channel ch, int_fast8_t change) -> void; - - auto SetZeroCrossDetect(bool enabled) -> void; - - auto GetMaxAttenuation() -> int_fast8_t; - auto GetMinAttenuation() -> int_fast8_t; - - private: - GpioExpander* gpios_; -}; - -} // namespace drivers diff --git a/src/drivers/include/display.hpp b/src/drivers/include/display.hpp index 4b63e1c4..23bbbab9 100644 --- a/src/drivers/include/display.hpp +++ b/src/drivers/include/display.hpp @@ -16,7 +16,7 @@ #include "tasks.hpp" #include "display_init.hpp" -#include "gpio_expander.hpp" +#include "gpios.hpp" namespace drivers { @@ -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(GpioExpander* expander, + static auto Create(IGpios* expander, const displays::InitialisationData& init_data) -> Display*; - Display(GpioExpander* gpio, spi_device_handle_t handle); + Display(IGpios* gpio, spi_device_handle_t handle); ~Display(); auto SetDisplayOn(bool) -> void; @@ -48,7 +48,7 @@ class Display { Display& operator=(const Display&) = delete; private: - GpioExpander* gpio_; + IGpios* gpio_; spi_device_handle_t handle_; std::unique_ptr worker_task_; diff --git a/src/drivers/include/gpio_expander.hpp b/src/drivers/include/gpio_expander.hpp deleted file mode 100644 index 8108d176..00000000 --- a/src/drivers/include/gpio_expander.hpp +++ /dev/null @@ -1,148 +0,0 @@ -/* - * Copyright 2023 jacqueline - * - * SPDX-License-Identifier: GPL-3.0-only - */ - -#pragma once - -#include - -#include -#include -#include -#include -#include -#include -#include - -#include "driver/i2c.h" -#include "esp_check.h" -#include "esp_err.h" -#include "esp_log.h" -#include "freertos/FreeRTOS.h" - -namespace drivers { - -/** - * Wrapper for interfacing with the PCA8575 GPIO expander. Includes basic - * low-level pin setting methods, as well as higher level convenience functions - * for reading, writing, and atomically interacting with the SPI chip select - * pins. - * - * Each method of this class can be called safely from any thread, and all - * updates are guaranteed to be atomic. Any access to chip select related pins - * should be done whilst holding `cs_lock` (preferably via the helper methods). - */ -class GpioExpander { - public: - static auto Create() -> GpioExpander*; - ~GpioExpander(); - - /* - * Convenience function for running some arbitrary pin writing code, then - * flushing a `Write()` to the expander. Example usage: - * - * ``` - * gpio_.with([&](auto& gpio) { - * gpio.set_pin(AUDIO_POWER_ENABLE, true); - * }); - * ``` - */ - template - auto with(F fn) -> void { - std::invoke(fn); - Write(); - } - - /** - * Sets the ports on the GPIO expander to the values currently represented - * in `ports`. - */ - auto Write(void) -> bool; - - /** - * Reads from the GPIO expander, populating `inputs` with the most recent - * values. - */ - auto Read(void) -> bool; - - /* Maps each pin of the expander to its number in a `pack`ed uint16. */ - enum Pin { - // Port A - SD_MUX_SWITCH = 0, - SD_MUX_EN_ACTIVE_LOW = 1, - KEY_UP = 2, - KEY_DOWN = 3, - KEY_LOCK = 4, - DISPLAY_RESET_ACTIVE_LOW = 5, - // UNUSED = 6, - SD_CARD_POWER_ENABLE_ACTIVE_LOW = 7, - - // Port B - PHONE_DETECT = 8, - AMP_EN = 9, - VOL_Z_CROSS = 10, - VOL_UP_DOWN = 11, - VOL_LEFT = 12, - VOL_RIGHT = 13, - // UNUSED = 14, - // UNUSED = 15, - }; - - /* Nicer value names for use with the SD_MUX_SWITCH pin. */ - enum SdController { - SD_MUX_ESP = 0, - SD_MUX_SAMD = 1, - }; - - /** - * Returns the current driven status of each of the ports. The first byte is - * port a, and the second byte is port b. - */ - std::atomic& ports() { return ports_; } - - /* - * Sets a single specific pin to the given value. `true` corresponds to - * HIGH, and `false` corresponds to LOW. - * - * Calls to this method will be buffered in memory until a call to `Write()` - * is made. - */ - void set_pin(Pin pin, bool value); - - /** - * Returns the input status of each of the ports. The first byte is port a, - * and the second byte is port b. - */ - const std::atomic& inputs() const { return inputs_; } - - /* Returns the most recently cached value of the given pin. Only valid for - * pins used as inputs; to check what value we're driving a pin, use - * `ports()`. - */ - bool get_input(Pin pin) const; - - auto listener() -> std::weak_ptr>& { - return listener_; - } - - auto set_listener(const std::weak_ptr>& l) -> void { - listener_ = l; - } - - // Not copyable or movable. There should usually only ever be once instance - // of this class, and that instance will likely have a static lifetime. - GpioExpander(const GpioExpander&) = delete; - GpioExpander& operator=(const GpioExpander&) = delete; - - private: - GpioExpander(); - - std::atomic ports_; - std::atomic inputs_; - - std::weak_ptr> listener_; -}; - -} // namespace drivers diff --git a/src/drivers/include/i2s_dac.hpp b/src/drivers/include/i2s_dac.hpp index 388d09fa..39eb9c4c 100644 --- a/src/drivers/include/i2s_dac.hpp +++ b/src/drivers/include/i2s_dac.hpp @@ -22,7 +22,7 @@ #include "result.hpp" #include "span.hpp" -#include "gpio_expander.hpp" +#include "gpios.hpp" #include "sys/_stdint.h" namespace drivers { @@ -32,9 +32,9 @@ namespace drivers { */ class I2SDac { public: - static auto create(GpioExpander* expander) -> std::optional; + static auto create(IGpios* expander) -> std::optional; - I2SDac(GpioExpander* gpio, i2s_chan_handle_t i2s_handle); + I2SDac(IGpios* gpio, i2s_chan_handle_t i2s_handle); ~I2SDac(); auto Start() -> void; @@ -70,7 +70,7 @@ class I2SDac { I2SDac& operator=(const I2SDac&) = delete; private: - GpioExpander* gpio_; + IGpios* gpio_; i2s_chan_handle_t i2s_handle_; bool i2s_active_; std::optional active_page_; diff --git a/src/drivers/include/relative_wheel.hpp b/src/drivers/include/relative_wheel.hpp index 8d74d551..6edc006a 100644 --- a/src/drivers/include/relative_wheel.hpp +++ b/src/drivers/include/relative_wheel.hpp @@ -13,7 +13,7 @@ #include "esp_err.h" #include "result.hpp" -#include "gpio_expander.hpp" +#include "gpios.hpp" #include "touchwheel.hpp" namespace drivers { diff --git a/src/drivers/include/storage.hpp b/src/drivers/include/storage.hpp index daee13c4..a9269261 100644 --- a/src/drivers/include/storage.hpp +++ b/src/drivers/include/storage.hpp @@ -15,7 +15,7 @@ #include "ff.h" #include "result.hpp" -#include "gpio_expander.hpp" +#include "gpios.hpp" namespace drivers { @@ -31,9 +31,9 @@ class SdStorage { FAILED_TO_MOUNT, }; - static auto Create(GpioExpander* gpio) -> cpp::result; + static auto Create(IGpios* gpio) -> cpp::result; - SdStorage(GpioExpander* gpio, + SdStorage(IGpios* gpio, esp_err_t (*do_transaction)(sdspi_dev_handle_t, sdmmc_command_t*), sdspi_dev_handle_t handle_, std::unique_ptr host_, @@ -52,7 +52,7 @@ class SdStorage { SdStorage& operator=(const SdStorage&) = delete; private: - GpioExpander* gpio_; + IGpios* gpio_; esp_err_t (*do_transaction_)(sdspi_dev_handle_t, sdmmc_command_t*) = nullptr; diff --git a/src/drivers/include/touchwheel.hpp b/src/drivers/include/touchwheel.hpp index 5c3442d2..f42b575b 100644 --- a/src/drivers/include/touchwheel.hpp +++ b/src/drivers/include/touchwheel.hpp @@ -12,7 +12,7 @@ #include "esp_err.h" #include "result.hpp" -#include "gpio_expander.hpp" +#include "gpios.hpp" namespace drivers { -- cgit v1.2.3