TT12-MCU/applications/alarmer.c

305 lines
6.6 KiB
C
Raw Normal View History

2023-06-24 02:20:01 +00:00
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-06-19 murmur the first version
*/
/*
** RTC 使
** alarm_sample
** alarm_sample
** RTC时间
**/
#include <rtthread.h>
#include <rtdevice.h>
#define LOG_TAG "alarm"
#define LOG_LVL LOG_LVL_DBG
#include <ulog.h>
//#define BOOT_PER_HOUR
2023-06-24 02:20:01 +00:00
void user_alarm_callback(rt_alarm_t alarm, time_t timestamp)
{
rt_kprintf("user alarm callback function.\n");
}
void alarm_sample(int argc, char *argv[])
{
// rt_device_t dev = rt_device_find("rtc");
2023-06-24 02:20:01 +00:00
struct rt_alarm_setup setup;
struct rt_alarm * alarm = RT_NULL;
static time_t now;
struct tm p_tm;
if (alarm != RT_NULL)
return;
/* 获取当前时间戳,并把下一秒时间设置为闹钟时间 */
now = time(NULL) + 10;
gmtime_r(&now,&p_tm);
setup.flag = RT_ALARM_DAILY;
setup.wktime.tm_year = p_tm.tm_year;
setup.wktime.tm_mon = p_tm.tm_mon;
setup.wktime.tm_mday = p_tm.tm_mday;
setup.wktime.tm_wday = p_tm.tm_wday;
setup.wktime.tm_hour = atoi(argv[1]);//p_tm.tm_hour;
setup.wktime.tm_min = 0;//p_tm.tm_min;
setup.wktime.tm_sec = 0;//p_tm.tm_sec;
alarm = rt_alarm_create(user_alarm_callback, &setup);
2023-06-24 02:20:01 +00:00
if(RT_NULL != alarm)
{
rt_alarm_start(alarm);
LOG_D("alarm started.");
}
}
/* export msh cmd */
MSH_CMD_EXPORT(alarm_sample,alarm sample);
2023-09-03 11:45:08 +00:00
/**
*
* @param alarm
* @param timestamp
*/
void poTT_callback(rt_alarm_t alarm, time_t timestamp)
{
LOG_I("power UP TT by RTC.");
// timerIsReady();
setWindowMode();
initTT();
}
2023-09-03 11:45:08 +00:00
/**
*
* @param alarm
* @param timestamp
*/
void pdTT_callback(rt_alarm_t alarm, time_t timestamp)
{
LOG_I("power DOWN TT by RTC.");
clearWindowMode();
deInitTT();
//预置开窗会更新多次尝试逻辑,未激活时会始终尝试
}
2023-09-03 11:45:08 +00:00
/**
*
* @param alarm
* @param timestamp
*/
void poTT2_callback(rt_alarm_t alarm, time_t timestamp)
{
// LOG_I("power UP TT by INTERVAL RTC");
// timerIsReady();
// initTT();
}
/**
* debug文件名
* @param alarm
* @param timestamp
*/
void updatelog_callback(rt_alarm_t alarm, time_t timestamp)
{
updateDebuglogName();
}
void addDefaultAlarm()
{
struct rt_alarm_setup setup;
struct rt_alarm * alarm = RT_NULL;
static time_t now;
struct tm p_tm;
/* 获取当前时间戳,并把下一秒时间设置为闹钟时间 */
now = time(NULL);
gmtime_r(&now, &p_tm);
setup.flag = RT_ALARM_HOUR;//小时重复
setup.wktime.tm_year = p_tm.tm_year;
setup.wktime.tm_mon = p_tm.tm_mon;
setup.wktime.tm_mday = p_tm.tm_mday;
setup.wktime.tm_wday = p_tm.tm_wday;
setup.wktime.tm_hour = 0; //整点
setup.wktime.tm_min = 0; //p_tm.tm_min;
setup.wktime.tm_sec = 1; //p_tm.tm_sec;
if (RT_NULL != alarm)
{
rt_alarm_delete(alarm); //
}
alarm = rt_alarm_create(updatelog_callback, &setup);
if (alarm != RT_NULL) {
rt_alarm_start(alarm);
// LOG_D("alarm %d(%02d:%02d) started.",var+1,alarm[var]->wktime.tm_hour+8,alarm[var]->wktime.tm_min);
}
else {
LOG_E("Failed to start alarm.");
}
}
#define BOOT_PER_HOUR
#ifdef BOOT_PER_HOUR
INIT_APP_EXPORT(addDefaultAlarm);
#endif
extern struct rt_alarm_container _container;
static rt_alarm_t a[50];
static size_t cnt = 0;
#ifdef BOOT_PER_HOUR
static size_t indexBegin=1;
#else
static size_t indexBegin=0;
#endif
/**
*RTC闹钟
*/
void getAllAlarm()
{
rt_list_t *next;
rt_alarm_t alarm;
cnt = 0;
for (next = _container.head.next; next != &_container.head; next = next->next)
{
alarm = rt_list_entry(next, struct rt_alarm, list);
a[cnt++] = alarm;
}
}
/**
* RTC闹钟
*/
void clearAlarm(void)
{
getAllAlarm();
for (size_t i = 0; i < cnt-indexBegin; ++i)
{
rt_alarm_delete(a[i]);
}
}
/**
* rtc闹钟
*/
void stopAlarm()
{
getAllAlarm();
for (size_t i = 0; i < cnt-indexBegin; ++i)
{
rt_alarm_stop(a[i]);
}
}
/**
* rtc闹钟
*/
void startAlarm()
{
getAllAlarm();
for (size_t i = 0; i < cnt-indexBegin; ++i)
{
rt_alarm_start(a[i]);
}
}
void addAlarm(uint8_t *din, rt_alarm_callback_t callback)
{
struct rt_alarm_setup setup;
struct rt_alarm * alarm = RT_NULL;
static time_t now;
struct tm p_tm;
if (alarm != RT_NULL)
return;
/* 获取当前时间戳,并把下一秒时间设置为闹钟时间 */
now = time(NULL) + 10;
gmtime_r(&now, &p_tm);
setup.flag = RT_ALARM_DAILY;
setup.wktime.tm_year = p_tm.tm_year;
setup.wktime.tm_mon = p_tm.tm_mon;
setup.wktime.tm_mday = p_tm.tm_mday;
setup.wktime.tm_wday = p_tm.tm_wday;
setup.wktime.tm_hour = din[0]; //p_tm.tm_hour;
setup.wktime.tm_min = din[1]; //p_tm.tm_min;
setup.wktime.tm_sec = 0; //p_tm.tm_sec;
alarm = rt_alarm_create(callback, &setup);
if (RT_NULL != alarm)
{
// rt_alarm_start(alarm);//默认不启动
// LOG_D("alarm started.");
}
else {
LOG_E("Failed to start alarm.");
}
}
/**
* rtc闹钟
* @param t rtc时刻组成的数组
*/
void updateAlarm(uint8_t *t, size_t len)
{
clearAlarm();
for (int var = 0; var < len; var+=4) {
addAlarm(t+var, poTT_callback);
addAlarm(t+var+2, pdTT_callback);
}
}
static int isInAlarm(rt_alarm_t stop, rt_alarm_t start)
{
struct tm alBegin = start->wktime;
alBegin.tm_hour += 8;
time_t tBegin = mktime(&alBegin);
struct tm alEnd = stop->wktime;
alEnd.tm_hour += 8;
time_t tEnd = mktime(&alEnd);
time_t now = time(RT_NULL);
int rst = (now >= tBegin) && (now <= tEnd);
return rst;
}
2023-07-27 01:34:00 +00:00
/**
* check if in the WINs zone
* @return 1-in,0-not in
*/
int isInWindowZone()
{
getAllAlarm();
2023-07-27 01:34:00 +00:00
int rst = 0;
for (size_t i = 0; i < cnt-indexBegin; i+=2)
2023-07-27 01:34:00 +00:00
{
if(isInAlarm(a[i], a[i+1]))
{
LOG_I("is in window zone.");
rst = 1;
break;
}
2023-07-27 01:34:00 +00:00
}
return rst;
2023-07-27 01:34:00 +00:00
}
MSH_CMD_EXPORT(clearAlarm,);
MSH_CMD_EXPORT(stopAlarm,stop);
MSH_CMD_EXPORT(startAlarm,start);
MSH_CMD_EXPORT(isInWindowZone,isInWindow);
2023-07-27 01:34:00 +00:00
//MSH_CMD_EXPORT(getCurrentTime,getCurrentTime);