#include "alarm.h"
#include "oled.h"
#include "motion.h"
#include "audio.h"
// 闹钟全局变量
static ALARM_TimeTypeDef alarm_time = {7, 0}; // 默认闹钟时间:7:00
static ALARM_WakeModeTypeDef wake_mode = {1, 10, 1, 20, 1, 5}; // 默认唤醒模式:全部开启
static uint8_t alarm_enabled = 0; // 闹钟是否启用
static uint8_t alarm_triggered = 0; // 闹钟是否触发
// 当前时间
static TIME_TimeTypeDef current_time = {12, 0, 0}; // 默认当前时间:12:00:00
// 闹钟初始化
void ALARM_Init(void)
{
// 初始化RTC(实时时钟)
RTC_Init();
}
// 检查闹钟是否应该触发
uint8_t ALARM_Check(void)
{
if(!alarm_enabled || alarm_triggered) return 0;
// 更新当前时间
TIME_Update();
// 检查是否到达闹钟时间
if(current_time.hour == alarm_time.hour &&
current_time.minute == alarm_time.minute &&
current_time.second == 0)
{
alarm_triggered = 1;
return 1;
}
return 0;
}
// 切换闹钟启用/禁用状态
void ALARM_Toggle(void)
{
alarm_enabled = !alarm_enabled;
}
// 设置闹钟时间
void ALARM_Set(u8 key)
{
static u8 set_index = 0; // 0-小时,1-分钟
if(key == KEY1_PRES)
{
// 切换设置项
set_index = (set_index + 1) % 2;
}
else if(key == KEY2_PRES)
{
// 增加数值
if(set_index == 0) // 小时
{
alarm_time.hour = (alarm_time.hour + 1) % 24;
}
else // 分钟
{
alarm_time.minute = (alarm_time.minute + 1) % 60;
}
}
else if(key == KEY3_PRES)
{
// 减少数值
if(set_index == 0) // 小时
{
alarm_time.hour = (alarm_time.hour - 1 + 24) % 24;
}
else // 分钟
{
alarm_time.minute = (alarm_time.minute - 1 + 60) % 60;
}
}
// 显示设置界面
OLED_Clear();
OLED_ShowString(0, 0, "Set Alarm Time", 16);
OLED_ShowString(40, 2, "Hour", 12);
OLED_ShowString(90, 2, "Minute", 12);
OLED_ShowNum(45, 4, alarm_time.hour, 2, 16);
OLED_ShowNum(95, 4, alarm_time.minute, 2, 16);
// 显示光标
if(set_index == 0)
OLED_ShowString(40, 5, "__", 12);
else
OLED_ShowString(90, 5, "__", 12);
OLED_Refresh_Gram();
}
// 设置唤醒模式
void ALARM_SetWakeMode(u8 key)
{
static u8 set_index = 0; // 0-摇晃模式,1-步数模式,2-深蹲模式
if(key == KEY1_PRES)
{
// 切换设置项
set_index = (set_index + 1) % 3;
}
else if(key == KEY2_PRES)
{
// 切换启用/禁用
if(set_index == 0)
wake_mode.shake_enabled = !wake_mode.shake_enabled;
else if(set_index == 1)
wake_mode.step_enabled = !wake_mode.step_enabled;
else
wake_mode.squat_enabled = !wake_mode.squat_enabled;
}
else if(key == KEY3_PRES)
{
// 增加目标数量
if(set_index == 0 && wake_mode.shake_enabled)
wake_mode.shake_count = (wake_mode.shake_count + 1) % 50;
else if(set_index == 1 && wake_mode.step_enabled)
wake_mode.step_count = (wake_mode.step_count + 1) % 50;
else if(set_index == 2 && wake_mode.squat_enabled)
wake_mode.squat_count = (wake_mode.squat_count + 1) % 30;
}
// 显示设置界面
OLED_Clear();
OLED_ShowString(0, 0, "Set Wake Mode", 16);
// 摇晃模式
OLED_ShowString(10, 2, "Shake:", 12);
if(wake_mode.shake_enabled)
OLED_ShowString(50, 2, "ON", 12);
else
OLED_ShowString(50, 2, "OFF", 12);
OLED_ShowString(70, 2, "Count:", 12);
OLED_ShowNum(110, 2, wake_mode.shake_count, 2, 12);
// 步数模式
OLED_ShowString(10, 4, "Steps:", 12);
if(wake_mode.step_enabled)
OLED_ShowString(50, 4, "ON", 12);
else
OLED_ShowString(50, 4, "OFF", 12);
OLED_ShowString(70, 4, "Count:", 12);
OLED_ShowNum(110, 4, wake_mode.step_count, 2, 12);
// 深蹲模式
OLED_ShowString(10, 6, "Squats:", 12);
if(wake_mode.squat_enabled)
OLED_ShowString(50, 6, "ON", 12);
else
OLED_ShowString(50, 6, "OFF", 12);
OLED_ShowString(70, 6, "Count:", 12);
OLED_ShowNum(110, 6, wake_mode.squat_count, 2, 12);
// 显示光标
switch(set_index)
{
case 0: OLED_ShowString(5, 2, ">", 12); break;
case 1: OLED_ShowString(5, 4, ">", 12); break;
case 2: OLED_ShowString(5, 6, ">", 12); break;
}
OLED_Refresh_Gram();
}
// 检查是否满足唤醒条件
uint8_t ALARM_CheckWakeCondition(void)
{
uint8_t result = 1;
// 检查摇晃条件
if(wake_mode.shake_enabled && MOTION_GetShakeCount() < wake_mode.shake_count)
result = 0;
// 检查步数条件
if(wake_mode.step_enabled && MOTION_GetStepCount() < wake_mode.step_count)
result = 0;
// 检查深蹲条件
if(wake_mode.squat_enabled && MOTION_GetSquatCount() < wake_mode.squat_count)
result = 0;
// 如果满足所有条件,重置闹钟触发状态
if(result)
{
alarm_triggered = 0;
}
return result;
}
// 显示闹钟状态
void ALARM_DisplayStatus(void)
{
// 显示当前时间
OLED_ShowString(40, 0, "Time:", 12);
OLED_ShowNum(80, 0, current_time.hour, 2, 12);
OLED_ShowString(96, 0, ":", 12);
OLED_ShowNum(104, 0, current_time.minute, 2, 12);
OLED_ShowString(120, 0, ":", 12);
OLED_ShowNum(128, 0, current_time.second, 2, 12);
// 显示闹钟状态
OLED_ShowString(10, 2, "Alarm:", 12);
if(alarm_enabled)
{
OLED_ShowString(50, 2, "ON", 12);
OLED_ShowNum(70, 2, alarm_time.hour, 2, 12);
OLED_ShowString(86, 2, ":", 12);
OLED_ShowNum(94, 2, alarm_time.minute, 2, 12);
}
else
{
OLED_ShowString(50, 2, "OFF", 12);
}
// 显示唤醒模式
OLED_ShowString(10, 4, "Wake Mode:", 12);
uint8_t mode_count = 0;
if(wake_mode.shake_enabled) mode_count++;
if(wake_mode.step_enabled) mode_count++;
if(wake_mode.squat_enabled) mode_count++;
OLED_ShowNum(80, 4, mode_count, 1, 12);
OLED_ShowString(90, 4, " modes", 12);
}
// 显示唤醒进度
void ALARM_ShowWakeProgress(void)
{
// 显示各唤醒模式的进度
uint8_t y_pos = 4;
if(wake_mode.shake_enabled)
{
OLED_ShowString(10, y_pos, "Shake:", 12);
OLED_ShowNum(60, y_pos, MOTION_GetShakeCount(), 2, 12);
OLED_ShowString(80, y_pos, "/", 12);
OLED_ShowNum(90, y_pos, wake_mode.shake_count, 2, 12);
y_pos += 1;
}
if(wake_mode.step_enabled)
{
OLED_ShowString(10, y_pos, "Steps:", 12);
OLED_ShowNum(60, y_pos, MOTION_GetStepCount(), 2, 12);
OLED_ShowString(80, y_pos, "/", 12);
OLED_ShowNum(90, y_pos, wake_mode.step_count, 2, 12);
y_pos += 1;
}
if(wake_mode.squat_enabled)
{
OLED_ShowString(10, y_pos, "Squats:", 12);
OLED_ShowNum(60, y_pos, MOTION_GetSquatCount(), 2, 12);
OLED_ShowString(80, y_pos, "/", 12);
OLED_ShowNum(90, y_pos, wake_mode.squat_count, 2, 12);
}
}
// 时间显示
void TIME_Display(void)
{
// 更新时间
TIME_Update();
// 显示时间
OLED_ShowString(40, 2, "Current Time", 16);
OLED_ShowNum(60, 4, current_time.hour, 2, 24);
OLED_ShowString(100, 4, ":", 24);
OLED_ShowNum(110, 4, current_time.minute, 2, 24);
}
// 设置时间
void TIME_Set(u8 key)
{
static u8 set_index = 0; // 0-小时,1-分钟,2-秒
if(key == KEY1_PRES)
{
// 切换设置项
set_index = (set_index + 1) % 3;
}
else if(key == KEY2_PRES)
{
// 增加数值
if(set_index == 0) // 小时
{
current_time.hour = (current_time.hour + 1) % 24;
}
else if(set_index == 1) // 分钟
{
current_time.minute = (current_time.minute + 1) % 60;
}
else // 秒
{
current_time.second = (current_time.second + 1) % 60;
}
}
else if(key == KEY3_PRES)
{
// 减少数值
if(set_index == 0) // 小时
{
current_time.hour = (current_time.hour - 1 + 24) % 24;
}
else if(set_index == 1) // 分钟
{
current_time.minute = (current_time.minute - 1 + 60) % 60;
}
else // 秒
{
current_time.second = (current_time.second - 1 + 60) % 60;
}
}
// 更新RTC时间
if(key == KEY2_PRES || key == KEY3_PRES)
{
RTC_SetTime(current_time.hour, current_time.minute, current_time.second);
}
// 显示设置界面
OLED_Clear();
OLED_ShowString(0, 0, "Set Time", 16);
OLED_ShowString(40, 2, "Hour", 12);
OLED_ShowString(90, 2, "Minute", 12);
OLED_ShowString(40, 4, "Second", 12);
OLED_ShowNum(45, 3, current_time.hour, 2, 16);
OLED_ShowNum(95, 3, current_time.minute, 2, 16);
OLED_ShowNum(45, 5, current_time.second, 2, 16);
// 显示光标
if(set_index == 0)
OLED_ShowString(40, 6, "__", 12);
else if(set_index == 1)
OLED_ShowString(90, 6, "__", 12);
else
OLED_ShowString(40, 7, "__", 12);
OLED_Refresh_Gram();
}
// 更新时间
void TIME_Update(void)
{
// 从RTC获取时间
RTC_GetTime(¤t_time.hour, ¤t_time.minute, ¤t_time.second);
}
// RTC初始化(简化版)
void RTC_Init(void)
{
// 实际项目中应包含完整的RTC初始化代码
// 这里仅做简化处理
}
// 设置RTC时间(简化版)
void RTC_SetTime(u8 hour, u8 minute, u8 second)
{
// 实际项目中应包含完整的RTC时间设置代码
// 这里仅做简化处理
}
// 获取RTC时间(简化版)
void RTC_GetTime(u8 *hour, u8 *minute, u8 *second)
{
// 实际项目中应包含完整的RTC时间获取代码
// 这里仅做简化处理
static uint32_t counter = 0;
counter++;
if(counter >= 1000) // 模拟1秒
{
counter = 0;
current_time.second = (current_time.second + 1) % 60;
if(current_time.second == 0)
{
current_time.minute = (current_time.minute + 1) % 60;
if(current_time.minute == 0)
{
current_time.hour = (current_time.hour + 1) % 24;
}
}
}
*hour = current_time.hour;
*minute = current_time.minute;
*second = current_time.second;
}这是我的alarm.c代码出现了以下报错Hardwore\alarm.c(70): error: #20: identifier "KEY3_PRES" is undefined
else if(key == KEY3_PRES)
Hardwore\alarm.c(120): error: #20: identifier "KEY3_PRES" is undefined
else if(key == KEY3_PRES)
Hardwore\alarm.c(308): error: #20: identifier "KEY3_PRES" is undefined
else if(key == KEY3_PRES)
Hardwore\alarm.c(326): error: #20: identifier "KEY3_PRES" is undefined
if(key ==
EY2_PRES || key == KEY3_PRES)
Hardwore\alarm.c: 0 warnings, 4 errors
最新发布