feat: 添加 IO 外设控制组件,支持水泵与光照控制
This commit is contained in:
@@ -4,6 +4,8 @@
|
||||
|
||||
- **Wi-Fi 配网组件(wifi-connect)**:手机连接设备热点后通过网页完成路由器配置
|
||||
- **LCD 显示组件(lvgl_st7735s_use)**:基于 LVGL 驱动 ST77xx SPI 屏并显示界面
|
||||
- **I2C 传感器组件(i2c_master_messager)**:统一读取 BH1750 / AHT30 数据
|
||||
- **IO 外设控制组件(io_device_control)**:控制水泵与光照开关(高电平有效)
|
||||
|
||||
## 功能特性
|
||||
|
||||
@@ -15,6 +17,7 @@
|
||||
- 串口中文状态日志,便于调试和现场维护
|
||||
- 支持 ST77xx SPI LCD 显示(LVGL)
|
||||
- 支持方向/偏移参数化配置,便于后续适配不同屏幕
|
||||
- 支持水泵(GPIO1)与光照(GPIO0)控制接口
|
||||
|
||||
## 目录结构
|
||||
|
||||
@@ -26,6 +29,10 @@
|
||||
- `BLOG.md`:博客草稿
|
||||
- `components/lvgl_st7735s_use/`:LCD 显示组件(LVGL + ST77xx)
|
||||
- `README.md`:组件说明与调参指南
|
||||
- `components/i2c_master_messager/`:I2C 传感器管理组件
|
||||
- `README.md`:传感器采集与配置说明
|
||||
- `components/io_device_control/`:IO 外设控制组件
|
||||
- `README.md`:水泵/光照控制接口说明
|
||||
|
||||
## 开发环境
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
idf_component_register(SRCS "main.c"
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES wifi-connect esp_lvgl_port lvgl_st7735s_use i2c_master_messager
|
||||
REQUIRES wifi-connect esp_lvgl_port lvgl_st7735s_use i2c_master_messager io_device_control
|
||||
)
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "wifi-connect.h"
|
||||
#include "lvgl_st7735s_use.h"
|
||||
#include "i2c_master_messager.h"
|
||||
#include "io_device_control.h"
|
||||
|
||||
#ifndef CONFIG_I2C_MASTER_MESSAGER_BH1750_ENABLE
|
||||
#define CONFIG_I2C_MASTER_MESSAGER_BH1750_ENABLE 0
|
||||
@@ -49,6 +50,9 @@ void app_main(void)
|
||||
// 启动 LVGL 演示程序,显示简单的界面
|
||||
ESP_ERROR_CHECK(start_lvgl_demo());
|
||||
|
||||
// 初始化 IO 设备控制组件(GPIO1 水泵,GPIO0 光照,高电平有效)
|
||||
ESP_ERROR_CHECK(io_device_control_init());
|
||||
|
||||
i2c_master_messager_config_t i2c_cfg = {
|
||||
.i2c_port = BOTANY_I2C_PORT,
|
||||
.scl_io_num = BOTANY_I2C_SCL_GPIO,
|
||||
|
||||
Reference in New Issue
Block a user