mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-06-06 04:48:27 +02:00
MCP23XXX Refactor (#1560)
* Refactor MCP23XXX classes to consolidate shared code * Update test mcp23xxx pin schemas
This commit is contained in:
@@ -1,76 +1,28 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome import pins
|
||||
from esphome.components import i2c
|
||||
from esphome.const import (
|
||||
CONF_ID,
|
||||
CONF_NUMBER,
|
||||
CONF_MODE,
|
||||
CONF_INVERTED,
|
||||
CONF_OPEN_DRAIN_INTERRUPT,
|
||||
)
|
||||
from esphome.components import i2c, mcp23x08_base, mcp23xxx_base
|
||||
from esphome.const import CONF_ID
|
||||
|
||||
AUTO_LOAD = ["mcp23x08_base"]
|
||||
CODEOWNERS = ["@jesserockz"]
|
||||
DEPENDENCIES = ["i2c"]
|
||||
MULTI_CONF = True
|
||||
|
||||
mcp23008_ns = cg.esphome_ns.namespace("mcp23008")
|
||||
MCP23008GPIOMode = mcp23008_ns.enum("MCP23008GPIOMode")
|
||||
MCP23008_GPIO_MODES = {
|
||||
"INPUT": MCP23008GPIOMode.MCP23008_INPUT,
|
||||
"INPUT_PULLUP": MCP23008GPIOMode.MCP23008_INPUT_PULLUP,
|
||||
"OUTPUT": MCP23008GPIOMode.MCP23008_OUTPUT,
|
||||
}
|
||||
|
||||
MCP23008 = mcp23008_ns.class_("MCP23008", cg.Component, i2c.I2CDevice)
|
||||
MCP23008GPIOPin = mcp23008_ns.class_("MCP23008GPIOPin", cg.GPIOPin)
|
||||
MCP23008 = mcp23008_ns.class_("MCP23008", mcp23x08_base.MCP23X08Base, i2c.I2CDevice)
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_ID): cv.declare_id(MCP23008),
|
||||
cv.Optional(CONF_OPEN_DRAIN_INTERRUPT, default=False): cv.boolean,
|
||||
}
|
||||
)
|
||||
.extend(cv.COMPONENT_SCHEMA)
|
||||
.extend(mcp23xxx_base.MCP23XXX_CONFIG_SCHEMA)
|
||||
.extend(i2c.i2c_device_schema(0x20))
|
||||
)
|
||||
|
||||
|
||||
def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
yield cg.register_component(var, config)
|
||||
var = yield mcp23xxx_base.register_mcp23xxx(config)
|
||||
yield i2c.register_i2c_device(var, config)
|
||||
cg.add(var.set_open_drain_ints(config[CONF_OPEN_DRAIN_INTERRUPT]))
|
||||
|
||||
|
||||
CONF_MCP23008 = "mcp23008"
|
||||
MCP23008_OUTPUT_PIN_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_MCP23008): cv.use_id(MCP23008),
|
||||
cv.Required(CONF_NUMBER): cv.int_,
|
||||
cv.Optional(CONF_MODE, default="OUTPUT"): cv.enum(
|
||||
MCP23008_GPIO_MODES, upper=True
|
||||
),
|
||||
cv.Optional(CONF_INVERTED, default=False): cv.boolean,
|
||||
}
|
||||
)
|
||||
MCP23008_INPUT_PIN_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_MCP23008): cv.use_id(MCP23008),
|
||||
cv.Required(CONF_NUMBER): cv.int_,
|
||||
cv.Optional(CONF_MODE, default="INPUT"): cv.enum(
|
||||
MCP23008_GPIO_MODES, upper=True
|
||||
),
|
||||
cv.Optional(CONF_INVERTED, default=False): cv.boolean,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@pins.PIN_SCHEMA_REGISTRY.register(
|
||||
CONF_MCP23008, (MCP23008_OUTPUT_PIN_SCHEMA, MCP23008_INPUT_PIN_SCHEMA)
|
||||
)
|
||||
def mcp23008_pin_to_code(config):
|
||||
parent = yield cg.get_variable(config[CONF_MCP23008])
|
||||
yield MCP23008GPIOPin.new(
|
||||
parent, config[CONF_NUMBER], config[CONF_MODE], config[CONF_INVERTED]
|
||||
)
|
||||
|
||||
@@ -9,85 +9,32 @@ static const char *TAG = "mcp23008";
|
||||
void MCP23008::setup() {
|
||||
ESP_LOGCONFIG(TAG, "Setting up MCP23008...");
|
||||
uint8_t iocon;
|
||||
if (!this->read_reg_(MCP23008_IOCON, &iocon)) {
|
||||
if (!this->read_reg(mcp23x08_base::MCP23X08_IOCON, &iocon)) {
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
|
||||
if (this->open_drain_ints_) {
|
||||
// enable open-drain interrupt pins, 3.3V-safe
|
||||
this->write_reg_(MCP23008_IOCON, 0x04);
|
||||
this->write_reg(mcp23x08_base::MCP23X08_IOCON, 0x04);
|
||||
}
|
||||
}
|
||||
bool MCP23008::digital_read(uint8_t pin) {
|
||||
uint8_t bit = pin % 8;
|
||||
uint8_t reg_addr = MCP23008_GPIO;
|
||||
uint8_t value = 0;
|
||||
this->read_reg_(reg_addr, &value);
|
||||
return value & (1 << bit);
|
||||
}
|
||||
void MCP23008::digital_write(uint8_t pin, bool value) {
|
||||
uint8_t reg_addr = MCP23008_OLAT;
|
||||
this->update_reg_(pin, value, reg_addr);
|
||||
}
|
||||
void MCP23008::pin_mode(uint8_t pin, uint8_t mode) {
|
||||
uint8_t iodir = MCP23008_IODIR;
|
||||
uint8_t gppu = MCP23008_GPPU;
|
||||
switch (mode) {
|
||||
case MCP23008_INPUT:
|
||||
this->update_reg_(pin, true, iodir);
|
||||
break;
|
||||
case MCP23008_INPUT_PULLUP:
|
||||
this->update_reg_(pin, true, iodir);
|
||||
this->update_reg_(pin, true, gppu);
|
||||
break;
|
||||
case MCP23008_OUTPUT:
|
||||
this->update_reg_(pin, false, iodir);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
float MCP23008::get_setup_priority() const { return setup_priority::HARDWARE; }
|
||||
bool MCP23008::read_reg_(uint8_t reg, uint8_t *value) {
|
||||
|
||||
void MCP23008::dump_config() { ESP_LOGCONFIG(TAG, "MCP23008:"); }
|
||||
|
||||
bool MCP23008::read_reg(uint8_t reg, uint8_t *value) {
|
||||
if (this->is_failed())
|
||||
return false;
|
||||
|
||||
return this->read_byte(reg, value);
|
||||
}
|
||||
bool MCP23008::write_reg_(uint8_t reg, uint8_t value) {
|
||||
|
||||
bool MCP23008::write_reg(uint8_t reg, uint8_t value) {
|
||||
if (this->is_failed())
|
||||
return false;
|
||||
|
||||
return this->write_byte(reg, value);
|
||||
}
|
||||
void MCP23008::update_reg_(uint8_t pin, bool pin_value, uint8_t reg_addr) {
|
||||
uint8_t bit = pin % 8;
|
||||
uint8_t reg_value = 0;
|
||||
if (reg_addr == MCP23008_OLAT) {
|
||||
reg_value = this->olat_;
|
||||
} else {
|
||||
this->read_reg_(reg_addr, ®_value);
|
||||
}
|
||||
|
||||
if (pin_value)
|
||||
reg_value |= 1 << bit;
|
||||
else
|
||||
reg_value &= ~(1 << bit);
|
||||
|
||||
this->write_reg_(reg_addr, reg_value);
|
||||
|
||||
if (reg_addr == MCP23008_OLAT) {
|
||||
this->olat_ = reg_value;
|
||||
}
|
||||
}
|
||||
|
||||
MCP23008GPIOPin::MCP23008GPIOPin(MCP23008 *parent, uint8_t pin, uint8_t mode, bool inverted)
|
||||
: GPIOPin(pin, mode, inverted), parent_(parent) {}
|
||||
void MCP23008GPIOPin::setup() { this->pin_mode(this->mode_); }
|
||||
void MCP23008GPIOPin::pin_mode(uint8_t mode) { this->parent_->pin_mode(this->pin_, mode); }
|
||||
bool MCP23008GPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; }
|
||||
void MCP23008GPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); }
|
||||
|
||||
} // namespace mcp23008
|
||||
} // namespace esphome
|
||||
|
||||
@@ -1,71 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/mcp23x08_base/mcp23x08_base.h"
|
||||
#include "esphome/core/esphal.h"
|
||||
#include "esphome/components/i2c/i2c.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace mcp23008 {
|
||||
|
||||
/// Modes for MCP23008 pins
|
||||
enum MCP23008GPIOMode : uint8_t {
|
||||
MCP23008_INPUT = INPUT, // 0x00
|
||||
MCP23008_INPUT_PULLUP = INPUT_PULLUP, // 0x02
|
||||
MCP23008_OUTPUT = OUTPUT // 0x01
|
||||
};
|
||||
|
||||
enum MCP23008GPIORegisters {
|
||||
// A side
|
||||
MCP23008_IODIR = 0x00,
|
||||
MCP23008_IPOL = 0x01,
|
||||
MCP23008_GPINTEN = 0x02,
|
||||
MCP23008_DEFVAL = 0x03,
|
||||
MCP23008_INTCON = 0x04,
|
||||
MCP23008_IOCON = 0x05,
|
||||
MCP23008_GPPU = 0x06,
|
||||
MCP23008_INTF = 0x07,
|
||||
MCP23008_INTCAP = 0x08,
|
||||
MCP23008_GPIO = 0x09,
|
||||
MCP23008_OLAT = 0x0A,
|
||||
};
|
||||
|
||||
class MCP23008 : public Component, public i2c::I2CDevice {
|
||||
class MCP23008 : public mcp23x08_base::MCP23X08Base, public i2c::I2CDevice {
|
||||
public:
|
||||
MCP23008() = default;
|
||||
|
||||
void setup() override;
|
||||
|
||||
bool digital_read(uint8_t pin);
|
||||
void digital_write(uint8_t pin, bool value);
|
||||
void pin_mode(uint8_t pin, uint8_t mode);
|
||||
|
||||
void set_open_drain_ints(const bool value) { open_drain_ints_ = value; }
|
||||
|
||||
float get_setup_priority() const override;
|
||||
void dump_config() override;
|
||||
|
||||
protected:
|
||||
// read a given register
|
||||
bool read_reg_(uint8_t reg, uint8_t *value);
|
||||
// write a value to a given register
|
||||
bool write_reg_(uint8_t reg, uint8_t value);
|
||||
// update registers with given pin value.
|
||||
void update_reg_(uint8_t pin, bool pin_value, uint8_t reg_a);
|
||||
|
||||
uint8_t olat_{0x00};
|
||||
bool open_drain_ints_;
|
||||
};
|
||||
|
||||
class MCP23008GPIOPin : public GPIOPin {
|
||||
public:
|
||||
MCP23008GPIOPin(MCP23008 *parent, uint8_t pin, uint8_t mode, bool inverted = false);
|
||||
|
||||
void setup() override;
|
||||
void pin_mode(uint8_t mode) override;
|
||||
bool digital_read() override;
|
||||
void digital_write(bool value) override;
|
||||
|
||||
protected:
|
||||
MCP23008 *parent_;
|
||||
bool read_reg(uint8_t reg, uint8_t *value) override;
|
||||
bool write_reg(uint8_t reg, uint8_t value) override;
|
||||
};
|
||||
|
||||
} // namespace mcp23008
|
||||
|
||||
Reference in New Issue
Block a user