ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
esphome::gpio_expander::CachedGpioExpanderBase< T, N, P > Class Template Referenceabstract

A class to cache the read state of a GPIO expander. More...

#include <cached_gpio.h>

Inheritance diagram for esphome::gpio_expander::CachedGpioExpanderBase< T, N, P >:
esphome::gpio_expander::CachedGpioExpander< uint16_t, 16 > esphome::gpio_expander::CachedGpioExpander< uint8_t, 8 > esphome::gpio_expander::GpioExpanderInputs< CachedGpioExpanderBase< T, N > > esphome::gpio_expander::CachedGpioExpander< T, N, P > esphome::sx1509::SX1509Component esphome::gpio_expander::GpioExpanderInputs< gpio_expander::CachedGpioExpander< uint8_t, 8 > > esphome::gpio_expander::CachedGpioExpanderComponent< T, N > esphome::waveshare_io_ch32v003::WaveshareIOCH32V003Component

Public Member Functions

void digital_write (P pin, bool value)
 

Protected Types

using bank_t = T
 
using pin_t = P
 

Protected Member Functions

bool cached_read_ (P pin, bool invalidate_on_read)
 Read pin through the bank cache.
 
virtual bool digital_read_hw (P pin)=0
 Read GPIO bank from hardware into internal state.
 
virtual bool digital_read_cache (P pin)=0
 Get cached pin value from internal state.
 
virtual void digital_write_hw (P pin, bool value)=0
 Write GPIO state to hardware.
 
void reset_pin_cache_ ()
 Invalidate cache. This function should be called in component loop().
 

Protected Attributes

T read_cache_valid_ [BANKS] {0}
 

Static Protected Attributes

static constexpr uint16_t BITS_PER_BYTE = 8
 
static constexpr uint16_t BANK_SIZE = sizeof(T) * BITS_PER_BYTE
 
static constexpr size_t BANKS = N / BANK_SIZE
 
static constexpr size_t CACHE_SIZE_BYTES = BANKS * sizeof(T)
 

Detailed Description

template<typename T, uint16_t N, typename P = expander_pin_t<N>>
class esphome::gpio_expander::CachedGpioExpanderBase< T, N, P >

A class to cache the read state of a GPIO expander.

This class caches reads between GPIO Pins which are on the same bank. This means that for reading whole Port (ex. 8 pins) component needs only one I2C/SPI read per main loop call. It assumes that one bit in byte identifies one GPIO pin.

Template parameters: T - Type which represents internal bank register. Could be uint8_t or uint16_t. Choose based on how your I/O expander reads pins:

  • uint8_t: For chips that read banks separately (8 pins at a time) Examples: MCP23017 (2x8-bit banks), TCA9555 (2x8-bit banks)
  • uint16_t: For chips that read all pins at once (up to 16 pins) Examples: PCF8574/8575 (8/16 pins), PCA9554/9555 (8/16 pins) N - Total number of pins (maximum 65535) P - Type for pin number parameters (automatically selected based on N: uint8_t for N<=256, uint16_t for N>256). Can be explicitly specified if needed (e.g., for components like SN74HC165 with >256 pins)

CachedGpioExpanderBase holds the cache; CachedGpioExpander adds the invalidate-on-read flag, and CachedGpioExpanderComponent takes it from the interrupt pin instead.

Definition at line 34 of file cached_gpio.h.

Member Typedef Documentation

◆ bank_t

template<typename T , uint16_t N, typename P = expander_pin_t<N>>
using esphome::gpio_expander::CachedGpioExpanderBase< T, N, P >::bank_t = T
protected

Definition at line 39 of file cached_gpio.h.

◆ pin_t

template<typename T , uint16_t N, typename P = expander_pin_t<N>>
using esphome::gpio_expander::CachedGpioExpanderBase< T, N, P >::pin_t = P
protected

Definition at line 40 of file cached_gpio.h.

Member Function Documentation

◆ cached_read_()

template<typename T , uint16_t N, typename P = expander_pin_t<N>>
bool esphome::gpio_expander::CachedGpioExpanderBase< T, N, P >::cached_read_ ( P pin,
bool invalidate_on_read )
inlineprotected

