mirror of
https://github.com/randybb/esphome-configs.git
synced 2026-01-02 19:47:29 +01:00
have fun
This commit is contained in:
28
components/pca9536d/README.md
Normal file
28
components/pca9536d/README.md
Normal file
@@ -0,0 +1,28 @@
|
||||
# PCA9536D i2c I/O expander
|
||||
|
||||
Requires a configured i2c bus
|
||||
|
||||
Example:
|
||||
```yaml
|
||||
pca9536d:
|
||||
- id: my_pca
|
||||
|
||||
switch:
|
||||
- platform: gpio
|
||||
pin:
|
||||
pca9536d: my_pca
|
||||
number: 0
|
||||
id: relay
|
||||
|
||||
binary_sensor:
|
||||
- platform: gpio
|
||||
pin:
|
||||
pca9536d: my_pca
|
||||
number: 1
|
||||
id: button
|
||||
```
|
||||
|
||||
# Optional parameters
|
||||
|
||||
`address:` defaults to 0x41
|
||||
|
||||
69
components/pca9536d/__init__.py
Normal file
69
components/pca9536d/__init__.py
Normal file
@@ -0,0 +1,69 @@
|
||||
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_INPUT,
|
||||
CONF_INVERTED,
|
||||
CONF_NUMBER,
|
||||
CONF_MODE,
|
||||
CONF_OUTPUT,
|
||||
)
|
||||
|
||||
DEPENDENCIES = ["i2c"]
|
||||
MULTI_CONF = True
|
||||
|
||||
CONF_PCA9536D = "pca9536d"
|
||||
|
||||
pca9536d_ns = cg.esphome_ns.namespace("pca9536d")
|
||||
PCA9536DGPIOMode = pca9536d_ns.enum("PCA9536DGPIOMode")
|
||||
|
||||
PCA9536D = pca9536d_ns.class_("PCA9536D", cg.Component, i2c.I2CDevice)
|
||||
PCA9536DGPIOPin = pca9536d_ns.class_("PCA9536DGPIOPin", cg.GPIOPin)
|
||||
|
||||
CONFIG_SCHEMA = cv.COMPONENT_SCHEMA.extend(
|
||||
{
|
||||
cv.Required(CONF_ID): cv.declare_id(PCA9536D),
|
||||
}
|
||||
).extend(i2c.i2c_device_schema(0x41))
|
||||
|
||||
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
|
||||
|
||||
PCA9536D_PIN_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(PCA9536DGPIOPin),
|
||||
cv.Required(CONF_PCA9536D): cv.use_id(PCA9536D),
|
||||
cv.Required(CONF_NUMBER): cv.int_range(min=0, max=7),
|
||||
cv.Optional(CONF_MODE, default={}): cv.All(
|
||||
{
|
||||
cv.Optional(CONF_INPUT, default=False): cv.boolean,
|
||||
cv.Optional(CONF_OUTPUT, default=False): cv.boolean,
|
||||
},
|
||||
validate_mode,
|
||||
),
|
||||
cv.Optional(CONF_INVERTED, default=False): cv.boolean,
|
||||
}
|
||||
)
|
||||
|
||||
@pins.PIN_SCHEMA_REGISTRY.register(CONF_PCA9536D, PCA9536D_PIN_SCHEMA)
|
||||
async def pca9536d_pin_to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
parent = await cg.get_variable(config[CONF_PCA9536D])
|
||||
|
||||
cg.add(var.set_parent(parent))
|
||||
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
|
||||
|
||||
BIN
components/pca9536d/__pycache__/__init__.cpython-39.pyc
Normal file
BIN
components/pca9536d/__pycache__/__init__.cpython-39.pyc
Normal file
Binary file not shown.
108
components/pca9536d/pca9536d.cpp
Normal file
108
components/pca9536d/pca9536d.cpp
Normal file
@@ -0,0 +1,108 @@
|
||||
#include "pca9536d.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace pca9536d {
|
||||
|
||||
static const char *TAG = "pca9536d";
|
||||
|
||||
void PCA9536D::setup() {
|
||||
ESP_LOGCONFIG(TAG, "Setting up PCA9536D...");
|
||||
if (!this->read_gpio_()) {
|
||||
ESP_LOGE(TAG, "PCA9536D not available under 0x%02X", this->address_);
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
|
||||
this->write_gpio_();
|
||||
}
|
||||
|
||||
void PCA9536D::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "PCA9536D:");
|
||||
LOG_I2C_DEVICE(this)
|
||||
if (this->is_failed())
|
||||
ESP_LOGE(TAG, "Communication with PCA9536D failed!");
|
||||
}
|
||||
|
||||
bool PCA9536D::digital_read(uint8_t pin) {
|
||||
this->read_gpio_();
|
||||
return this->inputs_ & (1 << pin);
|
||||
}
|
||||
|
||||
void PCA9536D::digital_write(uint8_t pin, bool value) {
|
||||
if (value)
|
||||
this->outputs_ |= (1 << pin);
|
||||
else
|
||||
this->outputs_ &= ~(1 << pin);
|
||||
this->write_gpio_();
|
||||
}
|
||||
|
||||
void PCA9536D::set_pin_mode(uint8_t pin, uint8_t mode) {
|
||||
switch (mode) {
|
||||
case gpio::FLAG_INPUT:
|
||||
this->modes_ |= 1 << pin;
|
||||
break;
|
||||
case gpio::FLAG_OUTPUT:
|
||||
this->modes_ &= ~(1 << pin);
|
||||
break;
|
||||
}
|
||||
this->set_modes_();
|
||||
}
|
||||
|
||||
bool PCA9536D::read_gpio_() {
|
||||
if (this->is_failed())
|
||||
return false;
|
||||
|
||||
uint8_t data;
|
||||
if (!this->read_bytes(0, &data, 1)) {
|
||||
this->status_set_warning();
|
||||
return false;
|
||||
}
|
||||
|
||||
this->inputs_ = data;
|
||||
this->status_clear_warning();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PCA9536D::write_gpio_() {
|
||||
if (this->is_failed())
|
||||
return false;
|
||||
|
||||
uint8_t data = this->outputs_;
|
||||
if (!this->write_bytes(1, &data, 1)) {
|
||||
this->status_set_warning();
|
||||
return false;
|
||||
}
|
||||
|
||||
this->status_clear_warning();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PCA9536D::set_modes_() {
|
||||
if (this->is_failed())
|
||||
return false;
|
||||
|
||||
uint8_t data = this->modes_;
|
||||
if (!this->write_bytes(3, &data, 1)) {
|
||||
this->status_set_warning();
|
||||
return false;
|
||||
}
|
||||
|
||||
this->status_clear_warning();
|
||||
return true;
|
||||
}
|
||||
|
||||
void PCA9536DGPIOPin::setup() { this->pin_mode(this->flags_); }
|
||||
|
||||
bool PCA9536DGPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; }
|
||||
|
||||
void PCA9536DGPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); }
|
||||
|
||||
void PCA9536DGPIOPin::pin_mode(gpio::Flags flags) { this->parent_->set_pin_mode(this->pin_, flags); }
|
||||
|
||||
std::string PCA9536DGPIOPin::dump_summary() const {
|
||||
return str_sprintf("%u via PCA9536D", pin_);
|
||||
}
|
||||
|
||||
} // namespace pca9536d
|
||||
} // namespace esphome
|
||||
52
components/pca9536d/pca9536d.h
Normal file
52
components/pca9536d/pca9536d.h
Normal file
@@ -0,0 +1,52 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/components/i2c/i2c.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace pca9536d {
|
||||
|
||||
class PCA9536D: public Component, public i2c::I2CDevice {
|
||||
public:
|
||||
void setup() override;
|
||||
float get_setup_priority() const { return setup_priority::IO; }
|
||||
void dump_config() override;
|
||||
|
||||
bool digital_read(uint8_t pin);
|
||||
void digital_write(uint8_t pin, bool value);
|
||||
void set_pin_mode(uint8_t pin, uint8_t mode);
|
||||
|
||||
protected:
|
||||
bool read_gpio_();
|
||||
bool write_gpio_();
|
||||
bool set_modes_();
|
||||
|
||||
// pin modes - 0 means output, 1 means input
|
||||
uint8_t modes_{0xff};
|
||||
uint8_t outputs_{0x00};
|
||||
uint8_t inputs_{0x00};
|
||||
};
|
||||
|
||||
class PCA9536DGPIOPin : public GPIOPin {
|
||||
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_parent(PCA9536D *parent) { parent_ = parent; }
|
||||
void set_pin(uint8_t pin) { pin_ = pin; }
|
||||
void set_inverted(bool inverted) { inverted_ = inverted; }
|
||||
void set_flags(gpio::Flags flags) { flags_ = flags; }
|
||||
|
||||
protected:
|
||||
PCA9536D *parent_;
|
||||
uint8_t pin_;
|
||||
bool inverted_;
|
||||
gpio::Flags flags_;
|
||||
};
|
||||
|
||||
} // namespace pca9536d
|
||||
} // namespace esphome
|
||||
Reference in New Issue
Block a user