StreamCommand.ino and ex2.
MIT License. See the LICENSE file for details.
uint16_t max_len)
: _stream(stream)
{
_buffer = buffer;
_buffer[0] = '\0';
_max_len = max_len;
_cur_len = 0;
_fTerminated = false;
}
char c;
if (_stream.available()) {
_fTerminated = false;
while (_stream.available()) {
c = _stream.peek();
if (c == 13) {
_stream.read();
} else if (c == 10) {
_stream.read();
_buffer[_cur_len] = '\0';
_fTerminated = true;
break;
} else if (_cur_len < _max_len - 1) {
_stream.read();
_buffer[_cur_len] = c;
_cur_len++;
} else {
_buffer[_cur_len] = '\0';
_fTerminated = true;
break;
}
}
}
return _fTerminated;
}
if (_fTerminated) {
_fTerminated = false;
_cur_len = 0;
return _buffer;
} else {
return (char *)_empty;
}
}
uint8_t *buffer,
uint16_t max_len,
const uint8_t *EOL,
uint8_t EOL_len)
: _stream(stream)
{
_buffer = buffer;
_buffer[0] = 0;
_max_len = max_len;
_cur_len = 0;
_EOL = EOL;
_EOL_len = EOL_len;
_found_EOL = false;
}
uint8_t c;
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 {
return -1;
}
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;
}
}
if (_found_EOL) {
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;
}
if (strlen(str_in) > pos) {
return (float)atof(&str_in[pos]);
} else {
return 0.0f;
}
}
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;
}
}
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