mirror of
https://github.com/esphome/esphome.git
synced 2026-01-10 12:10:48 -07:00
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
73 lines
1.9 KiB
Python
73 lines
1.9 KiB
Python
from esphome import pins
|
|
import esphome.codegen as cg
|
|
from esphome.components import i2c
|
|
import esphome.config_validation as cv
|
|
from esphome.const import (
|
|
CONF_ID,
|
|
CONF_INPUT,
|
|
CONF_INVERTED,
|
|
CONF_MODE,
|
|
CONF_NUMBER,
|
|
CONF_OUTPUT,
|
|
)
|
|
|
|
CODEOWNERS = ["@crgarcia12"]
|
|
|
|
AUTO_LOAD = ["gpio_expander"]
|
|
DEPENDENCIES = ["i2c"]
|
|
MULTI_CONF = True
|
|
|
|
tcal6416_ns = cg.esphome_ns.namespace("tcal6416")
|
|
|
|
TCAL6416Component = tcal6416_ns.class_("TCAL6416Component", cg.Component, i2c.I2CDevice)
|
|
TCAL6416GPIOPin = tcal6416_ns.class_("TCAL6416GPIOPin", cg.GPIOPin)
|
|
|
|
CONF_TCAL6416 = "tcal6416"
|
|
CONFIG_SCHEMA = (
|
|
cv.Schema(
|
|
{
|
|
cv.Required(CONF_ID): cv.declare_id(TCAL6416Component),
|
|
}
|
|
)
|
|
.extend(cv.COMPONENT_SCHEMA)
|
|
.extend(i2c.i2c_device_schema(0x20))
|
|
)
|
|
|
|
|
|
async def to_code(config):
|
|
var = cg.new_Pvariable(config[CONF_ID])
|
|
await cg.register_component(var, config)
|
|
await i2c.register_i2c_device(var, config)
|
|
|
|
|
|
def validate_mode(value):
|
|
if not (value[CONF_INPUT] or value[CONF_OUTPUT]):
|
|
raise cv.Invalid("Mode must be either input or output")
|
|
if value[CONF_INPUT] and value[CONF_OUTPUT]:
|
|
raise cv.Invalid("Mode must be either input or output")
|
|
return value
|
|
|
|
|
|
TCAL6416_PIN_SCHEMA = pins.gpio_base_schema(
|
|
TCAL6416GPIOPin,
|
|
cv.int_range(min=0, max=15),
|
|
modes=[CONF_INPUT, CONF_OUTPUT],
|
|
mode_validator=validate_mode,
|
|
invertible=True,
|
|
).extend(
|
|
{
|
|
cv.Required(CONF_TCAL6416): cv.use_id(TCAL6416Component),
|
|
}
|
|
)
|
|
|
|
|
|
@pins.PIN_SCHEMA_REGISTRY.register(CONF_TCAL6416, TCAL6416_PIN_SCHEMA)
|
|
async def tcal6416_pin_to_code(config):
|
|
var = cg.new_Pvariable(config[CONF_ID])
|
|
await cg.register_parented(var, config[CONF_TCAL6416])
|
|
|
|
cg.add(var.set_pin(config[CONF_NUMBER]))
|
|
cg.add(var.set_inverted(config[CONF_INVERTED]))
|
|
cg.add(var.set_flags(pins.gpio_flags_expr(config[CONF_MODE])))
|
|
return var
|