添加人体门状态检测功能,包含初始化和读取状态的逻辑,并更新主程序以集成此功能
This commit is contained in:
3
components/human_door/CMakeLists.txt
Normal file
3
components/human_door/CMakeLists.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
idf_component_register(SRCS "human_door.c"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES esp_driver_gpio)
|
||||
39
components/human_door/human_door.c
Normal file
39
components/human_door/human_door.c
Normal file
@@ -0,0 +1,39 @@
|
||||
#include "human_door.h"
|
||||
|
||||
#include "driver/gpio.h"
|
||||
#include "esp_check.h"
|
||||
|
||||
static const char *TAG = "human_door";
|
||||
static bool s_inited = false;
|
||||
|
||||
esp_err_t human_door_init(void)
|
||||
{
|
||||
if (s_inited) {
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
gpio_config_t io_cfg = {
|
||||
.pin_bit_mask = (1ULL << HUMAN_SENSOR_GPIO) | (1ULL << DOOR_SWITCH_GPIO),
|
||||
.mode = GPIO_MODE_INPUT,
|
||||
.pull_up_en = GPIO_PULLUP_ENABLE,
|
||||
.pull_down_en = GPIO_PULLDOWN_DISABLE,
|
||||
.intr_type = GPIO_INTR_DISABLE,
|
||||
};
|
||||
ESP_RETURN_ON_ERROR(gpio_config(&io_cfg), TAG, "gpio config failed");
|
||||
|
||||
s_inited = true;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t human_door_read(human_door_state_t *out_state)
|
||||
{
|
||||
ESP_RETURN_ON_FALSE(out_state != NULL, ESP_ERR_INVALID_ARG, TAG, "out_state is null");
|
||||
ESP_RETURN_ON_FALSE(s_inited, ESP_ERR_INVALID_STATE, TAG, "not initialized");
|
||||
|
||||
int human_level = gpio_get_level((gpio_num_t)HUMAN_SENSOR_GPIO);
|
||||
int door_level = gpio_get_level((gpio_num_t)DOOR_SWITCH_GPIO);
|
||||
|
||||
out_state->human_present = (human_level != 0);
|
||||
out_state->door_closed = (door_level == 0);
|
||||
return ESP_OK;
|
||||
}
|
||||
26
components/human_door/include/human_door.h
Normal file
26
components/human_door/include/human_door.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "esp_err.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define HUMAN_SENSOR_GPIO 16
|
||||
#define DOOR_SWITCH_GPIO 17
|
||||
|
||||
typedef struct {
|
||||
bool human_present;
|
||||
bool door_closed;
|
||||
} human_door_state_t;
|
||||
|
||||
// GPIO16: HC-SR312. High means motion detected.
|
||||
// GPIO17: Door switch. Low means door closed.
|
||||
esp_err_t human_door_init(void);
|
||||
esp_err_t human_door_read(human_door_state_t *out_state);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user