238 lines
5.5 KiB
C
238 lines
5.5 KiB
C
#include "date.h"
|
||
#include "main.h"
|
||
|
||
#include "lcd_pwm.h"
|
||
|
||
// UI界面与系统后台的输入输出交互接口
|
||
|
||
// 获取屏幕亮度
|
||
u8 ui_getScreenBrightness(void) { return LCD_PwmGetPulse(); }
|
||
|
||
// 设置屏幕亮度
|
||
void ui_setScreenBrightness(u8 t) { LCD_PwmSetPulse(t); }
|
||
|
||
// 屏幕背光
|
||
void ui_setScreenBackLightPower(int power) { LCD_Backlight(power); }
|
||
|
||
// 获取自动亮度控制状态,关闭
|
||
static int g_ScreenAutoLight = 0;
|
||
int ui_getScreenAutoLight(void) { return g_ScreenAutoLight; }
|
||
int ui_setScreenAutoLight(int k) {
|
||
g_ScreenAutoLight = k;
|
||
return 0;
|
||
}
|
||
|
||
// 获取自动熄屏时间
|
||
static int g_offTime = 15;
|
||
int ui_getScreenOffTime(void) { return g_offTime; }
|
||
|
||
int ui_setScreenOffTime(int time) {
|
||
g_offTime = time;
|
||
return 0;
|
||
}
|
||
|
||
static u8 g_NfcMode = 0;
|
||
|
||
u8 ui_getNfcMode(void) { return g_NfcMode; }
|
||
|
||
void ui_setNfcMode(u8 t) { g_NfcMode = t; }
|
||
|
||
static u8 g_NfcPower = 0;
|
||
u8 ui_getNfcPower(void) { return g_NfcPower; }
|
||
|
||
void ui_setNfcPower(u8 t) { g_NfcPower = t; }
|
||
|
||
// 获取在卡模式的数据内容
|
||
char *ui_getNfcCardModeData(void) { return "this is NFC data"; }
|
||
|
||
// 月份数据表
|
||
static u8 const table_week[12] = {0, 3, 3, 6, 1, 4,
|
||
6, 2, 5, 0, 3, 5}; // 月修正数据表
|
||
// 获得现在是星期几
|
||
// 功能描述:输入公历日期得到星期(只允许1901-2099年)
|
||
// 输入参数:公历年月日
|
||
// 返回值:星期号 (取值1~7)
|
||
u8 date_get_week_by_day(u16 year, u8 month, u8 day) {
|
||
u16 temp2;
|
||
u8 yearH, yearL;
|
||
|
||
yearH = year / 100;
|
||
yearL = year % 100;
|
||
// 如果为21世纪,年份数加100
|
||
if (yearH > 19)
|
||
yearL += 100;
|
||
// 所过闰年数只算1900年之后的
|
||
temp2 = yearL + yearL / 4;
|
||
temp2 = temp2 % 7;
|
||
temp2 = temp2 + day + table_week[month - 1];
|
||
if (yearL % 4 == 0 && month < 3)
|
||
temp2--;
|
||
if (temp2 % 7 == 0)
|
||
return 7;
|
||
else
|
||
return (temp2 % 7);
|
||
}
|
||
|
||
// 设置日期
|
||
int date_rtc_set_date(int year, int month, int day) {
|
||
RTC_DateTypeDef rtc_date = {0};
|
||
rtc_date.RTC_Date = day;
|
||
rtc_date.RTC_Month = month;
|
||
rtc_date.RTC_Year = year % 100;
|
||
rtc_date.RTC_WeekDay = date_get_week_by_day(year, month, day);
|
||
RTC_SetDate(RTC_Format_BIN, &rtc_date);
|
||
return 0;
|
||
}
|
||
|
||
// 设置时间
|
||
int date_rtc_set_time(int hour, int min, int sec) {
|
||
RTC_TimeTypeDef rtc_time = {0};
|
||
rtc_time.RTC_Hours = hour % 24;
|
||
rtc_time.RTC_Minutes = min % 60;
|
||
rtc_time.RTC_Seconds = sec % 60;
|
||
RTC_SetTime(RTC_Format_BIN, &rtc_time);
|
||
return 0;
|
||
}
|
||
|
||
// 获取日期
|
||
int date_rtc_get_date(int *year, int *month, int *day) {
|
||
RTC_DateTypeDef rtc_date = {0};
|
||
RTC_GetDate(RTC_Format_BIN, &rtc_date);
|
||
*year = 2000 + rtc_date.RTC_Year;
|
||
*month = rtc_date.RTC_Month;
|
||
*day = rtc_date.RTC_Date;
|
||
return 0;
|
||
}
|
||
|
||
// 获取时间
|
||
int date_rtc_get_time(int *hour, int *min, int *sec) {
|
||
RTC_TimeTypeDef rtc_time = {0};
|
||
RTC_GetTime(RTC_Format_BIN, &rtc_time);
|
||
*hour = rtc_time.RTC_Hours;
|
||
*min = rtc_time.RTC_Minutes;
|
||
*sec = rtc_time.RTC_Seconds;
|
||
return 0;
|
||
}
|
||
|
||
// 平年的月份日期表
|
||
static const u8 mon_table[12] = {31, 28, 31, 30, 31, 30,
|
||
31, 31, 30, 31, 30, 31};
|
||
|
||
// 输入:年份
|
||
// 输出:该年份是不是闰年.1,是.0,不是
|
||
u8 date_is_leap_year(u16 year) {
|
||
if (year % 4 == 0) // 必须能被4整除
|
||
{
|
||
if (year % 100 == 0) {
|
||
if (year % 400 == 0)
|
||
return 1; // 如果以00结尾,还要能被400整除
|
||
else
|
||
return 0;
|
||
} else
|
||
return 1;
|
||
} else
|
||
return 0;
|
||
}
|
||
|
||
// 获取月份的天数
|
||
int date_get_month_day_max(int year, int month) {
|
||
if ((month > 12) || (month < 1))
|
||
return 0;
|
||
if (month != 2) {
|
||
return mon_table[month - 1];
|
||
} else {
|
||
return date_is_leap_year(year) + mon_table[month - 1];
|
||
}
|
||
}
|
||
|
||
// 以1970年1月1日为基准
|
||
// 1970~2099年为合法年份
|
||
|
||
int date_get_year_max(void) { return 2099; }
|
||
|
||
int date_get_year_min(void) { return 1970; }
|
||
|
||
// 取得下一天
|
||
void date_get_next_day(int *year, int *month, int *day) {
|
||
if (*day < date_get_month_day_max(*year, *month)) {
|
||
(*day)++;
|
||
} else {
|
||
(*day) = 1;
|
||
if (*month < 12) {
|
||
(*month)++;
|
||
} else {
|
||
(*month) = 1;
|
||
(*year)++;
|
||
}
|
||
}
|
||
}
|
||
|
||
// 取得上一天
|
||
void date_get_last_day(int *year, int *month, int *day) {
|
||
if (*day > 1) {
|
||
(*day)--;
|
||
} else {
|
||
if (*month > 1) {
|
||
(*month)--;
|
||
} else {
|
||
(*year)--;
|
||
(*month) = 12;
|
||
}
|
||
(*day) = date_get_month_day_max(*year, *month);
|
||
}
|
||
}
|
||
|
||
// 取得下n天
|
||
void date_get_next_n_day(int *year, int *month, int *day, int n) {
|
||
while (n--)
|
||
date_get_next_day(year, month, day);
|
||
}
|
||
|
||
// 取得上n天
|
||
void date_get_last_n_day(int *year, int *month, int *day, int n) {
|
||
while (n--)
|
||
date_get_last_day(year, month, day);
|
||
}
|
||
|
||
// 日历转换为秒钟,返回自纪元 Epoch(1970-01-01 00:00:00
|
||
// UTC)起经过的时间,以秒为单位
|
||
time_t calendar_to_sec(int syear, int smon, int sday, int hour, int min,
|
||
int sec) {
|
||
int t;
|
||
time_t seccount = 0;
|
||
|
||
if (syear < 1970 || syear > 2099)
|
||
return (time_t)-1;
|
||
|
||
for (t = 1970; t < syear; t++) // 把所有年份的秒钟相加
|
||
{
|
||
if (date_is_leap_year(t))
|
||
seccount += 31622400; // 闰年的秒钟数
|
||
else
|
||
seccount += 31536000; // 平年的秒钟数
|
||
}
|
||
smon -= 1;
|
||
for (t = 0; t < smon; t++) // 把前面月份的秒钟数相加
|
||
{
|
||
seccount += (int)mon_table[t] * 86400; // 月份秒钟数相加
|
||
if (date_is_leap_year(syear) && t == 1)
|
||
seccount += 86400; // 闰年2月份增加一天的秒钟数
|
||
}
|
||
seccount += (int)(sday - 1) * 86400; // 把前面日期的秒钟数相加
|
||
seccount += (int)hour * 3600; // 小时秒钟数
|
||
seccount += (int)min * 60; // 分钟秒钟数
|
||
seccount += sec; // 最后的秒钟加上去
|
||
|
||
return seccount;
|
||
}
|
||
|
||
/*--C语言库函数--*/
|
||
|
||
time_t time(time_t *sec_t) {
|
||
int year, month, day, hour, min, sec;
|
||
date_rtc_get_date(&year, &month, &day);
|
||
date_rtc_get_time(&hour, &min, &sec);
|
||
|
||
return calendar_to_sec(year, month, day, hour, min, sec);
|
||
}
|