first version

This commit is contained in:
Nicu Hodos 2023-01-20 03:11:48 +01:00
commit 2aaf854ac2
2 changed files with 58 additions and 0 deletions

21
library.json Normal file
View File

@ -0,0 +1,21 @@
{
"name": "SerialReader",
"version": "1.0.0",
"description": "Helper class for reading Serial input, without blocking",
"repository":
{
"type": "git",
"url": "https://git.hodos.ro/arduino/lib_serial-reader.git"
},
"authors":
[
{
"name": "Nicu Hodos",
"email": "nicu@hodos.ro",
"maintainer": true
}
],
"license": "MIT",
"frameworks": "arduino",
"platforms": "*"
}

37
src/SerialReader.h Normal file
View File

@ -0,0 +1,37 @@
#pragma once
#include <Arduino.h>
class SerialReader {
static const int bufferLength = 50;
char buffer[bufferLength];
public:
char* getBuffer() {
return buffer;
}
int readLine(SerialUART &serial) {
static int pos = 0;
int 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;
}
};