add peek(size_t pos) Stream/HardwareSerial
It would be nice, if the Stream-Interface could be changed/extended with a peek method that takes a position/offset. I often have the need to search/wait, until a certain charakter is received - before i process the message. If course, storing in a temporary buffer works as well, but uses more memory and often complicates things...
Most simple way would be to add a default argument to peek int peek(size_t = 0);
int HardwareSerial::peek(size_t pos)
{
if (_rx_buffer->head == _rx_buffer->tail) {
return -1;
} else {
return _rx_buffer->buffer[(_rx_buffer->tail+pos)%SERIAL_BUFFER_SIZE];
}
}
I think this point has been discussed on the mailing list before. There is interest for this, but nobody really implemented it. I gues part of the problem is backward compatibility: this should really be added to the Stream interface as a virtual method, but then existing subclasses will break if they don't implement it...
Perhaps you could search around the mailing list archives and/or the issue tracker here (there's probably already issues open about this) to get some background?
Also, note that the code you pasted will return random data if peeking beyond the amount of available bytes, while I'd expect it to return -1 instead.