diff --git a/CmdMessenger.cpp b/CmdMessenger.cpp index cbb4ee7..174de52 100644 --- a/CmdMessenger.cpp +++ b/CmdMessenger.cpp @@ -71,13 +71,15 @@ 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++) +#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 (msgId >= 0 && msgId < MAXCALLBACKS) +#if CMDMESSENGER_MAXCALLBACKS != 0 + if (msgId >= 0 && msgId < CMDMESSENGER_MAXCALLBACKS) callbackList[msgId] = newFunction; +#endif } // **** Command processing **** @@ -130,7 +134,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,9 +183,11 @@ void CmdMessenger::handleMessage() { lastCommandId = readInt16Arg(); // if command attached, we will call it - if (lastCommandId >= 0 && lastCommandId < MAXCALLBACKS && ArgOk && callbackList[lastCommandId] != NULL) +#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)(); } @@ -353,7 +359,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 +371,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 +681,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..214dac7 100644 --- a/CmdMessenger.h +++ b/CmdMessenger.h @@ -40,10 +40,18 @@ 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) +#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 @@ -64,14 +72,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 +93,9 @@ 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 +#if CMDMESSENGER_MAXCALLBACKS != 0 + messengerCallbackFunction callbackList[CMDMESSENGER_MAXCALLBACKS]; // list of attached callback functions +#endif // **** Initialize **** @@ -97,7 +107,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 +198,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 +214,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 +231,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