arduino-audio-driver
Loading...
Searching...
No Matches
TCA9555.h
Go to the documentation of this file.
1#pragma once
8#include "API_GPIO.h"
9#include "Platforms/API_I2C.h"
11
16namespace audio_driver {
17
25class TCA9555 : public API_GPIO {
26 public:
30 TCA9555() : i2c_default_address(0x20), bus(nullptr) {}
31
37 bool begin(IDriverDeviceInfo& pins) override {
38 AD_LOGI("TCA9555::begin");
39 auto expanderPins = pins.getI2CPins(PinFunction::EXPANDER);
40 if (!expanderPins) {
41 AD_LOGI("TCA9555 not configured in DriverDeviceInfo");
42 return false;
43 }
44 // Setup I2CConfig and create the bus
45 I2CConfig cfg = expanderPins.value();
46 if (cfg.address > 0) {
48 }
49 AD_LOGI("TCA9555 address: 0x%02X", i2c_default_address);
50 i2c_bus_create(&cfg);
51 bus = cfg.p_wire;
52 return bus != nullptr;
53 }
54
59 void end() override {
60 if (bus != nullptr) {
61 i2c_bus_delete(bus);
62 bus = nullptr;
63 }
64 }
65
72 bool digitalWrite(GpioPin pin, bool value) override {
73 if (pin >= 1000) pin -= 1000;
74 AD_LOGI("TCA9555::digitalWrite %d: %d", pin, value);
75 if (pin > 15 || bus == nullptr) {
76 AD_LOGE("TCA9555 invalid pin: %d", pin);
77 return false;
78 }
79 uint8_t port = pin / 8;
80 uint8_t bit = pin % 8;
81 uint8_t reg = 0x02 + port; // Output port 0 or 1
82 uint8_t out;
83 if (!i2c_read(i2c_default_address, reg, &out, 1)) {
84 AD_LOGE("TCA9555 read failed for pin: %d", pin);
85 return false;
86 }
87 if (value)
88 out |= (1 << bit);
89 else
90 out &= ~(1 << bit);
91 bool rc = i2c_write(i2c_default_address, reg, &out, 1);
92 if (!rc) {
93 AD_LOGE("TCA9555 write failed for pin: %d", pin);
94 }
95 return rc;
96 }
97
103 bool digitalRead(GpioPin pin) override {
104 if (pin >= 1000) pin -= 1000;
105 AD_LOGI("TCA9555::digitalRead %d", pin);
106 if (pin > 15 || bus == nullptr) return false;
107 uint8_t port = pin / 8;
108 uint8_t bit = pin % 8;
109 uint8_t reg = 0x00 + port; // Input port 0 or 1
110 uint8_t in;
111 if (!i2c_read(i2c_default_address, reg, &in, 1)) return false;
112 return (in & (1 << bit)) != 0;
113 }
114
121 void pinMode(GpioPin pin, int mode) override {
122 if (pin >= 1000) pin -= 1000;
123 AD_LOGI("TCA9555::pinMode: %d", pin);
124 if (pin > 15 || bus == nullptr) {
125 AD_LOGE("TCA9555 pinMode: invalid pin: %d", pin);
126 return;
127 }
128 bool input = (mode == INPUT || mode == INPUT_PULLUP);
129 uint8_t port = pin / 8;
130 uint8_t bit = pin % 8;
131 uint8_t reg = 0x06 + port; // Configuration port 0 or 1
132 uint8_t cfg;
133 if (!i2c_read(i2c_default_address, reg, &cfg, 1)) {
134 AD_LOGE("TCA9555 read failed for pin: %d", pin);
135 return;
136 }
137 if (input)
138 cfg |= (1 << bit);
139 else
140 cfg &= ~(1 << bit);
141 i2c_write(i2c_default_address, reg, &cfg, 1);
142 }
143
145 int analogRead(ADCPin pin) {
146 AD_LOGE("TCA9555 analogRead() not supported");
147 return 0;
148 }
149
150
155 void setI2CAddress(uint8_t address) { i2c_default_address = address; }
156
157 protected:
160
165 bool i2c_read(uint8_t addr, uint8_t reg, uint8_t* data, size_t len) {
166 if (bus == nullptr) {
167 AD_LOGE("TCA9555 bus is null");
168 return false;
169 }
170 return i2c_bus_read_bytes(bus, addr, &reg, 1, data, len) == 0;
171 }
176 bool i2c_write(uint8_t addr, uint8_t reg, uint8_t* data, size_t len) {
177 if (bus == nullptr) {
178 AD_LOGE("TCA9555 bus is null");
179 return false;
180 }
181 return i2c_bus_write_bytes(bus, addr, &reg, 1, data, len) == 0;
182 }
183};
184
185} // namespace audio_driver
#define INPUT
Definition DriverCommon.h:41
#define INPUT_PULLUP
Definition DriverCommon.h:47
#define AD_LOGI(...)
Definition Logger.h:13
#define AD_LOGE(...)
Definition Logger.h:15
Definition API_GPIO.h:8
Definition IDriverDeviceInfo.h:171
virtual audio_driver_local::Optional< InfoI2C > getI2CPins(PinFunction function)=0
Driver for the TCA9555 I2C I/O expander (header-only).
Definition TCA9555.h:25
bool i2c_write(uint8_t addr, uint8_t reg, uint8_t *data, size_t len)
Helper to write bytes to the TCA9555 using the platform I2C abstraction.
Definition TCA9555.h:176
bool digitalWrite(GpioPin pin, bool value) override
Set the output state of a pin.
Definition TCA9555.h:72
uint8_t i2c_default_address
Definition TCA9555.h:158
void end() override
Deinitialize the TCA9555 and delete the I2C bus.
Definition TCA9555.h:59
TCA9555()
Construct a new TCA9555 object with default I2C address 0x20.
Definition TCA9555.h:30
bool digitalRead(GpioPin pin) override
Read the input state of a pin.
Definition TCA9555.h:103
int analogRead(ADCPin pin)
Not supported.
Definition TCA9555.h:145
void pinMode(GpioPin pin, int mode) override
Set the direction of a pin.
Definition TCA9555.h:121
bool i2c_read(uint8_t addr, uint8_t reg, uint8_t *data, size_t len)
Helper to read bytes from the TCA9555 using the platform I2C abstraction.
Definition TCA9555.h:165
void setI2CAddress(uint8_t address)
Set the I2C address for the TCA9555.
Definition TCA9555.h:155
bool begin(IDriverDeviceInfo &pins) override
Initialize the TCA9555 and create the I2C bus.
Definition TCA9555.h:37
i2c_bus_handle_t bus
Definition TCA9555.h:159
Namespace for audio driver components.
Definition AudioBoard.h:5
int16_t GpioPin
Definition DriverCommon.h:153
int16_t ADCPin
Definition DriverCommon.h:154
void * i2c_bus_handle_t
Definition DriverCommon.h:155
Definition API_I2C.h:23
int address
Definition API_I2C.h:27
i2c_bus_handle_t p_wire
Definition API_I2C.h:24