From 70e5a7e2cee6f62d46116709133d1c4ccb20ae11 Mon Sep 17 00:00:00 2001 From: Slavey Karadzhov Date: Wed, 22 Nov 2017 18:43:10 +0100 Subject: [PATCH] Added Stream::indexOf(char c) that finds a character in a stream without advancing the internal stream pointers. Fixes: #1214. --- Sming/SmingCore/HardwareSerial.cpp | 23 +++++++++++++++++++++++ Sming/SmingCore/HardwareSerial.h | 7 +++++++ Sming/Wiring/Stream.h | 9 +++++++++ samples/Basic_rBoot/app/application.cpp | 8 ++++---- 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/Sming/SmingCore/HardwareSerial.cpp b/Sming/SmingCore/HardwareSerial.cpp index 37fede7487..3b08f0aa82 100644 --- a/Sming/SmingCore/HardwareSerial.cpp +++ b/Sming/SmingCore/HardwareSerial.cpp @@ -290,5 +290,28 @@ HardwareSerial::operator bool() const return uart != 0; } +size_t HardwareSerial::indexOf(char c) +{ + int offset = uart->rx_buffer->rpos; + int pos = 0; + while(pos < available()) { + if(uart->rx_buffer->buffer[offset + pos] == c) { + return pos; + } + + pos++; + + if(pos + offset == uart->rx_buffer->wpos) { + break; + } + + if(pos + offset == uart->rx_buffer->size) { + offset = -pos; + } + } + + return -1; +} + HardwareSerial Serial(UART_ID_0); diff --git a/Sming/SmingCore/HardwareSerial.h b/Sming/SmingCore/HardwareSerial.h index a6c9f03c3e..bd7e36f9c9 100644 --- a/Sming/SmingCore/HardwareSerial.h +++ b/Sming/SmingCore/HardwareSerial.h @@ -252,6 +252,13 @@ class HardwareSerial : public Stream */ operator bool() const; + /* + * @brief Returns the location of the searched character + * @param char c - character to search for + * @retval size_t -1 if not found 0 or positive number otherwise + */ + size_t indexOf(char c); + private: int uartNr; static HWSerialMemberData memberData[NUMBER_UARTS]; diff --git a/Sming/Wiring/Stream.h b/Sming/Wiring/Stream.h index 807a0aadb5..e8ebc9510c 100644 --- a/Sming/Wiring/Stream.h +++ b/Sming/Wiring/Stream.h @@ -76,6 +76,15 @@ class Stream : public Print // Wiring String functions to be added here String readString(); String readStringUntil(char terminator); + + /* + * @brief Returns the location of the searched character + * @param char c - character to search for + * @retval size_t -1 if not found 0 or positive number otherwise + */ + virtual size_t indexOf(char c) { + return -1; + } protected: long parseInt(char skipChar); // as above but the given skipChar is ignored diff --git a/samples/Basic_rBoot/app/application.cpp b/samples/Basic_rBoot/app/application.cpp index 48c7567e97..421ecc1987 100644 --- a/samples/Basic_rBoot/app/application.cpp +++ b/samples/Basic_rBoot/app/application.cpp @@ -98,10 +98,10 @@ void ShowInfo() { } void serialCallBack(Stream& stream, char arrivedChar, unsigned short availableCharsCount) { - - if (arrivedChar == '\n') { - char str[availableCharsCount]; - for (int i = 0; i < availableCharsCount; i++) { + int pos = stream.indexOf('\n'); + if(pos > -1) { + char str[pos + 1]; + for (int i = 0; i < pos + 1; i++) { str[i] = stream.read(); if (str[i] == '\r' || str[i] == '\n') { str[i] = '\0';