ESPHome  2025.2.0
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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 
61  virtual gpio::Flags get_flags() const = 0;
62 
63  virtual bool digital_read() = 0;
64 
65  virtual void digital_write(bool value) = 0;
66 
67  virtual std::string dump_summary() const = 0;
68 
69  virtual bool is_internal() { return false; }
70 };
71 
74  public:
75  ISRInternalGPIOPin() = default;
76  ISRInternalGPIOPin(void *arg) : arg_(arg) {}
77  bool digital_read();
78  void digital_write(bool value);
79  void clear_interrupt();
80  void pin_mode(gpio::Flags flags);
81 
82  protected:
83  void *arg_{nullptr};
84 };
85 
86 class InternalGPIOPin : public GPIOPin {
87  public:
88  template<typename T> void attach_interrupt(void (*func)(T *), T *arg, gpio::InterruptType type) const {
89  this->attach_interrupt(reinterpret_cast<void (*)(void *)>(func), arg, type);
90  }
91 
92  virtual void detach_interrupt() const = 0;
93 
94  virtual ISRInternalGPIOPin to_isr() const = 0;
95 
96  virtual uint8_t get_pin() const = 0;
97 
98  bool is_internal() override { return true; }
99 
100  virtual bool is_inverted() const = 0;
101 
102  protected:
103  virtual void attach_interrupt(void (*func)(void *), void *arg, gpio::InterruptType type) const = 0;
104 };
105 
106 } // namespace esphome
void setup()
constexpr FlagsHelper operator|(Flags lhs, Flags rhs)
Definition: gpio.h:36
Copy of GPIOPin that is safe to use from ISRs (with no virtual functions)
Definition: gpio.h:73
mopeka_std_values val[4]
constexpr FlagsHelper operator &(Flags lhs, Flags rhs)
Definition: gpio.h:33
virtual bool is_internal()
Definition: gpio.h:69
uint8_t type
const uint32_t flags
Definition: stm32flash.h:85
InterruptType
Definition: gpio.h:40
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool is_internal() override
Definition: gpio.h:98
constexpr FlagsHelper(Flags val)
Definition: gpio.h:27
void attach_interrupt(void(*func)(T *), T *arg, gpio::InterruptType type) const
Definition: gpio.h:88
ISRInternalGPIOPin(void *arg)
Definition: gpio.h:76