ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
esphome::gpio_expander::CachedGpioExpander< T, N > 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 (T pin)
 Read the state of the given pin.
 
void digital_write (T pin, bool value)
 

Protected Member Functions

virtual bool digital_read_hw (T pin)=0
 Call component low level function to read GPIO state from device.
 
virtual bool digital_read_cache (T pin)=0
 Call component read function from internal cache.
 
virtual void digital_write_hw (T pin, bool value)=0
 Call component low level function to write GPIO state to device.
 
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 uint8_t BITS_PER_BYTE = 8
 
static constexpr uint8_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, T N>
class esphome::gpio_expander::CachedGpioExpander< T, N >

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 register. Could be uint8_t or uint16_t. Adjust to match size of your internal GPIO bank register. N - Number of pins

Definition at line 19 of file cached_gpio.h.

Member Function Documentation

◆ digital_read()

template<typename T , T N>
bool esphome::gpio_expander::CachedGpioExpander< T, N >::digital_read ( T 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 24 of file cached_gpio.h.

◆ digital_read_cache()

template<typename T , T N>
virtual bool esphome::gpio_expander::CachedGpioExpander< T, N >::digital_read_cache ( T pin)
protectedpure virtual

◆ digital_read_hw()

template<typename T , T N>
virtual bool esphome::gpio_expander::CachedGpioExpander< T, N >::digital_read_hw ( T pin)
protectedpure virtual

◆ digital_write()

template<typename T , T N>
void esphome::gpio_expander::CachedGpioExpander< T, N >::digital_write ( T pin,
bool value )
inline

Definition at line 41 of file cached_gpio.h.

◆ digital_write_hw()

template<typename T , T N>
virtual void esphome::gpio_expander::CachedGpioExpander< T, N >::digital_write_hw ( T pin,
bool value )
protectedpure virtual

◆ reset_pin_cache_()

template<typename T , T N>
void esphome::gpio_expander::CachedGpioExpander< T, N >::reset_pin_cache_ ( )
inlineprotected

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

Definition at line 52 of file cached_gpio.h.

Field Documentation

◆ BANK_SIZE

template<typename T , T N>
uint8_t esphome::gpio_expander::CachedGpioExpander< T, N >::BANK_SIZE = sizeof(T) * BITS_PER_BYTE
staticconstexprprotected

Definition at line 55 of file cached_gpio.h.

◆ BANKS

template<typename T , T N>
size_t esphome::gpio_expander::CachedGpioExpander< T, N >::BANKS = N / BANK_SIZE
staticconstexprprotected

Definition at line 56 of file cached_gpio.h.

◆ BITS_PER_BYTE

template<typename T , T N>
uint8_t esphome::gpio_expander::CachedGpioExpander< T, N >::BITS_PER_BYTE = 8
staticconstexprprotected

Definition at line 54 of file cached_gpio.h.

◆ CACHE_SIZE_BYTES

template<typename T , T N>
size_t esphome::gpio_expander::CachedGpioExpander< T, N >::CACHE_SIZE_BYTES = BANKS * sizeof(T)
staticconstexprprotected

Definition at line 57 of file cached_gpio.h.

◆ read_cache_valid_

template<typename T , T N>
T esphome::gpio_expander::CachedGpioExpander< T, N >::read_cache_valid_[BANKS] {0}
protected

Definition at line 59 of file cached_gpio.h.


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