38 lines
687 B
C++
38 lines
687 B
C++
#pragma once
|
|
|
|
#include <Arduino.h>
|
|
|
|
template <size_t bufferLength>
|
|
class SerialReader {
|
|
char buffer[bufferLength];
|
|
|
|
public:
|
|
char* getBuffer() {
|
|
return buffer;
|
|
}
|
|
|
|
int readLine(HardwareSerial &serial) {
|
|
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
|
|
rpos = pos;
|
|
pos = 0; // Reset position index ready for next time
|
|
return rpos;
|
|
default:
|
|
if (pos < bufferLength - 1) {
|
|
buffer[pos++] = readCh;
|
|
buffer[pos] = 0;
|
|
}
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
};
|