Compare commits
2 Commits
master
...
read-until
| Author | SHA1 | Date | |
|---|---|---|---|
| 9f462969c1 | |||
| a3cb224206 |
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "SerialReader",
|
||||
"version": "1.0.1",
|
||||
"version": "1.1.0",
|
||||
"description": "Helper class for reading Serial input, without blocking",
|
||||
"repository":
|
||||
{
|
||||
|
||||
@ -7,28 +7,24 @@ class SerialReader {
|
||||
char buffer[bufferLength];
|
||||
|
||||
public:
|
||||
char* getBuffer() {
|
||||
return buffer;
|
||||
}
|
||||
const char* getBuffer() {
|
||||
return buffer;
|
||||
}
|
||||
|
||||
int readLine(HardwareSerial &serial) {
|
||||
int readLine(HardwareSerial& serial, const char eol = '\n') {
|
||||
static size_t pos = 0;
|
||||
size_t rpos;
|
||||
int readCh;
|
||||
|
||||
for (int i = 0, avail = serial.available(); i < avail && (readCh = serial.read()) > 0; i++) {
|
||||
switch (readCh) {
|
||||
case '\r': // Ignore CR
|
||||
break;
|
||||
case '\n': // Return on new-line
|
||||
if (readCh == eol) {
|
||||
rpos = pos;
|
||||
pos = 0; // Reset position index ready for next time
|
||||
return rpos;
|
||||
default:
|
||||
if (pos < bufferLength - 1) {
|
||||
buffer[pos++] = readCh;
|
||||
buffer[pos] = 0;
|
||||
}
|
||||
}
|
||||
if ((readCh != '\r') && (readCh != '\n') && (pos < bufferLength - 1)) {
|
||||
buffer[pos++] = readCh;
|
||||
buffer[pos] = 0;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user