Skip to content

Extend Stream to provide remove(count) and peek(offset) #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
app/bin/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this gitignore file is really not needed.

app/pde.jar
build/.DS_Store
build/macosx/work/
core/bin/
core/core.jar
22 changes: 15 additions & 7 deletions hardware/arduino/cores/arduino/HardwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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
Expand Down
25 changes: 20 additions & 5 deletions hardware/arduino/cores/arduino/HardwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, virtual is not needed.

If a derived object implements peek() this is the one the virtual methods will call. For that object to use the parent versions, it'll have to explicitly call derived::peek().

I wouldn't use virtual for another reason: A client may want to use the derived version without implementing their own using derived::peek;.

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

Expand Down
28 changes: 28 additions & 0 deletions hardware/arduino/cores/arduino/Stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -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