-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFlowerPlatformArduinoRuntime.h
More file actions
258 lines (211 loc) · 5.3 KB
/
FlowerPlatformArduinoRuntime.h
File metadata and controls
258 lines (211 loc) · 5.3 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
* Author: Claudiu Matei
*/
#ifndef FlowerPlatformArduinoRuntime_h
#define FlowerPlatformArduinoRuntime_h
#include <Arduino.h>
#include <Print.h>
#include <stddef.h>
#ifdef ESP8266
extern "C" {
#include "user_interface.h"
}
#endif
#define DEBUG_FP 0
template <class E> class Callback {
public:
virtual void operator()(E* event) const = 0;
virtual ~Callback() { }
};
template <class T, class E> class DelegatingCallback : public Callback<E> {
protected:
T* instance;
void (T::*functionPointer)(E* event);
public:
DelegatingCallback(T* _instance, void (T::*_functionPointer)(E* event)) {
instance = _instance;
functionPointer = _functionPointer;
}
void operator()(E* event) const {
(*instance.*functionPointer)(event);
}
virtual ~DelegatingCallback() { }
};
class ValueChangedEvent {
public:
int previousValue;
int currentValue;
};
typedef void (*TDispatchFunction)(const char*, Print*);
char* __nextStringStart;
char* nextString(const char* s) {
if (s) {
__nextStringStart = (char*) s;
}
char* res = __nextStringStart;
__nextStringStart += strlen(__nextStringStart);
__nextStringStart++;
return res;
}
/**
* base on code from http://arduino.stackexchange.com/questions/18007/simple-url-decoding
*/
const char* decodeAndBreakUrl(const char* url) {
char *leader =(char*) url;
char *follower = leader;
// While we're not at the end of the string (current character not NULL)
while (*leader) {
if (*leader == '%') { // Check to see if the current character is a %
// Grab the next two characters and move leader forwards
leader++;
char high = *leader;
leader++;
char low = *leader;
// Convert ASCII 0-9A-F to a value 0-15
if (high > 0x39) high -= 7;
high &= 0x0f;
// Same again for the low byte:
if (low > 0x39) low -= 7;
low &= 0x0f;
// Combine the two into a single byte and store in follower:
*follower = (high << 4) | low;
} else if (*leader == '/') { // replace slashes with string terminator
*follower ='\0';
} else {
// All other characters copy verbatim
*follower = *leader;
}
// Move both pointers to the next character:
leader++;
follower++;
}
// Terminate the new string with a NULL character to trim it off
*follower = 0;
return url;
}
int freeRam() {
#ifdef ESP8266
return system_get_free_heap_size();
#elif SAMD21_SERIES
extern int __HeapLimit;
int v;
return (int) &v - (int) &__HeapLimit;
#elif __AVR_ARCH__
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
#else
return -1;
#endif
}
size_t write_P(Print* p, uint8_t* s, size_t size) {
uint8_t buf[16];
size_t k;
do {
k = size > 16 ? 16 : size;
memcpy_P(buf, s, k);
p->write(buf, k);
size -= k;
s += k;
} while (size);
return size;
}
size_t write_P(Print* p, const char* s) {
return write_P(p, (uint8_t*) s, strlen_P(s));
}
void parseBytes(const char* str, char sep, byte* bytes, int maxBytes, int base) {
for (int i = 0; i < maxBytes; i++) {
bytes[i] = strtoul(str, NULL, base); // Convert byte
str = strchr(str, sep); // Find next separator
if (str == NULL || *str == '\0') {
break; // No more separators, exit
}
str++; // Point to next character after separator
}
}
uint8_t b64_lookup(char c) {
if (c >= 'A' && c <= 'Z')
return c - 'A';
if (c >= 'a' && c <= 'z')
return c - 71;
if (c >= '0' && c <= '9')
return c + 4;
if (c == '+')
return 62;
if (c == '/')
return 63;
return -1;
}
int base64_decode(char* output, char* input, int inputLen) {
int i = 0, j = 0;
int decLen = 0;
unsigned char a3[3];
unsigned char a4[4];
while (inputLen--) {
if (*input == '=') {
break;
}
a4[i++] = *(input++);
if (i == 4) {
for (i = 0; i < 4; i++) {
a4[i] = b64_lookup(a4[i]);
}
a3[0] = (a4[0] << 2) + ((a4[1] & 0x30) >> 4);
a3[1] = ((a4[1] & 0xf) << 4) + ((a4[2] & 0x3c) >> 2);
a3[2] = ((a4[2] & 0x3) << 6) + a4[3];
for (i = 0; i < 3; i++) {
output[decLen++] = a3[i];
}
i = 0;
}
}
if (i) {
for (j = i; j < 4; j++) {
a4[j] = '\0';
}
for (j = 0; j < 4; j++) {
a4[j] = b64_lookup(a4[j]);
}
a3[0] = (a4[0] << 2) + ((a4[1] & 0x30) >> 4);
a3[1] = ((a4[1] & 0xf) << 4) + ((a4[2] & 0x3c) >> 2);
a3[2] = ((a4[2] & 0x3) << 6) + a4[3];
for (j = 0; j < i - 1; j++) {
output[decLen++] = a3[j];
}
}
output[decLen] = '\0';
return decLen;
}
void debug_println(const char* s1, const char* s2 = NULL) {
Serial.print("["); Serial.print(millis()); Serial.print("] ");
if (s1) {
Serial.print(s1);
}
if (s2) {
Serial.print(s2);
}
Serial.println();
}
void debug_println(const char* s1, int n, const char* s2 = NULL) {
Serial.print("["); Serial.print(millis()); Serial.print("] ");
if (s1) {
Serial.print(s1);
}
Serial.print(n);
if (s2) {
Serial.print(s2);
}
Serial.println();
}
void debug_printBuffer(uint8_t* buf, size_t size) {
Serial.print("["); Serial.print(millis()); Serial.print("] ");
for (size_t i = 0; i < size; i++) {
Serial.print((char) (buf[i]));
}
Serial.print("\t");
for (size_t i = 0; i < size; i++) {
Serial.print(buf[i]); Serial.print(".");
}
Serial.println();
}
#endif