parent
0347555d5b
commit
371f0a20ca
@ -1,85 +0,0 @@ |
||||
/*
|
||||
* Copyright 2023 jacqueline <me@jacqueline.id.au> |
||||
* |
||||
* SPDX-License-Identifier: GPL-3.0-only |
||||
*/ |
||||
|
||||
#include "digital_pot.hpp" |
||||
|
||||
#include <cstdint> |
||||
|
||||
namespace drivers { |
||||
|
||||
using GpioExpander::VOL_LEFT; |
||||
using GpioExpander::VOL_RIGHT; |
||||
using GpioExpander::VOL_UP_DOWN; |
||||
using GpioExpander::VOL_Z_CROSS; |
||||
|
||||
DigitalPot::DigitalPot(GpioExpander* gpios) : gpios_(gpios) { |
||||
gpios_->set_pin(VOL_Z_CROSS, true); // Active-low
|
||||
gpios_->set_pin(VOL_UP_DOWN, true); |
||||
gpios_->set_pin(VOL_LEFT, false); |
||||
gpios_->set_pin(VOL_RIGHT, false); |
||||
gpios_->Write(); |
||||
|
||||
// Power-on reset sets attenuation to maximum anyway, but we want to be safe
|
||||
// and not blow anyone's ears out.
|
||||
for (int i = 0; i < 32; i++) { |
||||
gpios_->set_pin(VOL_LEFT, true); |
||||
gpios_->set_pin(VOL_RIGHT, true); |
||||
gpios_->Write(); |
||||
gpios_->set_pin(VOL_LEFT, false); |
||||
gpios_->set_pin(VOL_RIGHT, false); |
||||
gpios_->Write(); |
||||
} |
||||
} |
||||
|
||||
auto DigitalPot::SetRelative(int_fast8_t change) -> void { |
||||
if (change == 0) { |
||||
return; |
||||
} |
||||
|
||||
gpios_->set_pin(VOL_UP_DOWN, change > 0); |
||||
gpios_->Write(); |
||||
|
||||
for (int i = 0; i < std::abs(change); i++) { |
||||
gpios_->set_pin(VOL_LEFT, true); |
||||
gpios_->set_pin(VOL_RIGHT, true); |
||||
gpios_->Write(); |
||||
gpios_->set_pin(VOL_LEFT, false); |
||||
gpios_->set_pin(VOL_RIGHT, false); |
||||
gpios_->Write(); |
||||
} |
||||
} |
||||
|
||||
auto DigitalPot::SetRelative(Channel ch, int_fast8_t change) -> void { |
||||
if (change == 0) { |
||||
return; |
||||
} |
||||
|
||||
GpioExpander::Pin pin = (ch == Channel::kLeft) ? VOL_LEFT : VOL_RIGHT; |
||||
gpios_->set_pin(VOL_UP_DOWN, change > 0); |
||||
gpios_->Write(); |
||||
|
||||
for (int i = 0; i < std::abs(change); i++) { |
||||
gpios_->set_pin(pin, true); |
||||
gpios_->Write(); |
||||
gpios_->set_pin(pin, false); |
||||
gpios_->Write(); |
||||
} |
||||
} |
||||
|
||||
auto DigitalPot::SetZeroCrossDetect(bool enabled) -> void { |
||||
gpios_->set_pin(VOL_Z_CROSS, !enabled); // Active-low
|
||||
gpios_->Write(); |
||||
} |
||||
|
||||
auto DigitalPot::GetMaxAttenuation() -> int_fast8_t { |
||||
return 31; |
||||
} |
||||
|
||||
auto DigitalPot::GetMinAttenuation() -> int_fast8_t { |
||||
return 0; |
||||
} |
||||
|
||||
} // namespace drivers
|
@ -1,140 +0,0 @@ |
||||
/*
|
||||
* Copyright 2023 jacqueline <me@jacqueline.id.au> |
||||
* |
||||
* SPDX-License-Identifier: GPL-3.0-only |
||||
*/ |
||||
|
||||
#include "gpio_expander.hpp" |
||||
#include <stdint.h> |
||||
|
||||
#include <cstdint> |
||||
|
||||
#include "driver/gpio.h" |
||||
#include "hal/gpio_types.h" |
||||
#include "i2c.hpp" |
||||
|
||||
namespace drivers { |
||||
|
||||
static const uint8_t kPca8575Address = 0x20; |
||||
|
||||
// Port A:
|
||||
// 0 - sd card mux switch
|
||||
// 1 - sd card mux enable (active low)
|
||||
// 2 - key up
|
||||
// 3 - key down
|
||||
// 4 - key lock
|
||||
// 5 - display reset (active low)
|
||||
// 6 - NC
|
||||
// 7 - sd card power (active low)
|
||||
// Default to SD card off, inputs high.
|
||||
static const uint8_t kPortADefault = 0b10111110; |
||||
|
||||
// Port B:
|
||||
// 0 - 3.5mm jack detect (active low)
|
||||
// 1 - headphone amp power enable
|
||||
// 2 - volume zero-cross detection
|
||||
// 3 - volume direction
|
||||
// 4 - volume left channel
|
||||
// 5 - volume right channel
|
||||
// 6 - NC
|
||||
// 7 - NC
|
||||
// Default input high, trs output low
|
||||
static const uint8_t kPortBDefault = 0b00000011; |
||||
|
||||
/*
|
||||
* Convenience mehod for packing the port a and b bytes into a single 16 bit |
||||
* value. |
||||
*/ |
||||
constexpr uint16_t pack(uint8_t a, uint8_t b) { |
||||
return ((uint16_t)b) << 8 | a; |
||||
} |
||||
|
||||
/*
|
||||
* Convenience mehod for unpacking the result of `pack` back into two single |
||||
* byte port datas. |
||||
*/ |
||||
constexpr std::pair<uint8_t, uint8_t> unpack(uint16_t ba) { |
||||
return std::pair((uint8_t)ba, (uint8_t)(ba >> 8)); |
||||
} |
||||
|
||||
void interrupt_isr(void* arg) { |
||||
GpioExpander* instance = reinterpret_cast<GpioExpander*>(arg); |
||||
auto listener = instance->listener().lock(); |
||||
if (listener) { |
||||
std::invoke(*listener); |
||||
} |
||||
} |
||||
|
||||
auto GpioExpander::Create() -> GpioExpander* { |
||||
GpioExpander* instance = new GpioExpander(); |
||||
// Read and write initial values on initialisation so that we do not have a
|
||||
// strange partially-initialised state.
|
||||
if (!instance->Write() || !instance->Read()) { |
||||
return nullptr; |
||||
} |
||||
return instance; |
||||
} |
||||
|
||||
GpioExpander::GpioExpander() |
||||
: ports_(pack(kPortADefault, kPortBDefault)), inputs_(0), listener_() { |
||||
gpio_config_t config{ |
||||
.pin_bit_mask = static_cast<uint64_t>(1) << GPIO_NUM_34, |
||||
.mode = GPIO_MODE_INPUT, |
||||
.pull_up_en = GPIO_PULLUP_ENABLE, |
||||
.pull_down_en = GPIO_PULLDOWN_DISABLE, |
||||
.intr_type = GPIO_INTR_NEGEDGE, |
||||
}; |
||||
gpio_config(&config); |
||||
gpio_install_isr_service(ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_SHARED | |
||||
ESP_INTR_FLAG_IRAM); |
||||
gpio_isr_handler_add(GPIO_NUM_34, &interrupt_isr, this); |
||||
} |
||||
|
||||
GpioExpander::~GpioExpander() { |
||||
gpio_isr_handler_remove(GPIO_NUM_34); |
||||
gpio_uninstall_isr_service(); |
||||
} |
||||
|
||||
bool GpioExpander::Write() { |
||||
std::pair<uint8_t, uint8_t> ports_ab = unpack(ports()); |
||||
|
||||
I2CTransaction transaction; |
||||
transaction.start() |
||||
.write_addr(kPca8575Address, I2C_MASTER_WRITE) |
||||
.write_ack(ports_ab.first, ports_ab.second) |
||||
.stop(); |
||||
|
||||
return transaction.Execute() == ESP_OK; |
||||
} |
||||
|
||||
bool GpioExpander::Read() { |
||||
uint8_t input_a, input_b; |
||||
|
||||
I2CTransaction transaction; |
||||
transaction.start() |
||||
.write_addr(kPca8575Address, I2C_MASTER_READ) |
||||
.read(&input_a, I2C_MASTER_ACK) |
||||
.read(&input_b, I2C_MASTER_LAST_NACK) |
||||
.stop(); |
||||
|
||||
esp_err_t ret = transaction.Execute(); |
||||
if (ret != ESP_OK) { |
||||
return false; |
||||
} |
||||
inputs_ = pack(input_a, input_b); |
||||
return true; |
||||
} |
||||
|
||||
void GpioExpander::set_pin(Pin pin, bool value) { |
||||
if (value) { |
||||
ports_ |= (1 << pin); |
||||
} else { |
||||
ports_ &= ~(1 << pin); |
||||
} |
||||
} |
||||
|
||||
bool GpioExpander::get_input(Pin pin) const { |
||||
return (inputs_ & (1 << pin)) > 0; |
||||
} |
||||
|
||||
} // namespace drivers
|
@ -1,49 +0,0 @@ |
||||
/*
|
||||
* Copyright 2023 jacqueline <me@jacqueline.id.au> |
||||
* |
||||
* SPDX-License-Identifier: GPL-3.0-only |
||||
*/ |
||||
|
||||
#pragma once |
||||
|
||||
#include <stdint.h> |
||||
#include <functional> |
||||
|
||||
#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
|
@ -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
|
Loading…
Reference in new issue