Skip to content

Commit

Permalink
Added Stream::indexOf(char c) that finds a character in a stream
Browse files Browse the repository at this point in the history
without advancing the internal stream pointers.

Fixes: #1214.
  • Loading branch information
slav-at-attachix committed Nov 22, 2017
1 parent f1e685f commit 70e5a7e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
23 changes: 23 additions & 0 deletions Sming/SmingCore/HardwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
7 changes: 7 additions & 0 deletions Sming/SmingCore/HardwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
9 changes: 9 additions & 0 deletions Sming/Wiring/Stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions samples/Basic_rBoot/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 70e5a7e

Please sign in to comment.