From d3cb43c1c981ee3726d26c05ec3e42fffaa080c6 Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Sun, 4 Jul 2010 23:31:55 +0000 Subject: Adding a peek() function to Stream and HardwareSerial (Serial). --- cores/arduino/HardwareSerial.cpp | 9 +++++++++ cores/arduino/HardwareSerial.h | 1 + cores/arduino/Stream.h | 1 + 3 files changed, 11 insertions(+) (limited to 'cores/arduino') diff --git a/cores/arduino/HardwareSerial.cpp b/cores/arduino/HardwareSerial.cpp index 638b6d1..f8cdebe 100755 --- a/cores/arduino/HardwareSerial.cpp +++ b/cores/arduino/HardwareSerial.cpp @@ -178,6 +178,15 @@ uint8_t HardwareSerial::available(void) return (RX_BUFFER_SIZE + _rx_buffer->head - _rx_buffer->tail) % RX_BUFFER_SIZE; } +int HardwareSerial::peek(void) +{ + if (_rx_buffer->head == _rx_buffer->tail) { + return -1; + } else { + return _rx_buffer->buffer[_rx_buffer->tail]; + } +} + int HardwareSerial::read(void) { // if the head isn't ahead of the tail, we don't have any characters diff --git a/cores/arduino/HardwareSerial.h b/cores/arduino/HardwareSerial.h index cae1447..884f9fe 100755 --- a/cores/arduino/HardwareSerial.h +++ b/cores/arduino/HardwareSerial.h @@ -49,6 +49,7 @@ class HardwareSerial : public Stream void begin(long); void end(); virtual uint8_t available(void); + virtual int peek(void); virtual int read(void); virtual void flush(void); virtual void write(uint8_t); diff --git a/cores/arduino/Stream.h b/cores/arduino/Stream.h index 3bf5283..1d3e50b 100644 --- a/cores/arduino/Stream.h +++ b/cores/arduino/Stream.h @@ -27,6 +27,7 @@ class Stream : public Print { public: virtual uint8_t available() = 0; + virtual int peek() = 0; virtual int read() = 0; virtual void flush() = 0; }; -- cgit v1.2.3-18-g5258