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/gpio_expander.hpp | 148 ---------------------------------- 1 file changed, 148 deletions(-) delete mode 100644 src/drivers/include/gpio_expander.hpp (limited to 'src/drivers/include/gpio_expander.hpp') 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 -- cgit v1.2.3