How to Best to Pass WireX to a Library

Status
Not open for further replies.

mjs513

Senior Member+
Hi All. This has probably been answered but I couldn't find it. Anyway, I am finding that a lot of sensor libraries use Wire as the default. What I want to do is be able to modify the library to be able use WireX (where x = 1, 2, 3...) since I have a T35. Wanted to know the best way to do this.

Any suggestions?

Mike
 
What I would do is pass either pass the pointer or reference to the object on the constructor or on some other method, like a logical begin method.

I know I did that earlier when I was experimenting with with some testing on the T3.6. In the past I mostly have done this with pointers, but have done it passing reference and converting the reference to a pointer inside the method...

An example library that passes a pointer is the EasyTransferI2C library that you pass in which I2c Bus on their begin method.
 
Thanks Kurt for the reference.

So using the construct in EasyTransferI2C I would have something like this:

Code:
xxx::begin(TwoWire *theSerial, ....){
	_serial = theSerial;
        //and anything else that gets set
}

and use

Code:
_serial->xxxxx
in place of Wire in the library

Of being called in the sketch by:
Code:
yy.begin( &WireX)

Thanks
Mike
 
So yes, using EasyTransferI2C example, your code would have:

Code:
#include <EasyTransferI2C.h>

EasytransferI2C  myTransfer;

void setup() {
...
    myTransfer.begin(&Wire2);
}

As you showed in your response... The begin method saves away the pointer into some member variable, like: wirex

Then everywhere in the library that used to do things like: Wire.write(), would change to wirex->write()
 
Thanks Kurt. After fixing a few typos works like a charm. I modified the Pololu VL53L0X library by adding a begin function - one for just defaulting to wire and the other to set it to anything you want. Only tested it Wire so far since the sensor is in another project. Will wire another one and do a double check but sure it will work. Do you think I should do a pull request to Pololu? Next up is a couple of the Adafruit sensors but I needed the vl53 setup first :)

Thanks again
Mike
 
I think it would be great to get as many libraries adapted to allow you to pass in which Wire or which SPI as with our current stuff, this also should work with several of the Arduino ARM boards like Due or M0... Which also use the same class names.
 
Same idea... Before current stuff I generated my SPIN library to allow me to do it. That is give a common class for each SPI object.

With current stuff, all are instances for the SPIClass, so the changes are like mentioned except different class... Again common class name with Teensy, Arduino DUE, M0...
 
Thanks Kurt. Came across that when I was trying to get the Arducam tft display working. Still no luck. Gave up on that one for now. Will look your for reference. Just posted a pull request to Pololu for the changes so we will see.
 
Ok just as a status submitted pull requests to Pololu for the VL53L0X sensor and to Adafruit for the TSL2561 light level sensor. Hopefully they will incorporate the changes. Have two more to go :)
 
HELP... Ran into Problem with BNO055 Adafruit Library

I managed to modify a few libraries including the Sparkfun and Adafruit Libs for the BME280 sensor but have a coding issue with the BNO055 lib by Adafruit. If I use the default call as in bno.begin() which should default to Wire and a op mode I get the following error messages, just can't figure out how to fix this since I am using a similar approach for the BME280 library and there is no problem:
Code:
C:\Users\CyberPalin\Documents\Arduino\libraries\Adafruit_BNO055-master\examples\sensorapi\sensorapi.ino: In function 'void setup()':

sensorapi:125: error: call of overloaded 'begin()' is ambiguous
   if(!bno.begin())
                 ^

C:\Users\CyberPalin\Documents\Arduino\libraries\Adafruit_BNO055-master\examples\sensorapi\sensorapi.ino:125:17: note: candidates are:

In file included from C:\Users\CyberPalin\Documents\Arduino\libraries\Adafruit_BNO055-master\examples\sensorapi\sensorapi.ino:3:0:

C:\Users\CyberPalin\Documents\Arduino\libraries\Adafruit_BNO055-master/Adafruit_BNO055.h:288:8: note: bool Adafruit_BNO055::begin()

  bool  begin( void );
        ^
C:\Users\CyberPalin\Documents\Arduino\libraries\Adafruit_BNO055-master/Adafruit_BNO055.h:290:8: note: bool Adafruit_BNO055::begin(Adafruit_BNO055::adafruit_bno055_opmode_t, TwoWire*)

  bool  begin( adafruit_bno055_opmode_t mode = OPERATION_MODE_NDOF, TwoWire *theWire = &Wire );
        ^

C:\Users\CyberPalin\Documents\Arduino\libraries\Adafruit_BNO055-master/Adafruit_BNO055.h:291:8: note: bool Adafruit_BNO055::begin(Adafruit_BNO055::adafruit_bno055_opmode_t)

  bool  begin( adafruit_bno055_opmode_t mode = OPERATION_MODE_NDOF );
        ^

exit status 1
call of overloaded 'begin()' is ambiguous

In the .h file I have:
Code:
	bool  begin( void );
        bool  begin( TwoWire *theWire );
	bool  begin( adafruit_bno055_opmode_t mode = OPERATION_MODE_NDOF, TwoWire *theWire = &Wire );
	bool  begin( adafruit_bno055_opmode_t mode = OPERATION_MODE_NDOF );

and the corresponding code in the .cpp file is:
Code:
bool  Adafruit_BNO055::begin( void )  //sets default to _wire ->  and opmode
{
	_wire = &Wire;
	_wire -> begin();
	adafruit_bno055_opmode_t mode = OPERATION_MODE_NDOF;
	return init(mode);
}
	
bool  Adafruit_BNO055::begin( TwoWire  *theWire ) //sets _wire ->  bus and default op mode
{
	_wire = theWire ;
	_wire -> begin();
	adafruit_bno055_opmode_t mode = OPERATION_MODE_NDOF;
	return init(mode);
}
	
bool Adafruit_BNO055::begin(adafruit_bno055_opmode_t mode) //default _wire ->  bus & selects op mode
{
  /* Enable I2C */
  _wire =  &Wire;
  _wire -> begin();
  return init(mode);
}

bool  Adafruit_BNO055::begin( adafruit_bno055_opmode_t mode, TwoWire *theWire) //selects both
{
  /* Enable I2C */
  _wire = theWire;
  _wire -> begin();
  return init(mode);
}

If I use bno.begin(&Wire) there is no problem.

UPDATE1: Got it solved basically by getting rid of the default values in the function.

Thanks
Mike
 
Last edited:
Status
Not open for further replies.
Back
Top