feat: 添加 IO 外设控制组件,支持水泵与光照控制
This commit is contained in:
3
components/io_device_control/CMakeLists.txt
Normal file
3
components/io_device_control/CMakeLists.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
idf_component_register(SRCS "io_device_control.c"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES esp_driver_gpio)
|
||||
42
components/io_device_control/README.md
Normal file
42
components/io_device_control/README.md
Normal file
@@ -0,0 +1,42 @@
|
||||
# io_device_control
|
||||
|
||||
`io_device_control` 组件用于统一管理项目中的简单 IO 外设控制。
|
||||
|
||||
当前定义:
|
||||
|
||||
- `GPIO1`:水泵控制(高电平有效)
|
||||
- `GPIO0`:光照控制(高电平有效)
|
||||
|
||||
## 对外接口
|
||||
|
||||
头文件:`include/io_device_control.h`
|
||||
|
||||
- `esp_err_t io_device_control_init(void);`
|
||||
- 初始化 GPIO 输出方向,并将水泵与光照默认置为关闭(低电平)。
|
||||
- `esp_err_t io_device_control_set_pump(bool on);`
|
||||
- 控制水泵开关,`true` 为开,`false` 为关。
|
||||
- `esp_err_t io_device_control_set_light(bool on);`
|
||||
- 控制光照开关,`true` 为开,`false` 为关。
|
||||
|
||||
## 使用方式
|
||||
|
||||
在 `app_main` 中先初始化,后续在业务逻辑中按需调用控制接口。
|
||||
|
||||
```c
|
||||
#include "esp_check.h"
|
||||
#include "io_device_control.h"
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
ESP_ERROR_CHECK(io_device_control_init());
|
||||
|
||||
// 后续按需调用
|
||||
// ESP_ERROR_CHECK(io_device_control_set_pump(true));
|
||||
// ESP_ERROR_CHECK(io_device_control_set_light(true));
|
||||
}
|
||||
```
|
||||
|
||||
## 注意事项
|
||||
|
||||
- 控制接口在未初始化时会返回 `ESP_ERR_INVALID_STATE`。
|
||||
- 若硬件驱动电路为反相,请在硬件层或组件内部统一处理,不建议在业务层散落取反逻辑。
|
||||
20
components/io_device_control/include/io_device_control.h
Normal file
20
components/io_device_control/include/io_device_control.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "esp_err.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// Initializes GPIO0/GPIO1 outputs and sets both devices off by default.
|
||||
esp_err_t io_device_control_init(void);
|
||||
|
||||
// High level control APIs, both are active-high outputs.
|
||||
esp_err_t io_device_control_set_pump(bool on);
|
||||
esp_err_t io_device_control_set_light(bool on);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
60
components/io_device_control/io_device_control.c
Normal file
60
components/io_device_control/io_device_control.c
Normal file
@@ -0,0 +1,60 @@
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "driver/gpio.h"
|
||||
#include "esp_check.h"
|
||||
#include "esp_log.h"
|
||||
#include "io_device_control.h"
|
||||
|
||||
static const char *TAG = "io_device_control";
|
||||
|
||||
#define IO_DEVICE_PUMP_GPIO GPIO_NUM_1
|
||||
#define IO_DEVICE_LIGHT_GPIO GPIO_NUM_0
|
||||
|
||||
static bool s_inited = false;
|
||||
|
||||
static esp_err_t io_device_control_set_level(gpio_num_t pin, bool on)
|
||||
{
|
||||
ESP_RETURN_ON_FALSE(s_inited, ESP_ERR_INVALID_STATE, TAG, "not initialized");
|
||||
return gpio_set_level(pin, on ? 1 : 0);
|
||||
}
|
||||
|
||||
esp_err_t io_device_control_init(void)
|
||||
{
|
||||
if (s_inited) {
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
const gpio_config_t out_cfg = {
|
||||
.pin_bit_mask = (1ULL << IO_DEVICE_PUMP_GPIO) | (1ULL << IO_DEVICE_LIGHT_GPIO),
|
||||
.mode = GPIO_MODE_OUTPUT,
|
||||
.pull_up_en = GPIO_PULLUP_DISABLE,
|
||||
.pull_down_en = GPIO_PULLDOWN_DISABLE,
|
||||
.intr_type = GPIO_INTR_DISABLE,
|
||||
};
|
||||
|
||||
ESP_RETURN_ON_ERROR(gpio_config(&out_cfg), TAG, "gpio_config failed");
|
||||
|
||||
// Active-high outputs; default low keeps devices off at boot.
|
||||
ESP_RETURN_ON_ERROR(gpio_set_level(IO_DEVICE_PUMP_GPIO, 0), TAG, "set pump default failed");
|
||||
ESP_RETURN_ON_ERROR(gpio_set_level(IO_DEVICE_LIGHT_GPIO, 0), TAG, "set light default failed");
|
||||
|
||||
s_inited = true;
|
||||
ESP_LOGI(TAG, "initialized: pump=GPIO%d light=GPIO%d active_high=1", IO_DEVICE_PUMP_GPIO, IO_DEVICE_LIGHT_GPIO);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t io_device_control_set_pump(bool on)
|
||||
{
|
||||
ESP_RETURN_ON_ERROR(io_device_control_set_level(IO_DEVICE_PUMP_GPIO, on),
|
||||
TAG,
|
||||
"set pump failed");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t io_device_control_set_light(bool on)
|
||||
{
|
||||
ESP_RETURN_ON_ERROR(io_device_control_set_level(IO_DEVICE_LIGHT_GPIO, on),
|
||||
TAG,
|
||||
"set light failed");
|
||||
return ESP_OK;
|
||||
}
|
||||
Reference in New Issue
Block a user