ESPHome  2024.11.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 
67  public:
68  ISRInternalGPIOPin() = default;
69  ISRInternalGPIOPin(void *arg) : arg_(arg) {}
70  bool digital_read();
71  void digital_write(bool value);
72  void clear_interrupt();
73  void pin_mode(gpio::Flags flags);
74 
75  protected:
76  void *arg_{nullptr};
77 };
78 
79 class InternalGPIOPin : public GPIOPin {
80  public:
81  template<typename T> void attach_interrupt(void (*func)(T *), T *arg, gpio::InterruptType type) const {
82  this->attach_interrupt(reinterpret_cast<void (*)(void *)>(func), arg, type);
83  }
84 
85  virtual void detach_interrupt() const = 0;
86 
87  virtual ISRInternalGPIOPin to_isr() const = 0;
88 
89  virtual uint8_t get_pin() const = 0;
90 
91  bool is_internal() override { return true; }
92 
93  virtual bool is_inverted() const = 0;
94 
95  protected:
96  virtual void attach_interrupt(void (*func)(void *), void *arg, gpio::InterruptType type) const = 0;
97 };
98 
99 } // 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:66
mopeka_std_values val[4]
constexpr FlagsHelper operator &(Flags lhs, Flags rhs)
Definition: gpio.h:33
virtual bool is_internal()
Definition: gpio.h:62
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:91
constexpr FlagsHelper(Flags val)
Definition: gpio.h:27
void attach_interrupt(void(*func)(T *), T *arg, gpio::InterruptType type) const
Definition: gpio.h:81
ISRInternalGPIOPin(void *arg)
Definition: gpio.h:69