Compare commits

..

No commits in common. "read-until" and "master" have entirely different histories.

2 changed files with 14 additions and 10 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "SerialReader", "name": "SerialReader",
"version": "1.1.0", "version": "1.0.1",
"description": "Helper class for reading Serial input, without blocking", "description": "Helper class for reading Serial input, without blocking",
"repository": "repository":
{ {

View File

@ -7,24 +7,28 @@ class SerialReader {
char buffer[bufferLength]; char buffer[bufferLength];
public: public:
const char* getBuffer() { char* getBuffer() {
return buffer; return buffer;
} }
int readLine(HardwareSerial& serial, const char eol = '\n') { int readLine(HardwareSerial &serial) {
static size_t pos = 0; static size_t pos = 0;
size_t rpos; size_t rpos;
int readCh; int readCh;
for (int i = 0, avail = serial.available(); i < avail && (readCh = serial.read()) > 0; i++) { for (int i = 0, avail = serial.available(); i < avail && (readCh = serial.read()) > 0; i++) {
if (readCh == eol) { switch (readCh) {
case '\r': // Ignore CR
break;
case '\n': // Return on new-line
rpos = pos; rpos = pos;
pos = 0; // Reset position index ready for next time pos = 0; // Reset position index ready for next time
return rpos; return rpos;
} default:
if ((readCh != '\r') && (readCh != '\n') && (pos < bufferLength - 1)) { if (pos < bufferLength - 1) {
buffer[pos++] = readCh; buffer[pos++] = readCh;
buffer[pos] = 0; buffer[pos] = 0;
}
} }
} }
return 0; return 0;