Skip to content

Commit

Permalink
convert to busio
Browse files Browse the repository at this point in the history
  • Loading branch information
caternuson committed Aug 4, 2021
1 parent e1bc2c6 commit a588cde
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 63 deletions.
91 changes: 34 additions & 57 deletions Adafruit_VL6180X.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

#include "Adafruit_VL6180X.h"
#include "Arduino.h"
#include <Wire.h>

// Define some additional registers mentioned in application notes and we use
///! period between each measurement when in continuous mode
Expand All @@ -52,12 +51,12 @@ Adafruit_VL6180X::Adafruit_VL6180X(uint8_t i2caddr) : _i2caddr(i2caddr) {}
*/
/**************************************************************************/
boolean Adafruit_VL6180X::begin(TwoWire *theWire) {
if (!theWire) {
_i2c = &Wire;
} else {
_i2c = theWire;
}
_i2c->begin();
// only needed to support setAddress()
_i2c = theWire;

i2c_dev = new Adafruit_I2CDevice(_i2caddr, _i2c);
if (!i2c_dev->begin())
return false;

if (read8(VL6180X_REG_IDENTIFICATION_MODEL_ID) != 0xB4) {
return false;
Expand Down Expand Up @@ -85,7 +84,11 @@ boolean Adafruit_VL6180X::setAddress(uint8_t newAddr) {
// The register is mentioned in app notes.
write8(VL6180X_REG_SLAVE_DEVICE_ADDRESS, newAddr & 0x7F);
_i2caddr = newAddr;
return true;

delete i2c_dev;
i2c_dev = new Adafruit_I2CDevice(_i2caddr, _i2c);

return i2c_dev->begin();
}

/**************************************************************************/
Expand Down Expand Up @@ -392,65 +395,39 @@ float Adafruit_VL6180X::readLux(uint8_t gain) {

// Read 1 byte from the VL6180X at 'address'
uint8_t Adafruit_VL6180X::read8(uint16_t address) {
uint8_t data;

_i2c->beginTransmission(_i2caddr);
_i2c->write(address >> 8);
_i2c->write(address);
_i2c->endTransmission();

_i2c->requestFrom(_i2caddr, (uint8_t)1);
data = _i2c->read();

#if defined(I2C_DEBUG)
Serial.print("\t$");
Serial.print(address, HEX);
Serial.print(": 0x");
Serial.println(data, HEX);
#endif

return data;
uint8_t buffer[2];
buffer[0] = uint8_t(address >> 8);
buffer[1] = uint8_t(address & 0xFF);
i2c_dev->write(buffer, 2);
i2c_dev->read(buffer, 1);
return buffer[0];
}

// Read 2 byte from the VL6180X at 'address'
uint16_t Adafruit_VL6180X::read16(uint16_t address) {
uint16_t data;

_i2c->beginTransmission(_i2caddr);
_i2c->write(address >> 8);
_i2c->write(address);
_i2c->endTransmission();

_i2c->requestFrom(_i2caddr, (uint8_t)2);
data = _i2c->read();
data <<= 8;
data |= _i2c->read();

return data;
uint8_t buffer[2];
buffer[0] = uint8_t(address >> 8);
buffer[1] = uint8_t(address & 0xFF);
i2c_dev->write(buffer, 2);
i2c_dev->read(buffer, 2);
return uint16_t(buffer[0]) << 8 | uint16_t(buffer[1]);
}

// write 1 byte
void Adafruit_VL6180X::write8(uint16_t address, uint8_t data) {
_i2c->beginTransmission(_i2caddr);
_i2c->write(address >> 8);
_i2c->write(address);
_i2c->write(data);
_i2c->endTransmission();

#if defined(I2C_DEBUG)
Serial.print("\t$");
Serial.print(address, HEX);
Serial.print(" = 0x");
Serial.println(data, HEX);
#endif
uint8_t buffer[3];
buffer[0] = uint8_t(address >> 8);
buffer[1] = uint8_t(address & 0xFF);
buffer[2] = data;
i2c_dev->write(buffer, 3);
}

// write 2 bytes
void Adafruit_VL6180X::write16(uint16_t address, uint16_t data) {
_i2c->beginTransmission(_i2caddr);
_i2c->write(address >> 8);
_i2c->write(address);
_i2c->write(data >> 8);
_i2c->write(data);
_i2c->endTransmission();
uint8_t buffer[4];
buffer[0] = uint8_t(address >> 8);
buffer[1] = uint8_t(address & 0xFF);
buffer[2] = uint8_t(data >> 8);
buffer[3] = uint8_t(data & 0xFF);
i2c_dev->write(buffer, 4);
}
7 changes: 3 additions & 4 deletions Adafruit_VL6180X.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
*/

#include "Arduino.h"
#include <Wire.h>

//#define I2C_DEBUG
#include <Adafruit_I2CDevice.h>

#define VL6180X_DEFAULT_I2C_ADDR 0x29 ///< The fixed I2C addres

Expand Down Expand Up @@ -79,7 +77,7 @@
class Adafruit_VL6180X {
public:
Adafruit_VL6180X(uint8_t i2caddr = VL6180X_DEFAULT_I2C_ADDR);
boolean begin(TwoWire *theWire = NULL);
boolean begin(TwoWire *theWire = &Wire);
boolean setAddress(uint8_t newAddr);
uint8_t getAddress(void);

Expand All @@ -97,6 +95,7 @@ class Adafruit_VL6180X {
// readRangeResult and isRangeComplete apply here is well

private:
Adafruit_I2CDevice *i2c_dev = NULL; ///< Pointer to I2C bus interface
void loadSettings(void);

void write8(uint16_t address, uint8_t data);
Expand Down
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name=Adafruit_VL6180X
version=1.2.0
version=1.3.0
author=Adafruit
maintainer=adafruit <[email protected]>
sentence=Sensor driver for VL6180X Time of Flight sensor
paragraph=Sensor driver for VL6180X Time of Flight sensor
category=Sensors
url=https://github.com/adafruit/Adafruit_VL6180X
architectures=*
depends=Adafruit SSD1306, Adafruit GFX Library
depends=Adafruit SSD1306, Adafruit GFX Library, Adafruit BusIO

0 comments on commit a588cde

Please sign in to comment.