Testato / SoftwareWire

Creates a software I2C/TWI bus on every pins
GNU General Public License v3.0
148 stars 33 forks source link

Inherit from TwoWire base class so that it can better operate with 3rd party libraries. #14

Closed mhenman closed 6 years ago

mhenman commented 6 years ago
Koepel commented 6 years ago

Thanks, I see that the Wire.h has indeed size_t for the write function. https://github.com/arduino/ArduinoCore-avr/blob/master/libraries/Wire/src/Wire.h I have to check any casting or conversion before I change that.

Making it dependent on the TwoWire object makes it less versatile in my opinion. Is source code compatible not enough ?

What if someone would make a layer on top of the Wire library that can choose any "Wire lookalike" library, even a simulated Wire library. When a Wire libary would be choosen that has nothing to do with the TWI hardware, there is no need to rely on a unused TWI hardware library.

mhenman commented 6 years ago

Source code compatibility is fine for the code I write myself, but most of the I2C devices I use provide libraries that expect to use the global Wire object or a provided TwoWire object. For the ones that only use the global Wire object, that means going into the library code and changing it to use an instance of SoftwareWire instead.

There several libraries, though, that allow the calling code to pass in a TwoWire object to use in place of the global Wire. For those libraries, if SoftwareWire inherited from TwoWire, then I can use them with no modification. If SoftwareWIre doesn't inherit from TwoWire, then I have no choice but to alter those libraries source to use a SoftwareWire because the compiler forces type compatibility.

Since SoftwareWire is intended to be source compatible with TwoWire, and in all my code has been used in place of a TwoWire object, then inheriting from TwoWire ensures that compatibility because the compiler will enforce it.

Koepel commented 6 years ago

Writing a library for a sensor that can use a few different Wire libraries by using the TwoWire with pointers to the functions might not be the final and single solution. Sometimes defines are used, which have to be adapted for a certain Wire library. I already gave a link to my CommonSensorClass with templates, and then the TwoTwire is not needed.

Using the TwoWire will make it more compatible and the ESP8266 software Wire does use it. As far as I know the TinyWire does not use the TwoWire.

I will wait and see if others have a preference.

Koepel commented 6 years ago

Since more and more libraries are using pointers with TwoWire, I am now in favor of the size_t and the TwoWire.