Read pin through the bank cache.

With invalidate_on_read the pin is read from hardware again next time; otherwise the bank stays cached until reset_pin_cache_().

Definition at line 44 of file cached_gpio.h.

◆ digital_read_cache()

template<typename T , uint16_t N, typename P = expander_pin_t<N>>
virtual bool esphome::gpio_expander::CachedGpioExpanderBase< T, N, P >::digital_read_cache ( P pin)
protectedpure virtual

Get cached pin value from internal state.

Parameters
pinPin number to read
Returns
Pin state (true = HIGH, false = LOW)

Implemented in esphome::gpio_expander::GpioExpanderInputs< CachedGpioExpanderBase< T, N > >, and esphome::gpio_expander::GpioExpanderInputs< gpio_expander::CachedGpioExpander< uint8_t, 8 > >.

◆ digital_read_hw()

template<typename T , uint16_t N, typename P = expander_pin_t<N>>
virtual bool esphome::gpio_expander::CachedGpioExpanderBase< T, N, P >::digital_read_hw ( P pin)
protectedpure virtual

Read GPIO bank from hardware into internal state.

Parameters
pinPin number (used to determine which bank to read)
Returns
true if read succeeded, false on communication error
Note
This does NOT return the pin state. It returns whether the read operation succeeded. The actual pin state should be returned by digital_read_cache().

◆ digital_write()

template<typename T , uint16_t N, typename P = expander_pin_t<N>>
void esphome::gpio_expander::CachedGpioExpanderBase< T, N, P >::digital_write ( P pin,
bool value )
inline

Definition at line 36 of file cached_gpio.h.

◆ digital_write_hw()

template<typename T , uint16_t N, typename P = expander_pin_t<N>>
virtual void esphome::gpio_expander::CachedGpioExpanderBase< T, N, P >::digital_write_hw ( P pin,
bool value )
protectedpure virtual

Write GPIO state to hardware.

Parameters
pinPin number to write
valuePin state to write (true = HIGH, false = LOW)

◆ reset_pin_cache_()

template<typename T , uint16_t N, typename P = expander_pin_t<N>>
void esphome::gpio_expander::CachedGpioExpanderBase< T, N, P >::reset_pin_cache_ ( )
inlineprotected

Invalidate cache. This function should be called in component loop().

Definition at line 82 of file cached_gpio.h.

Field Documentation

◆ BANK_SIZE

template<typename T , uint16_t N, typename P = expander_pin_t<N>>
uint16_t esphome::gpio_expander::CachedGpioExpanderBase< T, N, P >::BANK_SIZE = sizeof(T) * BITS_PER_BYTE
staticconstexprprotected

Definition at line 85 of file cached_gpio.h.

◆ BANKS

template<typename T , uint16_t N, typename P = expander_pin_t<N>>
size_t esphome::gpio_expander::CachedGpioExpanderBase< T, N, P >::BANKS = N / BANK_SIZE
staticconstexprprotected

Definition at line 86 of file cached_gpio.h.

◆ BITS_PER_BYTE

template<typename T , uint16_t N, typename P = expander_pin_t<N>>
uint16_t esphome::gpio_expander::CachedGpioExpanderBase< T, N, P >::BITS_PER_BYTE = 8
staticconstexprprotected

Definition at line 84 of file cached_gpio.h.

◆ CACHE_SIZE_BYTES

template<typename T , uint16_t N, typename P = expander_pin_t<N>>
size_t esphome::gpio_expander::CachedGpioExpanderBase< T, N, P >::CACHE_SIZE_BYTES = BANKS * sizeof(T)
staticconstexprprotected

Definition at line 87 of file cached_gpio.h.

◆ read_cache_valid_

template<typename T , uint16_t N, typename P = expander_pin_t<N>>
T esphome::gpio_expander::CachedGpioExpanderBase< T, N, P >::read_cache_valid_[BANKS] {0}
protected

Definition at line 89 of file cached_gpio.h.


The documentation for this class was generated from the following file: