添加对额外 UI 资源和火焰状态变量的支持
- 增加了 `ui.h` 中资源数组的大小以容纳新的 UI 元素。 - 更新了 `ui_font_chinese_16.c`,添加了更多中文字符以更好地支持本地化。 - 在 `vars.h` 中添加了用于管理火焰状态的新函数,增强了系统的监测能力。
This commit is contained in:
3
components/fire_sensor/CMakeLists.txt
Normal file
3
components/fire_sensor/CMakeLists.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
idf_component_register(SRCS "fire_sensor.c"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES esp_adc MQ-2)
|
||||
77
components/fire_sensor/fire_sensor.c
Normal file
77
components/fire_sensor/fire_sensor.c
Normal file
@@ -0,0 +1,77 @@
|
||||
#include "fire_sensor.h"
|
||||
|
||||
#include "MQ-2.h"
|
||||
|
||||
#include "esp_adc/adc_oneshot.h"
|
||||
#include "esp_check.h"
|
||||
|
||||
#define FIRE_ADC_UNIT ADC_UNIT_1
|
||||
#define FIRE_ADC_CHANNEL ADC_CHANNEL_2
|
||||
#define FIRE_SAMPLE_COUNT 8
|
||||
|
||||
static const char *TAG = "fire_sensor";
|
||||
static adc_oneshot_unit_handle_t s_adc_handle = NULL;
|
||||
static bool s_inited = false;
|
||||
|
||||
esp_err_t fire_sensor_init(void)
|
||||
{
|
||||
if (s_inited) {
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
// Reuse ADC1 oneshot unit created by MQ-2 to avoid "adc1 is already in use".
|
||||
ESP_RETURN_ON_ERROR(mq2_init(), TAG, "mq2 init failed");
|
||||
s_adc_handle = mq2_get_adc_handle();
|
||||
ESP_RETURN_ON_FALSE(s_adc_handle != NULL, ESP_ERR_INVALID_STATE, TAG, "shared adc handle is null");
|
||||
|
||||
adc_oneshot_chan_cfg_t chan_cfg = {
|
||||
.atten = ADC_ATTEN_DB_12,
|
||||
.bitwidth = ADC_BITWIDTH_DEFAULT,
|
||||
};
|
||||
ESP_RETURN_ON_ERROR(adc_oneshot_config_channel(s_adc_handle, FIRE_ADC_CHANNEL, &chan_cfg),
|
||||
TAG, "adc channel config failed");
|
||||
|
||||
s_inited = true;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t fire_sensor_read_raw(int *raw_out)
|
||||
{
|
||||
ESP_RETURN_ON_FALSE(raw_out != NULL, ESP_ERR_INVALID_ARG, TAG, "raw_out is null");
|
||||
ESP_RETURN_ON_FALSE(s_inited, ESP_ERR_INVALID_STATE, TAG, "fire sensor not initialized");
|
||||
|
||||
int sum = 0;
|
||||
for (int i = 0; i < FIRE_SAMPLE_COUNT; ++i) {
|
||||
int sample = 0;
|
||||
ESP_RETURN_ON_ERROR(adc_oneshot_read(s_adc_handle, FIRE_ADC_CHANNEL, &sample),
|
||||
TAG, "adc read failed");
|
||||
sum += sample;
|
||||
}
|
||||
|
||||
*raw_out = sum / FIRE_SAMPLE_COUNT;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t fire_sensor_read_percent(float *percent_out)
|
||||
{
|
||||
ESP_RETURN_ON_FALSE(percent_out != NULL, ESP_ERR_INVALID_ARG, TAG, "percent_out is null");
|
||||
|
||||
int raw = 0;
|
||||
ESP_RETURN_ON_ERROR(fire_sensor_read_raw(&raw), TAG, "read raw failed");
|
||||
|
||||
// Most flame modules output lower voltage when flame is stronger.
|
||||
// Map raw inversely to "danger percent":
|
||||
// raw=4095 -> 0%, raw=0 -> 100%
|
||||
*percent_out = ((4095.0f - (float)raw) * 100.0f) / 4095.0f;
|
||||
if (*percent_out < 0.0f) {
|
||||
*percent_out = 0.0f;
|
||||
} else if (*percent_out > 100.0f) {
|
||||
*percent_out = 100.0f;
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
bool fire_sensor_is_danger(float percent, float threshold_percent)
|
||||
{
|
||||
return percent >= threshold_percent;
|
||||
}
|
||||
21
components/fire_sensor/include/fire_sensor.h
Normal file
21
components/fire_sensor/include/fire_sensor.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "esp_err.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// ESP32-S3: GPIO3 -> ADC1_CHANNEL_2
|
||||
#define FIRE_SENSOR_ADC_GPIO 3
|
||||
|
||||
esp_err_t fire_sensor_init(void);
|
||||
esp_err_t fire_sensor_read_raw(int *raw_out);
|
||||
esp_err_t fire_sensor_read_percent(float *percent_out);
|
||||
bool fire_sensor_is_danger(float percent, float threshold_percent);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user