summaryrefslogtreecommitdiff
path: root/src/drivers/include/gpio_expander.hpp
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2023-06-30 20:48:40 +1000
committerjacqueline <me@jacqueline.id.au>2023-06-30 20:48:40 +1000
commit371f0a20cad4dfcb3237db6f72a7e35403950938 (patch)
tree48240a9c3d2121095e0f10537228603120435dd0 /src/drivers/include/gpio_expander.hpp
parent0347555d5b2314e1be58261ef29fa13a76c039e6 (diff)
downloadtangara-fw-371f0a20cad4dfcb3237db6f72a7e35403950938.tar.gz
Clean up gpios interface
Diffstat (limited to 'src/drivers/include/gpio_expander.hpp')
-rw-r--r--src/drivers/include/gpio_expander.hpp148
1 files changed, 0 insertions, 148 deletions
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 <me@jacqueline.id.au>
- *
- * SPDX-License-Identifier: GPL-3.0-only
- */
-
-#pragma once
-
-#include <stdint.h>
-
-#include <atomic>
-#include <functional>
-#include <memory>
-#include <mutex>
-#include <optional>
-#include <tuple>
-#include <utility>
-
-#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 <typename F>
- 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<uint16_t>& 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<uint16_t>& 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<std::function<void(void)>>& {
- return listener_;
- }
-
- auto set_listener(const std::weak_ptr<std::function<void(void)>>& 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<uint16_t> ports_;
- std::atomic<uint16_t> inputs_;
-
- std::weak_ptr<std::function<void(void)>> listener_;
-};
-
-} // namespace drivers