mirror of
https://github.com/esphome/esphome.git
synced 2026-06-24 13:27:14 +00:00
[pca6416a] Add interrupt pin support (#15614)
This commit is contained in:
@@ -5,6 +5,7 @@ import esphome.config_validation as cv
|
|||||||
from esphome.const import (
|
from esphome.const import (
|
||||||
CONF_ID,
|
CONF_ID,
|
||||||
CONF_INPUT,
|
CONF_INPUT,
|
||||||
|
CONF_INTERRUPT_PIN,
|
||||||
CONF_INVERTED,
|
CONF_INVERTED,
|
||||||
CONF_MODE,
|
CONF_MODE,
|
||||||
CONF_NUMBER,
|
CONF_NUMBER,
|
||||||
@@ -25,7 +26,12 @@ PCA6416AGPIOPin = pca6416a_ns.class_(
|
|||||||
|
|
||||||
CONF_PCA6416A = "pca6416a"
|
CONF_PCA6416A = "pca6416a"
|
||||||
CONFIG_SCHEMA = (
|
CONFIG_SCHEMA = (
|
||||||
cv.Schema({cv.Required(CONF_ID): cv.declare_id(PCA6416AComponent)})
|
cv.Schema(
|
||||||
|
{
|
||||||
|
cv.Required(CONF_ID): cv.declare_id(PCA6416AComponent),
|
||||||
|
cv.Optional(CONF_INTERRUPT_PIN): pins.internal_gpio_input_pin_schema,
|
||||||
|
}
|
||||||
|
)
|
||||||
.extend(cv.COMPONENT_SCHEMA)
|
.extend(cv.COMPONENT_SCHEMA)
|
||||||
.extend(i2c.i2c_device_schema(0x21))
|
.extend(i2c.i2c_device_schema(0x21))
|
||||||
)
|
)
|
||||||
@@ -35,6 +41,8 @@ async def to_code(config):
|
|||||||
var = cg.new_Pvariable(config[CONF_ID])
|
var = cg.new_Pvariable(config[CONF_ID])
|
||||||
await cg.register_component(var, config)
|
await cg.register_component(var, config)
|
||||||
await i2c.register_i2c_device(var, config)
|
await i2c.register_i2c_device(var, config)
|
||||||
|
if interrupt_pin := config.get(CONF_INTERRUPT_PIN):
|
||||||
|
cg.add(var.set_interrupt_pin(await cg.gpio_pin_expression(interrupt_pin)))
|
||||||
|
|
||||||
|
|
||||||
def validate_mode(value):
|
def validate_mode(value):
|
||||||
|
|||||||
@@ -49,11 +49,22 @@ void PCA6416AComponent::setup() {
|
|||||||
|
|
||||||
ESP_LOGD(TAG, "Initialization complete. Warning: %d, Error: %d", this->status_has_warning(),
|
ESP_LOGD(TAG, "Initialization complete. Warning: %d, Error: %d", this->status_has_warning(),
|
||||||
this->status_has_error());
|
this->status_has_error());
|
||||||
|
|
||||||
|
if (this->interrupt_pin_ != nullptr) {
|
||||||
|
this->interrupt_pin_->setup();
|
||||||
|
this->interrupt_pin_->attach_interrupt(&PCA6416AComponent::gpio_intr, this, gpio::INTERRUPT_FALLING_EDGE);
|
||||||
|
this->set_invalidate_on_read_(false);
|
||||||
|
}
|
||||||
|
this->disable_loop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void IRAM_ATTR PCA6416AComponent::gpio_intr(PCA6416AComponent *arg) { arg->enable_loop_soon_any_context(); }
|
||||||
void PCA6416AComponent::loop() {
|
void PCA6416AComponent::loop() {
|
||||||
// Invalidate cache at the start of each loop
|
// Invalidate cache at the start of each loop
|
||||||
this->reset_pin_cache_();
|
this->reset_pin_cache_();
|
||||||
|
if (this->interrupt_pin_ != nullptr) {
|
||||||
|
this->disable_loop();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PCA6416AComponent::dump_config() {
|
void PCA6416AComponent::dump_config() {
|
||||||
@@ -62,6 +73,7 @@ void PCA6416AComponent::dump_config() {
|
|||||||
} else {
|
} else {
|
||||||
ESP_LOGCONFIG(TAG, "PCA6416A:");
|
ESP_LOGCONFIG(TAG, "PCA6416A:");
|
||||||
}
|
}
|
||||||
|
LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
|
||||||
LOG_I2C_DEVICE(this)
|
LOG_I2C_DEVICE(this)
|
||||||
if (this->is_failed()) {
|
if (this->is_failed()) {
|
||||||
ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
|
ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
|
||||||
@@ -101,6 +113,9 @@ void PCA6416AComponent::pin_mode(uint8_t pin, gpio::Flags flags) {
|
|||||||
this->update_register_(pin, true, pull_dir);
|
this->update_register_(pin, true, pull_dir);
|
||||||
this->update_register_(pin, false, pull_en);
|
this->update_register_(pin, false, pull_en);
|
||||||
}
|
}
|
||||||
|
if (this->interrupt_pin_ == nullptr) {
|
||||||
|
this->enable_loop();
|
||||||
|
}
|
||||||
} else if (flags == (gpio::FLAG_INPUT | gpio::FLAG_PULLUP)) {
|
} else if (flags == (gpio::FLAG_INPUT | gpio::FLAG_PULLUP)) {
|
||||||
this->update_register_(pin, true, io_dir);
|
this->update_register_(pin, true, io_dir);
|
||||||
if (has_pullup_) {
|
if (has_pullup_) {
|
||||||
@@ -109,6 +124,9 @@ void PCA6416AComponent::pin_mode(uint8_t pin, gpio::Flags flags) {
|
|||||||
} else {
|
} else {
|
||||||
ESP_LOGW(TAG, "Your PCA6416A does not support pull-up resistors");
|
ESP_LOGW(TAG, "Your PCA6416A does not support pull-up resistors");
|
||||||
}
|
}
|
||||||
|
if (this->interrupt_pin_ == nullptr) {
|
||||||
|
this->enable_loop();
|
||||||
|
}
|
||||||
} else if (flags == gpio::FLAG_OUTPUT) {
|
} else if (flags == gpio::FLAG_OUTPUT) {
|
||||||
this->update_register_(pin, false, io_dir);
|
this->update_register_(pin, false, io_dir);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,7 +24,10 @@ class PCA6416AComponent : public Component,
|
|||||||
|
|
||||||
void dump_config() override;
|
void dump_config() override;
|
||||||
|
|
||||||
|
void set_interrupt_pin(InternalGPIOPin *pin) { this->interrupt_pin_ = pin; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
static void IRAM_ATTR gpio_intr(PCA6416AComponent *arg);
|
||||||
// Virtual methods from CachedGpioExpander
|
// Virtual methods from CachedGpioExpander
|
||||||
bool digital_read_hw(uint8_t pin) override;
|
bool digital_read_hw(uint8_t pin) override;
|
||||||
bool digital_read_cache(uint8_t pin) override;
|
bool digital_read_cache(uint8_t pin) override;
|
||||||
@@ -43,6 +46,7 @@ class PCA6416AComponent : public Component,
|
|||||||
esphome::i2c::ErrorCode last_error_;
|
esphome::i2c::ErrorCode last_error_;
|
||||||
/// Only the PCAL6416A has pull-up resistors
|
/// Only the PCAL6416A has pull-up resistors
|
||||||
bool has_pullup_{false};
|
bool has_pullup_{false};
|
||||||
|
InternalGPIOPin *interrupt_pin_{nullptr};
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Helper class to expose a PCA6416A pin as an internal input GPIO pin.
|
/// Helper class to expose a PCA6416A pin as an internal input GPIO pin.
|
||||||
|
|||||||
@@ -2,6 +2,10 @@ pca6416a:
|
|||||||
- id: pca6416a_hub
|
- id: pca6416a_hub
|
||||||
i2c_id: i2c_bus
|
i2c_id: i2c_bus
|
||||||
address: 0x21
|
address: 0x21
|
||||||
|
- id: pca6416a_hub_int
|
||||||
|
i2c_id: i2c_bus
|
||||||
|
address: 0x22
|
||||||
|
interrupt_pin: ${interrupt_pin}
|
||||||
|
|
||||||
binary_sensor:
|
binary_sensor:
|
||||||
- platform: gpio
|
- platform: gpio
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
substitutions:
|
||||||
|
interrupt_pin: GPIO15
|
||||||
|
|
||||||
packages:
|
packages:
|
||||||
i2c: !include ../../test_build_components/common/i2c/esp32-idf.yaml
|
i2c: !include ../../test_build_components/common/i2c/esp32-idf.yaml
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
substitutions:
|
||||||
|
interrupt_pin: GPIO15
|
||||||
|
|
||||||
packages:
|
packages:
|
||||||
i2c: !include ../../test_build_components/common/i2c/esp8266-ard.yaml
|
i2c: !include ../../test_build_components/common/i2c/esp8266-ard.yaml
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
substitutions:
|
||||||
|
interrupt_pin: GPIO2
|
||||||
|
|
||||||
packages:
|
packages:
|
||||||
i2c: !include ../../test_build_components/common/i2c/rp2040-ard.yaml
|
i2c: !include ../../test_build_components/common/i2c/rp2040-ard.yaml
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user