Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce mem footprint #35

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions CmdMessenger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 ****
Expand All @@ -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
Expand Down Expand Up @@ -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)();
}

Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -675,4 +681,4 @@ void CmdMessenger::printSci(double f, unsigned int digits)
char output[16];
sprintf(output, format, whole, part, exponent);
comms->print(output);
}
}
34 changes: 22 additions & 12 deletions CmdMessenger.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 ****
Expand All @@ -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 ****
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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
Expand Down