Skip to content

Commit ec63db2

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 did require the header file to now include the wire.h header file.
1 parent 9a6aee9 commit ec63db2

File tree

3 files changed

+56
-50
lines changed

3 files changed

+56
-50
lines changed

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: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@ The range readings are in units of mm. */
77
#include <Wire.h>
88
#include <VL53L0X.h>
99

10-
VL53L0X sensor;
10+
VL53L0X sensor(&Wire1);
1111

1212
void setup()
1313
{
1414
Serial.begin(9600);
15-
Wire.begin();
16-
15+
Wire1.begin();
16+
pinMode(0, OUTPUT); digitalWrite(0, HIGH); // use the first one
17+
pinMode(2, OUTPUT); digitalWrite(2, LOW); // reset
18+
pinMode(4, OUTPUT); digitalWrite(4, LOW); // reset
19+
pinMode(6, OUTPUT); digitalWrite(6, LOW); // reset
1720
sensor.setTimeout(500);
1821
if (!sensor.init())
1922
{

0 commit comments

Comments
 (0)