3232#include < WProgram.h>
3333#endif
3434
35+ #if defined(__MBED__)
36+ #include < mbed.h>
37+ #endif
38+
39+ #if defined(MBED_STREAM_H)
40+ /* * Mbed platform compatibility and mbed like Stream**/
41+ /* * #include <Serial.h> */
42+
43+ #define __DEVICESTREAMTYPE UARTClass
44+ #define CmdMsgByte uint8_t
45+ #endif
46+ #if defined(ARDUINO) && !defined(MBED_STREAM_H)
47+ #define __DEVICESTREAMTYPE Stream
48+ #define CmdMsgByte byte
49+ #endif
50+
3551// #include "Stream.h"
3652
3753extern " C"
@@ -78,7 +94,7 @@ class CmdMessenger
7894 char *current; // Pointer to current buffer position
7995 char *last; // Pointer to previous buffer position
8096 char prevChar; // Previous char (needed for unescaping)
81- Stream *comms; // Serial data stream
97+ __DEVICESTREAMTYPE *comms; // Serial data stream
8298
8399 char command_separator; // Character indicating end of command (default: ';')
84100 char field_separator; // Character indicating end of argument (default: ',')
@@ -90,15 +106,15 @@ class CmdMessenger
90106
91107 // **** Initialize ****
92108
93- void init (Stream & comms, const char fld_separator, const char cmd_separator, const char esc_character);
109+ void init (__DEVICESTREAMTYPE & comms, const char fld_separator, const char cmd_separator, const char esc_character);
94110 void reset ();
95111
96112 // **** Command processing ****
97113
98114 inline uint8_t processLine (char serialChar) __attribute__((always_inline));
99115 inline void handleMessage () __attribute__((always_inline));
100- inline bool blockedTillReply (unsigned int timeout = DEFAULT_TIMEOUT, byte ackCmdId = 1 ) __attribute__((always_inline));
101- inline bool checkForAck (byte AckCommand) __attribute__((always_inline));
116+ inline bool blockedTillReply (unsigned int timeout = DEFAULT_TIMEOUT, CmdMsgByte ackCmdId = 1 ) __attribute__((always_inline));
117+ inline bool checkForAck (CmdMsgByte AckCommand) __attribute__((always_inline));
102118
103119 // **** Command sending ****
104120
@@ -108,7 +124,7 @@ class CmdMessenger
108124 template < class T >
109125 void writeBin (const T & value)
110126 {
111- const byte *bytePointer = (const byte *)(const void *)&value;
127+ const CmdMsgByte *bytePointer = (const CmdMsgByte *)(const void *)&value;
112128 for (unsigned int i = 0 ; i < sizeof (value); i++)
113129 {
114130 printEsc (*bytePointer);
@@ -128,7 +144,7 @@ class CmdMessenger
128144 {
129145 T value;
130146 unescape (str);
131- byte *bytePointer = (byte *)(const void *)&value;
147+ CmdMsgByte *bytePointer = (CmdMsgByte *)(const void *)&value;
132148 for (unsigned int i = 0 ; i < sizeof (value); i++)
133149 {
134150 *bytePointer = str[i];
@@ -141,7 +157,7 @@ class CmdMessenger
141157 T empty ()
142158 {
143159 T value;
144- byte *bytePointer = (byte *)(const void *)&value;
160+ CmdMsgByte *bytePointer = (CmdMsgByte *)(const void *)&value;
145161 for (unsigned int i = 0 ; i < sizeof (value); i++)
146162 {
147163 *bytePointer = ' \0 ' ;
@@ -164,13 +180,13 @@ class CmdMessenger
164180
165181 // **** Initialization ****
166182
167- CmdMessenger (Stream & comms, const char fld_separator = ' ,' ,
183+ CmdMessenger (__DEVICESTREAMTYPE & comms, const char fld_separator = ' ,' ,
168184 const char cmd_separator = ' ;' ,
169185 const char esc_character = ' /' );
170186
171187 void printLfCr (bool addNewLine = true );
172188 void attach (messengerCallbackFunction newFunction);
173- void attach (byte msgId, messengerCallbackFunction newFunction);
189+ void attach (CmdMsgByte msgId, messengerCallbackFunction newFunction);
174190
175191 // **** Command processing ****
176192
@@ -187,7 +203,7 @@ class CmdMessenger
187203 * Note that the argument is sent as string
188204 */
189205 template < class T >
190- bool sendCmd (byte cmdId, T arg, bool reqAc = false , byte ackCmdId = 1 ,
206+ bool sendCmd (CmdMsgByte cmdId, T arg, bool reqAc = false , CmdMsgByte ackCmdId = 1 ,
191207 unsigned int timeout = DEFAULT_TIMEOUT)
192208 {
193209 if (!startCommand) {
@@ -203,7 +219,7 @@ class CmdMessenger
203219 * Note that the argument is sent in binary format
204220 */
205221 template < class T >
206- bool sendBinCmd (byte cmdId, T arg, bool reqAc = false , byte ackCmdId = 1 ,
222+ bool sendBinCmd (CmdMsgByte cmdId, T arg, bool reqAc = false , CmdMsgByte ackCmdId = 1 ,
207223 unsigned int timeout = DEFAULT_TIMEOUT)
208224 {
209225 if (!startCommand) {
@@ -214,27 +230,53 @@ class CmdMessenger
214230 return false ;
215231 }
216232
217- bool sendCmd (byte cmdId);
218- bool sendCmd (byte cmdId, bool reqAc, byte ackCmdId);
233+ bool sendCmd (CmdMsgByte cmdId);
234+ bool sendCmd (CmdMsgByte cmdId, bool reqAc, CmdMsgByte ackCmdId);
219235 // **** Command sending with multiple arguments ****
220236
221- void sendCmdStart (byte cmdId);
237+ void sendCmdStart (CmdMsgByte cmdId);
222238 void sendCmdEscArg (char *arg);
223239 void sendCmdfArg (char *fmt, ...);
224- bool sendCmdEnd (bool reqAc = false , byte ackCmdId = 1 , unsigned int timeout = DEFAULT_TIMEOUT);
240+ bool sendCmdEnd (bool reqAc = false , CmdMsgByte ackCmdId = 1 , unsigned int timeout = DEFAULT_TIMEOUT);
225241
226242 /* *
227243 * Send a single argument as string
228244 * Note that this will only succeed if a sendCmdStart has been issued first
229245 */
230246 template < class T > void sendCmdArg (T arg)
231247 {
232- if (startCommand) {
233- comms->print (field_separator);
234- comms->print (arg);
248+ if (startCommand) {
249+ #if !defined(MBED) // Arduino and ReadBear OK
250+ comms->print (field_separator);
251+ comms->print (arg);
252+ #else
253+ comms->putc (field_separator);
254+ comms->puts (arg);
255+ #endif
235256 }
257+
236258 }
237259
260+ #if defined(MBED)
261+ void sendCmdArg (bool arg) {
262+ comms->putc (field_separator);
263+ comms->printf (" %i" , arg);
264+ }
265+ void sendCmdArg (float arg) {
266+ comms->putc (field_separator);
267+ comms->printf (" %f" , arg);
268+ }
269+ void sendCmdArg (long arg) {
270+ comms->putc (field_separator);
271+ comms->printf (" %i" , arg);
272+ }
273+ void sendCmdArg (int arg) {
274+ comms->putc (field_separator);
275+ comms->printf (" %i" , arg);
276+ }
277+ #endif
278+
279+ #if !defined(MBED_STREAM_H)
238280 /* *
239281 * Send a single argument as string with custom accuracy
240282 * Note that this will only succeed if a sendCmdStart has been issued first
@@ -246,14 +288,15 @@ class CmdMessenger
246288 comms->print (arg, n);
247289 }
248290 }
291+ #endif
249292
250293 /* *
251294 * Send double argument in scientific format.
252295 * This will overcome the boundary of normal d sending which is limited to abs(f) <= MAXLONG
253296 */
254297 void sendCmdSciArg (double arg, unsigned int n = 6 );
255298
256-
299+ # if !defined(MBED_STREAM_H)
257300 /* *
258301 * Send a single argument in binary format
259302 * Note that this will only succeed if a sendCmdStart has been issued first
@@ -265,6 +308,7 @@ class CmdMessenger
265308 writeBin (arg);
266309 }
267310 }
311+ #endif
268312
269313 // **** Command receiving ****
270314 bool readBoolArg ();
0 commit comments