Skip to content

Commit

Permalink
Apply improvements by @sigmaeo
Browse files Browse the repository at this point in the history
Changes applied from:
<https://github.com/sigmaeo/Arduino-CmdMessenger/tree/eb8e413f1313dbf5012ae6b751a6246b41a4e5ad>

- Fix startCommand was not initialized
  not a problem on arduino, but with C++-Builder on Windows
- Add missing unescape() are missing (issue thijse#39)
- Make readBin() safe for less received bytes (see issue thijse#38)
- Replace Serial.print() with comms->print()
  • Loading branch information
MatthiasKunnen committed Aug 3, 2020
1 parent 458c51e commit a0d2340
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
12 changes: 8 additions & 4 deletions CmdMessenger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ char *CmdMessenger::readStringArg() {
if (next()) {
dumped = true;
ArgOk = true;
unescape(current);
return current;
}
ArgOk = false;
Expand All @@ -478,6 +479,7 @@ void CmdMessenger::copyStringArg(char *string, uint8_t size) {
if (next()) {
dumped = true;
ArgOk = true;
unescape(current);
strlcpy(string, current, size);
} else {
ArgOk = false;
Expand All @@ -490,6 +492,7 @@ void CmdMessenger::copyStringArg(char *string, uint8_t size) {
*/
uint8_t CmdMessenger::compareStringArg(char *string) {
if (next()) {
unescape(current);
if (strcmp(string, current) == 0) {
dumped = true;
ArgOk = true;
Expand Down Expand Up @@ -544,7 +547,8 @@ char *CmdMessenger::split_r(char *str, const char delim, char **nextp) {
// Set start of return pointer to this position
ret = str;
// Find next delimiter
str += findNext(str, delim);
LastArgLength = findNext(str, delim);
str += LastArgLength;
// and exchange this for a a \0 char. This will terminate the char
if (*str) {
*str++ = '\0';
Expand Down Expand Up @@ -596,18 +600,18 @@ void CmdMessenger::printEsc(char str) {
void CmdMessenger::printSci(double f, unsigned int digits) {
// handle sign
if (f < 0.0) {
Serial.print('-');
comms->print('-');
f = -f;
}

// handle infinite values
if (isinf(f)) {
Serial.print("INF");
comms->print("INF");
return;
}
// handle Not a Number
if (isnan(f)) {
Serial.print("NaN");
comms->print("NaN");
return;
}

Expand Down
8 changes: 7 additions & 1 deletion CmdMessenger.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class CmdMessenger {
uint8_t bufferIndex; // Index where to write data in buffer
uint8_t bufferLength; // Is set to MESSENGERBUFFERSIZE
uint8_t bufferLastIndex; // The last index of the buffer
uint8_t LastArgLength; // The length of the last received argument
char ArglastChar; // Bookkeeping of argument escape char
char CmdlastChar; // Bookkeeping of command escape char
bool pauseProcessing; // pauses processing of new commands, during sending
Expand Down Expand Up @@ -134,7 +135,10 @@ class CmdMessenger {
unescape(str);
byte *bytePointer = (byte *) (const void *) &value;
for (unsigned int i = 0; i < sizeof(value); i++) {
*bytePointer = str[i];
*bytePointer = 0;
if (i < LastArgLength) {
*bytePointer = str[i];
}
bytePointer++;
}
return value;
Expand Down Expand Up @@ -308,8 +312,10 @@ class CmdMessenger {
template<class T> T readBinArg() {
if (next()) {
dumped = true;
ArgOk = true;
return readBin<T>(current);
} else {
ArgOk = false;
return empty<T>();
}
}
Expand Down

0 comments on commit a0d2340

Please sign in to comment.