/* * @Description: * @Version: 1.0 * @Author: lzc * @Date: 2023-01-05 08:48:11 * @LastEditors: lzc * @LastEditTime: 2023-06-21 12:41:08 */ #ifndef __SLLEPREPORT_H #define __SLLEPREPORT_H #include "stm32f4xx.h" #include "main.h" #ifndef NULL #define NULL 0 #endif // 随机数据 static const char BreathDataRandom[720] = { 23, 17, 27, 21, 26, 25, 17, 22, 22, 24, 17, 19, 18, 19, 23, 21, 22, 26, 26, 20, 23, 26, 27, 17, 22, 26, 20, 19, 20, 26, 24, 20, 26, 21, 17, 24, 26, 23, 26, 20, 27, 19, 20, 24, 19, 25, 17, 19, 20, 21, 20, 19, 27, 23, 23, 20, 24, 24, 23, 26, 26, 20, 21, 21, 27, 27, 22, 25, 24, 17, 17, 19, 17, 21, 25, 21, 17, 27, 27, 21, 23, 17, 24, 25, 23, 20, 25, 21, 25, 27, 22, 19, 21, 26, 23, 19, 17, 24, 20, 27, 22, 26, 27, 25, 22, 21, 17, 24, 27, 25, 19, 22, 20, 20, 19, 21, 19, 22, 25, 17, 22, 19, 19, 18, 25, 25, 26, 22, 22, 23, 24, 19, 21, 26, 19, 25, 24, 23, 22, 18, 18, 27, 18, 20, 18, 18, 22, 24, 22, 20, 17, 24, 24, 27, 20, 19, 22, 21, 17, 22, 17, 19, 21, 23, 24, 17, 25, 26, 19, 23, 23, 19, 18, 19, 20, 18, 18, 27, 22, 18, 24, 21, 24, 27, 26, 20, 25, 19, 20, 18, 20, 17, 19, 19, 26, 19, 26, 23, 19, 20, 21, 26, 20, 20, 23, 19, 27, 18, 25, 25, 19, 27, 19, 25, 26, 17, 22, 27, 27, 22, 21, 21, 17, 19, 25, 20, 25, 22, 24, 24, 27, 23, 24, 17, 20, 18, 17, 25, 21, 26, 21, 21, 27, 26, 18, 23, 26, 25, 19, 24, 27, 26, 25, 22, 21, 21, 19, 23, 24, 17, 26, 23, 27, 27, 20, 20, 18, 20, 17, 20, 26, 20, 20, 17, 18, 18, 24, 25, 18, 20, 22, 23, 22, 18, 24, 20, 17, 24, 17, 26, 24, 19, 27, 26, 17, 26, 25, 25, 19, 19, 25, 19, 23, 18, 21, 25, 18, 23, 26, 19, 17, 24, 23, 24, 26, 18, 18, 26, 21, 20, 21, 22, 18, 26, 22, 18, 18, 23, 22, 24, 25, 19, 18, 25, 23, 22, 25, 23, 26, 27, 25, 23, 25, 26, 19, 17, 20, 17, 17, 26, 21, 23, 23, 17, 19, 21, 18, 24, 25, 18, 24, 25, 17, 20, 18, 17, 20, 19, 26, 24, 21, 20, 25, 20, 24, 24, 21, 19, 27, 22, 17, 17, 27, 26, 20, 18, 25, 24, 22, 19, 22, 20, 23, 18, 19, 21, 23, 17, 18, 25, 24, 27, 18, 24, 17, 20, 17, 27, 19, 27, 23, 17, 20, 26, 20, 27, 21, 18, 27, 24, 23, 19, 27, 24, 17, 17, 24, 19, 25, 20, 25, 20, 19, 17, 25, 19, 25, 26, 22, 23, 22, 23, 22, 22, 24, 26, 25, 27, 21, 22, 27, 22, 26, 23, 25, 21, 20, 23, 18, 21, 22, 17, 26, 19, 22, 17, 23, 24, 25, 19, 26, 17, 24, 26, 18, 21, 19, 24, 25, 27, 22, 26, 27, 20, 20, 18, 23, 23, 27, 27, 22, 26, 25, 17, 19, 26, 25, 27, 18, 24, 19, 27, 26, 22, 18, 27, 27, 22, 24, 17, 26, 27, 24, 21, 21, 17, 26, 21, 24, 22, 27, 17, 27, 22, 25, 22, 20, 24, 21, 21, 26, 27, 27, 21, 19, 20, 20, 25, 17, 21, 25, 22, 27, 26, 17, 24, 17, 19, 18, 25, 20, 18, 27, 25, 23, 24, 21, 27, 20, 19, 26, 23, 25, 23, 27, 17, 20, 18, 23, 18, 23, 21, 22, 23, 26, 18, 27, 22, 23, 24, 27, 20, 23, 19, 21, 22, 23, 21, 26, 27, 20, 23, 24, 25, 20, 25, 22, 27, 24, 22, 23, 20, 22, 23, 23, 25, 20, 22, 23, 17, 20, 26, 25, 23, 22, 27, 18, 22, 21, 21, 26, 18, 25, 24, 20, 17, 25, 18, 26, 19, 20, 26, 17, 17, 24, 18, 19, 18, 25, 23, 17, 22, 26, 18, 23, 20, 23, 22, 19, 23, 18, 19, 18, 23, 23, 25, 17, 19, 27, 24, 17, 19, 19, 20, 22, 19, 23, 21, 24, 18, 27, 26, 25, 26, 25, 25, 27, 19, 21, 17, 23, 22, 18, 18, 20, 18, 19, 18, 18, 25, 23, 25, 27, 22, 27, 20, 21, 21, 20, 25, 23, 18, 26, 20, 18, 22, 21, 17, 25, 20, 26, 24, 18, 27, 27, 18, 23, 26, 19, 17}; // 心率 static const char HeartBeatDataRandom[720] = { 82, 85, 86, 86, 74, 73, 83, 77, 83, 69, 86, 70, 83, 72, 73, 72, 90, 82, 85, 72, 82, 66, 84, 83, 69, 77, 76, 90, 66, 67, 77, 79, 66, 83, 73, 66, 66, 74, 68, 65, 85, 75, 89, 78, 79, 71, 74, 65, 74, 88, 72, 89, 71, 76, 77, 72, 76, 76, 74, 88, 87, 78, 67, 83, 69, 90, 83, 86, 70, 70, 79, 74, 80, 68, 65, 69, 71, 83, 69, 76, 77, 66, 90, 89, 84, 90, 72, 79, 65, 69, 77, 88, 89, 77, 70, 78, 88, 76, 76, 69, 72, 86, 73, 80, 81, 74, 83, 78, 89, 85, 65, 77, 83, 78, 71, 79, 81, 71, 84, 79, 66, 66, 79, 77, 67, 85, 81, 85, 79, 72, 71, 70, 70, 68, 81, 68, 81, 77, 83, 71, 66, 89, 82, 77, 79, 66, 83, 81, 77, 85, 79, 68, 82, 69, 88, 78, 69, 76, 81, 90, 85, 73, 76, 77, 68, 67, 81, 73, 71, 71, 83, 81, 71, 78, 78, 84, 80, 70, 89, 89, 90, 82, 76, 86, 75, 67, 80, 72, 81, 77, 71, 75, 90, 77, 75, 73, 86, 89, 70, 74, 74, 74, 80, 69, 71, 65, 84, 79, 73, 84, 66, 84, 86, 89, 85, 88, 69, 85, 79, 83, 81, 78, 86, 76, 65, 73, 70, 71, 70, 82, 68, 73, 71, 78, 87, 90, 83, 83, 65, 66, 74, 79, 82, 67, 68, 67, 66, 72, 72, 89, 82, 83, 88, 69, 79, 73, 82, 82, 84, 89, 67, 70, 65, 70, 65, 88, 84, 77, 68, 81, 73, 70, 68, 83, 80, 78, 75, 90, 72, 67, 90, 80, 81, 68, 69, 80, 79, 84, 70, 78, 69, 71, 67, 87, 88, 83, 69, 79, 82, 74, 68, 69, 85, 79, 65, 73, 73, 79, 84, 82, 86, 78, 71, 90, 89, 73, 69, 89, 80, 70, 88, 86, 73, 89, 65, 73, 79, 76, 68, 82, 86, 85, 89, 88, 65, 88, 84, 68, 82, 84, 88, 65, 82, 84, 89, 65, 69, 79, 76, 81, 74, 80, 81, 73, 65, 79, 80, 86, 75, 80, 84, 78, 68, 85, 81, 80, 74, 68, 86, 74, 67, 76, 69, 75, 68, 76, 66, 80, 77, 90, 82, 87, 87, 69, 65, 69, 70, 87, 80, 72, 69, 84, 90, 78, 73, 66, 84, 90, 68, 87, 74, 83, 65, 75, 81, 71, 77, 65, 81, 90, 79, 84, 74, 90, 76, 85, 79, 65, 86, 87, 81, 72, 82, 68, 71, 70, 73, 69, 86, 73, 74, 83, 69, 76, 71, 89, 70, 79, 76, 88, 83, 79, 84, 76, 74, 82, 73, 83, 70, 68, 65, 74, 82, 81, 87, 76, 82, 71, 82, 90, 70, 84, 90, 68, 88, 71, 66, 77, 84, 77, 76, 72, 73, 79, 71, 87, 76, 69, 82, 80, 90, 84, 88, 77, 86, 77, 67, 83, 86, 82, 75, 79, 78, 83, 81, 68, 85, 78, 86, 69, 86, 79, 66, 78, 88, 85, 72, 84, 82, 71, 66, 86, 80, 87, 71, 75, 77, 89, 88, 76, 73, 67, 86, 68, 80, 68, 84, 82, 65, 74, 70, 67, 68, 72, 82, 78, 76, 77, 80, 78, 84, 84, 74, 81, 85, 82, 72, 72, 72, 65, 85, 66, 69, 78, 72, 77, 68, 70, 85, 79, 89, 74, 86, 79, 67, 86, 89, 69, 80, 77, 85, 65, 77, 68, 65, 66, 87, 69, 72, 90, 85, 76, 75, 81, 80, 75, 72, 86, 84, 85, 87, 87, 81, 84, 89, 73, 84, 84, 87, 70, 83, 65, 80, 68, 81, 81, 67, 82, 67, 89, 68, 70, 65, 67, 72, 71, 65, 78, 70, 78, 84, 85, 86, 76, 71, 80, 90, 71, 70, 84, 86, 69, 65, 80, 76, 76, 70, 86, 85, 80, 83, 76, 80, 66, 82, 79, 65, 83, 67, 74, 88, 80, 79, 70, 75, 79, 65, 69, 83, 83, 70, 89, 85, 85, 76, 88, 75, 71, 79, 86, 87, 90, 76, 67, 84, 72, 74, 68, 76, 90, 82, 77, 66, 90, 70, 73, 73, 77, 68, 82, 83, 84, 70, 75, 66, 87, 85, 87, 85, 87, 68, 79, 65, 89, 84, 75, 87, 73, 75, 88, 89, 85, 67, 74, 81, 85, 87, 75, 69, 68}; // 体动 static const char MoveDataRandom[720] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 3, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 4, 4, 0}; /* 睡眠报告 { "date":"2023-2-2", //日期 "score":88, //得分 "eiff":90, //效率 "rollOver":8, //翻身 "maxheart":8, //最大心率 "minheart":8, //最小心率 "maxrespiratory":8, //最大呼吸 "minrespiratory":8, //最小呼吸 "sleepTime":"1675385275", //开始睡觉时间 * "wakeUpTime":"1676385275" //起床时间 * } */ ///////////////////////睡眠阶段////////////////////////////// enum SleepStatus_enum { SLEEP_PHASE_WAKE, // 睡眠阶段——清醒 SLEEP_PHASE_SLEEP, // 睡眠阶段——浅睡 SLEEP_PHASE_REM, // 睡眠阶段——眼动 SLEEP_PHASE_DEEP, // 睡眠阶段——深睡 SLEEP_PHASE_AWAY // 睡眠阶段——离开 }; //////////////////////////////////////////////////////////// typedef struct sleepReport_T { char SleepState; // 睡眠阶段 char SleepAge; // 年龄用于计算得分 float DeepSleepTime; // 深度睡眠时长 // 内部计算 int InBedSumMinute; // 在床时长(20:00之后开始计算) char SleepWakeFlag; // 清醒标志位 float body_move_avg; // 体动计数平均值 float sleep_delay; // 睡眠延时 float deep_delay; // 深度睡眠延时 uint16_t wakeTime_cnt; // 清醒计数 uint16_t body_move_cnt; // 体动计数 uint16_t non_body_move_cnt; // 无体动计数 uint16_t body_move_sum; // 总体动计数 uint16_t MinuteTime_cnt; // 分钟累加 // 数据输入 float move_avg; // 平均体动 float breath_avg; // 平均呼吸 float heartbeat_avg; // 平均心率 uint64_t InBed_TimeStamp; // 在床时间戳(UTC) uint64_t Asleep_TimeStamp; // 入睡时间戳(UTC) uint64_t AwayBed_TimeStamp; // 离床时间戳(UTC) uint64_t AsleepAddZone_TimeStamp; // 在床时间戳(UTC+Zone) // 数据输出 char max_heartbeat; // 最大心率 char min_heartbeat; // 最小心率 char max_breath; // 最大呼吸 char min_breath; // 最小呼吸 char roll_over; // 翻身次数 float SleepEfficiency; // 睡眠效率 float SleepScore; // 睡眠得分 uint16_t rem_cnt; // REM计时 uint16_t sleep_cnt; // 睡眠计时 uint16_t deep_cnt; // 深度睡眠计时 uint16_t SleepSumTime; // 实际睡眠总时长 // 输出指针 char *SleepStatusBuf_p; // 睡眠指针 char *HeartRateBuf_p; // 心率指针 char *MovementBuf_p; // 体动指针 } sleepReport_t; /////////////////////// ////////////////////////////// #define ANA_MOVE_CNT_WAKE 4 // 体动计时阈值 #define ANA_INT_MOVE_WAKE 100 // 上一版本数值错误, 体动清醒阈值 #define ANA_INT_MOVE_SLEEP 5 // 上一版本缺少该数值 #define ANA_MOVE_DELAY_SLEEP 5 #define ANA_WAKE_TO_SLEEP_TIME 3 /////////////////////// ////////////////////////////// #define ANA_MOVE_DELAY_DEEP 5 #define ANA_DEEP_SLEEP_HEART_OFFSET 0 #define ANA_DELAY_SLEEP_TO_DEEP 5 /////////////////////// ////////////////////////////// #define ANA_REM_HEART_OFFSET 2 #define ANA_DELAY_DEEP_TO_REM 5 /////////////////////// ////////////////////////////// #define ANA_SLEEPSCORE_WAKE_FACTOR 1 #define ANA_SLEEPSCORE_SLEEP_FACTOR 10 #define ANA_SLEEPSCORE_DEEP_FACTOR 16 #define ANA_SLEEPSCORE_REM_FACTOR 14 #define ANA_SLEEPSCORE_SNORE_FACTOR -8 #define ANA_START_WAKE_TO_SLEEP_TIME 10 #define EE_HEADER_MIN_REC_TIME 15 void SleepReportGenerate(uint64_t AwayFromBed_TimeStamp, uint16_t CountNum, char Resend); float SleepReportScore(char age); float SleepReportEfficiency(void); void ClearReportBuffer(void); #endif