Fork of original library from sburg. Identical except it takes an I2C object as the input rather than individual pin names. Useful if you have multiple I2C devices on a single bus.
Dependents: TestBenchSerenity-proto_F429ZI TestBenchFlow HSPFLOW1 TestBenchFlow1 ... more
Fork of VL6180 by
Revision 5:190457e8c69e, committed 2017-07-20
- Comitter:
- dmwahl
- Date:
- Thu Jul 20 20:44:03 2017 +0000
- Parent:
- 4:7ab2e81d6596
- Commit message:
- Input is I2C object instead of pin names
Changed in this revision
VL6180.cpp | Show annotated file Show diff for this revision Revisions of this file |
VL6180.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 7ab2e81d6596 -r 190457e8c69e VL6180.cpp --- a/VL6180.cpp Fri Jul 14 18:51:32 2017 +0000 +++ b/VL6180.cpp Thu Jul 20 20:44:03 2017 +0000 @@ -1,7 +1,8 @@ #include "VL6180.h" #include "mbed.h" -VL6180::VL6180(I2C &i2c) : _i2c(i2c) { +VL6180::VL6180(I2C &i2c) : _i2c(i2c) +{ char poweron; poweron = readI2C(0x16); if(poweron == 1) { @@ -40,61 +41,64 @@ writeI2C(0x0011,0x10);// Enables polling for ‘New Sample ready’ when measurement completes writeI2C(0x010a,0x30);// Set the averaging sample period (compromise between lower noise and increased execution time) writeI2C(0x003f,0x46);// Sets the light and dark gain (upper nibble). Dark gain should not be changed. - writeI2C(0x0031,0xFF);// sets the # of range measurements after which auto calibration of system is performed + writeI2C(0x0031,0xFF);// sets the # of range measurements after which auto calibration of system is performed writeI2C(0x0040,0x63);// Set ALS integration time to 100ms - writeI2C(0x002e,0x01);// perform a single temperature calibration of the ranging sensor + writeI2C(0x002e,0x01);// perform a single temperature calibration of the ranging sensor //optional initialization writeI2C(0x001b,0x09);// Set default ranging inter-measurement period to 100ms writeI2C(0x003e,0x31);// Set default ALS inter-measurement period to 500ms writeI2C(0x0014,0x24);// Configures interrupt on ‘New Sample Ready threshold event’ - + writeI2C(0x016,0x00);//change fresh out of set status to 0 } } -float VL6180::read() { +float VL6180::read() +{ char status; char retn; - + writeI2C(0x18, 0x01); - + status = readI2C(0x4F); while((status & 0x7) != 4) { status = readI2C(0x4F); } - + retn = readI2C(0x62); - + writeI2C(0x15, 0x07); - + return (float)retn / 10.0; } -VL6180::operator float() { +VL6180::operator float() +{ return read(); } -void VL6180::writeI2C(int reg, char data) { +void VL6180::writeI2C(int reg, char data) +{ char dataout[3]; - + dataout[0] = (reg >> 8) & 0xFF; dataout[1] = reg & 0xFF; dataout[2] = data & 0xFF; - + _i2c.write(_addr, dataout, 3); } -char VL6180::readI2C(int reg) { +char VL6180::readI2C(int reg) +{ char dataout[2]; char datain[1]; - + dataout[0] = (reg >> 8) & 0xFF; dataout[1] = reg & 0xFF; - + _i2c.write(_addr, dataout, 2); _i2c.read(_addr, datain, 1); - + return datain[0]; -} - \ No newline at end of file +} \ No newline at end of file
diff -r 7ab2e81d6596 -r 190457e8c69e VL6180.h --- a/VL6180.h Fri Jul 14 18:51:32 2017 +0000 +++ b/VL6180.h Thu Jul 20 20:44:03 2017 +0000 @@ -23,24 +23,25 @@ * } * @endcode */ -class VL6180 { +class VL6180 +{ public: /** Create a VL6180 object at I2C address 0x29 (7 bit). - * + * * @param sda I2C sda pin number * @param scl I2C scl pin number */ VL6180(I2C &i2c); - + /** Make a range reading. - * + * * @param return Distance to target in cm. Approx 0.0 cm to 20.0 cm. */ float read(); - + /** Float cast is shorthand for read() */ operator float(); - + private: void writeI2C(int reg, char data); char readI2C(int reg);