Files
esphome/esphome/components/tcal6416/tcal6416.h
Claude b85c98abb4 [tcal6416] Add support for TCAL6416 I2C I/O expander
Implements support for the TI TCAL6416 16-bit I2C I/O expander with the following features:
- 16 GPIO pins (2 banks of 8)
- Bidirectional I/O with configurable input/output modes
- Support for inverted pins
- Compatible with ESP32, ESP8266, and RP2040 platforms
- I2C interface with configurable address (default: 0x20)

The TCAL6416 provides higher current latched outputs suitable for directly
driving LEDs or keypads. It operates at voltages from 1.08V to 3.6V and
supports I2C speeds up to 1MHz.

Addresses: https://github.com/orgs/esphome/discussions/3233
2025-11-17 22:43:46 +00:00

67 lines
1.9 KiB
C++

#pragma once
#include "esphome/components/gpio_expander/cached_gpio.h"
#include "esphome/components/i2c/i2c.h"
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
namespace esphome {
namespace tcal6416 {
class TCAL6416Component : public Component,
public i2c::I2CDevice,
public gpio_expander::CachedGpioExpander<uint8_t, 16> {
public:
TCAL6416Component() = default;
/// Check i2c availability and setup masks
void setup() override;
void pin_mode(uint8_t pin, gpio::Flags flags);
float get_setup_priority() const override;
void dump_config() override;
void loop() override;
protected:
bool digital_read_hw(uint8_t pin) override;
bool digital_read_cache(uint8_t pin) override;
void digital_write_hw(uint8_t pin, bool value) override;
/// Mask for the pin mode - 1 means input, 0 means output
uint16_t mode_mask_{0xFFFF};
/// The mask to write as output state - 1 means HIGH, 0 means LOW
uint16_t output_mask_{0x00};
/// The state read in digital_read_hw - 1 means HIGH, 0 means LOW
uint16_t input_mask_{0x00};
bool read_gpio_modes_();
bool write_gpio_modes_();
bool read_gpio_outputs_();
};
/// Helper class to expose a TCAL6416 pin as an internal input GPIO pin.
class TCAL6416GPIOPin : public GPIOPin, public Parented<TCAL6416Component> {
public:
void setup() override;
void pin_mode(gpio::Flags flags) override;
bool digital_read() override;
void digital_write(bool value) override;
std::string dump_summary() const override;
void set_pin(uint8_t pin) { this->pin_ = pin; }
void set_inverted(bool inverted) { this->inverted_ = inverted; }
void set_flags(gpio::Flags flags) { this->flags_ = flags; }
gpio::Flags get_flags() const override { return this->flags_; }
protected:
uint8_t pin_;
bool inverted_;
gpio::Flags flags_;
};
} // namespace tcal6416
} // namespace esphome