Pages: [1]   Go Down
Author Topic: Wire.begin() already called  (Read 624 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Connecticut
Offline Offline
Edison Member
*
Karma: 17
Posts: 1216
RTFD (Datasheet in our case)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
#define WIRE_BEGIN_CALLED

Then, to test if it has been called:

Code:
#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...)
Logged

Seattle, WA
Offline Offline
God Member
*****
Karma: 11
Posts: 673
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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().
Logged


Connecticut
Offline Offline
Edison Member
*
Karma: 17
Posts: 1216
RTFD (Datasheet in our case)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Jr. Member
**
Karma: 2
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: