Files
smart-home-control-system/include/shs/sensor.h
T
sambu 578a770737 chore(M0): 建立 CLion/CMake 工程骨架与测试框架
- CMake 多目标工程:shs_core 静态库 + smart_home 可执行 + shs_tests 测试
- CMakePresets.json:debug/release/asan/coverage 四个预设(debug 告警即错误)
- 自研零依赖测试框架 tests/framework/test_framework.h + 运行器(套件过滤、CTest 集成)
- 工具:tools/header_check.sh 头文件独立编译检查(接口冻结门)
- 已实现公共层:errors / string_utils / num_utils,main 启动自检
- 实测:debug 预设 0 告警、ctest 3/3 通过(45 项断言)
2026-09-14 19:34:18 +08:00

58 lines
2.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* @file sensor.h
* @brief 传感器采集、报警判断与统计
*
* 教材对应:项目二(类型转换:原始值→物理量)·项目三(多分支报警判断)·
* 项目四(循环遍历与异常数据跳过)·项目七(指针出参多值统计)
*/
#ifndef SHS_SENSOR_H
#define SHS_SENSOR_H
#include "shs/common.h"
#include "shs/errors.h"
#include "shs/device.h"
/** 一次采样结果 */
typedef struct {
int raw_temperature; /**< 温度原始值(12 位 ADC 量程 0~4095) */
int raw_humidity; /**< 湿度原始值 */
float temperature_c; /**< 换算后的摄氏度 */
float humidity_pct; /**< 换算后的相对湿度 */
uint32_t ts; /**< 采样时间戳 */
bool valid; /**< 数据是否有效 */
} SensorSample;
/** 报警等级(项目三 多分支) */
typedef enum {
SHS_ALARM_NONE = 0, /**< 正常 */
SHS_ALARM_LOW, /**< 低于下限 */
SHS_ALARM_HIGH, /**< 高于上限 */
SHS_ALARM_SENSOR_FAULT /**< 传感器故障(原始值越界) */
} shs_alarm_t;
/** 12 位 ADC 原始值 → 摄氏度(线性插值:-40℃ ~ 125℃) */
float sensor_raw_to_celsius(int raw);
/** 12 位 ADC 原始值 → 相对湿度百分比(0~100) */
float sensor_raw_to_humidity(int raw);
/** 原始值是否在有效量程内 */
bool sensor_raw_is_valid(int raw);
/** 读取一次采样(经由 HAL 的 I2C 接口,模拟后端) */
shs_err_t sensor_read(uint8_t channel, SensorSample *out);
/** 连续读取 n 个通道 */
shs_err_t sensor_read_all(SensorSample out[], int max_samples, int *out_count);
/** 报警判断:低于 low 或高于 high 分别返回不同等级(含故障判断) */
shs_alarm_t sensor_alarm_check(const SensorSample *sample, float low, float high);
/** 报警等级文案 */
const char *sensor_alarm_text(shs_alarm_t alarm);
/** 过滤无效样本(项目四 break/continue:遇到越界值跳过,全无效则提前结束) */
int sensor_scan_valid(const SensorSample in[], int in_count, SensorSample out[], int out_max);
/** 批量统计(项目七 指针出参):样本数、最小、最大、平均 */
shs_err_t sensor_stats(const SensorSample samples[], int count,
int *out_count, float *out_min, float *out_max, double *out_avg);
#endif /* SHS_SENSOR_H */