完成初代的雏形设计
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)
|
||||
50
components/io_device_control/README.md
Normal file
50
components/io_device_control/README.md
Normal file
@@ -0,0 +1,50 @@
|
||||
# io_device_control
|
||||
|
||||
`io_device_control` 组件用于统一管理项目中的简单 IO 外设控制。
|
||||
|
||||
当前定义:
|
||||
|
||||
- `GPIO1`:风扇控制(高电平有效)
|
||||
- `GPIO0`:光照控制(高电平有效)
|
||||
- `GPIO12`:加热控制(高电平有效)
|
||||
- `GPIO13`:制冷控制(高电平有效)
|
||||
|
||||
## 对外接口
|
||||
|
||||
头文件:`include/io_device_control.h`
|
||||
|
||||
- `esp_err_t io_device_control_init(void);`
|
||||
- 初始化 GPIO 输出方向,并将风扇/光照/加热/制冷默认置为关闭(低电平)。
|
||||
- `esp_err_t io_device_control_set_fan(bool on);`
|
||||
- 控制风扇开关,`true` 为开,`false` 为关。
|
||||
- `esp_err_t io_device_control_set_light(bool on);`
|
||||
- 控制光照开关,`true` 为开,`false` 为关。
|
||||
- `esp_err_t io_device_control_set_hot(bool on);`
|
||||
- 控制加热开关,`true` 为开,`false` 为关。
|
||||
- `esp_err_t io_device_control_set_cool(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_fan(true));
|
||||
// ESP_ERROR_CHECK(io_device_control_set_light(true));
|
||||
// ESP_ERROR_CHECK(io_device_control_set_hot(true));
|
||||
// ESP_ERROR_CHECK(io_device_control_set_cool(true));
|
||||
}
|
||||
```
|
||||
|
||||
## 注意事项
|
||||
|
||||
- 控制接口在未初始化时会返回 `ESP_ERR_INVALID_STATE`。
|
||||
- 若硬件驱动电路为反相,请在硬件层或组件内部统一处理,不建议在业务层散落取反逻辑。
|
||||
28
components/io_device_control/include/io_device_control.h
Normal file
28
components/io_device_control/include/io_device_control.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "esp_err.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// Initializes fan/light/hot/cool outputs and sets all devices off by default.
|
||||
esp_err_t io_device_control_init(void);
|
||||
|
||||
// High level control APIs, all are active-high outputs.
|
||||
esp_err_t io_device_control_set_fan(bool on);
|
||||
esp_err_t io_device_control_set_light(bool on);
|
||||
esp_err_t io_device_control_set_hot(bool on);
|
||||
esp_err_t io_device_control_set_cool(bool on);
|
||||
|
||||
// Read current output states from GPIO.
|
||||
esp_err_t io_device_control_get_states(bool *fan_on,
|
||||
bool *light_on,
|
||||
bool *hot_on,
|
||||
bool *cool_on);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
106
components/io_device_control/io_device_control.c
Normal file
106
components/io_device_control/io_device_control.c
Normal file
@@ -0,0 +1,106 @@
|
||||
#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_FAN_GPIO GPIO_NUM_1
|
||||
#define IO_DEVICE_LIGHT_GPIO GPIO_NUM_0
|
||||
#define IO_DEVICE_HOT_GPIO GPIO_NUM_12
|
||||
#define IO_DEVICE_COOL_GPIO GPIO_NUM_13
|
||||
|
||||
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_FAN_GPIO) |
|
||||
(1ULL << IO_DEVICE_LIGHT_GPIO) |
|
||||
(1ULL << IO_DEVICE_HOT_GPIO) |
|
||||
(1ULL << IO_DEVICE_COOL_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_FAN_GPIO, 0), TAG, "set fan default failed");
|
||||
ESP_RETURN_ON_ERROR(gpio_set_level(IO_DEVICE_LIGHT_GPIO, 0), TAG, "set light default failed");
|
||||
ESP_RETURN_ON_ERROR(gpio_set_level(IO_DEVICE_HOT_GPIO, 0), TAG, "set hot default failed");
|
||||
ESP_RETURN_ON_ERROR(gpio_set_level(IO_DEVICE_COOL_GPIO, 0), TAG, "set cool default failed");
|
||||
|
||||
s_inited = true;
|
||||
ESP_LOGI(TAG,
|
||||
"initialized: fan=GPIO%d light=GPIO%d hot=GPIO%d cool=GPIO%d active_high=1",
|
||||
IO_DEVICE_FAN_GPIO,
|
||||
IO_DEVICE_LIGHT_GPIO,
|
||||
IO_DEVICE_HOT_GPIO,
|
||||
IO_DEVICE_COOL_GPIO);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t io_device_control_set_fan(bool on)
|
||||
{
|
||||
ESP_RETURN_ON_ERROR(io_device_control_set_level(IO_DEVICE_FAN_GPIO, on),
|
||||
TAG,
|
||||
"set fan 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;
|
||||
}
|
||||
|
||||
esp_err_t io_device_control_set_hot(bool on)
|
||||
{
|
||||
ESP_RETURN_ON_ERROR(io_device_control_set_level(IO_DEVICE_HOT_GPIO, on),
|
||||
TAG,
|
||||
"set hot failed");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t io_device_control_set_cool(bool on)
|
||||
{
|
||||
ESP_RETURN_ON_ERROR(io_device_control_set_level(IO_DEVICE_COOL_GPIO, on),
|
||||
TAG,
|
||||
"set cool failed");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t io_device_control_get_states(bool *fan_on,
|
||||
bool *light_on,
|
||||
bool *hot_on,
|
||||
bool *cool_on)
|
||||
{
|
||||
ESP_RETURN_ON_FALSE(fan_on != NULL && light_on != NULL && hot_on != NULL && cool_on != NULL,
|
||||
ESP_ERR_INVALID_ARG,
|
||||
TAG,
|
||||
"null state pointer");
|
||||
ESP_RETURN_ON_FALSE(s_inited, ESP_ERR_INVALID_STATE, TAG, "not initialized");
|
||||
|
||||
*fan_on = (gpio_get_level(IO_DEVICE_FAN_GPIO) != 0);
|
||||
*light_on = (gpio_get_level(IO_DEVICE_LIGHT_GPIO) != 0);
|
||||
*hot_on = (gpio_get_level(IO_DEVICE_HOT_GPIO) != 0);
|
||||
*cool_on = (gpio_get_level(IO_DEVICE_COOL_GPIO) != 0);
|
||||
return ESP_OK;
|
||||
}
|
||||
Reference in New Issue
Block a user