@@ -34,12 +34,10 @@ MyMessage::MyMessage(uint8_t _sensor, uint8_t _type) {
34
34
type = _type;
35
35
}
36
36
37
-
38
37
bool MyMessage::isAck () const {
39
38
return miGetAck ();
40
39
}
41
40
42
-
43
41
/* Getters for payload converted to desired form */
44
42
void * MyMessage::getCustom () const {
45
43
return (void *)data;
@@ -63,16 +61,20 @@ char MyMessage::i2h(uint8_t i) const {
63
61
return ' A' + k - 10 ;
64
62
}
65
63
64
+ char * MyMessage::getCustomString (char *buffer) const {
65
+ for (uint8_t i = 0 ; i < miGetLength (); i++)
66
+ {
67
+ buffer[i * 2 ] = i2h (data[i] >> 4 );
68
+ buffer[(i * 2 ) + 1 ] = i2h (data[i]);
69
+ }
70
+ buffer[miGetLength () * 2 ] = ' \0 ' ;
71
+ return buffer;
72
+ }
73
+
66
74
char * MyMessage::getStream (char *buffer) const {
67
75
uint8_t cmd = miGetCommand ();
68
76
if ((cmd == C_STREAM) && (buffer != NULL )) {
69
- for (uint8_t i = 0 ; i < miGetLength (); i++)
70
- {
71
- buffer[i * 2 ] = i2h (data[i] >> 4 );
72
- buffer[(i * 2 ) + 1 ] = i2h (data[i]);
73
- }
74
- buffer[miGetLength () * 2 ] = ' \0 ' ;
75
- return buffer;
77
+ return getCustomString (buffer);
76
78
} else {
77
79
return NULL ;
78
80
}
@@ -94,12 +96,11 @@ char* MyMessage::getString(char *buffer) const {
94
96
} else if (payloadType == P_LONG32) {
95
97
ltoa (lValue, buffer, 10 );
96
98
} else if (payloadType == P_ULONG32) {
97
-
98
99
ultoa (ulValue, buffer, 10 );
99
100
} else if (payloadType == P_FLOAT32) {
100
101
dtostrf (fValue ,2 ,fPrecision ,buffer);
101
102
} else if (payloadType == P_CUSTOM) {
102
- return getStream (buffer);
103
+ return getCustomString (buffer);
103
104
}
104
105
return buffer;
105
106
} else {
@@ -172,7 +173,6 @@ unsigned int MyMessage::getUInt() const {
172
173
173
174
}
174
175
175
-
176
176
MyMessage& MyMessage::setType (uint8_t _type) {
177
177
type = _type;
178
178
return *this ;
@@ -196,12 +196,11 @@ MyMessage& MyMessage::set(void* value, uint8_t length) {
196
196
return *this ;
197
197
}
198
198
199
-
200
199
MyMessage& MyMessage::set (const char * value) {
201
- uint8_t length = strlen (value);
200
+ uint8_t length = min ( strlen (value), MAX_PAYLOAD );
202
201
miSetLength (length);
203
202
miSetPayloadType (P_STRING);
204
- strncpy (data, value, min ( length, MAX_PAYLOAD) );
203
+ strncpy (data, value, length);
205
204
return *this ;
206
205
}
207
206
@@ -212,7 +211,6 @@ MyMessage& MyMessage::set(uint8_t value) {
212
211
return *this ;
213
212
}
214
213
215
-
216
214
MyMessage& MyMessage::set (float value, uint8_t decimals) {
217
215
miSetLength (5 ); // 32 bit float + persi
218
216
miSetPayloadType (P_FLOAT32);
@@ -436,6 +434,4 @@ char *ultoa(unsigned long num, char *str, int radix)
436
434
return str;
437
435
}
438
436
439
-
440
437
#endif
441
-
0 commit comments