diff --git a/esphome/components/gpio_expander/cached_gpio.h b/esphome/components/gpio_expander/cached_gpio.h index 782efe0f2d..eeff98cb6e 100644 --- a/esphome/components/gpio_expander/cached_gpio.h +++ b/esphome/components/gpio_expander/cached_gpio.h @@ -5,7 +5,6 @@ #include #include #include -#include "esphome/core/application.h" #include "esphome/core/hal.h" namespace esphome::gpio_expander { @@ -33,15 +32,6 @@ class CachedGpioExpander { /// @param pin Pin number to read /// @return Pin state bool digital_read(P pin) { - // Invalidate cache once per loop iteration so we always get a fresh - // hardware read on the first access each loop, while still caching - // within the same iteration for other pins in the same bank. - const uint32_t now = App.get_loop_component_start_time(); - if (now != this->last_loop_time_) { - this->last_loop_time_ = now; - this->reset_pin_cache_(); - } - const P bank = pin / BANK_SIZE; const T pin_mask = (1 << (pin % BANK_SIZE)); // Check if specific pin cache is valid @@ -78,7 +68,7 @@ class CachedGpioExpander { /// @param value Pin state to write (true = HIGH, false = LOW) virtual void digital_write_hw(P pin, bool value) = 0; - /// @brief Invalidate all cached pin states, forcing the next digital_read() to read from hardware. + /// @brief Invalidate cache. This function should be called in component loop(). void reset_pin_cache_() { memset(this->read_cache_valid_, 0x00, CACHE_SIZE_BYTES); } static constexpr uint16_t BITS_PER_BYTE = 8; @@ -86,7 +76,6 @@ class CachedGpioExpander { static constexpr size_t BANKS = N / BANK_SIZE; static constexpr size_t CACHE_SIZE_BYTES = BANKS * sizeof(T); - uint32_t last_loop_time_{0}; T read_cache_valid_[BANKS]{0}; }; diff --git a/esphome/components/mcp23016/mcp23016.cpp b/esphome/components/mcp23016/mcp23016.cpp index 1a7507f7ab..fbdb6903b8 100644 --- a/esphome/components/mcp23016/mcp23016.cpp +++ b/esphome/components/mcp23016/mcp23016.cpp @@ -26,6 +26,10 @@ void MCP23016::setup() { this->write_reg_(MCP23016_IODIR1, 0xFFFF); } +void MCP23016::loop() { + // Invalidate cache at the start of each loop + this->reset_pin_cache_(); +} bool MCP23016::digital_read_hw(uint8_t pin) { return this->read_reg_(MCP23016_GP1, &this->input_mask_); } bool MCP23016::digital_read_cache(uint8_t pin) { return this->input_mask_ & (1 << pin); } diff --git a/esphome/components/mcp23016/mcp23016.h b/esphome/components/mcp23016/mcp23016.h index 4ab9a81597..494bc9c197 100644 --- a/esphome/components/mcp23016/mcp23016.h +++ b/esphome/components/mcp23016/mcp23016.h @@ -30,6 +30,7 @@ class MCP23016 : public Component, public i2c::I2CDevice, public gpio_expander:: MCP23016() = default; void setup() override; + void loop() override; void pin_mode(uint8_t pin, gpio::Flags flags); float get_setup_priority() const override; diff --git a/esphome/components/mcp23xxx_base/mcp23xxx_base.h b/esphome/components/mcp23xxx_base/mcp23xxx_base.h index 955f581744..fb992466d5 100644 --- a/esphome/components/mcp23xxx_base/mcp23xxx_base.h +++ b/esphome/components/mcp23xxx_base/mcp23xxx_base.h @@ -17,6 +17,8 @@ template class MCP23XXXBase : public Component, public gpio_expander: void set_open_drain_ints(const bool value) { this->open_drain_ints_ = value; } float get_setup_priority() const override { return setup_priority::IO; } + void loop() override { this->reset_pin_cache_(); } + protected: // read a given register virtual bool read_reg(uint8_t reg, uint8_t *value) = 0; diff --git a/esphome/components/pca6416a/pca6416a.cpp b/esphome/components/pca6416a/pca6416a.cpp index 66d4e1782d..f393af88ce 100644 --- a/esphome/components/pca6416a/pca6416a.cpp +++ b/esphome/components/pca6416a/pca6416a.cpp @@ -51,6 +51,11 @@ void PCA6416AComponent::setup() { this->status_has_error()); } +void PCA6416AComponent::loop() { + // Invalidate cache at the start of each loop + this->reset_pin_cache_(); +} + void PCA6416AComponent::dump_config() { if (this->has_pullup_) { ESP_LOGCONFIG(TAG, "PCAL6416A:"); diff --git a/esphome/components/pca6416a/pca6416a.h b/esphome/components/pca6416a/pca6416a.h index a60d65271d..138a51cc20 100644 --- a/esphome/components/pca6416a/pca6416a.h +++ b/esphome/components/pca6416a/pca6416a.h @@ -16,6 +16,7 @@ class PCA6416AComponent : public Component, /// Check i2c availability and setup masks void setup() override; + void loop() override; /// Helper function to set the pin mode of a pin. void pin_mode(uint8_t pin, gpio::Flags flags); diff --git a/esphome/components/pca9554/pca9554.cpp b/esphome/components/pca9554/pca9554.cpp index ceb264472b..adc7bc0fb5 100644 --- a/esphome/components/pca9554/pca9554.cpp +++ b/esphome/components/pca9554/pca9554.cpp @@ -36,6 +36,12 @@ void PCA9554Component::setup() { this->status_has_error()); } +void PCA9554Component::loop() { + // Invalidate the cache at the start of each loop. + // The actual read will happen on demand when digital_read() is called + this->reset_pin_cache_(); +} + void PCA9554Component::dump_config() { ESP_LOGCONFIG(TAG, "PCA9554:\n" diff --git a/esphome/components/pca9554/pca9554.h b/esphome/components/pca9554/pca9554.h index 799cbfede7..1d877f9ce2 100644 --- a/esphome/components/pca9554/pca9554.h +++ b/esphome/components/pca9554/pca9554.h @@ -16,6 +16,8 @@ class PCA9554Component : public Component, /// Check i2c availability and setup masks void setup() override; + /// Invalidate cache at start of each loop + void loop() override; /// Helper function to set the pin mode of a pin. void pin_mode(uint8_t pin, gpio::Flags flags); diff --git a/esphome/components/pcf8574/pcf8574.cpp b/esphome/components/pcf8574/pcf8574.cpp index 55afeba4f6..d3ec31436d 100644 --- a/esphome/components/pcf8574/pcf8574.cpp +++ b/esphome/components/pcf8574/pcf8574.cpp @@ -16,6 +16,10 @@ void PCF8574Component::setup() { this->write_gpio_(); this->read_gpio_(); } +void PCF8574Component::loop() { + // Invalidate the cache at the start of each loop + this->reset_pin_cache_(); +} void PCF8574Component::dump_config() { ESP_LOGCONFIG(TAG, "PCF8574:\n" diff --git a/esphome/components/pcf8574/pcf8574.h b/esphome/components/pcf8574/pcf8574.h index 861fc1014c..b039173789 100644 --- a/esphome/components/pcf8574/pcf8574.h +++ b/esphome/components/pcf8574/pcf8574.h @@ -20,6 +20,8 @@ class PCF8574Component : public Component, /// Check i2c availability and setup masks void setup() override; + /// Invalidate cache at start of each loop + void loop() override; /// Helper function to set the pin mode of a pin. void pin_mode(uint8_t pin, gpio::Flags flags); diff --git a/esphome/components/pi4ioe5v6408/pi4ioe5v6408.cpp b/esphome/components/pi4ioe5v6408/pi4ioe5v6408.cpp index 11053df6e2..9247e114f0 100644 --- a/esphome/components/pi4ioe5v6408/pi4ioe5v6408.cpp +++ b/esphome/components/pi4ioe5v6408/pi4ioe5v6408.cpp @@ -60,6 +60,8 @@ void PI4IOE5V6408Component::pin_mode(uint8_t pin, gpio::Flags flags) { this->write_gpio_modes_(); } +void PI4IOE5V6408Component::loop() { this->reset_pin_cache_(); } + bool PI4IOE5V6408Component::read_gpio_outputs_() { if (this->is_failed()) return false; diff --git a/esphome/components/pi4ioe5v6408/pi4ioe5v6408.h b/esphome/components/pi4ioe5v6408/pi4ioe5v6408.h index b069167423..4dc31201ce 100644 --- a/esphome/components/pi4ioe5v6408/pi4ioe5v6408.h +++ b/esphome/components/pi4ioe5v6408/pi4ioe5v6408.h @@ -18,6 +18,7 @@ class PI4IOE5V6408Component : public Component, float get_setup_priority() const override; void dump_config() override; + void loop() override; /// Indicate if the component should reset the state during setup void set_reset(bool reset) { this->reset_ = reset; } diff --git a/esphome/components/sx1509/sx1509.cpp b/esphome/components/sx1509/sx1509.cpp index e7238f0fa1..1cdae76eaf 100644 --- a/esphome/components/sx1509/sx1509.cpp +++ b/esphome/components/sx1509/sx1509.cpp @@ -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; diff --git a/esphome/components/tca9555/tca9555.cpp b/esphome/components/tca9555/tca9555.cpp index fba92d92bb..79c5253898 100644 --- a/esphome/components/tca9555/tca9555.cpp +++ b/esphome/components/tca9555/tca9555.cpp @@ -43,6 +43,8 @@ void TCA9555Component::pin_mode(uint8_t pin, gpio::Flags flags) { // Write GPIO to enable input mode this->write_gpio_modes_(); } +void TCA9555Component::loop() { this->reset_pin_cache_(); } + bool TCA9555Component::read_gpio_outputs_() { if (this->is_failed()) return false; diff --git a/esphome/components/tca9555/tca9555.h b/esphome/components/tca9555/tca9555.h index 47a1e3702c..9f7273b1e7 100644 --- a/esphome/components/tca9555/tca9555.h +++ b/esphome/components/tca9555/tca9555.h @@ -22,6 +22,8 @@ class TCA9555Component : public Component, void dump_config() override; + void loop() override; + protected: bool digital_read_hw(uint8_t pin) override; bool digital_read_cache(uint8_t pin) override;