felias-fogg / SoftI2CMaster

Software I2C Arduino library
GNU General Public License v3.0
368 stars 100 forks source link

Handling multiple includes in one project by introducing the macros USE_SOFT_I2C_MASTER_H_AS_PLAIN_INCLUDE and USE_SOFTWIRE_H_AS_PLAIN_INCLUDE #69

Closed ArminJo closed 2 years ago

ArminJo commented 2 years ago

Hello, I've now split the files and adjusted the examples. I had to insert SoftWire Wire = SoftWire(); to be compatible with the Wire library. Best regards Armin

ArminJo commented 2 years ago

Something missing, or just busy? Best regards Armin

felias-fogg commented 2 years ago

Hi Armin,

sorry for the late reply. I was busy finishing the hardware debugger and other things.

So, I am still a bit uncertain of what one has gained. The change really breaks the interface since we now have to include the .hpp file instead of the .h file, since we now the .h file with the declarations and the .hpp file with the definitions. But what is the advantage? Please tell me a use case where splitting makes a difference for the user of the library (or for the developer).

Best, Bernhard

ArminJo commented 2 years ago

The use case is here and here. If I use #include "SoftWire.hpp" in both files, I get a lot of errors!!! I need the forward definitions in both files and once the implementation! I hope this is understandable.

Best regards Armin

felias-fogg commented 2 years ago

Hi Armin,

this happens when you do things that you are not supposed to do (I mean myself). ".h" files are supposed to be header files and according to the C++ book by Stroustrup, you definitely should not put any (non-inline) function definitions in there. Well, I did because there was no other way to 'pass' the 'macro parameters'. And it all usually works when you just import it into one Arduino sketch.

If you have a more modular structure with many modules, this does not work any more. You definitely should not import SoftI2CMaster.h more than once because you otherwise define functions twice.

Your solution having one header file with declarations ".h" and one with definitions ".hpp" works, of course. What I hate about it is that it breaks the code of people using earlier versions. And I see many people complaining instead of reading the explanations in the manual.

So, why using .hpp for the definitions and .h for the declarations? Why not the other way around? I also could imagine a solution where one calls the declaration file SofI2CMasterDeclarations.h. Doing it that way, one could just add something to the manual/readme that people can now also use modular programming and it would not break any existing code.

Cheers & sorry for my unresponsiveness - work & dw-link were quite demanding, Bernhard

ArminJo commented 2 years ago

Ok I found a solution. For all who require plain deklarations, they must now use:

#define USE_SOFT_I2C_MASTER_H_AS_PLAIN_INCLUDE
#include <SoftI2CMaster.h>

thats all 😀 .

P.S. And I felt so free and added a few curling braces, they were used inconsistently for some 1 line blocks.

felias-fogg commented 2 years ago

That's perfect!