From 31601ffe77682dfd54958191ee18f79c772a6226 Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Sun, 4 Jul 2010 23:31:55 +0000 Subject: [PATCH] Adding a peek() function to Stream and HardwareSerial (Serial). --- build/shared/lib/keywords.txt | 1 + hardware/arduino/cores/arduino/HardwareSerial.cpp | 9 +++++++++ hardware/arduino/cores/arduino/HardwareSerial.h | 1 + hardware/arduino/cores/arduino/Stream.h | 1 + 4 files changed, 12 insertions(+) diff --git a/build/shared/lib/keywords.txt b/build/shared/lib/keywords.txt index fbf8f3943..770acfbd8 100644 --- a/build/shared/lib/keywords.txt +++ b/build/shared/lib/keywords.txt @@ -163,6 +163,7 @@ Serial2 KEYWORD3 Serial Serial3 KEYWORD3 Serial begin KEYWORD2 Serial_Begin end KEYWORD2 Serial_End +peek KEYWORD2 Serial_Peek read KEYWORD2 Serial_Read print KEYWORD2 Serial_Print println KEYWORD2 Serial_Println diff --git a/hardware/arduino/cores/arduino/HardwareSerial.cpp b/hardware/arduino/cores/arduino/HardwareSerial.cpp index 638b6d1b0..f8cdebe46 100755 --- a/hardware/arduino/cores/arduino/HardwareSerial.cpp +++ b/hardware/arduino/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/hardware/arduino/cores/arduino/HardwareSerial.h b/hardware/arduino/cores/arduino/HardwareSerial.h index cae1447a0..884f9feef 100755 --- a/hardware/arduino/cores/arduino/HardwareSerial.h +++ b/hardware/arduino/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/hardware/arduino/cores/arduino/Stream.h b/hardware/arduino/cores/arduino/Stream.h index 3bf5283da..1d3e50be9 100644 --- a/hardware/arduino/cores/arduino/Stream.h +++ b/hardware/arduino/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; };