diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000000..eed22d5588a --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +app/bin/ +app/pde.jar +build/.DS_Store +build/macosx/work/ +core/bin/ +core/core.jar diff --git a/hardware/arduino/cores/arduino/HardwareSerial.cpp b/hardware/arduino/cores/arduino/HardwareSerial.cpp index 4397efb7ee0..6485dd08985 100644 --- a/hardware/arduino/cores/arduino/HardwareSerial.cpp +++ b/hardware/arduino/cores/arduino/HardwareSerial.cpp @@ -37,12 +37,6 @@ // using a ring buffer (I think), in which rx_buffer_head is the index of the // location to which to write the next incoming character and rx_buffer_tail // is the index of the location from which to read. -#if (RAMEND < 1000) - #define RX_BUFFER_SIZE 32 -#else - #define RX_BUFFER_SIZE 128 -#endif - struct ring_buffer { unsigned char buffer[RX_BUFFER_SIZE]; @@ -235,14 +229,28 @@ int HardwareSerial::available(void) } int HardwareSerial::peek(void) +{ + return peek(0); +} + +int HardwareSerial::peek(uint8_t offset) { if (_rx_buffer->head == _rx_buffer->tail) { return -1; } else { - return _rx_buffer->buffer[_rx_buffer->tail]; + return _rx_buffer->buffer[(_rx_buffer->tail + offset) % RX_BUFFER_SIZE]; } } +/** + Removes count number of bytes from the buffer. +**/ +void HardwareSerial::remove(uint8_t count) { + if (_rx_buffer->head != _rx_buffer->tail) { + _rx_buffer->tail = (_rx_buffer->tail + count) % RX_BUFFER_SIZE; + } +} + 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 3efa775f843..f2aaa7ba1fe 100644 --- a/hardware/arduino/cores/arduino/HardwareSerial.h +++ b/hardware/arduino/cores/arduino/HardwareSerial.h @@ -26,6 +26,16 @@ #include "Stream.h" +// Define constants and variables for buffering incoming serial data. We're +// using a ring buffer (I think), in which rx_buffer_head is the index of the +// location to which to write the next incoming character and rx_buffer_tail +// is the index of the location from which to read. +#if (RAMEND < 1000) + #define RX_BUFFER_SIZE 32 +#else + #define RX_BUFFER_SIZE 128 +#endif + struct ring_buffer; class HardwareSerial : public Stream @@ -50,11 +60,16 @@ class HardwareSerial : public Stream uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udre, uint8_t u2x); void begin(long); void end(); - virtual int available(void); - virtual int peek(void); - virtual int read(void); - virtual void flush(void); - virtual void write(uint8_t); + + // @todo I think these only need to be declared virtual if HWSerial's going to be extended + int available(void); + int peek(void); + int peek(uint8_t); + void remove(uint8_t); + int read(void); + void flush(void); + void write(uint8_t); + using Print::write; // pull in write(str) and write(buf, size) from Print }; diff --git a/hardware/arduino/cores/arduino/Stream.h b/hardware/arduino/cores/arduino/Stream.h index 93d8275dc22..5a44b578144 100644 --- a/hardware/arduino/cores/arduino/Stream.h +++ b/hardware/arduino/cores/arduino/Stream.h @@ -28,8 +28,36 @@ class Stream : public Print public: virtual int available() = 0; virtual int read() = 0; + + /* + * Return the next byte waiting to be read. + */ virtual int peek() = 0; + + /* + * Forces output to the underlying device. + */ virtual void flush() = 0; + + /* + * Return the byte offset places from the beginning of the buffer. + * + * Default implementation for classes without a buffer. + */ + virtual int peek(uint8_t offset) { + return (offset == 0) ? peek() : -1; + } + + /* + * Remove count bytes from the buffer. + * + * Inefficient default implementation. + */ + virtual void remove(uint8_t count) { + for (uint8_t i = 0; i < count; i++) { + read(); + } + } }; #endif