forked from pixelit-project/PixelIt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tools.h
132 lines (118 loc) · 2.29 KB
/
Tools.h
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
/// <summary>
/// Adds a leading 0 to a number if it is smaller than 10
/// </summary>
String IntFormat(int _int)
{
if (_int < 10)
{
return "0" + String(_int);
}
return String(_int);
}
/// <summary>
/// Returns 1 hour in daylight saving time and outside 0
/// </summary>
int DSToffset(time_t _date, int _clockTimeZone)
{
bool _summerTime;
if (month(_date) < 3 || month(_date) > 10)
{
_summerTime = false; // no summertime in Jan, Feb, Nov, Dec
}
else if (month(_date) > 3 && month(_date) < 10) {
_summerTime = true; // Summertime in Apr, May, Jun, Jul, Aug, Sep
}
else if (month(_date) == 3 && (hour(_date) + 24 * day(_date)) >= (1 + String(_clockTimeZone).toInt() + 24 * (31 - (5 * year(_date) / 4 + 4) % 7)) || month(_date) == 10 && (hour(_date) + 24 * day(_date)) < (1 + String(_clockTimeZone).toInt() + 24 * (31 - (5 * year(_date) / 4 + 1) % 7)))
{
_summerTime = true;
}
else
{
_summerTime = false;
}
return _summerTime ? 1 : 0;
}
/// <summary>
/// Checks if it is a valid IP address
/// </summary>
boolean isIP(String _str) {
for (char i = 0; i < _str.length(); i++) {
if (!(isDigit(_str.charAt(i)) || _str.charAt(i) == '.')) {
return false;
}
}
return true;
}
/// <summary>
/// Convert UTF8 byte to ASCII
/// </summary>
byte Utf8ToAscii(byte _ascii) {
static byte _thisByte;
if (_ascii < 128)
{
_thisByte = 0;
return (_ascii);
}
byte last = _thisByte;
_thisByte = _ascii;
byte _result = 0;
switch (last)
{
case 0xC2:
_result = _ascii - 34;
break;
case 0xC3:
_result = (_ascii | 0xC0) - 34;
break;
case 0x82:
if (_ascii == 0xAC)
{
_result = (0xEA);
}
break;
}
return _result;
}
/// <summary>
/// Convert UTF8 Chars to ASCII
/// </summary>
String Utf8ToAscii(String _str) {
String _result = "";
char _thisChar;
for (int i = 0; i < _str.length(); i++)
{
_thisChar = Utf8ToAscii(_str.charAt(i));
if (_thisChar != 0)
{
_result += _thisChar;
}
}
return _result;
}
/// <summary>
/// Returns the chip id
/// </summary>
String GetChipID()
{
return String(ESP.getChipId());
}
/// <summary>
/// Convert RSSI to percentage quality
/// </summary>
int GetRSSIasQuality(int rssi)
{
int quality = 0;
if (rssi <= -100)
{
quality = 0;
}
else if (rssi >= -50)
{
quality = 100;
}
else
{
quality = 2 * (rssi + 100);
}
return quality;
}