-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtimeManager.cpp
More file actions
125 lines (95 loc) · 2.65 KB
/
timeManager.cpp
File metadata and controls
125 lines (95 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "timeManager.h"
#include <time.h>
#include "userBoardDefines.h"
#ifdef M5STACK
#include <M5StickCPlus.h>
#endif
#ifdef GENERIC_ESP32
#include <Arduino.h>
#endif
const long gmtOffset_sec = 0;
const int daylightOffset_sec = -3600 * 3;
const char* ntpServer = "pool.ntp.org";
void timeManagerbegin() {
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
#ifdef M5STACK
M5.Rtc.begin();
syncRTCToNTP();
#endif
}
#ifdef M5STACK
void syncRTCToNTP() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
return;
}
RTC_TimeTypeDef currentTime;
currentTime.Hours = timeinfo.tm_hour;
currentTime.Minutes = timeinfo.tm_min;
currentTime.Seconds = timeinfo.tm_sec;
RTC_DateTypeDef currentDate;
currentDate.Year = timeinfo.tm_year + 1900;
currentDate.Month = timeinfo.tm_mon + 1;
currentDate.Date = timeinfo.tm_mday;
M5.Rtc.SetTime(¤tTime);
M5.Rtc.SetData(¤tDate);
}
#endif
time_t getTimestampFromRTC() {
#ifdef M5STACK
RTC_TimeTypeDef currentTime = getTime();
RTC_DateTypeDef currentDate = getDate();
struct tm timeinfo;
timeinfo.tm_year = currentDate.Year + 1900;
timeinfo.tm_mon = currentDate.Month - 1;
timeinfo.tm_mday = currentDate.Date;
timeinfo.tm_hour = currentTime.Hours;
timeinfo.tm_min = currentTime.Minutes;
timeinfo.tm_sec = currentTime.Seconds;
return mktime(&timeinfo);
#endif
#ifdef GENERIC_ESP32
struct tm timeinfo;
getLocalTime(&timeinfo);
time_t timestamp = mktime(&timeinfo);
return timestamp;
#endif
}
BlockClockDateAndTime getDateAndTime() {
#ifdef M5STACK
RTC_TimeTypeDef currentTime = getTime();
RTC_DateTypeDef currentDate = getDate();
BlockClockDateAndTime currentDateAndTime;
currentDateAndTime.year = currentDate.Year;
currentDateAndTime.month = currentDate.Month;
currentDateAndTime.day = currentDate.Date;
currentDateAndTime.hour = currentTime.Hours;
currentDateAndTime.minutes = currentTime.Minutes;
currentDateAndTime.seconds = currentTime.Seconds;
return currentDateAndTime;
#endif
#ifdef GENERIC_ESP32
struct tm timeinfo;
getLocalTime(&timeinfo);
BlockClockDateAndTime currentDateAndTime;
currentDateAndTime.year = timeinfo.tm_year + 1900;
currentDateAndTime.month = timeinfo.tm_mon + 1;
currentDateAndTime.day = timeinfo.tm_mday;
currentDateAndTime.hour = timeinfo.tm_hour;
currentDateAndTime.minutes = timeinfo.tm_min;
currentDateAndTime.seconds = timeinfo.tm_sec;
return currentDateAndTime;
#endif
}
#ifdef M5STACK
RTC_TimeTypeDef getTime() {
RTC_TimeTypeDef timeNow;
M5.Rtc.GetTime(&timeNow);
return timeNow;
}
RTC_DateTypeDef getDate() {
RTC_DateTypeDef dateNow;
M5.Rtc.GetData(&dateNow);
return dateNow;
}
#endif