Hendrik Langer
7 years ago
6 changed files with 190 additions and 98 deletions
@ -0,0 +1,112 @@ |
|||
#include "AlarmClock.h" |
|||
|
|||
AlarmClock::AlarmClock() { |
|||
|
|||
} |
|||
|
|||
bool AlarmClock::update(void) { |
|||
// time_t now;
|
|||
// struct tm timeinfo;
|
|||
time(&now); // update 'now' variable with current time
|
|||
setenv("TZ", "CET-1CEST,M3.5.0/2,M10.5.0/3", 1); |
|||
tzset(); |
|||
localtime_r(&now, &timeinfo); |
|||
} |
|||
|
|||
bool AlarmClock::isTimeValid(void) { |
|||
update(); |
|||
|
|||
if (timeinfo.tm_year < (2016 - 1900)) { |
|||
return false; // time not set
|
|||
} |
|||
return true; |
|||
} |
|||
|
|||
bool AlarmClock::isNight(void) { |
|||
update(); |
|||
return (timeinfo.tm_hour < 7 || timeinfo.tm_hour >= 22); |
|||
} |
|||
|
|||
bool AlarmClock::isAlarmArmed(void) { |
|||
return alarmArmed; |
|||
} |
|||
|
|||
double AlarmClock::getSecondsToAlarm(void) { |
|||
update(); |
|||
return -difftime(now, mktime(&alarmTime)); |
|||
} |
|||
|
|||
bool AlarmClock::isPending(double window) { |
|||
double seconds = getSecondsToAlarm(); |
|||
return (alarmArmed && seconds <= window); |
|||
} |
|||
|
|||
void AlarmClock::setAlarmTime(struct tm time) { |
|||
alarmTime = time; |
|||
alarmArmed = true; |
|||
} |
|||
|
|||
void AlarmClock::disableAlarm(void) { |
|||
alarmArmed = false; |
|||
} |
|||
|
|||
struct tm AlarmClock::getAlarmTime() { |
|||
return alarmTime; |
|||
} |
|||
|
|||
void AlarmClock::printTimeUnix(whichTime type) { |
|||
char strftime_buf[64]; |
|||
getTime(strftime_buf, sizeof(strftime_buf), "%c", type); |
|||
Serial.println(strftime_buf); |
|||
} |
|||
|
|||
void AlarmClock::getTime(char* ptr, size_t maxsize, const char* format, whichTime type) { |
|||
update(); |
|||
|
|||
switch (type) { |
|||
case TIME_CURRENT: |
|||
strftime(ptr, maxsize, format, &timeinfo); |
|||
break; |
|||
case TIME_ALARM: |
|||
strftime(ptr, maxsize, format, &alarmTime); |
|||
break; |
|||
default: |
|||
break; |
|||
} |
|||
} |
|||
|
|||
/*
|
|||
bool AlarmClock::getRTCTime(struct tm *timeinfo) { |
|||
time_t now; |
|||
time(&now); // update 'now' variable with current time
|
|||
setenv("TZ", "CET-1CEST,M3.5.0/2,M10.5.0/3", 1); |
|||
tzset(); |
|||
localtime_r(&now, timeinfo); |
|||
|
|||
if (timeinfo->tm_year < (2016 - 1900)) { |
|||
// time not set
|
|||
return false; |
|||
} |
|||
return true; |
|||
} |
|||
*/ |
|||
|
|||
bool AlarmClock::updateNTPTime(void) { |
|||
sntp_setoperatingmode(SNTP_OPMODE_POLL); |
|||
sntp_setservername(0, "de.pool.ntp.org"); |
|||
sntp_init(); |
|||
|
|||
// wait for time to be set
|
|||
time_t now = 0; |
|||
struct tm timeinfo = { 0 }; |
|||
int retry = 0; |
|||
const int retry_count = 10; |
|||
while(timeinfo.tm_year < (2016 - 1900) && ++retry < retry_count) { |
|||
ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count); |
|||
vTaskDelay(2000 / portTICK_PERIOD_MS); |
|||
time(&now); |
|||
localtime_r(&now, &timeinfo); |
|||
} |
|||
if (timeinfo.tm_year < (2016 - 1900)) return false; |
|||
else return true; |
|||
} |
@ -0,0 +1,38 @@ |
|||
#ifndef _ALARMCLOCK_H |
|||
#define _ALARMCLOCK_H |
|||
|
|||
#include <Arduino.h> |
|||
|
|||
#include <time.h> |
|||
#include <sys/time.h> |
|||
#include "apps/sntp/sntp.h" |
|||
|
|||
#include "driver/rtc_io.h" |
|||
|
|||
enum whichTime { TIME_CURRENT, TIME_ALARM }; |
|||
|
|||
RTC_DATA_ATTR static struct tm alarmTime; |
|||
RTC_DATA_ATTR static bool alarmArmed; |
|||
|
|||
class AlarmClock { |
|||
public: |
|||
AlarmClock(); |
|||
bool isPending(double); |
|||
bool isAlarmArmed(void); |
|||
void setAlarmTime(struct tm time); |
|||
void disableAlarm(void); |
|||
struct tm getAlarmTime(void); |
|||
void getTime(char* ptr, size_t maxsize, const char* format = "%H:%M:%S", whichTime type = TIME_CURRENT); |
|||
bool isTimeValid(void); |
|||
bool isNight(void); |
|||
bool getRTCTime(struct tm *timeinfo); |
|||
bool updateNTPTime(void); |
|||
double getSecondsToAlarm(void); |
|||
void printTimeUnix(whichTime); |
|||
private: |
|||
bool update(void); |
|||
time_t now; |
|||
struct tm timeinfo; |
|||
}; |
|||
|
|||
#endif /* _ALARMCLOCK_H */ |
Loading…
Reference in new issue