1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-08 22:32:21 +01:00

[sx1509] Migrate to CachedGpioExpander to reduce I2C bus usage

This commit is contained in:
J. Nick Koston
2025-09-04 14:35:34 -05:00
parent e843f1759b
commit d9ded6b87e
3 changed files with 26 additions and 11 deletions

View File

@@ -25,7 +25,7 @@ CONF_SCAN_TIME = "scan_time"
CONF_DEBOUNCE_TIME = "debounce_time"
CONF_SX1509_ID = "sx1509_id"
AUTO_LOAD = ["key_provider"]
AUTO_LOAD = ["key_provider", "gpio_expander"]
DEPENDENCIES = ["i2c"]
MULTI_CONF = True

View File

@@ -39,6 +39,9 @@ void SX1509Component::dump_config() {
}
void SX1509Component::loop() {
// Reset cache at the start of each loop
this->reset_pin_cache_();
if (this->has_keypad_) {
if (millis() - this->last_loop_timestamp_ < min_loop_period_)
return;
@@ -73,18 +76,23 @@ void SX1509Component::loop() {
}
}
bool SX1509Component::digital_read(uint8_t pin) {
bool SX1509Component::digital_read_hw(uint8_t pin) {
if (this->ddr_mask_ & (1 << pin)) {
uint16_t temp_reg_data;
if (!this->read_byte_16(REG_DATA_B, &temp_reg_data))
if (!this->read_byte_16(REG_DATA_B, &this->input_mask_))
return false;
if (temp_reg_data & (1 << pin))
return true;
return true;
}
return false;
}
void SX1509Component::digital_write(uint8_t pin, bool bit_value) {
bool SX1509Component::digital_read_cache(uint8_t pin) {
if (this->ddr_mask_ & (1 << pin)) {
return this->input_mask_ & (1 << pin);
}
return false;
}
void SX1509Component::digital_write_hw(uint8_t pin, bool bit_value) {
if ((~this->ddr_mask_) & (1 << pin)) {
// If the pin is an output, write high/low
uint16_t temp_reg_data = 0;

View File

@@ -2,6 +2,7 @@
#include "esphome/components/i2c/i2c.h"
#include "esphome/components/key_provider/key_provider.h"
#include "esphome/components/gpio_expander/cached_gpio.h"
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
#include "sx1509_gpio_pin.h"
@@ -30,7 +31,10 @@ class SX1509Processor {
class SX1509KeyTrigger : public Trigger<uint8_t> {};
class SX1509Component : public Component, public i2c::I2CDevice, public key_provider::KeyProvider {
class SX1509Component : public Component,
public i2c::I2CDevice,
public gpio_expander::CachedGpioExpander<uint16_t, 16>,
public key_provider::KeyProvider {
public:
SX1509Component() = default;
@@ -39,11 +43,9 @@ class SX1509Component : public Component, public i2c::I2CDevice, public key_prov
float get_setup_priority() const override { return setup_priority::HARDWARE; }
void loop() override;
bool digital_read(uint8_t pin);
uint16_t read_key_data();
void set_pin_value(uint8_t pin, uint8_t i_on) { this->write_byte(REG_I_ON[pin], i_on); };
void pin_mode(uint8_t pin, gpio::Flags flags);
void digital_write(uint8_t pin, bool bit_value);
uint32_t get_clock() { return this->clk_x_; };
void set_rows_cols(uint8_t rows, uint8_t cols) {
this->rows_ = rows;
@@ -61,10 +63,15 @@ class SX1509Component : public Component, public i2c::I2CDevice, public key_prov
void setup_led_driver(uint8_t pin);
protected:
// Virtual methods from CachedGpioExpander
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;
uint32_t clk_x_ = 2000000;
uint8_t frequency_ = 0;
uint16_t ddr_mask_ = 0x00;
uint16_t input_mask_ = 0x00;
uint16_t input_mask_ = 0x00; // Cache for input values (16-bit for all pins)
uint16_t port_mask_ = 0x00;
uint16_t output_state_ = 0x00;
bool has_keypad_ = false;