I'm writing a new serial library (eRCaGuy_Peer2Peer) to allow any 2 pins on an Arduino to be used for peer-to-peer type communication between Arduinos, withOUT interrupts or timers/counters.
Currently SPI, Serial, and I2C are pin-specific, and SoftwareSerial requires pin change interrupts. My implementation will solve these problems.
However, I first am trying to understand some things. As I study SoftwareSerial, I see it inherits from the Stream class, which inherits from the Print class.
I don't understand how Print's write
is implemented. This is from Print.h:
virtual size_t write(uint8_t) = 0;
size_t write(const char *str) {
if (str == NULL) return 0;
return write((const uint8_t *)str, strlen(str));
}
virtual size_t write(const uint8_t *buffer, size_t size);
size_t write(const char *buffer, size_t size) {
return write((const uint8_t *)buffer, size);
}
This is from Print.cpp:
/* default implementation: may be overridden */
size_t Print::write(const uint8_t *buffer, size_t size)
{
size_t n = 0;
while (size--) {
if (write(*buffer++)) n++; //<--where is this write defined?
else break;
}
return n;
}
My Questions:
- What does this mean? I've never seen anything like this before:
virtual size_t write(uint8_t) = 0;
Why the= 0
? What does it mean/how does it work defining functions in .h files? I've never done this before: ex:
size_t write(const char *str) { if (str == NULL) return 0; return write((const uint8_t *)str, strlen(str)); }
Where is the
write
command I've marked above defined? Is Print::write() calling itself recursively?- Also, why does SoftwareSerial.h call
using Print::write;
(shown below) when it already re-implements the virtualwrite
function just above that?
virtual size_t write(uint8_t byte);
virtual int read();
virtual int available();
virtual void flush();
operator bool() { return true; }
using Print::write;