ESPHome 2026.8.0-dev
Loading...
Searching...
No Matches
gpio.h
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3#include <cstdint>
4
6#include "esphome/core/log.h"
7
8namespace esphome {
9
11inline constexpr size_t GPIO_SUMMARY_MAX_LEN = 48;
12
13#ifdef USE_ESP8266
14#define LOG_PIN(prefix, pin) log_pin(TAG, F(prefix), pin)
15#else
16#define LOG_PIN(prefix, pin) log_pin(TAG, prefix, pin)
17#endif
18
19// put GPIO flags in a namespace to not pollute esphome namespace
20namespace gpio {
21
22enum Flags : uint8_t {
23 // Can't name these just INPUT because of Arduino defines :(
24 FLAG_NONE = 0x00,
25 FLAG_INPUT = 0x01,
30};
31
33 public:
34 constexpr FlagsHelper(Flags val) : val_(val) {}
35 constexpr operator Flags() const { return val_; }
36
37 protected:
39};
40constexpr FlagsHelper operator&(Flags lhs, Flags rhs) {
41 return static_cast<Flags>(static_cast<uint8_t>(lhs) & static_cast<uint8_t>(rhs));
42}
43constexpr FlagsHelper operator|(Flags lhs, Flags rhs) {
44 return static_cast<Flags>(static_cast<uint8_t>(lhs) | static_cast<uint8_t>(rhs));
45}
46
54
55} // namespace gpio
56
57class GPIOPin {
58 public:
59 virtual void setup() = 0;
60
61 virtual void pin_mode(gpio::Flags flags) = 0;
62
68 virtual gpio::Flags get_flags() const = 0;
69
70 virtual bool digital_read() = 0;
71
72 virtual void digital_write(bool value) = 0;
73
79 virtual size_t dump_summary(char *buffer, size_t len) const;
80
81 virtual bool is_internal() { return false; }
82};
83
86 public:
87 ISRInternalGPIOPin() = default;
88 ISRInternalGPIOPin(void *arg) : arg_(arg) {}
89 bool digital_read();
90 void digital_write(bool value);
91 void clear_interrupt();
93
94 protected:
95 void *arg_{nullptr};
96};
97
98class InternalGPIOPin : public GPIOPin {
99 public:
100 template<typename T> void attach_interrupt(void (*func)(T *), T *arg, gpio::InterruptType type) const {
101 this->attach_interrupt(reinterpret_cast<void (*)(void *)>(func), arg, type);
102 }
103
104 virtual void detach_interrupt() const = 0;
105
106 virtual ISRInternalGPIOPin to_isr() const = 0;
107
108 virtual uint8_t get_pin() const = 0;
109
110 bool is_internal() override { return true; }
111
112 virtual bool is_inverted() const = 0;
113
114 protected:
115 virtual void attach_interrupt(void (*func)(void *), void *arg, gpio::InterruptType type) const = 0;
116};
117
118// Inline default implementation for GPIOPin::dump_summary.
119// Writes an empty summary; subclasses override to provide pin details.
120inline size_t GPIOPin::dump_summary(char *buffer, size_t len) const {
121 if (len > 0)
122 buffer[0] = '\0';
123 return 0;
124}
125
126// Inline helper for log_pin - allows compiler to inline into log_pin in gpio.cpp
127inline void log_pin_with_prefix(const char *tag, const char *prefix, GPIOPin *pin) {
128 char buffer[GPIO_SUMMARY_MAX_LEN];
129 size_t len = pin->dump_summary(buffer, sizeof(buffer));
130 len = std::min(len, sizeof(buffer) - 1);
131 esp_log_printf_(ESPHOME_LOG_LEVEL_CONFIG, tag, __LINE__, "%s%.*s", prefix, (int) len, buffer);
132}
133
134// log_pin function declarations - implementation in gpio.cpp
135#ifdef USE_ESP8266
136void log_pin(const char *tag, const __FlashStringHelper *prefix, GPIOPin *pin);
137#else
138void log_pin(const char *tag, const char *prefix, GPIOPin *pin);
139#endif
140
141} // namespace esphome
virtual void pin_mode(gpio::Flags flags)=0
virtual void setup()=0
virtual void digital_write(bool value)=0
virtual bool is_internal()
Definition gpio.h:81
virtual gpio::Flags get_flags() const =0
Retrieve GPIO pin flags.
virtual bool digital_read()=0
virtual size_t dump_summary(char *buffer, size_t len) const
Write a summary of this pin to the provided buffer.
Definition gpio.h:120
Copy of GPIOPin that is safe to use from ISRs (with no virtual functions)
Definition gpio.h:85
ISRInternalGPIOPin(void *arg)
Definition gpio.h:88
void digital_write(bool value)
Definition gpio.cpp:148
void pin_mode(gpio::Flags flags)
Definition gpio.cpp:157
virtual uint8_t get_pin() const =0
bool is_internal() override
Definition gpio.h:110
virtual void detach_interrupt() const =0
void attach_interrupt(void(*func)(T *), T *arg, gpio::InterruptType type) const
Definition gpio.h:100
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:34
uint16_t type
uint16_t flags
mopeka_std_values val[3]
@ INTERRUPT_FALLING_EDGE
Definition gpio.h:49
@ INTERRUPT_RISING_EDGE
Definition gpio.h:48
@ INTERRUPT_HIGH_LEVEL
Definition gpio.h:52
@ INTERRUPT_LOW_LEVEL
Definition gpio.h:51
@ INTERRUPT_ANY_EDGE
Definition gpio.h:50
@ FLAG_OUTPUT
Definition gpio.h:26
@ FLAG_OPEN_DRAIN
Definition gpio.h:27
@ FLAG_NONE
Definition gpio.h:24
@ FLAG_PULLUP
Definition gpio.h:28
@ FLAG_INPUT
Definition gpio.h:25
@ FLAG_PULLDOWN
Definition gpio.h:29
constexpr FlagsHelper operator|(Flags lhs, Flags rhs)
Definition gpio.h:43
constexpr FlagsHelper operator&(Flags lhs, Flags rhs)
Definition gpio.h:40
const char * tag
Definition log.h:74
void HOT esp_log_printf_(int level, const char *tag, int line, const char *format,...)
Definition log.cpp:21
const void size_t len
Definition hal.h:64
void log_pin_with_prefix(const char *tag, const char *prefix, GPIOPin *pin)
Definition gpio.h:127
constexpr size_t GPIO_SUMMARY_MAX_LEN
Maximum buffer size for dump_summary output.
Definition gpio.h:11
void log_pin(const char *tag, const __FlashStringHelper *prefix, GPIOPin *pin)
Definition gpio.cpp:7