MorganS:
Well, you probably didn't want the long explanation but this is a really great time to introduce object-oriented programming. For libraries which run LCD's or Ethernet or even just Serial, it would be a waste of time to copy-paste all the print functions into them. There's actually quite a lot in there. So they all use the Stream class to provide the basic functionality of printing numbers, strings and whatever. But the Stream class doesn't know how to print to every different device. So it has a virtual function called write() which just writes one character to the output device. Each different device will have a real implementation of that function, which does the special work for that device (I2C, SPI, buffering, whatever) and then Stream can call that function when printing any character.
If you want to checksum every single character written to the device or you have sufficient control to start/stop/reset/display the checksum, then yes, just modify the real function write() inside the Ethernet library. No need to bother Stream with this detail.
inline void wrapperPrint(char c) {
globalChecksum += c;
thingy.print(c);
}
The inline directive will mean the compiler will try to write those two lines out in full each time, instead of using a function call. There may be reasons why it doesn't but you can usually assume that it will.
But you'd have to have many versions of this for all the different types you want to print. If you have access to modify the write() function, then use that.
Thank you for long and good explanation, I am a little bit
familiar with OOP concept, i am a long time Delphi, java and php programmer, but i am not so familiar with c++ as far as with arduino classes hierarchy, this is my problem, i believe. I have looked inside EthernetClient library, seems like I could do what i want to do by modifying code there, but i do not really wish to modify standard library, I would like to (try to) make my own class derived from EthernetClient and override write() there. But I did not really get yet how to do it.
I see it like this:
in h-file (just copy from base-class):
class MyEthernetClient : public EthernetClient {
public:
virtual size_t write(const uint8_t *buf, size_t size);
}
in cpp-file:
size_t MyEthernetClient::write(const uint8_t *buf, size_t size) {
//perform my checksum calculatons here
return size write (buf, size); //here is base-class (EthernetClient) method call
}
is this legal approach? At least in java and delphi I was able to do some similar tricks... Sure, I will need few extra methods to get checksum, reset checksum, etc., it is clear how to do it, not clear how to properly override write(...) to not brake existing functionality and add required features. And in this case seems like I do not need to modify many functions to cover all different datatypes.