ESPHome  2024.5.0
gpio.h
Go to the documentation of this file.
1 #pragma once
2 #include <cstdint>
3 #include <string>
4 
5 namespace esphome {
6 
7 #define LOG_PIN(prefix, pin) \
8  if ((pin) != nullptr) { \
9  ESP_LOGCONFIG(TAG, prefix "%s", (pin)->dump_summary().c_str()); \
10  }
11 
12 // put GPIO flags in a namespace to not pollute esphome namespace
13 namespace gpio {
14 
15 enum Flags : uint8_t {
16  // Can't name these just INPUT because of Arduino defines :(
17  FLAG_NONE = 0x00,
18  FLAG_INPUT = 0x01,
19  FLAG_OUTPUT = 0x02,
21  FLAG_PULLUP = 0x08,
22  FLAG_PULLDOWN = 0x10,
23 };
24 
25 class FlagsHelper {
26  public:
27  constexpr FlagsHelper(Flags val) : val_(val) {}
28  constexpr operator Flags() const { return val_; }
29 
30  protected:
32 };
33 constexpr FlagsHelper operator&(Flags lhs, Flags rhs) {
34  return static_cast<Flags>(static_cast<uint8_t>(lhs) & static_cast<uint8_t>(rhs));
35 }
36 constexpr FlagsHelper operator|(Flags lhs, Flags rhs) {
37  return static_cast<Flags>(static_cast<uint8_t>(lhs) | static_cast<uint8_t>(rhs));
38 }
39 
40 enum InterruptType : uint8_t {
46 };
47 
48 } // namespace gpio
49 
50 class GPIOPin {
51  public:
52  virtual void setup() = 0;
53 
54  virtual void pin_mode(gpio::Flags flags) = 0;
55 
56  virtual bool digital_read() = 0;
57 
58  virtual void digital_write(bool value) = 0;
59 
60  virtual std::string dump_summary() const = 0;
61 
62  virtual bool is_internal() { return false; }
63 };
64 
68 class NullPin : public GPIOPin {
69  public:
70  void setup() override {}
71 
72  void pin_mode(gpio::Flags _) override {}
73 
74  bool digital_read() override { return false; }
75 
76  void digital_write(bool _) override {}
77 
78  std::string dump_summary() const override { return {"Not used"}; }
79 };
80 
81 static GPIOPin *const NULL_PIN = new NullPin();
82 
85  public:
86  ISRInternalGPIOPin() = default;
87  ISRInternalGPIOPin(void *arg) : arg_(arg) {}
88  bool digital_read();
89  void digital_write(bool value);
90  void clear_interrupt();
91  void pin_mode(gpio::Flags flags);
92 
93  protected:
94  void *arg_{nullptr};
95 };
96 
97 class InternalGPIOPin : public GPIOPin {
98  public:
99  template<typename T> void attach_interrupt(void (*func)(T *), T *arg, gpio::InterruptType type) const {
100  this->attach_interrupt(reinterpret_cast<void (*)(void *)>(func), arg, type);
101  }
102 
103  virtual void detach_interrupt() const = 0;
104 
105  virtual ISRInternalGPIOPin to_isr() const = 0;
106 
107  virtual uint8_t get_pin() const = 0;
108 
109  bool is_internal() override { return true; }
110 
111  virtual bool is_inverted() const = 0;
112 
113  protected:
114  virtual void attach_interrupt(void (*func)(void *), void *arg, gpio::InterruptType type) const = 0;
115 };
116 
117 } // namespace esphome
void setup()
constexpr FlagsHelper operator|(Flags lhs, Flags rhs)
Definition: gpio.h:36
void setup() override
Definition: gpio.h:70
Copy of GPIOPin that is safe to use from ISRs (with no virtual functions)
Definition: gpio.h:84
mopeka_std_values val[4]
constexpr FlagsHelper operator &(Flags lhs, Flags rhs)
Definition: gpio.h:33
bool digital_read() override
Definition: gpio.h:74
virtual bool is_internal()
Definition: gpio.h:62
void pin_mode(gpio::Flags _) override
Definition: gpio.h:72
uint8_t type
A pin to replace those that don&#39;t exist.
Definition: gpio.h:68
const uint32_t flags
Definition: stm32flash.h:85
std::string dump_summary() const override
Definition: gpio.h:78
InterruptType
Definition: gpio.h:40
void digital_write(bool _) override
Definition: gpio.h:76
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
bool is_internal() override
Definition: gpio.h:109
constexpr FlagsHelper(Flags val)
Definition: gpio.h:27
void attach_interrupt(void(*func)(T *), T *arg, gpio::InterruptType type) const
Definition: gpio.h:99
ISRInternalGPIOPin(void *arg)
Definition: gpio.h:87