ESPHome 2025.5.0
Loading...
Searching...
No Matches
cached_gpio.h
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <cstdint>
5#include "esphome/core/hal.h"
6
7namespace esphome {
8namespace gpio_expander {
9
18template<typename T, T N> class CachedGpioExpander {
19 public:
20 bool digital_read(T pin) {
21 uint8_t bank = pin / (sizeof(T) * BITS_PER_BYTE);
22 if (this->read_cache_invalidated_[bank]) {
23 this->read_cache_invalidated_[bank] = false;
24 if (!this->digital_read_hw(pin))
25 return false;
26 }
27 return this->digital_read_cache(pin);
28 }
29
30 void digital_write(T pin, bool value) { this->digital_write_hw(pin, value); }
31
32 protected:
34 virtual bool digital_read_hw(T pin) = 0;
36 virtual bool digital_read_cache(T pin) = 0;
38 virtual void digital_write_hw(T pin, bool value) = 0;
39 const uint8_t cache_byte_size_ = N / (sizeof(T) * BITS_PER_BYTE);
40
43 for (T i = 0; i < this->cache_byte_size_; i++) {
44 this->read_cache_invalidated_[i] = true;
45 }
46 }
47
48 static const uint8_t BITS_PER_BYTE = 8;
49 std::array<bool, N / (sizeof(T) * BITS_PER_BYTE)> read_cache_invalidated_{};
50};
51
52} // namespace gpio_expander
53} // namespace esphome
A class to cache the read state of a GPIO expander.
Definition cached_gpio.h:18
virtual void digital_write_hw(T pin, bool value)=0
Call component low level function to write GPIO state to device.
std::array< bool, N/(sizeof(T) *BITS_PER_BYTE)> read_cache_invalidated_
Definition cached_gpio.h:49
virtual bool digital_read_cache(T pin)=0
Call component read function from internal cache.
void reset_pin_cache_()
Invalidate cache. This function should be called in component loop().
Definition cached_gpio.h:42
virtual bool digital_read_hw(T pin)=0
Call component low level function to read GPIO state from device.
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7