release v0.9
This commit is contained in:
1 parent
0045c0c390
commit
14f2ff94d0
11 files changed
+337
-61
No files matched your search
@@ -0,0 +1,3 @@
|
||||
idf_component_register(SRCS "key.c"
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES esp_driver_gpio driver freertos)
|
||||
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* @file key.c
|
||||
* @brief 单 GPIO 按键控制实现 (低电平有效, 软件消抖)
|
||||
*/
|
||||
|
||||
#include "key.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
static int s_gpio = -1;
|
||||
|
||||
void key_init(int gpio)
|
||||
{
|
||||
s_gpio = gpio;
|
||||
|
||||
gpio_reset_pin(s_gpio);
|
||||
gpio_set_direction(s_gpio, GPIO_MODE_INPUT);
|
||||
gpio_set_pull_mode(s_gpio, GPIO_PULLUP_ONLY);
|
||||
}
|
||||
|
||||
bool key_is_pressed(void)
|
||||
{
|
||||
if (s_gpio < 0) return false;
|
||||
|
||||
/* 低电平有效 + 简单消抖: 连续两次读到低电平才算按下 */
|
||||
if (gpio_get_level(s_gpio) == 0) {
|
||||
vTaskDelay(pdMS_TO_TICKS(20));
|
||||
return (gpio_get_level(s_gpio) == 0);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* @file key.h
|
||||
* @brief 单 GPIO 按键控制 (低电平有效, 软件消抖)
|
||||
*/
|
||||
|
||||
#ifndef KEY_H
|
||||
#define KEY_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "driver/gpio.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief 初始化按键 (内部上拉, 低电平有效)
|
||||
* @param gpio GPIO 引脚号, 如 GPIO_NUM_8
|
||||
*/
|
||||
void key_init(int gpio);
|
||||
|
||||
/**
|
||||
* @brief 读取按键状态 (已消抖)
|
||||
* @return true=按下, false=释放
|
||||
*/
|
||||
bool key_is_pressed(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* KEY_H */
|
||||
Reference in new issue
Block a user