From 2aaf854ac213909038e51e3f7be4ad1d7d6b6dca Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Fri, 20 Jan 2023 03:11:48 +0100 Subject: [PATCH] first version --- library.json | 21 +++++++++++++++++++++ src/SerialReader.h | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 library.json create mode 100644 src/SerialReader.h diff --git a/library.json b/library.json new file mode 100644 index 0000000..2f1cee2 --- /dev/null +++ b/library.json @@ -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": "*" + } diff --git a/src/SerialReader.h b/src/SerialReader.h new file mode 100644 index 0000000..77d6e09 --- /dev/null +++ b/src/SerialReader.h @@ -0,0 +1,37 @@ +#pragma once + +#include + +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; + } + +};