ESPHome  2024.12.2
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 
7 namespace esphome {
8 namespace gpio_expander {
9 
11 template<typename T, T N> class CachedGpioExpander {
12  public:
13  bool digital_read(T pin) {
14  if (!this->read_cache_invalidated_[pin]) {
15  this->read_cache_invalidated_[pin] = true;
16  return this->digital_read_cache(pin);
17  }
18  return this->digital_read_hw(pin);
19  }
20 
21  void digital_write(T pin, bool value) { this->digital_write_hw(pin, value); }
22 
23  protected:
24  virtual bool digital_read_hw(T pin) = 0;
25  virtual bool digital_read_cache(T pin) = 0;
26  virtual void digital_write_hw(T pin, bool value) = 0;
27 
29  for (T i = 0; i < N; i++) {
30  this->read_cache_invalidated_[i] = false;
31  }
32  }
33 
34  std::array<bool, N> read_cache_invalidated_{};
35 };
36 
37 } // namespace gpio_expander
38 } // namespace esphome
virtual void digital_write_hw(T pin, bool value)=0
void digital_write(T pin, bool value)
Definition: cached_gpio.h:21
A class to cache the read state of a GPIO expander.
Definition: cached_gpio.h:11
std::array< bool, N > read_cache_invalidated_
Definition: cached_gpio.h:34
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7