ESPHome 2025.5.0
Loading...
Searching...
No Matches
gpio.h
Go to the documentation of this file.
1#pragma once
2#include <cstdint>
3#include <string>
4
5namespace 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
13namespace gpio {
14
15enum Flags : uint8_t {
16 // Can't name these just INPUT because of Arduino defines :(
17 FLAG_NONE = 0x00,
18 FLAG_INPUT = 0x01,
23};
24
26 public:
27 constexpr FlagsHelper(Flags val) : val_(val) {}
28 constexpr operator Flags() const { return val_; }
29
30 protected:
32};
33constexpr FlagsHelper operator&(Flags lhs, Flags rhs) {
34 return static_cast<Flags>(static_cast<uint8_t>(lhs) & static_cast<uint8_t>(rhs));
35}
36constexpr FlagsHelper operator|(Flags lhs, Flags rhs) {
37 return static_cast<Flags>(static_cast<uint8_t>(lhs) | static_cast<uint8_t>(rhs));
38}
39
47
48} // namespace gpio
49
50class 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
86class 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
virtual void pin_mode(gpio::Flags flags)=0
virtual void setup()=0
virtual std::string dump_summary() const =0
virtual void digital_write(bool value)=0
virtual bool is_internal()
Definition gpio.h:69
virtual gpio::Flags get_flags() const =0
Retrieve GPIO pin flags.
virtual bool digital_read()=0
Copy of GPIOPin that is safe to use from ISRs (with no virtual functions)
Definition gpio.h:73
ISRInternalGPIOPin(void *arg)
Definition gpio.h:76
void digital_write(bool value)
Definition gpio.cpp:147
void pin_mode(gpio::Flags flags)
Definition gpio.cpp:156
virtual uint8_t get_pin() const =0
bool is_internal() override
Definition gpio.h:98
virtual void detach_interrupt() const =0
void attach_interrupt(void(*func)(T *), T *arg, gpio::InterruptType type) const
Definition gpio.h:88
virtual bool is_inverted() const =0
virtual ISRInternalGPIOPin to_isr() const =0
virtual void attach_interrupt(void(*func)(void *), void *arg, gpio::InterruptType type) const =0
constexpr FlagsHelper(Flags val)
Definition gpio.h:27
uint8_t type
mopeka_std_values val[4]
@ INTERRUPT_FALLING_EDGE
Definition gpio.h:42
@ INTERRUPT_RISING_EDGE
Definition gpio.h:41
@ INTERRUPT_HIGH_LEVEL
Definition gpio.h:45
@ INTERRUPT_LOW_LEVEL
Definition gpio.h:44
@ INTERRUPT_ANY_EDGE
Definition gpio.h:43
@ FLAG_OUTPUT
Definition gpio.h:19
@ FLAG_OPEN_DRAIN
Definition gpio.h:20
@ FLAG_NONE
Definition gpio.h:17
@ FLAG_PULLUP
Definition gpio.h:21
@ FLAG_INPUT
Definition gpio.h:18
@ FLAG_PULLDOWN
Definition gpio.h:22
constexpr FlagsHelper operator|(Flags lhs, Flags rhs)
Definition gpio.h:36
constexpr FlagsHelper operator&(Flags lhs, Flags rhs)
Definition gpio.h:33
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7