DvG_StreamCommand
E:/Work/_GitHub_repo/DvG_StreamCommand/src/DvG_StreamCommand.cpp

StreamCommand.ino and ex2.

Author

Dennis van Gils (vangi.nosp@m.ls.d.nosp@m.ennis.nosp@m.@gma.nosp@m.il.co.nosp@m.m)

Version

Changelog

License

MIT License. See the LICENSE file for details.

/*
* @section ex1 Example: ASCII commands over Serial
* @include examples/StreamCommand/StreamCommand.ino
*
* @section ex2 Example: Binary commands over Serial
* @include examples/BinaryStreamCommand/BinaryStreamCommand.ino
*/
/*******************************************************************************
DvG_StreamCommand
*******************************************************************************/
DvG_StreamCommand::DvG_StreamCommand(Stream &stream, char *buffer,
uint16_t max_len)
: _stream(stream) // Initialize reference before body
{
_buffer = buffer;
_buffer[0] = '\0';
_max_len = max_len;
_cur_len = 0;
_fTerminated = false;
}
char c;
// Poll the input buffer of the stream for data
if (_stream.available()) {
_fTerminated = false;
while (_stream.available()) {
c = _stream.peek();
if (c == 13) {
// Ignore ASCII 13 (carriage return)
_stream.read(); // Remove char from the stream input buffer
} else if (c == 10) {
// Found ASCII 10 (line feed) --> Terminate string
_stream.read(); // Remove char from the stream input buffer
_buffer[_cur_len] = '\0';
_fTerminated = true;
break;
} else if (_cur_len < _max_len - 1) {
// Append char to string
_stream.read(); // Remove char from the stream input buffer
_buffer[_cur_len] = c;
_cur_len++;
} else {
// Maximum buffer length is reached. Forcefully terminate the string
// in the command buffer now. Leave the char in the stream input buffer.
_buffer[_cur_len] = '\0';
_fTerminated = true;
break;
}
}
}
return _fTerminated;
}
if (_fTerminated) {
_fTerminated = false;
_cur_len = 0;
return _buffer;
} else {
return (char *)_empty;
}
}
/*******************************************************************************
DvG_BinaryStreamCommand
*******************************************************************************/
uint8_t *buffer,
uint16_t max_len,
const uint8_t *EOL,
uint8_t EOL_len)
: _stream(stream) // Initialize reference before body
{
_buffer = buffer;
_buffer[0] = 0;
_max_len = max_len;
_cur_len = 0;
_EOL = EOL;
_EOL_len = EOL_len;
_found_EOL = false;
}
int8_t DvG_BinaryStreamCommand::available(bool debug_info) {
uint8_t c;
// Poll the input buffer of the stream for data
while (_stream.available()) {
c = _stream.read();
if (debug_info) {
_stream.print(c, HEX);
_stream.write('\t');
}
if (_cur_len < _max_len) {
_buffer[_cur_len] = c;
_cur_len++;
} else {
// Maximum buffer length is reached. Drop the byte and return the special
// value of -1 to signal the user.
return -1;
}
// Check for EOL at the end
if (_cur_len >= _EOL_len) {
_found_EOL = true;
for (uint8_t i = 0; i < _EOL_len; ++i) {
if (_buffer[_cur_len - i - 1] != _EOL[_EOL_len - i - 1]) {
_found_EOL = false;
break; // Any mismatch will exit early
}
}
if (_found_EOL) {
// Wait with reading in more bytes from the stream input buffer to let
// the user act upon the currently received command
if (debug_info) {
_stream.print("EOL\t");
}
break;
}
}
}
return _found_EOL;
}
uint16_t len;
if (_found_EOL) {
len = _cur_len - _EOL_len;
_found_EOL = false;
_cur_len = 0;
} else {
len = 0;
}
return len;
}
/*******************************************************************************
Parse functions
*******************************************************************************/
float parseFloatInString(const char *str_in, uint16_t pos) {
if (strlen(str_in) > pos) {
return (float)atof(&str_in[pos]);
} else {
return 0.0f;
}
}
bool parseBoolInString(const char *str_in, uint16_t pos) {
if (strlen(str_in) > pos) {
if (strncmp(&str_in[pos], "true", 4) == 0 || //
strncmp(&str_in[pos], "True", 4) == 0 || //
strncmp(&str_in[pos], "TRUE", 4) == 0) {
return true;
}
return (atoi(&str_in[pos]) != 0);
} else {
return false;
}
}
int parseIntInString(const char *str_in, uint16_t pos) {
if (strlen(str_in) > pos) {
return atoi(&str_in[pos]);
} else {
return 0;
}
}
int parseIntInString(const char *str_in, uint16_t pos)
Safely parse an integer value in C-string str_in from of position pos.
Definition: DvG_StreamCommand.cpp:214
bool parseBoolInString(const char *str_in, uint16_t pos)
Safely parse a boolean value in C-string str_in from of position pos.
Definition: DvG_StreamCommand.cpp:201
float parseFloatInString(const char *str_in, uint16_t pos)
Safely parse a float value in C-string str_in from of position pos.
Definition: DvG_StreamCommand.cpp:193
A lightweight Arduino library to listen to a stream, such as Serial or Wire, for incoming commands (o...
int8_t available(bool debug_info=false)
Poll the stream for incoming bytes and append them one-by-one to the command buffer buffer....
Definition: DvG_StreamCommand.cpp:131
DvG_BinaryStreamCommand(Stream &stream, uint8_t *buffer, uint16_t max_len, const uint8_t *EOL, uint8_t EOL_len)
Construct a new DvG_BinaryStreamCommand object.
Definition: DvG_StreamCommand.cpp:115
uint16_t getCommandLength()
Return the length of the command without the EOL sentinel in bytes, only when a complete command has ...
Definition: DvG_StreamCommand.cpp:174
DvG_StreamCommand(Stream &stream, char *buffer, uint16_t max_len)
Construct a new DvG_StreamCommand object.
Definition: DvG_StreamCommand.cpp:50
char * getCommand()
Return the reference to the command buffer only when a complete command has been received....
Definition: DvG_StreamCommand.cpp:100
bool available()
Poll the stream for incoming characters and append them one-by-one to the command buffer buffer....
Definition: DvG_StreamCommand.cpp:61