Go Down

Topic: Wire.begin() already called (Read 686 times) previous topic - next topic

CustomDev

Is there an easy way to know if the Wire.begin() has already been called?
I am creating a library and I want to call it only if it has not been done already.

baum

Open the Arduino source code, and find the wire lib. Now open up wire.cpp, and in wire.begin() add this line:

Code: [Select]
#define WIRE_BEGIN_CALLED

Then, to test if it has been called:

Code: [Select]
#ifndef WIRE_BEGIN_CALLED //if not defined
Wire.begin() // will only call if it hasn't been called yet
#endif


Good Luck!

(and sorry this is very late...)

No, that won't do it at all.  #defines are handled by the pre-processor, way before the MCU gets around to calling begin() or not.

From looking at the code, Wire.begin() looks non-destructive, so as long as you haven't done any transmissions yet it looks safe to call it multiple times during setup().

baum

Then use a global boolean 1-bit variable. That should function the same.

virtual1


Open the Arduino source code, and find the wire lib. Now open up wire.cpp, and in wire.begin() add this line:


is there a way to do it WITHOUT modifying the IDE?  I would like to further bulletproof a library that uses wire, because arduinos tend to shoot craps if you forget to begin it.  I'm expecting the user to begin wire for me, and I'd like to know if they forgot and I need to or not. 

I know that in most circumstances you can do things like   if (Serial) { bla } to see if the lenoardo serial is open.  anything like that for twi?  I don't mind taking advantage of a side effect somewhere like looking at a weird twi property that is visible to me that just happens to get changed by the begin process.

Go Up