-
Notifications
You must be signed in to change notification settings - Fork 0
/
RTC.ino
executable file
·168 lines (147 loc) · 3.84 KB
/
RTC.ino
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "rtc.h"
/**
* @brief
* @param none
* @return none
*/
auto rtc::init() -> void
{
#if defined GET_OPTA_OTP_BOARD_INFO
info = boardInfo();
if (info->magic == 0xB5)
{
for (auto i = 0; i < 6; i++)
{
mainCore.byMacAddress[i] = info->mac_address[i];
}
}
if (Ethernet.begin(mainCore.byMacAddress) == 0)
{
Serial.println("Failed to configure Ethernet using DHCP");
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware)
{
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
}
else if (Ethernet.linkStatus() == LinkOFF)
{
Serial.println("Ethernet cable is not connected.");
}
}
ntpUdp.begin(PORT);
delay(1000);
timeClient.begin();
Serial.println("Time Client started");
updateTime();
#elif
Serial.println("No Opta Board Info, could not Init RTC");
#endif
}
/**
* @brief
* Checks whether day has changed
* -> if yes, return true
* @param xSettDayChangeActive
* @return bool
*/
auto rtc::rtcCheckDayChange(bool xSettDayChangeActive) -> bool
{
static uint16_t uiDateOld = 0; // needed for detecting whether day has changed
if (!xSettDayChangeActive)
{
return false;
}
// if xSettDayChangeActive is true, check whether day has changed
else
{
auto uiDayActual = getTimeData(timeOption::DAY_OF_YEAR, debugInformations::ACTIVE);
if (uiDayActual != uiDateOld)
{
uiDateOld = uiDayActual;
return true;
}
else
return false;
}
}
/**
* @brief Updates internal RTC of Arduino Opta by using NTPClient
*
* @param info
*/
auto rtc::updateTime(debugInformations info) -> void
{
timeClient.update();
const uint32_t epoch = timeClient.getEpochTime();
set_time(epoch);
if (info == debugInformations::ACTIVE)
{
sOptaLog = mainCore.debugMessenger("UTC Time: " + String(getLocalTime()));
sOptaLog = mainCore.debugMessenger("Unix Time: " + String(epoch));
}
}
/**
* @brief Time as String in format "hh:mm:ss"
*
* @param none
* @return Opta Time as String in format "hh:mm:ss"
*/
auto rtc::getLocalTime() -> String
{
char buffer[32];
tm t; // tm is a structure defined in time.h
_rtc_localtime(time(NULL), &t, RTC_FULL_LEAP_YEAR_SUPPORT);
strftime(buffer, sizeof(buffer), "%k:%M:%S", &t);
return String(buffer);
}
/**
* @brief
*
* @param timeOption enum class of rtc class
* @param debugInformations enum class of rtc class
* @return uinz16_t
* /
**/
auto rtc::getTimeData(timeOption to, debugInformations debug) -> uint16_t
{
uint16_t uiValue = 0;
tm t;
_rtc_localtime(time(NULL), &t, RTC_FULL_LEAP_YEAR_SUPPORT);
switch (to)
{
case timeOption::SECONDS:
uiValue = static_cast<uint16_t>(t.tm_sec);
break;
case timeOption::MINUTES:
uiValue = static_cast<uint16_t>(t.tm_min);
break;
case timeOption::HOURS:
uiValue = static_cast<uint16_t>(t.tm_hour);
break;
case timeOption::DAY_OF_MONTH:
uiValue = static_cast<uint16_t>(t.tm_mday);
break;
case timeOption::MONTH:
uiValue = static_cast<uint16_t>(t.tm_mon);
break;
case timeOption::YEAR:
uiValue = static_cast<uint16_t>(t.tm_year);
break;
case timeOption::DAY_OF_WEEK:
uiValue = static_cast<uint16_t>(t.tm_wday);
break;
case timeOption::DAY_OF_YEAR:
uiValue = static_cast<uint16_t>(t.tm_yday);
break;
case timeOption::DAYLIGHT_SAVING_TIME:
uiValue = static_cast<uint16_t>(t.tm_isdst);
break;
default:
sOptaLog = mainCore.debugMessenger("No valid option passed");
break;
}
if (debug == debugInformations::ACTIVE)
{
sOptaLog = mainCore.debugMessenger(String(__func__) + "Value of selected time Option: " + String(uiValue));
}
}