Serial1.peek

Status
Not open for further replies.

IanHaynes

Member
Good Evening,

I'm just in the process of moving my project from Arduino to Teensy. I'm all up and running with my shiny new Teensy 3.1, but I'm struggling with a modification I made to the Arduino Hardware serial core.

I added a function that I called Serial.peekn(). It's just like the regular Serial.peek, but allows you to specify the location in the serial buffer that you want to peek at. For example, Serial.peekn(0); gives you the first byte, and Serial.peekn(1); gives the second etc (both without actually removing the bytes from the buffer). There was also a second function that was called Serial.remove(). As the name suggests, it allows you to discard a specified number of bytes from the buffer without having to read and then discard them.

I'm currently using Arduino IDE 1.0.6, and these are the modifications I made.

In HardwareSerial.h I added the following:

Code:
virtual int peekn(int a);
virtual void remove(uint8_t);

In HardwareSerial.cpp I added the following:

Code:
int HardwareSerial::peekn(int a)
{
  if (_rx_buffer->head == _rx_buffer->tail) {
    return -1;
  }  else {
    return _rx_buffer->buffer[(_rx_buffer->tail + a) % SERIAL_BUFFER_SIZE];
  }
}

void HardwareSerial::remove(uint8_t n) {
    if (_rx_buffer->head != _rx_buffer->tail) {
        _rx_buffer->tail = (_rx_buffer->tail + n) % SERIAL_BUFFER_SIZE;
    }
}

I've been looking through the teensy3 HardwareSerial.h (can't even find teensy3 HardwareSerial.cpp), and can't figure out how to implement similar changes. I found HardwareSerial.cpp under the teensy core, and think I've figured out the changes there (although as yet unable to test):

Code:
int HardwareSerial::peekn(int n)
{
	uint8_t head, tail;

	head = rx_buffer_head;
	tail = rx_buffer_tail;
	if (head == tail) return -1;
	if (++tail + a >= RX_BUFFER_SIZE) tail = 0;
	return rx_buffer[tail + n];
}

int HardwareSerial::remove(int n)
{
	uint8_t c, i;

	if (rx_buffer_head == rx_buffer_tail) return -1;
	i = rx_buffer_tail + n;
	if (i >= RX_BUFFER_SIZE) i = 0;
	rx_buffer_tail = i;
}

Assuming the above is correct, can anyone point me in the right direction regarding changes to the teensy3 HardwareSerial.h file ? I just can't figure out what needs to be added.

Thanks,

Ian.
 
Status
Not open for further replies.
Back
Top