完成初代的雏形设计
This commit is contained in:
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