From b351ca950de515f2ca3d8237dbafa4eb38a3d06f Mon Sep 17 00:00:00 2001 From: Brian Lalor Date: Fri, 3 Dec 2010 22:02:19 -0500 Subject: [PATCH 1/3] seting up .gitignore --- .gitignore | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .gitignore 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 From a663bbdbe42fd8e9f10b29b8169960953b1a8e49 Mon Sep 17 00:00:00 2001 From: Brian Lalor Date: Sat, 4 Dec 2010 08:32:58 -0500 Subject: [PATCH 2/3] Adding peek(uint8_t) and remove(uint8_t) to Stream interface. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I need more access to HardwareSerial's RX buffer; peek()'s a great step forward, but I need to be able to peek at an arbitrary byte, and quickly remove a number of bytes. • Adding peek(uint8_t) and remove(uint8_t) to Stream interface. • Provided default implementation to minimize (and hopefully eliminate) impact on other Stream implementations. • Implemented peek(uint8_t) and remove(uint8_t) in HardwareSerial. • Moved RX_BUFFER_SIZE to HardwareSerial.h so it's accessible to others. --- .../arduino/cores/arduino/HardwareSerial.cpp | 22 ++++++++++----- .../arduino/cores/arduino/HardwareSerial.h | 12 ++++++++ hardware/arduino/cores/arduino/Stream.h | 28 +++++++++++++++++++ 3 files changed, 55 insertions(+), 7 deletions(-) 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..81ddde2d2ee 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 @@ -52,6 +62,8 @@ class HardwareSerial : public Stream void end(); virtual int available(void); virtual int peek(void); + virtual int peek(uint8_t); + virtual void remove(uint8_t); 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 93d8275dc22..204c1fe7610 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. + */ + int peek(uint8_t offset) { + return (offset == 0) ? peek() : -1; + } + + /* + * Remove count bytes from the buffer. + * + * Inefficient default implementation. + */ + void remove(uint8_t count) { + for (uint8_t i = 0; i < count; i++) { + read(); + } + } }; #endif From d4080fff7dda87d9b223909c6df044bfc298d4a0 Mon Sep 17 00:00:00 2001 From: Brian Lalor Date: Sat, 4 Dec 2010 08:49:30 -0500 Subject: [PATCH 3/3] Virtual cleanup. Declared Stream's default implementations of peek(uint8_t) and remove(uint8_t) to be virtual, so that they can be appropriately overridden by extending classes. I believe HardwareSerial's header should only declare member functions to be virtual if it's going to be further extended, and that's not currently possible in the current Arduino codebase (the user has no ability to intercept the creation of HardwareSerial instances, something I hope to rectify soon). --- hardware/arduino/cores/arduino/HardwareSerial.h | 17 ++++++++++------- hardware/arduino/cores/arduino/Stream.h | 4 ++-- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/hardware/arduino/cores/arduino/HardwareSerial.h b/hardware/arduino/cores/arduino/HardwareSerial.h index 81ddde2d2ee..f2aaa7ba1fe 100644 --- a/hardware/arduino/cores/arduino/HardwareSerial.h +++ b/hardware/arduino/cores/arduino/HardwareSerial.h @@ -60,13 +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 peek(uint8_t); - virtual void remove(uint8_t); - 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 204c1fe7610..5a44b578144 100644 --- a/hardware/arduino/cores/arduino/Stream.h +++ b/hardware/arduino/cores/arduino/Stream.h @@ -44,7 +44,7 @@ class Stream : public Print * * Default implementation for classes without a buffer. */ - int peek(uint8_t offset) { + virtual int peek(uint8_t offset) { return (offset == 0) ? peek() : -1; } @@ -53,7 +53,7 @@ class Stream : public Print * * Inefficient default implementation. */ - void remove(uint8_t count) { + virtual void remove(uint8_t count) { for (uint8_t i = 0; i < count; i++) { read(); }