ESPHome 2025.9.3
Loading...
Searching...
No Matches
esphome::gpio_expander::CachedGpioExpander< T, N, P > Class Template Referenceabstract

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

#include <cached_gpio.h>

Public Member Functions

bool digital_read (P pin)
 Read the state of the given pin.
 
void digital_write (P pin, bool value)
 

Protected Member Functions

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

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 = typename std::conditional<(N > 256), uint16_t, uint8_t>::type>
class esphome::gpio_expander::CachedGpioExpander< 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)

Definition at line 29 of file cached_gpio.h.

Member Function Documentation

◆ digital_read()

template<typename T , uint16_t N, typename P = typename std::conditional<(N > 256), uint16_t, uint8_t>::type>
bool esphome::gpio_expander::CachedGpioExpander< T, N, P >::digital_read ( P pin)
inline

Read the state of the given pin.

This will invalidate the cache for the given pin number.

Parameters
pinPin number to read
Returns
Pin state

Definition at line 34 of file cached_gpio.h.

◆ digital_read_cache()

template<typename T , uint16_t N, typename P = typename std::conditional<(N > 256), uint16_t, uint8_t>::type>
virtual bool esphome::gpio_expander::CachedGpioExpander< 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)

◆ digital_read_hw()

template<typename T , uint16_t N, typename P = typename std::conditional<(N > 256), uint16_t, uint8_t>::type>
virtual bool esphome::gpio_expander::CachedGpioExpander< 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 = typename std::conditional<(N > 256), uint16_t, uint8_t>::type>
void esphome::gpio_expander::CachedGpioExpander< T, N, P >::digital_write ( P pin,
bool value )
inline

Definition at line 51 of file cached_gpio.h.

◆ digital_write_hw()

template<typename T , uint16_t N, typename P = typename std::conditional<(N > 256), uint16_t, uint8_t>::type>
virtual void esphome::gpio_expander::CachedGpioExpander< 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 = typename std::conditional<(N > 256), uint16_t, uint8_t>::type>
void esphome::gpio_expander::CachedGpioExpander< T, N, P >::reset_pin_cache_ ( )
inlineprotected

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

Definition at line 72 of file cached_gpio.h.

Field Documentation

◆ BANK_SIZE

template<typename T , uint16_t N, typename P = typename std::conditional<(N > 256), uint16_t, uint8_t>::type>
uint16_t esphome::gpio_expander::CachedGpioExpander< T, N, P >::BANK_SIZE = sizeof(T) * BITS_PER_BYTE
staticconstexprprotected

Definition at line 75 of file cached_gpio.h.

◆ BANKS

template<typename T , uint16_t N, typename P = typename std::conditional<(N > 256), uint16_t, uint8_t>::type>
size_t esphome::gpio_expander::CachedGpioExpander< T, N, P >::BANKS = N / BANK_SIZE
staticconstexprprotected

Definition at line 76 of file cached_gpio.h.

◆ BITS_PER_BYTE

template<typename T , uint16_t N, typename P = typename std::conditional<(N > 256), uint16_t, uint8_t>::type>
uint16_t esphome::gpio_expander::CachedGpioExpander< T, N, P >::BITS_PER_BYTE = 8
staticconstexprprotected

Definition at line 74 of file cached_gpio.h.

◆ CACHE_SIZE_BYTES

template<typename T , uint16_t N, typename P = typename std::conditional<(N > 256), uint16_t, uint8_t>::type>
size_t esphome::gpio_expander::CachedGpioExpander< T, N, P >::CACHE_SIZE_BYTES = BANKS * sizeof(T)
staticconstexprprotected

Definition at line 77 of file cached_gpio.h.

◆ read_cache_valid_

template<typename T , uint16_t N, typename P = typename std::conditional<(N > 256), uint16_t, uint8_t>::type>
T esphome::gpio_expander::CachedGpioExpander< T, N, P >::read_cache_valid_[BANKS] {0}
protected

Definition at line 79 of file cached_gpio.h.


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