-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlynkParam.h
executable file
·372 lines (310 loc) · 8.12 KB
/
BlynkParam.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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/**
* @file BlynkParam.h
* @author Volodymyr Shymanskyy
* @license This project is released under the MIT License (MIT)
* @copyright Copyright (c) 2015 Volodymyr Shymanskyy
* @date Jan 2015
* @brief Container for handler parameters
*
*/
#ifndef BlynkParam_h
#define BlynkParam_h
#include <string.h>
#include <stdlib.h>
#include "BlynkConfig.h"
#include "BlynkDebug.h"
#define BLYNK_PARAM_KV(k, v) k "\0" v "\0"
class BlynkParam
{
public:
class iterator
{
public:
iterator(char* c) : ptr(c) {}
static iterator invalid() { return iterator(NULL); }
operator const char* () const { return asStr(); }
operator int () const { return asInt(); }
const char* asStr() const { return ptr; }
const char* asString() const { return ptr; }
int asInt() const { return atoi(ptr); }
long asLong() const { return atol(ptr); }
//long long asLongLong() const { return atoll(ptr); }
#ifndef BLYNK_NO_FLOAT
double asDouble() const { return atof(ptr); }
float asFloat() const { return atof(ptr); }
#endif
bool isValid() const { return ptr != NULL; }
bool isEmpty() const { return *ptr == '\0'; }
bool operator < (const iterator& it) const { return ptr < it.ptr; }
bool operator >= (const iterator& it) const { return ptr >= it.ptr; }
iterator& operator ++() {
ptr += strlen(ptr)+1;
return *this;
}
private:
const char* ptr;
};
public:
explicit
BlynkParam(void* addr, size_t length)
: buff((char*)addr), len(length), buff_size(length)
{}
explicit
BlynkParam(void* addr, size_t length, size_t buffsize)
: buff((char*)addr), len(length), buff_size(buffsize)
{}
const char* asStr() const { return buff; }
const char* asString() const { return buff; }
int asInt() const { return atoi(buff); }
long asLong() const { return atol(buff); }
//long long asLongLong() const { return atoll(buff); }
#ifndef BLYNK_NO_FLOAT
double asDouble() const { return atof(buff); }
float asFloat() const { return atof(buff); }
#endif
bool isEmpty() const { return *buff == '\0'; }
iterator begin() const { return iterator(buff); }
iterator end() const { return iterator(buff+len); }
iterator operator[](int index) const;
iterator operator[](const char* key) const;
void* getBuffer() const { return (void*)buff; }
size_t getLength() const { return len; }
// Modification
void add(int value);
void add(unsigned int value);
void add(long value);
void add(unsigned long value);
void add(long long value);
void add(unsigned long long value);
#ifndef BLYNK_NO_FLOAT
void add(float value);
void add(double value);
#endif
void add(const char* str);
void add(const void* b, size_t l);
#if defined(ARDUINO) || defined(SPARK) || defined(PARTICLE)
void add(const String& str);
#if defined(BLYNK_HAS_PROGMEM)
void add(const __FlashStringHelper* str);
#endif
#endif
template<typename T, typename... Args>
void add_multi(T last) {
add(last);
}
template<typename T, typename... Args>
void add_multi(T head, Args... tail) {
add(head);
add_multi(tail...);
}
template <typename TV>
void add_key(const char* key, const TV& val) {
add(key);
add(val);
}
protected:
char* buff;
size_t len;
size_t buff_size;
};
class BlynkParamAllocated
: public BlynkParam
{
public:
BlynkParamAllocated(size_t size)
: BlynkParam(malloc(size), 0, size)
{}
~BlynkParamAllocated() {
free(buff);
}
};
inline
BlynkParam::iterator BlynkParam::operator[](int index) const
{
const iterator e = end();
for (iterator it = begin(); it < e; ++it) {
if (!index--) {
return it;
}
}
return iterator::invalid();
}
inline
BlynkParam::iterator BlynkParam::operator[](const char* key) const
{
const iterator e = end();
for (iterator it = begin(); it < e; ++it) {
if (!strcmp(it.asStr(), key)) {
return ++it;
}
++it;
if (it >= e) break;
}
return iterator::invalid();
}
inline
void BlynkParam::add(const void* b, size_t l)
{
if (len + l > buff_size)
return;
memcpy(buff+len, b, l);
len += l;
}
inline
void BlynkParam::add(const char* str)
{
add(str, strlen(str)+1);
}
#if defined(ARDUINO) || defined(SPARK) || defined(PARTICLE)
inline
void BlynkParam::add(const String& str)
{
#if defined(ARDUINO_AVR_DIGISPARK) \
|| defined(__ARDUINO_X86__) \
|| defined(__RFduino__)
size_t len = str.length()+1;
char buff[len];
const_cast<String&>(str).toCharArray(buff, len);
add(buff, len);
#else
add(str.c_str());
#endif
}
#if defined(BLYNK_HAS_PROGMEM)
inline
void BlynkParam::add(const __FlashStringHelper* ifsh)
{
PGM_P p = reinterpret_cast<PGM_P>(ifsh);
size_t l = strlen_P(p) + 1;
if (len + l > buff_size)
return;
memcpy_P(buff+len, p, l);
len += l;
buff[len] = '\0';
}
#endif
#endif
#if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32)
#include <stdlib.h>
inline
void BlynkParam::add(int value)
{
char str[2 + 8 * sizeof(value)];
itoa(value, str, 10);
add(str);
}
inline
void BlynkParam::add(unsigned int value)
{
char str[1 + 8 * sizeof(value)];
utoa(value, str, 10);
add(str);
}
inline
void BlynkParam::add(long value)
{
char str[2 + 8 * sizeof(value)];
ltoa(value, str, 10);
add(str);
}
inline
void BlynkParam::add(unsigned long value)
{
char str[1 + 8 * sizeof(value)];
ultoa(value, str, 10);
add(str);
}
inline
void BlynkParam::add(long long value) // TODO: this currently adds just a long
{
char str[2 + 8 * sizeof(value)];
ltoa(value, str, 10);
add(str);
}
inline
void BlynkParam::add(unsigned long long value) // TODO: this currently adds just a long
{
char str[1 + 8 * sizeof(value)];
ultoa(value, str, 10);
add(str);
}
#ifndef BLYNK_NO_FLOAT
inline
void BlynkParam::add(float value)
{
char str[33];
dtostrf(value, 5, 3, str);
add(str);
}
inline
void BlynkParam::add(double value)
{
char str[33];
dtostrf(value, 5, 3, str);
add(str);
}
#endif
#else
#include <stdio.h>
inline
void BlynkParam::add(int value)
{
len += snprintf(buff+len, buff_size-len, "%i", value)+1;
}
inline
void BlynkParam::add(unsigned int value)
{
len += snprintf(buff+len, buff_size-len, "%u", value)+1;
}
inline
void BlynkParam::add(long value)
{
len += snprintf(buff+len, buff_size-len, "%li", value)+1;
}
inline
void BlynkParam::add(unsigned long value)
{
len += snprintf(buff+len, buff_size-len, "%lu", value)+1;
}
inline
void BlynkParam::add(long long value)
{
len += snprintf(buff+len, buff_size-len, "%lli", value)+1;
}
inline
void BlynkParam::add(unsigned long long value)
{
len += snprintf(buff+len, buff_size-len, "%llu", value)+1;
}
#ifndef BLYNK_NO_FLOAT
#if defined(ESP8266)
extern char* dtostrf_internal(double number, signed char width, unsigned char prec, char *s);
inline
void BlynkParam::add(float value)
{
char str[33];
dtostrf_internal(value, 5, 3, str);
add(str);
}
inline
void BlynkParam::add(double value)
{
char str[33];
dtostrf_internal(value, 5, 3, str);
add(str);
}
#else
inline
void BlynkParam::add(float value)
{
len += snprintf(buff+len, buff_size-len, "%2.3f", value)+1;
}
inline
void BlynkParam::add(double value)
{
len += snprintf(buff+len, buff_size-len, "%2.3f", value)+1;
}
#endif
#endif
#endif
#endif