From d6d76a39533249530d9e9977cfa12f8e990777fe Mon Sep 17 00:00:00 2001 From: Victor Tseng Date: Wed, 7 Mar 2018 23:25:44 +0800 Subject: [PATCH 1/3] rename define constants avoid name collision --- CmdMessenger.cpp | 18 +++++++++--------- CmdMessenger.h | 24 ++++++++++++------------ 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/CmdMessenger.cpp b/CmdMessenger.cpp index cbb4ee7..1e43190 100644 --- a/CmdMessenger.cpp +++ b/CmdMessenger.cpp @@ -71,12 +71,12 @@ void CmdMessenger::init(Stream &ccomms, const char fld_separator, const char cmd field_separator = fld_separator; command_separator = cmd_separator; escape_character = esc_character; - bufferLength = MESSENGERBUFFERSIZE; - bufferLastIndex = MESSENGERBUFFERSIZE - 1; + bufferLength = CMDMESSENGER_MESSENGERBUFFERSIZE; + bufferLastIndex = CMDMESSENGER_MESSENGERBUFFERSIZE - 1; reset(); default_callback = NULL; - for (int i = 0; i < MAXCALLBACKS; i++) + for (int i = 0; i < CMDMESSENGER_MAXCALLBACKS; i++) callbackList[i] = NULL; pauseProcessing = false; @@ -114,7 +114,7 @@ void CmdMessenger::attach(messengerCallbackFunction newFunction) */ void CmdMessenger::attach(byte msgId, messengerCallbackFunction newFunction) { - if (msgId >= 0 && msgId < MAXCALLBACKS) + if (msgId >= 0 && msgId < CMDMESSENGER_MAXCALLBACKS) callbackList[msgId] = newFunction; } @@ -130,7 +130,7 @@ void CmdMessenger::feedinSerialData() // The Stream class has a readBytes() function that reads many bytes at once. On Teensy 2.0 and 3.0, readBytes() is optimized. // Benchmarks about the incredible difference it makes: http://www.pjrc.com/teensy/benchmark_usb_serial_receive.html - size_t bytesAvailable = min(comms->available(), MAXSTREAMBUFFERSIZE); + size_t bytesAvailable = min(comms->available(), CMDMESSENGER_MAXSTREAMBUFFERSIZE); comms->readBytes(streamBuffer, bytesAvailable); // Process the bytes in the stream buffer, and handles dispatches callbacks, if commands are received @@ -179,7 +179,7 @@ void CmdMessenger::handleMessage() { lastCommandId = readInt16Arg(); // if command attached, we will call it - if (lastCommandId >= 0 && lastCommandId < MAXCALLBACKS && ArgOk && callbackList[lastCommandId] != NULL) + if (lastCommandId >= 0 && lastCommandId < CMDMESSENGER_MAXCALLBACKS && ArgOk && callbackList[lastCommandId] != NULL) (*callbackList[lastCommandId])(); else // If command not attached, call default callback (if attached) if (default_callback != NULL) (*default_callback)(); @@ -353,7 +353,7 @@ bool CmdMessenger::sendCmd(byte cmdId, bool reqAc, byte ackCmdId) { if (!startCommand) { sendCmdStart(cmdId); - return sendCmdEnd(reqAc, ackCmdId, DEFAULT_TIMEOUT); + return sendCmdEnd(reqAc, ackCmdId, CMDMESSENGER_DEFAULT_TIMEOUT); } return false; } @@ -365,7 +365,7 @@ bool CmdMessenger::sendCmd(byte cmdId) { if (!startCommand) { sendCmdStart(cmdId); - return sendCmdEnd(false, 1, DEFAULT_TIMEOUT); + return sendCmdEnd(false, 1, CMDMESSENGER_DEFAULT_TIMEOUT); } return false; } @@ -675,4 +675,4 @@ void CmdMessenger::printSci(double f, unsigned int digits) char output[16]; sprintf(output, format, whole, part, exponent); comms->print(output); -} \ No newline at end of file +} diff --git a/CmdMessenger.h b/CmdMessenger.h index 1072bd7..1943237 100644 --- a/CmdMessenger.h +++ b/CmdMessenger.h @@ -40,10 +40,10 @@ extern "C" typedef void(*messengerCallbackFunction) (void); } -#define MAXCALLBACKS 50 // The maximum number of commands (default: 50) -#define MESSENGERBUFFERSIZE 64 // The length of the commandbuffer (default: 64) -#define MAXSTREAMBUFFERSIZE 512 // The length of the streambuffer (default: 64) -#define DEFAULT_TIMEOUT 5000 // Time out on unanswered messages. (default: 5s) +#define CMDMESSENGER_MAXCALLBACKS 50 // The maximum number of commands (default: 50) +#define CMDMESSENGER_MESSENGERBUFFERSIZE 64 // The length of the commandbuffer (default: 64) +#define CMDMESSENGER_MAXSTREAMBUFFERSIZE 512 // The length of the streambuffer (default: 64) +#define CMDMESSENGER_DEFAULT_TIMEOUT 5000 // Time out on unanswered messages. (default: 5s) // Message States enum @@ -64,14 +64,14 @@ class CmdMessenger bool startCommand; // Indicates if sending of a command is underway uint8_t lastCommandId; // ID of last received command uint8_t bufferIndex; // Index where to write data in buffer - uint8_t bufferLength; // Is set to MESSENGERBUFFERSIZE + uint8_t bufferLength; // Is set to CMDMESSENGER_MESSENGERBUFFERSIZE uint8_t bufferLastIndex; // The last index of the buffer char ArglastChar; // Bookkeeping of argument escape char char CmdlastChar; // Bookkeeping of command escape char bool pauseProcessing; // pauses processing of new commands, during sending bool print_newlines; // Indicates if \r\n should be added after send command - char commandBuffer[MESSENGERBUFFERSIZE]; // Buffer that holds the data - char streamBuffer[MAXSTREAMBUFFERSIZE]; // Buffer that holds the data + char commandBuffer[CMDMESSENGER_MESSENGERBUFFERSIZE]; // Buffer that holds the data + char streamBuffer[CMDMESSENGER_MAXSTREAMBUFFERSIZE]; // Buffer that holds the data uint8_t messageState; // Current state of message processing bool dumped; // Indicates if last argument has been externally read bool ArgOk; // Indicated if last fetched argument could be read @@ -85,7 +85,7 @@ class CmdMessenger char escape_character; // Character indicating escaping of special chars messengerCallbackFunction default_callback; // default callback function - messengerCallbackFunction callbackList[MAXCALLBACKS]; // list of attached callback functions + messengerCallbackFunction callbackList[CMDMESSENGER_MAXCALLBACKS]; // list of attached callback functions // **** Initialize **** @@ -97,7 +97,7 @@ class CmdMessenger inline uint8_t processLine(char serialChar) __attribute__((always_inline)); inline void handleMessage() __attribute__((always_inline)); - inline bool blockedTillReply(unsigned int timeout = DEFAULT_TIMEOUT, byte ackCmdId = 1) __attribute__((always_inline)); + inline bool blockedTillReply(unsigned int timeout = CMDMESSENGER_DEFAULT_TIMEOUT, byte ackCmdId = 1) __attribute__((always_inline)); inline bool checkForAck(byte AckCommand) __attribute__((always_inline)); // **** Command sending **** @@ -188,7 +188,7 @@ class CmdMessenger */ template < class T > bool sendCmd(byte cmdId, T arg, bool reqAc = false, byte ackCmdId = 1, - unsigned int timeout = DEFAULT_TIMEOUT) + unsigned int timeout = CMDMESSENGER_DEFAULT_TIMEOUT) { if (!startCommand) { sendCmdStart(cmdId); @@ -204,7 +204,7 @@ class CmdMessenger */ template < class T > bool sendBinCmd(byte cmdId, T arg, bool reqAc = false, byte ackCmdId = 1, - unsigned int timeout = DEFAULT_TIMEOUT) + unsigned int timeout = CMDMESSENGER_DEFAULT_TIMEOUT) { if (!startCommand) { sendCmdStart(cmdId); @@ -221,7 +221,7 @@ class CmdMessenger void sendCmdStart(byte cmdId); void sendCmdEscArg(char *arg); void sendCmdfArg(char *fmt, ...); - bool sendCmdEnd(bool reqAc = false, byte ackCmdId = 1, unsigned int timeout = DEFAULT_TIMEOUT); + bool sendCmdEnd(bool reqAc = false, byte ackCmdId = 1, unsigned int timeout = CMDMESSENGER_DEFAULT_TIMEOUT); /** * Send a single argument as string From 2fe75ac3c48ea8b05236060faeeb0b13a02bd110 Mon Sep 17 00:00:00 2001 From: Victor Tseng Date: Wed, 7 Mar 2018 23:27:59 +0800 Subject: [PATCH 2/3] guard define constants so we can override them with compiler flags, or #define them before #include . --- CmdMessenger.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CmdMessenger.h b/CmdMessenger.h index 1943237..11919ca 100644 --- a/CmdMessenger.h +++ b/CmdMessenger.h @@ -40,10 +40,18 @@ extern "C" typedef void(*messengerCallbackFunction) (void); } +#ifndef CMDMESSENGER_MAXCALLBACKS #define CMDMESSENGER_MAXCALLBACKS 50 // The maximum number of commands (default: 50) +#endif +#ifndef CMDMESSENGER_MESSENGERBUFFERSIZE #define CMDMESSENGER_MESSENGERBUFFERSIZE 64 // The length of the commandbuffer (default: 64) +#endif +#ifndef CMDMESSENGER_MAXSTREAMBUFFERSIZE #define CMDMESSENGER_MAXSTREAMBUFFERSIZE 512 // The length of the streambuffer (default: 64) +#endif +#ifndef CMDMESSENGER_DEFAULT_TIMEOUT #define CMDMESSENGER_DEFAULT_TIMEOUT 5000 // Time out on unanswered messages. (default: 5s) +#endif // Message States enum From 7c5a722d62bdf04cc826b6f48079769ec0c64074 Mon Sep 17 00:00:00 2001 From: Victor Tseng Date: Wed, 7 Mar 2018 23:29:32 +0800 Subject: [PATCH 3/3] do not instantiate callback list when MAXCALLBACKS is 0 increase performance, reduce memory and binary footprint. --- CmdMessenger.cpp | 6 ++++++ CmdMessenger.h | 2 ++ 2 files changed, 8 insertions(+) diff --git a/CmdMessenger.cpp b/CmdMessenger.cpp index 1e43190..174de52 100644 --- a/CmdMessenger.cpp +++ b/CmdMessenger.cpp @@ -76,8 +76,10 @@ void CmdMessenger::init(Stream &ccomms, const char fld_separator, const char cmd reset(); default_callback = NULL; +#if CMDMESSENGER_MAXCALLBACKS != 0 for (int i = 0; i < CMDMESSENGER_MAXCALLBACKS; i++) callbackList[i] = NULL; +#endif pauseProcessing = false; } @@ -114,8 +116,10 @@ void CmdMessenger::attach(messengerCallbackFunction newFunction) */ void CmdMessenger::attach(byte msgId, messengerCallbackFunction newFunction) { +#if CMDMESSENGER_MAXCALLBACKS != 0 if (msgId >= 0 && msgId < CMDMESSENGER_MAXCALLBACKS) callbackList[msgId] = newFunction; +#endif } // **** Command processing **** @@ -179,9 +183,11 @@ void CmdMessenger::handleMessage() { lastCommandId = readInt16Arg(); // if command attached, we will call it +#if CMDMESSENGER_MAXCALLBACKS != 0 if (lastCommandId >= 0 && lastCommandId < CMDMESSENGER_MAXCALLBACKS && ArgOk && callbackList[lastCommandId] != NULL) (*callbackList[lastCommandId])(); else // If command not attached, call default callback (if attached) +#endif if (default_callback != NULL) (*default_callback)(); } diff --git a/CmdMessenger.h b/CmdMessenger.h index 11919ca..214dac7 100644 --- a/CmdMessenger.h +++ b/CmdMessenger.h @@ -93,7 +93,9 @@ class CmdMessenger char escape_character; // Character indicating escaping of special chars messengerCallbackFunction default_callback; // default callback function +#if CMDMESSENGER_MAXCALLBACKS != 0 messengerCallbackFunction callbackList[CMDMESSENGER_MAXCALLBACKS]; // list of attached callback functions +#endif // **** Initialize ****