diff --git a/esphome/components/gpio_expander/cached_gpio.h b/esphome/components/gpio_expander/cached_gpio.h index d7230eb0b3..9ab3f2f445 100644 --- a/esphome/components/gpio_expander/cached_gpio.h +++ b/esphome/components/gpio_expander/cached_gpio.h @@ -16,12 +16,12 @@ namespace esphome::gpio_expander { /// T - Type which represents internal register. Could be uint8_t or uint16_t. Adjust to /// match size of your internal GPIO bank register. /// N - Number of pins -template class CachedGpioExpander { +template class CachedGpioExpander { public: /// @brief Read the state of the given pin. This will invalidate the cache for the given pin number. /// @param pin Pin number to read /// @return Pin state - bool digital_read(T pin) { + bool digital_read(uint8_t pin) { const uint8_t bank = pin / BANK_SIZE; const T pin_mask = (1 << (pin % BANK_SIZE)); // Check if specific pin cache is valid @@ -38,15 +38,15 @@ template class CachedGpioExpander { return this->digital_read_cache(pin); } - void digital_write(T pin, bool value) { this->digital_write_hw(pin, value); } + void digital_write(uint8_t pin, bool value) { this->digital_write_hw(pin, value); } protected: /// @brief Call component low level function to read GPIO state from device - virtual bool digital_read_hw(T pin) = 0; + virtual bool digital_read_hw(uint8_t pin) = 0; /// @brief Call component read function from internal cache. - virtual bool digital_read_cache(T pin) = 0; + virtual bool digital_read_cache(uint8_t pin) = 0; /// @brief Call component low level function to write GPIO state to device - virtual void digital_write_hw(T pin, bool value) = 0; + virtual void digital_write_hw(uint8_t pin, bool value) = 0; /// @brief Invalidate cache. This function should be called in component loop(). void reset_pin_cache_() { memset(this->read_cache_valid_, 0x00, CACHE_SIZE_BYTES); } diff --git a/esphome/components/pcf8574/pcf8574.cpp b/esphome/components/pcf8574/pcf8574.cpp index 67e84d6629..20dd0f97b1 100644 --- a/esphome/components/pcf8574/pcf8574.cpp +++ b/esphome/components/pcf8574/pcf8574.cpp @@ -28,14 +28,21 @@ void PCF8574Component::dump_config() { ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL); } } -bool PCF8574Component::digital_read(uint8_t pin) { return this->get_pin_value_(pin); } +bool PCF8574Component::digital_read(uint8_t pin) { + // Call the base class method + return this->CachedGpioExpander::digital_read(pin); +} bool PCF8574Component::digital_read_hw(uint8_t pin) { return this->read_gpio_() ? (this->input_mask_ & (1 << pin)) : false; } bool PCF8574Component::digital_read_cache(uint8_t pin) { return this->input_mask_ & (1 << pin); } -void PCF8574Component::digital_write(uint8_t pin, bool value) { this->set_pin_value_(pin, value); } + +void PCF8574Component::digital_write(uint8_t pin, bool value) { + // Call the base class method + this->CachedGpioExpander::digital_write(pin, value); +} void PCF8574Component::digital_write_hw(uint8_t pin, bool value) { if (value) { diff --git a/esphome/components/pcf8574/pcf8574.h b/esphome/components/pcf8574/pcf8574.h index cb848060aa..524766d18c 100644 --- a/esphome/components/pcf8574/pcf8574.h +++ b/esphome/components/pcf8574/pcf8574.h @@ -22,6 +22,8 @@ class PCF8574Component : public Component, void setup() override; /// Invalidate cache at start of each loop void loop() override; + /// Helper function to read the value of a pin. + bool digital_read(uint8_t pin); /// Helper function to write the value of a pin. void digital_write(uint8_t pin, bool value); /// Helper function to set the pin mode of a pin.