forked from madmartin/Jarolift_MQTT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.h
135 lines (115 loc) · 3.86 KB
/
helpers.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
133
134
135
/* Controlling Jarolift TDEF 433MHZ radio shutters via ESP8266 and CC1101 Transceiver Module in asynchronous mode.
Copyright (C) 2017-2018 Steffen Hille et al.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef HELPERS_H
#define HELPERS_H
//####################################################################
// Check if the Value is between 0-255
//####################################################################
boolean checkRange(String Value)
{
if (Value.toInt() < 0 || Value.toInt() > 255)
{
return false;
}
else
{
return true;
}
} // boolean checkRange
//####################################################################
void WriteStringToEEPROM(int beginaddress, String string)
{
char charBuf[string.length() + 1];
string.toCharArray(charBuf, string.length() + 1);
for (int t = 0; t < sizeof(charBuf); t++)
{
EEPROM.write(beginaddress + t, charBuf[t]);
}
} // void WriteStringToEEPROM
//####################################################################
String ReadStringFromEEPROM(int beginaddress, int MaxLen)
{
byte counter = 0;
char rChar;
String retString = "";
while (1)
{
rChar = EEPROM.read(beginaddress + counter);
if (rChar == 0) break;
if (counter > MaxLen) break;
counter++;
retString.concat(rChar);
}
return retString;
} // String ReadStringFromEEPROM
//####################################################################
void EEPROMWritelong(int address, long value)
{
byte four = (value & 0xFF);
byte three = ((value >> 8) & 0xFF);
byte two = ((value >> 16) & 0xFF);
byte one = ((value >> 24) & 0xFF);
// Write the 4 bytes into the eeprom memory.
EEPROM.write(address, four);
EEPROM.write(address + 1, three);
EEPROM.write(address + 2, two);
EEPROM.write(address + 3, one);
} // void EEPROMWritelong
//####################################################################
long EEPROMReadlong(long address)
{
// Read the 4 bytes from the eeprom memory.
long four = EEPROM.read(address);
long three = EEPROM.read(address + 1);
long two = EEPROM.read(address + 2);
long one = EEPROM.read(address + 3);
// Return the recomposed long by using bitshift.
return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
} // long EEPROMReadlong
//####################################################################
// convert a single hex digit character to its integer value (from https://code.google.com/p/avr-netino/)
//####################################################################
unsigned char h2int(char c)
{
if (c >= '0' && c <= '9') {
return ((unsigned char)c - '0');
}
if (c >= 'a' && c <= 'f') {
return ((unsigned char)c - 'a' + 10);
}
if (c >= 'A' && c <= 'F') {
return ((unsigned char)c - 'A' + 10);
}
return (0);
} // unsigned char h2int
//####################################################################
String urldecode(String input) // (based on https://code.google.com/p/avr-netino/)
{
char c;
String ret = "";
for (byte t = 0; t < input.length(); t++)
{
c = input[t];
if (c == '+') c = ' ';
if (c == '%') {
t++;
c = input[t];
t++;
c = (h2int(c) << 4) | h2int(input[t]);
}
ret.concat(c);
}
return ret;
} // String urldecode
#endif