3#include "driver/gpio.h"
19 explicit ESP32GPIO(gpio_num_t gpio_num) : gpio_num_(gpio_num)
21 map_[gpio_num_] =
this;
24 bool Read()
override {
return gpio_get_level(gpio_num_) != 0; }
26 void Write(
bool value)
override { gpio_set_level(gpio_num_, value ? 1 : 0); }
30 if (!isr_service_installed_)
32 gpio_install_isr_service(0);
33 isr_service_installed_ =
true;
36 gpio_isr_handler_add(gpio_num_, ESP32GPIO::InterruptDispatcher, (
void*)gpio_num_);
37 gpio_intr_enable(gpio_num_);
43 gpio_intr_disable(gpio_num_);
44 gpio_isr_handler_remove(gpio_num_);
50 gpio_config_t io_conf = {};
51 io_conf.pin_bit_mask = 1ULL << gpio_num_;
56 io_conf.mode = GPIO_MODE_INPUT;
59 io_conf.mode = GPIO_MODE_OUTPUT;
62 io_conf.mode = GPIO_MODE_OUTPUT_OD;
65 io_conf.mode = GPIO_MODE_INPUT;
66 io_conf.intr_type = GPIO_INTR_NEGEDGE;
69 io_conf.mode = GPIO_MODE_INPUT;
70 io_conf.intr_type = GPIO_INTR_POSEDGE;
73 io_conf.mode = GPIO_MODE_INPUT;
74 io_conf.intr_type = GPIO_INTR_ANYEDGE;
81 io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
82 io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
85 io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
86 io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
89 io_conf.pull_down_en = GPIO_PULLDOWN_ENABLE;
90 io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
94 gpio_config(&io_conf);
99 static void IRAM_ATTR InterruptDispatcher(
void* arg);
102 gpio_num_t gpio_num_;
103 static inline bool isr_service_installed_ =
false;
104 static inline ESP32GPIO* map_[GPIO_NUM_MAX];
ESP32 GPIO 驱动实现 / ESP32 GPIO driver implementation.
bool Read() override
读取 GPIO 引脚状态。Reads the GPIO pin state.
ErrorCode DisableInterrupt() override
禁用 GPIO 引脚中断。Disables the GPIO pin interrupt.
ESP32GPIO(gpio_num_t gpio_num)
构造 GPIO 对象 / Construct GPIO object
void Write(bool value) override
写入 GPIO 引脚状态。Writes the GPIO pin state.
ErrorCode SetConfig(Configuration config) override
配置 GPIO 引脚参数。Configures the GPIO pin settings.
ErrorCode EnableInterrupt() override
使能 GPIO 引脚中断。Enables the GPIO pin interrupt.
通用输入输出(GPIO)接口类。General Purpose Input/Output (GPIO) interface class.
@ OUTPUT_PUSH_PULL
推挽输出模式。Push-pull output mode.
@ RISING_INTERRUPT
上升沿中断模式。Rising edge interrupt mode.
@ FALL_RISING_INTERRUPT
双沿触发中断模式。Both edge interrupt mode.
@ OUTPUT_OPEN_DRAIN
开漏输出模式。Open-drain output mode.
@ FALL_INTERRUPT
下降沿中断模式。Falling edge interrupt mode.
@ NONE
无上拉或下拉。No pull-up or pull-down.
@ DOWN
下拉模式。Pull-down mode.
存储 GPIO 配置参数的结构体。Structure storing GPIO configuration parameters.
Pull pull
GPIO 上拉/下拉配置。GPIO pull-up/pull-down configuration.
Direction direction
GPIO 引脚方向。GPIO pin direction.