Files
Smart-granary-code/components/io_device_control
2026-03-11 20:14:14 +08:00
..
2026-03-11 20:14:14 +08:00
2026-03-11 20:14:14 +08:00
2026-03-11 20:14:14 +08:00

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 中先初始化,后续在业务逻辑中按需调用控制接口。

#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
  • 若硬件驱动电路为反相,请在硬件层或组件内部统一处理,不建议在业务层散落取反逻辑。