@@ -33,191 +33,6 @@ Code adapted from Blynk library BlynkMessage.cpp. Copyright info below.
33
33
#include < stdlib.h>
34
34
#include " CayenneMQTTClient/CayenneMQTTClient.h"
35
35
36
- class CayenneValueArray
37
- {
38
- public:
39
- CayenneValueArray (char * buffer, size_t size) : _buffer(buffer), _size(size), _index(0 ), _valueCount(0 ) {
40
- clear ();
41
- }
42
-
43
- void clear () {
44
- for (int i = 0 ; i < CAYENNE_MAX_MESSAGE_VALUES; ++i) {
45
- _values[i].unit = NULL ;
46
- _values[i].value = NULL ;
47
- }
48
- _valueCount = 0 ;
49
- _index = 0 ;
50
- }
51
-
52
- void add (const char * unit, const char * value, bool unitInFlash = false , bool valueInFlash = false ) {
53
- if (_valueCount >= CAYENNE_MAX_MESSAGE_VALUES)
54
- return ;
55
-
56
- size_t unitLength = 0 ;
57
- if (unit) {
58
- unitLength = (unitInFlash ? CAYENNE_STRLEN (unit) : strlen (unit)) + 1 ;
59
- }
60
- size_t valueLength = 0 ;
61
- if (value) {
62
- valueLength = (valueInFlash ? CAYENNE_STRLEN (value) : strlen (value)) + 1 ;
63
- }
64
- if (_index + unitLength + valueLength > _size)
65
- return ;
66
-
67
- if (unit) {
68
- unitInFlash ? CAYENNE_MEMCPY (_buffer + _index, unit, unitLength) : memcpy (_buffer + _index, unit, unitLength);
69
- _values[_valueCount].unit = _buffer + _index;
70
- _index += unitLength;
71
- }
72
- else {
73
- _values[_valueCount].unit = NULL ;
74
- }
75
-
76
- if (value) {
77
- valueInFlash ? CAYENNE_MEMCPY (_buffer + _index, value, valueLength) : memcpy (_buffer + _index, value, valueLength);
78
- _values[_valueCount].value = _buffer + _index;
79
- _index += valueLength;
80
- }
81
- else {
82
- _values[_valueCount].value = NULL ;
83
- }
84
-
85
- _valueCount++;
86
- }
87
-
88
- void add (const char * unit, const __FlashStringHelper* value) {
89
- const char * valueString = reinterpret_cast <const char *>(value);
90
- add (unit, valueString, false , true );
91
- }
92
-
93
- void add (const __FlashStringHelper* unit, const char * value) {
94
- const char * unitString = reinterpret_cast <const char *>(unit);
95
- add (unitString, value, true , false );
96
- }
97
-
98
- void add (const __FlashStringHelper* unit, const __FlashStringHelper* value) {
99
- const char * unitString = reinterpret_cast <const char *>(unit);
100
- const char * valueString = reinterpret_cast <const char *>(value);
101
- add (unitString, valueString, true , true );
102
- }
103
-
104
- inline void add (const char * unit, const int value) {
105
- char str[2 + 8 * sizeof (value)];
106
- itoa (value, str, 10 );
107
- add (unit, str);
108
- }
109
-
110
- inline void add (const char * unit, const unsigned int value) {
111
- char str[1 + 8 * sizeof (value)];
112
- utoa (value, str, 10 );
113
- add (unit, str);
114
- }
115
-
116
- inline void add (const char * unit, const long value) {
117
- char str[2 + 8 * sizeof (value)];
118
- ltoa (value, str, 10 );
119
- add (unit, str);
120
- }
121
-
122
- inline void add (const char * unit, const unsigned long value) {
123
- char str[1 + 8 * sizeof (value)];
124
- ultoa (value, str, 10 );
125
- add (unit, str);
126
- }
127
-
128
- #if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32)
129
-
130
- inline void add (const char * unit, const float value) {
131
- char str[33 ];
132
- dtostrf (value, 5 , 3 , str);
133
- add (unit, str);
134
- }
135
-
136
- inline void add (const char * unit, const double value) {
137
- char str[33 ];
138
- dtostrf (value, 5 , 3 , str);
139
- add (unit, str);
140
- }
141
-
142
- #else
143
-
144
- inline void add (const char * unit, const float value) {
145
- char str[33 ];
146
- snprintf (str, 33 , " %2.3f" , value);
147
- add (unit, str);
148
- }
149
-
150
- inline void add (const char * unit, const double value) {
151
- char str[33 ];
152
- snprintf (str, 33 , " %2.3f" , value);
153
- add (unit, str);
154
- }
155
-
156
- #endif
157
-
158
- #ifdef CAYENNE_USING_PROGMEM
159
-
160
- inline void add (const __FlashStringHelper* unit, const int value) {
161
- char str[2 + 8 * sizeof (value)];
162
- itoa (value, str, 10 );
163
- add (unit, str);
164
- }
165
-
166
- inline void add (const __FlashStringHelper* unit, const unsigned int value) {
167
- char str[1 + 8 * sizeof (value)];
168
- utoa (value, str, 10 );
169
- add (unit, str);
170
- }
171
-
172
- inline void add (const __FlashStringHelper* unit, const long value) {
173
- char str[2 + 8 * sizeof (value)];
174
- ltoa (value, str, 10 );
175
- add (unit, str);
176
- }
177
-
178
- inline void add (const __FlashStringHelper* unit, const unsigned long value) {
179
- char str[1 + 8 * sizeof (value)];
180
- ultoa (value, str, 10 );
181
- add (unit, str);
182
- }
183
-
184
- inline void add (const __FlashStringHelper* unit, const float value) {
185
- char str[33 ];
186
- #if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32)
187
- dtostrf (value, 5 , 3 , str);
188
- #else
189
- snprintf (str, 33 , " %2.3f" , value);
190
- #endif
191
- add (unit, str);
192
- }
193
-
194
- inline void add (const __FlashStringHelper* unit, const double value) {
195
- char str[33 ];
196
- #if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32)
197
- dtostrf (value, 5 , 3 , str);
198
- #else
199
- snprintf (str, 33 , " %2.3f" , value);
200
- #endif
201
- add (unit, str);
202
- }
203
-
204
- #endif
205
-
206
- const CayenneValuePair* getArray () const {
207
- return _values;
208
- }
209
-
210
- const size_t getCount () const {
211
- return _valueCount;
212
- }
213
-
214
- private:
215
- CayenneValuePair _values[CAYENNE_MAX_MESSAGE_VALUES];
216
- size_t _valueCount;
217
- char * _buffer;
218
- size_t _size;
219
- size_t _index;
220
- };
221
36
222
37
class CayenneMessage
223
38
{
0 commit comments