1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-01 12:24:14 +01:00

Adding a peek() function to Stream and HardwareSerial (Serial).

This commit is contained in:
David A. Mellis 2010-07-04 23:31:55 +00:00
parent 934312682e
commit 31601ffe77
4 changed files with 12 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -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);

View File

@ -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;
};