first version
This commit is contained in:
commit
2aaf854ac2
21
library.json
Normal file
21
library.json
Normal 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
37
src/SerialReader.h
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user