From 299ac12422e0064032a3021c21c20bee62b482b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juraj=20Andr=C3=A1ssy?= Date: Tue, 14 May 2024 09:55:55 +0200 Subject: [PATCH] Stream - new method `clear()` to clear the receive buffer --- api/Stream.cpp | 5 +++++ api/Stream.h | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/api/Stream.cpp b/api/Stream.cpp index f6f9bda6..577c5845 100644 --- a/api/Stream.cpp +++ b/api/Stream.cpp @@ -86,6 +86,11 @@ int Stream::peekNextDigit(LookaheadMode lookahead, bool detectDecimal) // Public Methods ////////////////////////////////////////////////////////////// +void Stream::clear() { + while (available()) + read(); +} + void Stream::setTimeout(unsigned long timeout) // sets the maximum number of milliseconds to wait { _timeout = timeout; diff --git a/api/Stream.h b/api/Stream.h index e81c71ba..fb28ea95 100644 --- a/api/Stream.h +++ b/api/Stream.h @@ -61,6 +61,8 @@ class Stream : public Print virtual int read() = 0; virtual int peek() = 0; + virtual void clear(); // clears the receive buffer + Stream() {_timeout=1000;} // parsing methods @@ -130,4 +132,4 @@ class Stream : public Print } -using arduino::Stream; \ No newline at end of file +using arduino::Stream;