Skip to content

Commit 9bc65ea

Browse files
committed
Optionally support other Wire objects.
Many processors, now days have more than one hardware I2C on them. Sometimes it is convienent to use a different I2C object like Wire1 or Wire2 instead of the default Wire object. This change allows you to pass in a pointer to the desired I2C(Wire) object on the constructor. which defaults to Wire. This change requires the main header file to include the wire.h header file.
1 parent 9a6aee9 commit 9bc65ea

File tree

4 files changed

+52
-50
lines changed

4 files changed

+52
-50
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ This library is intended to provide a quicker and easier way to get started usin
7474
* `uint8_t last_status`<br>
7575
The status of the last I&sup2;C write transmission. See the [`Wire.endTransmission()` documentation](http://arduino.cc/en/Reference/WireEndTransmission) for return values.
7676

77-
* `VL53L0X(void)`<br>
78-
Constructor.
77+
* `VL53L0X(TwoWire *theWire = &Wire)`<br>
78+
Constructor. Optionally for systems with mutliple Wire objects, you can choose which Wire object this object should use.
7979

8080
* `void setAddress(uint8_t new_addr)`<br>
8181
Changes the I&sup2;C slave device address of the VL53L0X to the given value (7-bit).

VL53L0X.cpp

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@
3434

3535
// Constructors ////////////////////////////////////////////////////////////////
3636

37-
VL53L0X::VL53L0X(void)
38-
: address(ADDRESS_DEFAULT)
37+
VL53L0X::VL53L0X(TwoWire *theWire)
38+
: wire_ptr(theWire)
39+
, address(ADDRESS_DEFAULT)
3940
, io_timeout(0) // no timeout
4041
, did_timeout(false)
4142
{
@@ -284,45 +285,45 @@ bool VL53L0X::init(bool io_2v8)
284285
// Write an 8-bit register
285286
void VL53L0X::writeReg(uint8_t reg, uint8_t value)
286287
{
287-
Wire.beginTransmission(address);
288-
Wire.write(reg);
289-
Wire.write(value);
290-
last_status = Wire.endTransmission();
288+
wire_ptr->beginTransmission(address);
289+
wire_ptr->write(reg);
290+
wire_ptr->write(value);
291+
last_status = wire_ptr->endTransmission();
291292
}
292293

293294
// Write a 16-bit register
294295
void VL53L0X::writeReg16Bit(uint8_t reg, uint16_t value)
295296
{
296-
Wire.beginTransmission(address);
297-
Wire.write(reg);
298-
Wire.write((value >> 8) & 0xFF); // value high byte
299-
Wire.write( value & 0xFF); // value low byte
300-
last_status = Wire.endTransmission();
297+
wire_ptr->beginTransmission(address);
298+
wire_ptr->write(reg);
299+
wire_ptr->write((value >> 8) & 0xFF); // value high byte
300+
wire_ptr->write( value & 0xFF); // value low byte
301+
last_status = wire_ptr->endTransmission();
301302
}
302303

303304
// Write a 32-bit register
304305
void VL53L0X::writeReg32Bit(uint8_t reg, uint32_t value)
305306
{
306-
Wire.beginTransmission(address);
307-
Wire.write(reg);
308-
Wire.write((value >> 24) & 0xFF); // value highest byte
309-
Wire.write((value >> 16) & 0xFF);
310-
Wire.write((value >> 8) & 0xFF);
311-
Wire.write( value & 0xFF); // value lowest byte
312-
last_status = Wire.endTransmission();
307+
wire_ptr->beginTransmission(address);
308+
wire_ptr->write(reg);
309+
wire_ptr->write((value >> 24) & 0xFF); // value highest byte
310+
wire_ptr->write((value >> 16) & 0xFF);
311+
wire_ptr->write((value >> 8) & 0xFF);
312+
wire_ptr->write( value & 0xFF); // value lowest byte
313+
last_status = wire_ptr->endTransmission();
313314
}
314315

315316
// Read an 8-bit register
316317
uint8_t VL53L0X::readReg(uint8_t reg)
317318
{
318319
uint8_t value;
319320

320-
Wire.beginTransmission(address);
321-
Wire.write(reg);
322-
last_status = Wire.endTransmission();
321+
wire_ptr->beginTransmission(address);
322+
wire_ptr->write(reg);
323+
last_status = wire_ptr->endTransmission();
323324

324-
Wire.requestFrom(address, (uint8_t)1);
325-
value = Wire.read();
325+
wire_ptr->requestFrom(address, (uint8_t)1);
326+
value = wire_ptr->read();
326327

327328
return value;
328329
}
@@ -332,13 +333,13 @@ uint16_t VL53L0X::readReg16Bit(uint8_t reg)
332333
{
333334
uint16_t value;
334335

335-
Wire.beginTransmission(address);
336-
Wire.write(reg);
337-
last_status = Wire.endTransmission();
336+
wire_ptr->beginTransmission(address);
337+
wire_ptr->write(reg);
338+
last_status = wire_ptr->endTransmission();
338339

339-
Wire.requestFrom(address, (uint8_t)2);
340-
value = (uint16_t)Wire.read() << 8; // value high byte
341-
value |= Wire.read(); // value low byte
340+
wire_ptr->requestFrom(address, (uint8_t)2);
341+
value = (uint16_t)wire_ptr->read() << 8; // value high byte
342+
value |= wire_ptr->read(); // value low byte
342343

343344
return value;
344345
}
@@ -348,15 +349,15 @@ uint32_t VL53L0X::readReg32Bit(uint8_t reg)
348349
{
349350
uint32_t value;
350351

351-
Wire.beginTransmission(address);
352-
Wire.write(reg);
353-
last_status = Wire.endTransmission();
352+
wire_ptr->beginTransmission(address);
353+
wire_ptr->write(reg);
354+
last_status = wire_ptr->endTransmission();
354355

355-
Wire.requestFrom(address, (uint8_t)4);
356-
value = (uint32_t)Wire.read() << 24; // value highest byte
357-
value |= (uint32_t)Wire.read() << 16;
358-
value |= (uint16_t)Wire.read() << 8;
359-
value |= Wire.read(); // value lowest byte
356+
wire_ptr->requestFrom(address, (uint8_t)4);
357+
value = (uint32_t)wire_ptr->read() << 24; // value highest byte
358+
value |= (uint32_t)wire_ptr->read() << 16;
359+
value |= (uint16_t)wire_ptr->read() << 8;
360+
value |= wire_ptr->read(); // value lowest byte
360361

361362
return value;
362363
}
@@ -365,30 +366,30 @@ uint32_t VL53L0X::readReg32Bit(uint8_t reg)
365366
// starting at the given register
366367
void VL53L0X::writeMulti(uint8_t reg, uint8_t const * src, uint8_t count)
367368
{
368-
Wire.beginTransmission(address);
369-
Wire.write(reg);
369+
wire_ptr->beginTransmission(address);
370+
wire_ptr->write(reg);
370371

371372
while (count-- > 0)
372373
{
373-
Wire.write(*(src++));
374+
wire_ptr->write(*(src++));
374375
}
375376

376-
last_status = Wire.endTransmission();
377+
last_status = wire_ptr->endTransmission();
377378
}
378379

379380
// Read an arbitrary number of bytes from the sensor, starting at the given
380381
// register, into the given array
381382
void VL53L0X::readMulti(uint8_t reg, uint8_t * dst, uint8_t count)
382383
{
383-
Wire.beginTransmission(address);
384-
Wire.write(reg);
385-
last_status = Wire.endTransmission();
384+
wire_ptr->beginTransmission(address);
385+
wire_ptr->write(reg);
386+
last_status = wire_ptr->endTransmission();
386387

387-
Wire.requestFrom(address, count);
388+
wire_ptr->requestFrom(address, count);
388389

389390
while (count-- > 0)
390391
{
391-
*(dst++) = Wire.read();
392+
*(dst++) = wire_ptr->read();
392393
}
393394
}
394395

VL53L0X.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define VL53L0X_h
33

44
#include <Arduino.h>
5+
#include <Wire.h>
56

67
class VL53L0X
78
{
@@ -96,7 +97,7 @@ class VL53L0X
9697

9798
uint8_t last_status; // status of last I2C transmission
9899

99-
VL53L0X(void);
100+
VL53L0X(TwoWire *theWire = &Wire);
100101

101102
void setAddress(uint8_t new_addr);
102103
inline uint8_t getAddress(void) { return address; }
@@ -149,6 +150,7 @@ class VL53L0X
149150
uint32_t msrc_dss_tcc_us, pre_range_us, final_range_us;
150151
};
151152

153+
TwoWire *wire_ptr;
152154
uint8_t address;
153155
uint16_t io_timeout;
154156
bool did_timeout;

examples/Continuous/Continuous.ino

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ void setup()
1313
{
1414
Serial.begin(9600);
1515
Wire.begin();
16-
1716
sensor.setTimeout(500);
1817
if (!sensor.init())
1918
{

0 commit comments

Comments
 (0)