代码拉取完成,页面将自动刷新
同步操作将从 海石生风/ha102m 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
/*
* calendar.c
*
* Created on: 201863
* Author: chenss
*/
#include "calendar.h"
#define IS_LEAP_YEAR(year) (((year) % 400 == 0)||((year) % 4 == 0) && ((year) % 100 != 0))
static Calendar_DateTime_t _dateTime;
static uint8_t _weekOfYear;
static Calendar_UpdateCallback_t _updateCallback;
uint8_t _CalcWeekOfYear(Calendar_Date_t *date);
static void _SetAlarm(void);
void Calendar_Init(Calendar_UpdateCallback_t cb)
{
RTC_InitTypeDef RTC_InitStr;
RTC_TimeTypeDef RTC_TimeStr;
RTC_DateTypeDef RTC_DateStr;
Calendar_Date_t date;
_updateCallback = cb;
RTC_InitStr.RTC_HourFormat = RTC_HourFormat_24;
RTC_InitStr.RTC_AsynchPrediv = 0x7F;
RTC_InitStr.RTC_SynchPrediv = 0x00FF;
RTC_Init(&RTC_InitStr);
RTC_DateStructInit(&RTC_DateStr);
RTC_DateStr.RTC_WeekDay = RTC_Weekday_Friday;
RTC_DateStr.RTC_Date = 1;
RTC_DateStr.RTC_Month = RTC_Month_June;
RTC_DateStr.RTC_Year = 18;
RTC_SetDate(RTC_Format_BIN, &RTC_DateStr);
Calendar_GetDate(&date);
_weekOfYear = _CalcWeekOfYear(&date);
RTC_TimeStructInit(&RTC_TimeStr);
RTC_TimeStr.RTC_Hours = 12;
RTC_TimeStr.RTC_Minutes = 00;
RTC_TimeStr.RTC_Seconds = 00;
RTC_SetTime(RTC_Format_BIN, &RTC_TimeStr);
_SetAlarm();
}
static const uint8_t _daysOfYear[12] =
{
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
};
static const uint8_t _daysOfMonth[12] =
{
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
uint16_t _CalcDayOfYear(Calendar_Date_t *date)
{
uint16_t days;
uint16_t year;
days = _daysOfYear[date->month - 1] + date->day;
year = (uint16_t) 2000 + date->year;
if((date->month > 2) && IS_LEAP_YEAR(year))
{
days += 1;
}
return days;
}
uint8_t _CalcWeekOfYear(Calendar_Date_t *date)
{
uint16_t d, base, dayPassOfYear;
dayPassOfYear = _CalcDayOfYear(date) - 1;
d = dayPassOfYear % 7;
if(date->weekDay >= d)
{
base = date->weekDay - d;
}
else
{
base = (7 + date->weekDay) - d;
}
return 1 + ((base + dayPassOfYear) / 7);
}
void Calendar_GetDate(Calendar_Date_t *date)
{
RTC_DateTypeDef RTC_DateStruct;
RTC_GetDate(RTC_Format_BIN, &RTC_DateStruct);
date->day = RTC_DateStruct.RTC_Date;
date->month = RTC_DateStruct.RTC_Month;
date->year = RTC_DateStruct.RTC_Year;
date->weekDay = RTC_DateStruct.RTC_WeekDay - RTC_Weekday_Monday; // һΪ0
}
bool Calendar_SetDate(Calendar_Date_t *date)
{
RTC_DateTypeDef RTC_DateStruct;
bool ret;
RTC_DateStruct.RTC_Date = date->day;
RTC_DateStruct.RTC_Month = date->month;
RTC_DateStruct.RTC_Year = date->year;
ret = RTC_SetDate(RTC_Format_BIN, &RTC_DateStruct) == SUCCESS;
if(ret == SUCCESS)
{
_weekOfYear = _CalcWeekOfYear(date);
}
return ret;
}
void Calendar_GetTime(Calendar_Time_t *time)
{
RTC_TimeTypeDef RTC_TimeStruct;
RTC_GetTime(RTC_Format_BIN, &RTC_TimeStruct);
time->second = RTC_TimeStruct.RTC_Seconds;
time->minute = RTC_TimeStruct.RTC_Minutes;
time->hour = RTC_TimeStruct.RTC_Hours;
}
void Calendar_SetTime(Calendar_Time_t *time)
{
RTC_TimeTypeDef RTC_TimeStruct;
RTC_TimeStruct.RTC_Seconds = time->second;
RTC_TimeStruct.RTC_Minutes = time->minute;
RTC_TimeStruct.RTC_Hours = time->hour;
return RTC_SetTime(RTC_Format_BIN, &RTC_TimeStruct) == SUCCESS;
}
static void _SetAlarm(void)
{
RTC_AlarmTypeDef RTC_AlarmStr;
RTC_AlarmStructInit(&RTC_AlarmStr);
RTC_AlarmStr.RTC_AlarmTime.RTC_Hours = 00;
RTC_AlarmStr.RTC_AlarmTime.RTC_Minutes = 00;
RTC_AlarmStr.RTC_AlarmTime.RTC_Seconds = 00;
RTC_AlarmStr.RTC_AlarmMask = RTC_AlarmMask_Minutes;
RTC_SetAlarm(RTC_Format_BIN, &RTC_AlarmStr);
RTC_ITConfig(RTC_IT_ALRA, ENABLE);
RTC_AlarmCmd(ENABLE);
}
void Calendar_AlarmIrs(void)
{
_SetAlarm();
TASK_SET(TASK_Id_CalendarUpdate);
}
void Calendar_OnUpdate(void)
{
Calendar_DateTime_t dateTime;
Calendar_GetDate(& dateTime.date);
Calendar_GetTime(& dateTime.time);
if((dateTime.date.weekDay == 0) && (dateTime.time.hour == 0) && (dateTime.time.minute == 0))
{
_weekOfYear = _CalcWeekOfYear(& dateTime.date);
}
dateTime.weekOfYear = _weekOfYear;
_updateCallback(& dateTime);
}
uint8_t Calendar_MaxDayOfMonth(Calendar_Date_t *date)
{
uint8_t day;
day = _daysOfMonth[date->month - 1];
if(IS_LEAP_YEAR(date->year))
{
day ++;
}
return day;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。