The Cayenne MQTT mbed Library provides functions to easily connect to the Cayenne IoT project builder.
Dependents: Cayenne-ESP8266Interface Cayenne-WIZnet_Library Cayenne-WIZnetInterface Cayenne-X-NUCLEO-IDW01M1 ... more
src/CayenneUtils/CayenneDataArray.h@6:82e142a864ad, 2016-10-25 (annotated)
- Committer:
- jburhenn
- Date:
- Tue Oct 25 16:19:29 2016 -0600
- Revision:
- 6:82e142a864ad
- Parent:
- 1:90dccf306268
- Child:
- 8:aec9cfdd4c8e
Updates from Cayenne-MQTT-CPP repo.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jburhenn | 0:09ef59d2d0f7 | 1 | /* |
jburhenn | 0:09ef59d2d0f7 | 2 | The MIT License(MIT) |
jburhenn | 0:09ef59d2d0f7 | 3 | |
jburhenn | 0:09ef59d2d0f7 | 4 | Cayenne MQTT Client Library |
jburhenn | 0:09ef59d2d0f7 | 5 | Copyright (c) 2016 myDevices |
jburhenn | 0:09ef59d2d0f7 | 6 | |
jburhenn | 0:09ef59d2d0f7 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
jburhenn | 0:09ef59d2d0f7 | 8 | documentation files(the "Software"), to deal in the Software without restriction, including without limitation |
jburhenn | 0:09ef59d2d0f7 | 9 | the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, |
jburhenn | 0:09ef59d2d0f7 | 10 | and to permit persons to whom the Software is furnished to do so, subject to the following conditions : |
jburhenn | 0:09ef59d2d0f7 | 11 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
jburhenn | 0:09ef59d2d0f7 | 12 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
jburhenn | 0:09ef59d2d0f7 | 13 | WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR |
jburhenn | 0:09ef59d2d0f7 | 14 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
jburhenn | 0:09ef59d2d0f7 | 15 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
jburhenn | 0:09ef59d2d0f7 | 16 | */ |
jburhenn | 0:09ef59d2d0f7 | 17 | |
jburhenn | 0:09ef59d2d0f7 | 18 | #ifndef _CAYENNEDATAARRAY_h |
jburhenn | 0:09ef59d2d0f7 | 19 | #define _CAYENNEDATAARRAY_h |
jburhenn | 0:09ef59d2d0f7 | 20 | |
jburhenn | 0:09ef59d2d0f7 | 21 | #include "CayenneUtils.h" |
jburhenn | 0:09ef59d2d0f7 | 22 | |
jburhenn | 0:09ef59d2d0f7 | 23 | #if defined(__cplusplus) |
jburhenn | 0:09ef59d2d0f7 | 24 | |
jburhenn | 0:09ef59d2d0f7 | 25 | // C++ version of the data array. This is defined here so it requires no source file. |
jburhenn | 0:09ef59d2d0f7 | 26 | |
jburhenn | 0:09ef59d2d0f7 | 27 | namespace Cayenne |
jburhenn | 0:09ef59d2d0f7 | 28 | { |
jburhenn | 1:90dccf306268 | 29 | /** |
jburhenn | 1:90dccf306268 | 30 | * @class DataArray |
jburhenn | 1:90dccf306268 | 31 | * Class for manipulating a data array of unit/value pairs. |
jburhenn | 1:90dccf306268 | 32 | * @param BUFFER_SIZE Maximum buffer size to use for data array, in bytes. |
jburhenn | 1:90dccf306268 | 33 | * @param MAX_VALUES Maximum number of unit/value pairs in the array. |
jburhenn | 1:90dccf306268 | 34 | */ |
jburhenn | 0:09ef59d2d0f7 | 35 | template<int BUFFER_SIZE = CAYENNE_MAX_MESSAGE_SIZE, int MAX_VALUES = CAYENNE_MAX_MESSAGE_VALUES> |
jburhenn | 0:09ef59d2d0f7 | 36 | class DataArray |
jburhenn | 0:09ef59d2d0f7 | 37 | { |
jburhenn | 0:09ef59d2d0f7 | 38 | public: |
jburhenn | 1:90dccf306268 | 39 | /** |
jburhenn | 1:90dccf306268 | 40 | * Construct an empty array. |
jburhenn | 1:90dccf306268 | 41 | */ |
jburhenn | 0:09ef59d2d0f7 | 42 | DataArray() { |
jburhenn | 0:09ef59d2d0f7 | 43 | clear(); |
jburhenn | 0:09ef59d2d0f7 | 44 | } |
jburhenn | 0:09ef59d2d0f7 | 45 | |
jburhenn | 1:90dccf306268 | 46 | /** |
jburhenn | 1:90dccf306268 | 47 | * Clear the array. |
jburhenn | 1:90dccf306268 | 48 | */ |
jburhenn | 0:09ef59d2d0f7 | 49 | void clear() { |
jburhenn | 0:09ef59d2d0f7 | 50 | for (int i = 0; i < CAYENNE_MAX_MESSAGE_VALUES; ++i) { |
jburhenn | 0:09ef59d2d0f7 | 51 | _values[i].unit = NULL; |
jburhenn | 0:09ef59d2d0f7 | 52 | _values[i].value = NULL; |
jburhenn | 0:09ef59d2d0f7 | 53 | } |
jburhenn | 0:09ef59d2d0f7 | 54 | _valueCount = 0; |
jburhenn | 0:09ef59d2d0f7 | 55 | _index = 0; |
jburhenn | 0:09ef59d2d0f7 | 56 | } |
jburhenn | 0:09ef59d2d0f7 | 57 | |
jburhenn | 1:90dccf306268 | 58 | /** |
jburhenn | 1:90dccf306268 | 59 | * Add the specified unit/value pair to the array. |
jburhenn | 1:90dccf306268 | 60 | * @param[in] unit The unit to add. |
jburhenn | 1:90dccf306268 | 61 | * @param[in] value The value to add. |
jburhenn | 1:90dccf306268 | 62 | * @param[in] unitInFlash If true the unit string is in flash memory, otherwise false. |
jburhenn | 1:90dccf306268 | 63 | * @param[in] valueInFlash If true the value string is in flash memory, otherwise false. |
jburhenn | 1:90dccf306268 | 64 | */ |
jburhenn | 0:09ef59d2d0f7 | 65 | void add(const char* unit, const char* value, bool unitInFlash = false, bool valueInFlash = false) { |
jburhenn | 0:09ef59d2d0f7 | 66 | if (_valueCount >= CAYENNE_MAX_MESSAGE_VALUES) |
jburhenn | 0:09ef59d2d0f7 | 67 | return; |
jburhenn | 0:09ef59d2d0f7 | 68 | |
jburhenn | 0:09ef59d2d0f7 | 69 | size_t unitLength = 0; |
jburhenn | 0:09ef59d2d0f7 | 70 | if (unit) { |
jburhenn | 0:09ef59d2d0f7 | 71 | unitLength = (unitInFlash ? CAYENNE_STRLEN(unit) : strlen(unit)) + 1; |
jburhenn | 0:09ef59d2d0f7 | 72 | } |
jburhenn | 0:09ef59d2d0f7 | 73 | size_t valueLength = 0; |
jburhenn | 0:09ef59d2d0f7 | 74 | if (value) { |
jburhenn | 0:09ef59d2d0f7 | 75 | valueLength = (valueInFlash ? CAYENNE_STRLEN(value) : strlen(value)) + 1; |
jburhenn | 0:09ef59d2d0f7 | 76 | } |
jburhenn | 0:09ef59d2d0f7 | 77 | if (_index + unitLength + valueLength > BUFFER_SIZE) |
jburhenn | 0:09ef59d2d0f7 | 78 | return; |
jburhenn | 0:09ef59d2d0f7 | 79 | |
jburhenn | 0:09ef59d2d0f7 | 80 | if (unit) { |
jburhenn | 0:09ef59d2d0f7 | 81 | unitInFlash ? CAYENNE_MEMCPY(_buffer + _index, unit, unitLength) : memcpy(_buffer + _index, unit, unitLength); |
jburhenn | 0:09ef59d2d0f7 | 82 | _values[_valueCount].unit = _buffer + _index; |
jburhenn | 0:09ef59d2d0f7 | 83 | _index += unitLength; |
jburhenn | 0:09ef59d2d0f7 | 84 | } |
jburhenn | 0:09ef59d2d0f7 | 85 | else { |
jburhenn | 0:09ef59d2d0f7 | 86 | _values[_valueCount].unit = NULL; |
jburhenn | 0:09ef59d2d0f7 | 87 | } |
jburhenn | 0:09ef59d2d0f7 | 88 | |
jburhenn | 0:09ef59d2d0f7 | 89 | if (value) { |
jburhenn | 0:09ef59d2d0f7 | 90 | valueInFlash ? CAYENNE_MEMCPY(_buffer + _index, value, valueLength) : memcpy(_buffer + _index, value, valueLength); |
jburhenn | 0:09ef59d2d0f7 | 91 | _values[_valueCount].value = _buffer + _index; |
jburhenn | 0:09ef59d2d0f7 | 92 | _index += valueLength; |
jburhenn | 0:09ef59d2d0f7 | 93 | } |
jburhenn | 0:09ef59d2d0f7 | 94 | else { |
jburhenn | 0:09ef59d2d0f7 | 95 | _values[_valueCount].value = NULL; |
jburhenn | 0:09ef59d2d0f7 | 96 | } |
jburhenn | 0:09ef59d2d0f7 | 97 | |
jburhenn | 0:09ef59d2d0f7 | 98 | _valueCount++; |
jburhenn | 0:09ef59d2d0f7 | 99 | } |
jburhenn | 0:09ef59d2d0f7 | 100 | |
jburhenn | 1:90dccf306268 | 101 | /** |
jburhenn | 1:90dccf306268 | 102 | * Add the specified unit/value pair to the array. |
jburhenn | 1:90dccf306268 | 103 | * @param[in] unit The unit to add. |
jburhenn | 1:90dccf306268 | 104 | * @param[in] value The value to add. |
jburhenn | 1:90dccf306268 | 105 | */ |
jburhenn | 0:09ef59d2d0f7 | 106 | inline void add(const char* unit, const int value) { |
jburhenn | 0:09ef59d2d0f7 | 107 | char str[2 + 8 * sizeof(value)]; |
jburhenn | 0:09ef59d2d0f7 | 108 | #if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32) |
jburhenn | 0:09ef59d2d0f7 | 109 | itoa(value, str, 10); |
jburhenn | 0:09ef59d2d0f7 | 110 | #else |
jburhenn | 0:09ef59d2d0f7 | 111 | snprintf(str, sizeof(str), "%d", value); |
jburhenn | 0:09ef59d2d0f7 | 112 | #endif |
jburhenn | 0:09ef59d2d0f7 | 113 | add(unit, str); |
jburhenn | 0:09ef59d2d0f7 | 114 | } |
jburhenn | 0:09ef59d2d0f7 | 115 | |
jburhenn | 1:90dccf306268 | 116 | /** |
jburhenn | 1:90dccf306268 | 117 | * Add the specified unit/value pair to the array. |
jburhenn | 1:90dccf306268 | 118 | * @param[in] unit The unit to add. |
jburhenn | 1:90dccf306268 | 119 | * @param[in] value The value to add. |
jburhenn | 1:90dccf306268 | 120 | */ |
jburhenn | 0:09ef59d2d0f7 | 121 | inline void add(const char* unit, const unsigned int value) { |
jburhenn | 0:09ef59d2d0f7 | 122 | char str[1 + 8 * sizeof(value)]; |
jburhenn | 0:09ef59d2d0f7 | 123 | #if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32) |
jburhenn | 0:09ef59d2d0f7 | 124 | utoa(value, str, 10); |
jburhenn | 0:09ef59d2d0f7 | 125 | #else |
jburhenn | 0:09ef59d2d0f7 | 126 | snprintf(str, sizeof(str), "%u", value); |
jburhenn | 0:09ef59d2d0f7 | 127 | #endif |
jburhenn | 0:09ef59d2d0f7 | 128 | add(unit, str); |
jburhenn | 0:09ef59d2d0f7 | 129 | } |
jburhenn | 0:09ef59d2d0f7 | 130 | |
jburhenn | 1:90dccf306268 | 131 | /** |
jburhenn | 1:90dccf306268 | 132 | * Add the specified unit/value pair to the array. |
jburhenn | 1:90dccf306268 | 133 | * @param[in] unit The unit to add. |
jburhenn | 1:90dccf306268 | 134 | * @param[in] value The value to add. |
jburhenn | 1:90dccf306268 | 135 | */ |
jburhenn | 0:09ef59d2d0f7 | 136 | inline void add(const char* unit, const long value) { |
jburhenn | 0:09ef59d2d0f7 | 137 | char str[2 + 8 * sizeof(value)]; |
jburhenn | 0:09ef59d2d0f7 | 138 | #if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32) |
jburhenn | 0:09ef59d2d0f7 | 139 | ltoa(value, str, 10); |
jburhenn | 0:09ef59d2d0f7 | 140 | #else |
jburhenn | 0:09ef59d2d0f7 | 141 | snprintf(str, sizeof(str), "%ld", value); |
jburhenn | 0:09ef59d2d0f7 | 142 | #endif |
jburhenn | 0:09ef59d2d0f7 | 143 | add(unit, str); |
jburhenn | 0:09ef59d2d0f7 | 144 | } |
jburhenn | 0:09ef59d2d0f7 | 145 | |
jburhenn | 1:90dccf306268 | 146 | /** |
jburhenn | 1:90dccf306268 | 147 | * Add the specified unit/value pair to the array. |
jburhenn | 1:90dccf306268 | 148 | * @param[in] unit The unit to add. |
jburhenn | 1:90dccf306268 | 149 | * @param[in] value The value to add. |
jburhenn | 1:90dccf306268 | 150 | */ |
jburhenn | 0:09ef59d2d0f7 | 151 | inline void add(const char* unit, const unsigned long value) { |
jburhenn | 0:09ef59d2d0f7 | 152 | char str[1 + 8 * sizeof(value)]; |
jburhenn | 0:09ef59d2d0f7 | 153 | #if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32) |
jburhenn | 0:09ef59d2d0f7 | 154 | ultoa(value, str, 10); |
jburhenn | 0:09ef59d2d0f7 | 155 | #else |
jburhenn | 0:09ef59d2d0f7 | 156 | snprintf(str, sizeof(str), "%lu", value); |
jburhenn | 0:09ef59d2d0f7 | 157 | #endif |
jburhenn | 0:09ef59d2d0f7 | 158 | add(unit, str); |
jburhenn | 0:09ef59d2d0f7 | 159 | } |
jburhenn | 0:09ef59d2d0f7 | 160 | |
jburhenn | 0:09ef59d2d0f7 | 161 | #if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32) |
jburhenn | 1:90dccf306268 | 162 | /** |
jburhenn | 1:90dccf306268 | 163 | * Add the specified unit/value pair to the array. |
jburhenn | 1:90dccf306268 | 164 | * @param[in] unit The unit to add. |
jburhenn | 1:90dccf306268 | 165 | * @param[in] value The value to add. |
jburhenn | 1:90dccf306268 | 166 | */ |
jburhenn | 0:09ef59d2d0f7 | 167 | inline void add(const char* unit, const float value) { |
jburhenn | 0:09ef59d2d0f7 | 168 | char str[33]; |
jburhenn | 0:09ef59d2d0f7 | 169 | dtostrf(value, 5, 3, str); |
jburhenn | 0:09ef59d2d0f7 | 170 | add(unit, str); |
jburhenn | 0:09ef59d2d0f7 | 171 | } |
jburhenn | 0:09ef59d2d0f7 | 172 | |
jburhenn | 1:90dccf306268 | 173 | /** |
jburhenn | 1:90dccf306268 | 174 | * Add the specified unit/value pair to the array. |
jburhenn | 1:90dccf306268 | 175 | * @param[in] unit The unit to add. |
jburhenn | 1:90dccf306268 | 176 | * @param[in] value The value to add. |
jburhenn | 1:90dccf306268 | 177 | */ |
jburhenn | 0:09ef59d2d0f7 | 178 | inline void add(const char* unit, const double value) { |
jburhenn | 0:09ef59d2d0f7 | 179 | char str[33]; |
jburhenn | 0:09ef59d2d0f7 | 180 | dtostrf(value, 5, 3, str); |
jburhenn | 0:09ef59d2d0f7 | 181 | add(unit, str); |
jburhenn | 0:09ef59d2d0f7 | 182 | } |
jburhenn | 0:09ef59d2d0f7 | 183 | |
jburhenn | 0:09ef59d2d0f7 | 184 | #else |
jburhenn | 1:90dccf306268 | 185 | /** |
jburhenn | 1:90dccf306268 | 186 | * Add the specified unit/value pair to the array. |
jburhenn | 1:90dccf306268 | 187 | * @param[in] unit The unit to add. |
jburhenn | 1:90dccf306268 | 188 | * @param[in] value The value to add. |
jburhenn | 1:90dccf306268 | 189 | */ |
jburhenn | 0:09ef59d2d0f7 | 190 | inline void add(const char* unit, const float value) { |
jburhenn | 0:09ef59d2d0f7 | 191 | char str[33]; |
jburhenn | 0:09ef59d2d0f7 | 192 | snprintf(str, 33, "%2.3f", value); |
jburhenn | 0:09ef59d2d0f7 | 193 | add(unit, str); |
jburhenn | 0:09ef59d2d0f7 | 194 | } |
jburhenn | 0:09ef59d2d0f7 | 195 | |
jburhenn | 1:90dccf306268 | 196 | /** |
jburhenn | 1:90dccf306268 | 197 | * Add the specified unit/value pair to the array. |
jburhenn | 1:90dccf306268 | 198 | * @param[in] unit The unit to add. |
jburhenn | 1:90dccf306268 | 199 | * @param[in] value The value to add. |
jburhenn | 1:90dccf306268 | 200 | */ |
jburhenn | 0:09ef59d2d0f7 | 201 | inline void add(const char* unit, const double value) { |
jburhenn | 0:09ef59d2d0f7 | 202 | char str[33]; |
jburhenn | 0:09ef59d2d0f7 | 203 | snprintf(str, 33, "%2.3f", value); |
jburhenn | 0:09ef59d2d0f7 | 204 | add(unit, str); |
jburhenn | 0:09ef59d2d0f7 | 205 | } |
jburhenn | 0:09ef59d2d0f7 | 206 | |
jburhenn | 0:09ef59d2d0f7 | 207 | #endif |
jburhenn | 0:09ef59d2d0f7 | 208 | |
jburhenn | 0:09ef59d2d0f7 | 209 | #ifdef CAYENNE_USING_PROGMEM |
jburhenn | 1:90dccf306268 | 210 | /** |
jburhenn | 1:90dccf306268 | 211 | * Add the specified unit/value pair to the array. |
jburhenn | 1:90dccf306268 | 212 | * @param[in] unit The unit to add. |
jburhenn | 1:90dccf306268 | 213 | * @param[in] value The value to add. |
jburhenn | 1:90dccf306268 | 214 | */ |
jburhenn | 0:09ef59d2d0f7 | 215 | void add(const char* unit, const __FlashStringHelper* value) { |
jburhenn | 0:09ef59d2d0f7 | 216 | const char* valueString = reinterpret_cast<const char *>(value); |
jburhenn | 0:09ef59d2d0f7 | 217 | add(unit, valueString, false, true); |
jburhenn | 0:09ef59d2d0f7 | 218 | } |
jburhenn | 0:09ef59d2d0f7 | 219 | |
jburhenn | 1:90dccf306268 | 220 | /** |
jburhenn | 1:90dccf306268 | 221 | * Add the specified unit/value pair to the array. |
jburhenn | 1:90dccf306268 | 222 | * @param[in] unit The unit to add. |
jburhenn | 1:90dccf306268 | 223 | * @param[in] value The value to add. |
jburhenn | 1:90dccf306268 | 224 | */ |
jburhenn | 0:09ef59d2d0f7 | 225 | void add(const __FlashStringHelper* unit, const char* value) { |
jburhenn | 0:09ef59d2d0f7 | 226 | const char* unitString = reinterpret_cast<const char *>(unit); |
jburhenn | 0:09ef59d2d0f7 | 227 | add(unitString, value, true, false); |
jburhenn | 0:09ef59d2d0f7 | 228 | } |
jburhenn | 1:90dccf306268 | 229 | |
jburhenn | 1:90dccf306268 | 230 | /** |
jburhenn | 1:90dccf306268 | 231 | * Add the specified unit/value pair to the array. |
jburhenn | 1:90dccf306268 | 232 | * @param[in] unit The unit to add. |
jburhenn | 1:90dccf306268 | 233 | * @param[in] value The value to add. |
jburhenn | 1:90dccf306268 | 234 | */ |
jburhenn | 0:09ef59d2d0f7 | 235 | void add(const __FlashStringHelper* unit, const __FlashStringHelper* value) { |
jburhenn | 0:09ef59d2d0f7 | 236 | const char* unitString = reinterpret_cast<const char *>(unit); |
jburhenn | 0:09ef59d2d0f7 | 237 | const char* valueString = reinterpret_cast<const char *>(value); |
jburhenn | 0:09ef59d2d0f7 | 238 | add(unitString, valueString, true, true); |
jburhenn | 0:09ef59d2d0f7 | 239 | } |
jburhenn | 0:09ef59d2d0f7 | 240 | |
jburhenn | 1:90dccf306268 | 241 | /** |
jburhenn | 1:90dccf306268 | 242 | * Add the specified unit/value pair to the array. |
jburhenn | 1:90dccf306268 | 243 | * @param[in] unit The unit to add. |
jburhenn | 1:90dccf306268 | 244 | * @param[in] value The value to add. |
jburhenn | 1:90dccf306268 | 245 | */ |
jburhenn | 0:09ef59d2d0f7 | 246 | inline void add(const __FlashStringHelper* unit, const int value) { |
jburhenn | 0:09ef59d2d0f7 | 247 | char str[2 + 8 * sizeof(value)]; |
jburhenn | 0:09ef59d2d0f7 | 248 | itoa(value, str, 10); |
jburhenn | 0:09ef59d2d0f7 | 249 | add(unit, str); |
jburhenn | 0:09ef59d2d0f7 | 250 | } |
jburhenn | 0:09ef59d2d0f7 | 251 | |
jburhenn | 1:90dccf306268 | 252 | /** |
jburhenn | 1:90dccf306268 | 253 | * Add the specified unit/value pair to the array. |
jburhenn | 1:90dccf306268 | 254 | * @param[in] unit The unit to add. |
jburhenn | 1:90dccf306268 | 255 | * @param[in] value The value to add. |
jburhenn | 1:90dccf306268 | 256 | */ |
jburhenn | 0:09ef59d2d0f7 | 257 | inline void add(const __FlashStringHelper* unit, const unsigned int value) { |
jburhenn | 0:09ef59d2d0f7 | 258 | char str[1 + 8 * sizeof(value)]; |
jburhenn | 0:09ef59d2d0f7 | 259 | utoa(value, str, 10); |
jburhenn | 0:09ef59d2d0f7 | 260 | add(unit, str); |
jburhenn | 0:09ef59d2d0f7 | 261 | } |
jburhenn | 0:09ef59d2d0f7 | 262 | |
jburhenn | 1:90dccf306268 | 263 | /** |
jburhenn | 1:90dccf306268 | 264 | * Add the specified unit/value pair to the array. |
jburhenn | 1:90dccf306268 | 265 | * @param[in] unit The unit to add. |
jburhenn | 1:90dccf306268 | 266 | * @param[in] value The value to add. |
jburhenn | 1:90dccf306268 | 267 | */ |
jburhenn | 0:09ef59d2d0f7 | 268 | inline void add(const __FlashStringHelper* unit, const long value) { |
jburhenn | 0:09ef59d2d0f7 | 269 | char str[2 + 8 * sizeof(value)]; |
jburhenn | 0:09ef59d2d0f7 | 270 | ltoa(value, str, 10); |
jburhenn | 0:09ef59d2d0f7 | 271 | add(unit, str); |
jburhenn | 0:09ef59d2d0f7 | 272 | } |
jburhenn | 1:90dccf306268 | 273 | |
jburhenn | 1:90dccf306268 | 274 | /** |
jburhenn | 1:90dccf306268 | 275 | * Add the specified unit/value pair to the array. |
jburhenn | 1:90dccf306268 | 276 | * @param[in] unit The unit to add. |
jburhenn | 1:90dccf306268 | 277 | * @param[in] value The value to add. |
jburhenn | 1:90dccf306268 | 278 | */ |
jburhenn | 0:09ef59d2d0f7 | 279 | inline void add(const __FlashStringHelper* unit, const unsigned long value) { |
jburhenn | 0:09ef59d2d0f7 | 280 | char str[1 + 8 * sizeof(value)]; |
jburhenn | 0:09ef59d2d0f7 | 281 | ultoa(value, str, 10); |
jburhenn | 0:09ef59d2d0f7 | 282 | add(unit, str); |
jburhenn | 0:09ef59d2d0f7 | 283 | } |
jburhenn | 0:09ef59d2d0f7 | 284 | |
jburhenn | 1:90dccf306268 | 285 | /** |
jburhenn | 1:90dccf306268 | 286 | * Add the specified unit/value pair to the array. |
jburhenn | 1:90dccf306268 | 287 | * @param[in] unit The unit to add. |
jburhenn | 1:90dccf306268 | 288 | * @param[in] value The value to add. |
jburhenn | 1:90dccf306268 | 289 | */ |
jburhenn | 0:09ef59d2d0f7 | 290 | inline void add(const __FlashStringHelper* unit, const float value) { |
jburhenn | 0:09ef59d2d0f7 | 291 | char str[33]; |
jburhenn | 6:82e142a864ad | 292 | #if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32) |
jburhenn | 6:82e142a864ad | 293 | dtostrf(value, 5, 3, str); |
jburhenn | 6:82e142a864ad | 294 | #else |
jburhenn | 6:82e142a864ad | 295 | snprintf(str, 33, "%2.3f", value); |
jburhenn | 6:82e142a864ad | 296 | #endif |
jburhenn | 0:09ef59d2d0f7 | 297 | add(unit, str); |
jburhenn | 0:09ef59d2d0f7 | 298 | } |
jburhenn | 0:09ef59d2d0f7 | 299 | |
jburhenn | 1:90dccf306268 | 300 | /** |
jburhenn | 1:90dccf306268 | 301 | * Add the specified unit/value pair to the array. |
jburhenn | 1:90dccf306268 | 302 | * @param[in] unit The unit to add. |
jburhenn | 1:90dccf306268 | 303 | * @param[in] value The value to add. |
jburhenn | 1:90dccf306268 | 304 | */ |
jburhenn | 0:09ef59d2d0f7 | 305 | inline void add(const __FlashStringHelper* unit, const double value) { |
jburhenn | 0:09ef59d2d0f7 | 306 | char str[33]; |
jburhenn | 6:82e142a864ad | 307 | #if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32) |
jburhenn | 6:82e142a864ad | 308 | dtostrf(value, 5, 3, str); |
jburhenn | 6:82e142a864ad | 309 | #else |
jburhenn | 6:82e142a864ad | 310 | snprintf(str, 33, "%2.3f", value); |
jburhenn | 6:82e142a864ad | 311 | #endif |
jburhenn | 0:09ef59d2d0f7 | 312 | add(unit, str); |
jburhenn | 0:09ef59d2d0f7 | 313 | } |
jburhenn | 0:09ef59d2d0f7 | 314 | |
jburhenn | 0:09ef59d2d0f7 | 315 | #endif |
jburhenn | 1:90dccf306268 | 316 | /** |
jburhenn | 1:90dccf306268 | 317 | * Get the unit/value pair array. |
jburhenn | 1:90dccf306268 | 318 | * @return Pointer to the array. |
jburhenn | 1:90dccf306268 | 319 | */ |
jburhenn | 0:09ef59d2d0f7 | 320 | const CayenneValuePair* getArray() const { |
jburhenn | 0:09ef59d2d0f7 | 321 | return _values; |
jburhenn | 0:09ef59d2d0f7 | 322 | } |
jburhenn | 0:09ef59d2d0f7 | 323 | |
jburhenn | 1:90dccf306268 | 324 | /** |
jburhenn | 1:90dccf306268 | 325 | * Get the number of items in the unit/value pair array. |
jburhenn | 1:90dccf306268 | 326 | * @return Count of items. |
jburhenn | 1:90dccf306268 | 327 | */ |
jburhenn | 0:09ef59d2d0f7 | 328 | size_t getCount() const { |
jburhenn | 0:09ef59d2d0f7 | 329 | return _valueCount; |
jburhenn | 0:09ef59d2d0f7 | 330 | } |
jburhenn | 0:09ef59d2d0f7 | 331 | |
jburhenn | 0:09ef59d2d0f7 | 332 | private: |
jburhenn | 0:09ef59d2d0f7 | 333 | CayenneValuePair _values[MAX_VALUES]; |
jburhenn | 0:09ef59d2d0f7 | 334 | size_t _valueCount; |
jburhenn | 0:09ef59d2d0f7 | 335 | char _buffer[BUFFER_SIZE]; |
jburhenn | 0:09ef59d2d0f7 | 336 | size_t _index; |
jburhenn | 0:09ef59d2d0f7 | 337 | }; |
jburhenn | 0:09ef59d2d0f7 | 338 | } |
jburhenn | 0:09ef59d2d0f7 | 339 | |
jburhenn | 0:09ef59d2d0f7 | 340 | typedef Cayenne::DataArray<> CayenneDataArray; |
jburhenn | 0:09ef59d2d0f7 | 341 | |
jburhenn | 0:09ef59d2d0f7 | 342 | #else |
jburhenn | 0:09ef59d2d0f7 | 343 | |
jburhenn | 0:09ef59d2d0f7 | 344 | // C version of the data array. Requires source file to be compiled and linked. |
jburhenn | 0:09ef59d2d0f7 | 345 | |
jburhenn | 0:09ef59d2d0f7 | 346 | typedef struct CayenneDataArray |
jburhenn | 0:09ef59d2d0f7 | 347 | { |
jburhenn | 0:09ef59d2d0f7 | 348 | CayenneValuePair values[CAYENNE_MAX_MESSAGE_VALUES]; |
jburhenn | 0:09ef59d2d0f7 | 349 | unsigned int valueCount; |
jburhenn | 0:09ef59d2d0f7 | 350 | char* buffer; |
jburhenn | 0:09ef59d2d0f7 | 351 | unsigned int bufferSize; |
jburhenn | 0:09ef59d2d0f7 | 352 | unsigned int bufferIndex; |
jburhenn | 0:09ef59d2d0f7 | 353 | } CayenneDataArray; |
jburhenn | 0:09ef59d2d0f7 | 354 | |
jburhenn | 0:09ef59d2d0f7 | 355 | /** |
jburhenn | 0:09ef59d2d0f7 | 356 | * Initialize a data array of unit/value pairs. |
jburhenn | 0:09ef59d2d0f7 | 357 | * @param[out] dataArray The initialized data array |
jburhenn | 0:09ef59d2d0f7 | 358 | * @param[in] buffer Buffer for storing unit/value pairs. This buffer should be available for as long as the data array is used. |
jburhenn | 0:09ef59d2d0f7 | 359 | * @param[in] bufferSize Size of the buffer |
jburhenn | 0:09ef59d2d0f7 | 360 | */ |
jburhenn | 0:09ef59d2d0f7 | 361 | DLLExport void CayenneDataArrayInit(CayenneDataArray* dataArray, char* buffer, unsigned int bufferSize); |
jburhenn | 0:09ef59d2d0f7 | 362 | |
jburhenn | 0:09ef59d2d0f7 | 363 | /** |
jburhenn | 0:09ef59d2d0f7 | 364 | * Add the specified unit/value pair to the array. |
jburhenn | 0:09ef59d2d0f7 | 365 | * @param[in] dataArray The data array to add values to |
jburhenn | 0:09ef59d2d0f7 | 366 | * @param[in] unit The unit to add |
jburhenn | 0:09ef59d2d0f7 | 367 | * @param[in] value The value to add |
jburhenn | 0:09ef59d2d0f7 | 368 | * @return CAYENNE_SUCCESS if unit/value pair was add, CAYENNE_FAILURE otherwise |
jburhenn | 0:09ef59d2d0f7 | 369 | */ |
jburhenn | 0:09ef59d2d0f7 | 370 | DLLExport int CayenneDataArrayAdd(CayenneDataArray* dataArray, const char* unit, const char* value); |
jburhenn | 0:09ef59d2d0f7 | 371 | |
jburhenn | 0:09ef59d2d0f7 | 372 | /** |
jburhenn | 0:09ef59d2d0f7 | 373 | * Add the specified unit/value pair to the array. |
jburhenn | 0:09ef59d2d0f7 | 374 | * @param[in] dataArray The data array to add values to |
jburhenn | 0:09ef59d2d0f7 | 375 | * @param[in] unit The unit to add |
jburhenn | 0:09ef59d2d0f7 | 376 | * @param[in] value The value to add |
jburhenn | 0:09ef59d2d0f7 | 377 | * @return CAYENNE_SUCCESS if unit/value pair was add, CAYENNE_FAILURE otherwise |
jburhenn | 0:09ef59d2d0f7 | 378 | */ |
jburhenn | 0:09ef59d2d0f7 | 379 | DLLExport int CayenneDataArrayAddInt(CayenneDataArray* dataArray, const char* unit, int value); |
jburhenn | 0:09ef59d2d0f7 | 380 | |
jburhenn | 0:09ef59d2d0f7 | 381 | /** |
jburhenn | 0:09ef59d2d0f7 | 382 | * Add the specified unit/value pair to the array. |
jburhenn | 0:09ef59d2d0f7 | 383 | * @param[in] dataArray The data array to add values to |
jburhenn | 0:09ef59d2d0f7 | 384 | * @param[in] unit The unit to add |
jburhenn | 0:09ef59d2d0f7 | 385 | * @param[in] value The value to add |
jburhenn | 0:09ef59d2d0f7 | 386 | * @return CAYENNE_SUCCESS if unit/value pair was add, CAYENNE_FAILURE otherwise |
jburhenn | 0:09ef59d2d0f7 | 387 | */ |
jburhenn | 0:09ef59d2d0f7 | 388 | DLLExport int CayenneDataArrayAddUInt(CayenneDataArray* dataArray, const char* unit, unsigned int value); |
jburhenn | 0:09ef59d2d0f7 | 389 | |
jburhenn | 0:09ef59d2d0f7 | 390 | /** |
jburhenn | 0:09ef59d2d0f7 | 391 | * Add the specified unit/value pair to the array. |
jburhenn | 0:09ef59d2d0f7 | 392 | * @param[in] dataArray The data array to add values to |
jburhenn | 0:09ef59d2d0f7 | 393 | * @param[in] unit The unit to add |
jburhenn | 0:09ef59d2d0f7 | 394 | * @param[in] value The value to add |
jburhenn | 0:09ef59d2d0f7 | 395 | * @return CAYENNE_SUCCESS if unit/value pair was add, CAYENNE_FAILURE otherwise |
jburhenn | 0:09ef59d2d0f7 | 396 | */ |
jburhenn | 0:09ef59d2d0f7 | 397 | DLLExport int CayenneDataArrayAddLong(CayenneDataArray* dataArray, const char* unit, long value); |
jburhenn | 0:09ef59d2d0f7 | 398 | |
jburhenn | 0:09ef59d2d0f7 | 399 | /** |
jburhenn | 0:09ef59d2d0f7 | 400 | * Add the specified unit/value pair to the array. |
jburhenn | 0:09ef59d2d0f7 | 401 | * @param[in] dataArray The data array to add values to |
jburhenn | 0:09ef59d2d0f7 | 402 | * @param[in] unit The unit to add |
jburhenn | 0:09ef59d2d0f7 | 403 | * @param[in] value The value to add |
jburhenn | 0:09ef59d2d0f7 | 404 | * @return CAYENNE_SUCCESS if unit/value pair was add, CAYENNE_FAILURE otherwise |
jburhenn | 0:09ef59d2d0f7 | 405 | */ |
jburhenn | 0:09ef59d2d0f7 | 406 | DLLExport int CayenneDataArrayAddULong(CayenneDataArray* dataArray, const char* unit, unsigned long value); |
jburhenn | 0:09ef59d2d0f7 | 407 | |
jburhenn | 0:09ef59d2d0f7 | 408 | /** |
jburhenn | 0:09ef59d2d0f7 | 409 | * Add the specified unit/value pair to the array. |
jburhenn | 0:09ef59d2d0f7 | 410 | * @param[in] dataArray The data array to add values to |
jburhenn | 0:09ef59d2d0f7 | 411 | * @param[in] unit The unit to add |
jburhenn | 0:09ef59d2d0f7 | 412 | * @param[in] value The value to add |
jburhenn | 0:09ef59d2d0f7 | 413 | * @return CAYENNE_SUCCESS if unit/value pair was add, CAYENNE_FAILURE otherwise |
jburhenn | 0:09ef59d2d0f7 | 414 | */ |
jburhenn | 0:09ef59d2d0f7 | 415 | DLLExport int CayenneDataArrayAddDouble(CayenneDataArray* dataArray, const char* unit, double value); |
jburhenn | 0:09ef59d2d0f7 | 416 | |
jburhenn | 0:09ef59d2d0f7 | 417 | /** |
jburhenn | 0:09ef59d2d0f7 | 418 | * Add the specified unit/value pair to the array. |
jburhenn | 0:09ef59d2d0f7 | 419 | * @param[in] dataArray The data array to add values to |
jburhenn | 0:09ef59d2d0f7 | 420 | * @param[in] unit The unit to add |
jburhenn | 0:09ef59d2d0f7 | 421 | * @param[in] value The value to add |
jburhenn | 0:09ef59d2d0f7 | 422 | * @return CAYENNE_SUCCESS if unit/value pair was add, CAYENNE_FAILURE otherwise |
jburhenn | 0:09ef59d2d0f7 | 423 | */ |
jburhenn | 0:09ef59d2d0f7 | 424 | DLLExport int CayenneDataArrayAddFloat(CayenneDataArray* dataArray, const char* unit, float value); |
jburhenn | 0:09ef59d2d0f7 | 425 | |
jburhenn | 0:09ef59d2d0f7 | 426 | /** |
jburhenn | 0:09ef59d2d0f7 | 427 | * Clear the data array. |
jburhenn | 0:09ef59d2d0f7 | 428 | * @param[in] dataArray The data array to clear |
jburhenn | 0:09ef59d2d0f7 | 429 | */ |
jburhenn | 0:09ef59d2d0f7 | 430 | DLLExport void CayenneDataArrayClear(CayenneDataArray* dataArray); |
jburhenn | 0:09ef59d2d0f7 | 431 | |
jburhenn | 0:09ef59d2d0f7 | 432 | #endif |
jburhenn | 0:09ef59d2d0f7 | 433 | |
jburhenn | 0:09ef59d2d0f7 | 434 | #endif |