Skip to content

Commit df345ff

Browse files
committed
Mbed compatible, not tested
1 parent 80a55a0 commit df345ff

2 files changed

Lines changed: 90 additions & 32 deletions

File tree

CmdMessenger.cpp

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,34 @@ extern "C" {
5050

5151
#define _CMDMESSENGER_VERSION 3_6 // software version of this library
5252

53-
// **** Initialization ****
53+
#if defined(__MBED__) || defined(MBED_STREAM_H)
54+
#define BYTEAVAILLABLE(comms) comms->readable()
55+
#define READONECHAR(comms) comms->getc()
56+
#define PRINTONECHAR(comms,c) comms->putc(c)
57+
#define PRINTSTRING(comms,s) comms->puts(s)
58+
#define millis() (us_ticker_read()/1000)
59+
#endif
60+
#if defined(ARDUINO) && !defined(MBED_STREAM_H)
61+
#define BYTEAVAILLABLE(comms) comms->available()
62+
#define READONECHAR(comms) comms->read()
63+
#define PRINTONECHAR(comms,c) comms->print(c)
64+
#define PRINTSTRING(comms,s) comms->print(s)
65+
#endif
66+
67+
// **** Initialization ****
5468

5569
/**
5670
* CmdMessenger constructor
5771
*/
58-
CmdMessenger::CmdMessenger(Stream &ccomms, const char fld_separator, const char cmd_separator, const char esc_character)
72+
CmdMessenger::CmdMessenger(__DEVICESTREAMTYPE &ccomms, const char fld_separator, const char cmd_separator, const char esc_character)
5973
{
6074
init(ccomms, fld_separator, cmd_separator, esc_character);
6175
}
6276

6377
/**
6478
* Enables printing newline after a sent command
6579
*/
66-
void CmdMessenger::init(Stream &ccomms, const char fld_separator, const char cmd_separator, const char esc_character)
80+
void CmdMessenger::init(__DEVICESTREAMTYPE &ccomms, const char fld_separator, const char cmd_separator, const char esc_character)
6781
{
6882
default_callback = NULL;
6983
comms = &ccomms;
@@ -112,7 +126,7 @@ void CmdMessenger::attach(messengerCallbackFunction newFunction)
112126
/**
113127
* Attaches a function to a command ID
114128
*/
115-
void CmdMessenger::attach(byte msgId, messengerCallbackFunction newFunction)
129+
void CmdMessenger::attach(CmdMsgByte msgId, messengerCallbackFunction newFunction)
116130
{
117131
if (msgId >= 0 && msgId < MAXCALLBACKS)
118132
callbackList[msgId] = newFunction;
@@ -127,7 +141,7 @@ void CmdMessenger::feedinSerialData()
127141
{
128142
while (!pauseProcessing && comms->available())
129143
{
130-
// The Stream class has a readBytes() function that reads many bytes at once. On Teensy 2.0 and 3.0, readBytes() is optimized.
144+
// The Stream class has a readBytes() function that reads many bytes at once. On Teensy 2.0 and 3.0, readBytes() is optimized.
131145
// Benchmarks about the incredible difference it makes: http://www.pjrc.com/teensy/benchmark_usb_serial_receive.html
132146

133147
size_t bytesAvailable = min(comms->available(), MAXSTREAMBUFFERSIZE);
@@ -188,7 +202,7 @@ void CmdMessenger::handleMessage()
188202
/**
189203
* Waits for reply from sender or timeout before continuing
190204
*/
191-
bool CmdMessenger::blockedTillReply(unsigned int timeout, byte ackCmdId)
205+
bool CmdMessenger::blockedTillReply(unsigned int timeout, CmdMsgByte ackCmdId)
192206
{
193207
unsigned long time = millis();
194208
unsigned long start = time;
@@ -203,10 +217,10 @@ bool CmdMessenger::blockedTillReply(unsigned int timeout, byte ackCmdId)
203217
/**
204218
* Loops as long data is available to determine if acknowledge has come in
205219
*/
206-
bool CmdMessenger::checkForAck(byte ackCommand)
220+
bool CmdMessenger::checkForAck(CmdMsgByte ackCommand)
207221
{
208222
while (comms->available()) {
209-
//Processes a byte and determines if an acknowlegde has come in
223+
//Processes a CmdMsgByte and determines if an acknowlegde has come in
210224
int messageState = processLine(comms->read());
211225
if (messageState == kEndOfMessage) {
212226
int id = readInt16Arg();
@@ -275,7 +289,7 @@ uint8_t CmdMessenger::commandID()
275289
/**
276290
* Send start of command. This makes it easy to send multiple arguments per command
277291
*/
278-
void CmdMessenger::sendCmdStart(byte cmdId)
292+
void CmdMessenger::sendCmdStart(CmdMsgByte cmdId)
279293
{
280294
if (!startCommand) {
281295
startCommand = true;
@@ -330,7 +344,7 @@ void CmdMessenger::sendCmdSciArg(double arg, unsigned int n)
330344
/**
331345
* Send end of command
332346
*/
333-
bool CmdMessenger::sendCmdEnd(bool reqAc, byte ackCmdId, unsigned int timeout)
347+
bool CmdMessenger::sendCmdEnd(bool reqAc, CmdMsgByte ackCmdId, unsigned int timeout)
334348
{
335349
bool ackReply = false;
336350
if (startCommand) {
@@ -349,7 +363,7 @@ bool CmdMessenger::sendCmdEnd(bool reqAc, byte ackCmdId, unsigned int timeout)
349363
/**
350364
* Send a command without arguments, with acknowledge
351365
*/
352-
bool CmdMessenger::sendCmd(byte cmdId, bool reqAc, byte ackCmdId)
366+
bool CmdMessenger::sendCmd(CmdMsgByte cmdId, bool reqAc, CmdMsgByte ackCmdId)
353367
{
354368
if (!startCommand) {
355369
sendCmdStart(cmdId);
@@ -361,7 +375,7 @@ bool CmdMessenger::sendCmd(byte cmdId, bool reqAc, byte ackCmdId)
361375
/**
362376
* Send a command without arguments, without acknowledge
363377
*/
364-
bool CmdMessenger::sendCmd(byte cmdId)
378+
bool CmdMessenger::sendCmd(CmdMsgByte cmdId)
365379
{
366380
if (!startCommand) {
367381
sendCmdStart(cmdId);
@@ -675,4 +689,4 @@ void CmdMessenger::printSci(double f, unsigned int digits)
675689
char output[16];
676690
sprintf(output, format, whole, part, exponent);
677691
comms->print(output);
678-
}
692+
}

CmdMessenger.h

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,22 @@
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

3753
extern "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

Comments
 (0)