-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cpp
More file actions
132 lines (116 loc) · 2.6 KB
/
Utils.cpp
File metadata and controls
132 lines (116 loc) · 2.6 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
126
127
128
129
130
131
132
#include "Utils.h"
#include <WiFi.h>
#include <WiFiMulti.h>
#include "Config.h"
namespace SA::Utils
{
void InitializeSerial()
{
Serial.begin(115200);
while (!Serial) {}
}
WiFiMulti InitializeWiFi()
{
WiFiMulti WiFiMulti;
WiFi.mode(WIFI_STA);
WiFiMulti.addAP(SA::Config::c_wifiNetworkName, SA::Config::c_wifiPassword);
Serial.print("Waiting for WiFi to connect...");
while ((WiFiMulti.run() != WL_CONNECTED))
{
Serial.print(".");
}
Serial.println(" connected");
return WiFiMulti;
}
void InitializeClock()
{
configTime(0, 0, "pool.ntp.org");
Serial.print(F("Waiting for NTP time sync: "));
time_t nowSecs = time(nullptr);
while (nowSecs < 8 * 3600 * 2)
{
delay(500);
Serial.print(F("."));
yield();
nowSecs = time(nullptr);
}
Serial.println();
struct tm timeinfo;
gmtime_r(&nowSecs, &timeinfo);
Serial.print(F("Current time: "));
char buf[26];
Serial.print(asctime_r(&timeinfo, buf));
}
bool StrCmpIgnoreCase(const char* l, const char* r)
{
for(int offset = 0;; ++offset)
{
const char lChar = *(l + offset);
const char rChar = *(r + offset);
if (std::tolower(lChar) != std::tolower(rChar))
{
return false;
}
if (lChar == '\0')
{
return true;
}
}
return false;
}
template<>
std::optional<const char*> ParseValue<const char*>(const char* str)
{
return str;
}
template<>
std::optional<int> ParseValue<int>(const char* str)
{
char *endptr;
int result = strtol(str, &endptr, 10);
if (endptr == str)
{
return {};
}
return result;
}
template<>
std::optional<bool> ParseValue<bool>(const char* str)
{
if (const std::optional<int> asInt = ParseValue<int>(str))
{
return *asInt > 0;
}
else if (const std::optional<const char*> asStr = ParseValue<const char*>(str))
{
if (SA::Utils::StrCmpIgnoreCase(*asStr, "true"))
{
return true;
}
else if (SA::Utils::StrCmpIgnoreCase(*asStr, "false"))
{
return false;
}
else if (SA::Utils::StrCmpIgnoreCase(*asStr, "on"))
{
return true;
}
else if (SA::Utils::StrCmpIgnoreCase(*asStr, "off"))
{
return false;
}
}
return {};
}
template<>
std::optional<float> ParseValue<float>(const char* str)
{
char *endptr;
float result = strtof(str, &endptr);
if (endptr == str)
{
return {};
}
return result;
}
}