Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of WIZwiki-7500_Blynk by
BlynkParam.h
00001 /** 00002 * @file BlynkParam.h 00003 * @author Volodymyr Shymanskyy 00004 * @license This project is released under the MIT License (MIT) 00005 * @copyright Copyright (c) 2015 Volodymyr Shymanskyy 00006 * @date Jan 2015 00007 * @brief Container for handler parameters 00008 * 00009 */ 00010 00011 #ifndef BlynkParam_h 00012 #define BlynkParam_h 00013 00014 #include <string.h> 00015 #include <stdlib.h> 00016 #include <Blynk/BlynkConfig.h> 00017 #include <Blynk/BlynkDebug.h> 00018 00019 #define BLYNK_PARAM_KV(k, v) k "\0" v "\0" 00020 00021 class BlynkParam 00022 { 00023 public: 00024 class iterator 00025 { 00026 public: 00027 iterator(char* c) : ptr(c) {} 00028 static iterator invalid() { return iterator(NULL); } 00029 00030 operator const char* () const { return asStr(); } 00031 operator int () const { return asInt(); } 00032 const char* asStr() const { return ptr; } 00033 const char* asString() const { return ptr; } 00034 int asInt() const { return atoi(ptr); } 00035 long asLong() const { return atol(ptr); } 00036 //long long asLongLong() const { return atoll(ptr); } 00037 #ifndef BLYNK_NO_FLOAT 00038 double asDouble() const { return atof(ptr); } 00039 float asFloat() const { return atof(ptr); } 00040 #endif 00041 bool isValid() const { return ptr != NULL; } 00042 bool isEmpty() const { return *ptr == '\0'; } 00043 00044 bool operator < (const iterator& it) const { return ptr < it.ptr; } 00045 bool operator >= (const iterator& it) const { return ptr >= it.ptr; } 00046 00047 iterator& operator ++() { 00048 ptr += strlen(ptr)+1; 00049 return *this; 00050 } 00051 private: 00052 const char* ptr; 00053 }; 00054 00055 public: 00056 explicit 00057 BlynkParam(void* addr, size_t length) 00058 : buff((char*)addr), len(length), buff_size(length) 00059 {} 00060 00061 explicit 00062 BlynkParam(void* addr, size_t length, size_t buffsize) 00063 : buff((char*)addr), len(length), buff_size(buffsize) 00064 {} 00065 00066 const char* asStr() const { return buff; } 00067 const char* asString() const { return buff; } 00068 int asInt() const { return atoi(buff); } 00069 long asLong() const { return atol(buff); } 00070 //long long asLongLong() const { return atoll(buff); } 00071 #ifndef BLYNK_NO_FLOAT 00072 double asDouble() const { return atof(buff); } 00073 float asFloat() const { return atof(buff); } 00074 #endif 00075 bool isEmpty() const { return *buff == '\0'; } 00076 00077 iterator begin() const { return iterator(buff); } 00078 iterator end() const { return iterator(buff+len); } 00079 00080 iterator operator[](int index) const; 00081 iterator operator[](const char* key) const; 00082 00083 void* getBuffer() const { return (void*)buff; } 00084 size_t getLength() const { return len; } 00085 00086 // Modification 00087 void add(int value); 00088 void add(unsigned int value); 00089 void add(long value); 00090 void add(unsigned long value); 00091 void add(long long value); 00092 void add(unsigned long long value); 00093 #ifndef BLYNK_NO_FLOAT 00094 void add(float value); 00095 void add(double value); 00096 #endif 00097 void add(const char* str); 00098 void add(const void* b, size_t l); 00099 #if defined(ARDUINO) || defined(SPARK) || defined(PARTICLE) 00100 void add(const String& str); 00101 #if defined(BLYNK_HAS_PROGMEM) 00102 void add(const __FlashStringHelper* str); 00103 #endif 00104 #endif 00105 00106 template<typename T, typename... Args> 00107 void add_multi(T last) { 00108 add(last); 00109 } 00110 00111 template<typename T, typename... Args> 00112 void add_multi(T head, Args... tail) { 00113 add(head); 00114 add_multi(tail...); 00115 } 00116 00117 template <typename TV> 00118 void add_key(const char* key, const TV& val) { 00119 add(key); 00120 add(val); 00121 } 00122 00123 protected: 00124 char* buff; 00125 size_t len; 00126 size_t buff_size; 00127 }; 00128 00129 00130 class BlynkParamAllocated 00131 : public BlynkParam 00132 { 00133 public: 00134 BlynkParamAllocated(size_t size) 00135 : BlynkParam(malloc(size), 0, size) 00136 {} 00137 ~BlynkParamAllocated() { 00138 free(buff); 00139 } 00140 }; 00141 00142 inline 00143 BlynkParam::iterator BlynkParam::operator[](int index) const 00144 { 00145 const iterator e = end(); 00146 for (iterator it = begin(); it < e; ++it) { 00147 if (!index--) { 00148 return it; 00149 } 00150 } 00151 return iterator::invalid(); 00152 } 00153 00154 inline 00155 BlynkParam::iterator BlynkParam::operator[](const char* key) const 00156 { 00157 const iterator e = end(); 00158 for (iterator it = begin(); it < e; ++it) { 00159 if (!strcmp(it.asStr(), key)) { 00160 return ++it; 00161 } 00162 ++it; 00163 if (it >= e) break; 00164 } 00165 return iterator::invalid(); 00166 } 00167 00168 inline 00169 void BlynkParam::add(const void* b, size_t l) 00170 { 00171 if (len + l > buff_size) 00172 return; 00173 memcpy(buff+len, b, l); 00174 len += l; 00175 } 00176 00177 inline 00178 void BlynkParam::add(const char* str) 00179 { 00180 add(str, strlen(str)+1); 00181 } 00182 00183 #if defined(ARDUINO) || defined(SPARK) || defined(PARTICLE) 00184 inline 00185 void BlynkParam::add(const String& str) 00186 { 00187 #if defined(ARDUINO_AVR_DIGISPARK) \ 00188 || defined(__ARDUINO_X86__) \ 00189 || defined(__RFduino__) 00190 00191 size_t len = str.length()+1; 00192 char buff[len]; 00193 const_cast<String&>(str).toCharArray(buff, len); 00194 add(buff, len); 00195 #else 00196 add(str.c_str()); 00197 #endif 00198 } 00199 00200 #if defined(BLYNK_HAS_PROGMEM) 00201 00202 inline 00203 void BlynkParam::add(const __FlashStringHelper* ifsh) 00204 { 00205 PGM_P p = reinterpret_cast<PGM_P>(ifsh); 00206 size_t l = strlen_P(p) + 1; 00207 if (len + l > buff_size) 00208 return; 00209 memcpy_P(buff+len, p, l); 00210 len += l; 00211 buff[len] = '\0'; 00212 } 00213 00214 #endif 00215 00216 #endif 00217 00218 #if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32) 00219 00220 #include <stdlib.h> 00221 00222 inline 00223 void BlynkParam::add(int value) 00224 { 00225 char str[2 + 8 * sizeof(value)]; 00226 itoa(value, str, 10); 00227 add(str); 00228 } 00229 00230 inline 00231 void BlynkParam::add(unsigned int value) 00232 { 00233 char str[1 + 8 * sizeof(value)]; 00234 utoa(value, str, 10); 00235 add(str); 00236 } 00237 00238 inline 00239 void BlynkParam::add(long value) 00240 { 00241 char str[2 + 8 * sizeof(value)]; 00242 ltoa(value, str, 10); 00243 add(str); 00244 } 00245 00246 inline 00247 void BlynkParam::add(unsigned long value) 00248 { 00249 char str[1 + 8 * sizeof(value)]; 00250 ultoa(value, str, 10); 00251 add(str); 00252 } 00253 00254 inline 00255 void BlynkParam::add(long long value) // TODO: this currently adds just a long 00256 { 00257 char str[2 + 8 * sizeof(value)]; 00258 ltoa(value, str, 10); 00259 add(str); 00260 } 00261 00262 inline 00263 void BlynkParam::add(unsigned long long value) // TODO: this currently adds just a long 00264 { 00265 char str[1 + 8 * sizeof(value)]; 00266 ultoa(value, str, 10); 00267 add(str); 00268 } 00269 00270 #ifndef BLYNK_NO_FLOAT 00271 00272 inline 00273 void BlynkParam::add(float value) 00274 { 00275 char str[33]; 00276 dtostrf(value, 5, 3, str); 00277 add(str); 00278 } 00279 00280 inline 00281 void BlynkParam::add(double value) 00282 { 00283 char str[33]; 00284 dtostrf(value, 5, 7, str); 00285 add(str); 00286 } 00287 #endif 00288 00289 #else 00290 00291 #include <stdio.h> 00292 00293 inline 00294 void BlynkParam::add(int value) 00295 { 00296 len += snprintf(buff+len, buff_size-len, "%i", value)+1; 00297 } 00298 00299 inline 00300 void BlynkParam::add(unsigned int value) 00301 { 00302 len += snprintf(buff+len, buff_size-len, "%u", value)+1; 00303 } 00304 00305 inline 00306 void BlynkParam::add(long value) 00307 { 00308 len += snprintf(buff+len, buff_size-len, "%li", value)+1; 00309 } 00310 00311 inline 00312 void BlynkParam::add(unsigned long value) 00313 { 00314 len += snprintf(buff+len, buff_size-len, "%lu", value)+1; 00315 } 00316 00317 inline 00318 void BlynkParam::add(long long value) 00319 { 00320 len += snprintf(buff+len, buff_size-len, "%lli", value)+1; 00321 } 00322 00323 inline 00324 void BlynkParam::add(unsigned long long value) 00325 { 00326 len += snprintf(buff+len, buff_size-len, "%llu", value)+1; 00327 } 00328 00329 #ifndef BLYNK_NO_FLOAT 00330 00331 #if defined(ESP8266) 00332 00333 extern char* dtostrf_internal(double number, signed char width, unsigned char prec, char *s); 00334 00335 inline 00336 void BlynkParam::add(float value) 00337 { 00338 char str[33]; 00339 dtostrf_internal(value, 5, 3, str); 00340 add(str); 00341 } 00342 00343 inline 00344 void BlynkParam::add(double value) 00345 { 00346 char str[33]; 00347 dtostrf_internal(value, 5, 7, str); 00348 add(str); 00349 } 00350 00351 #else 00352 00353 inline 00354 void BlynkParam::add(float value) 00355 { 00356 len += snprintf(buff+len, buff_size-len, "%2.3f", value)+1; 00357 } 00358 00359 inline 00360 void BlynkParam::add(double value) 00361 { 00362 len += snprintf(buff+len, buff_size-len, "%2.7f", value)+1; 00363 } 00364 00365 #endif 00366 00367 #endif 00368 00369 #endif 00370 00371 00372 #endif
Generated on Thu Jul 14 2022 00:21:50 by
1.7.2
