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 Steven Burg

Committer:
dmwahl
Date:
Thu Jul 20 20:44:03 2017 +0000
Revision:
5:190457e8c69e
Parent:
4:7ab2e81d6596
Input is I2C object instead of pin names

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sburg 0:f23c00f688b2 1 #include "VL6180.h"
sburg 0:f23c00f688b2 2 #include "mbed.h"
sburg 0:f23c00f688b2 3
dmwahl 5:190457e8c69e 4 VL6180::VL6180(I2C &i2c) : _i2c(i2c)
dmwahl 5:190457e8c69e 5 {
sburg 0:f23c00f688b2 6 char poweron;
sburg 0:f23c00f688b2 7 poweron = readI2C(0x16);
sburg 0:f23c00f688b2 8 if(poweron == 1) {
sburg 0:f23c00f688b2 9 //mandatory initialization
sburg 0:f23c00f688b2 10 writeI2C(0x0207,0x01);
sburg 0:f23c00f688b2 11 writeI2C(0x0208,0x01);
sburg 0:f23c00f688b2 12 writeI2C(0x0096,0x00);
sburg 0:f23c00f688b2 13 writeI2C(0x0097,0xfd);
sburg 0:f23c00f688b2 14 writeI2C(0x00e3,0x00);
sburg 0:f23c00f688b2 15 writeI2C(0x00e4,0x04);
sburg 0:f23c00f688b2 16 writeI2C(0x00e5,0x02);
sburg 0:f23c00f688b2 17 writeI2C(0x00e6,0x01);
sburg 0:f23c00f688b2 18 writeI2C(0x00e7,0x03);
sburg 0:f23c00f688b2 19 writeI2C(0x00f5,0x02);
sburg 0:f23c00f688b2 20 writeI2C(0x00d9,0x05);
sburg 0:f23c00f688b2 21 writeI2C(0x00db,0xce);
sburg 0:f23c00f688b2 22 writeI2C(0x00dc,0x03);
sburg 0:f23c00f688b2 23 writeI2C(0x00dd,0xf8);
sburg 0:f23c00f688b2 24 writeI2C(0x009f,0x00);
sburg 0:f23c00f688b2 25 writeI2C(0x00a3,0x3c);
sburg 0:f23c00f688b2 26 writeI2C(0x00b7,0x00);
sburg 0:f23c00f688b2 27 writeI2C(0x00bb,0x3c);
sburg 0:f23c00f688b2 28 writeI2C(0x00b2,0x09);
sburg 0:f23c00f688b2 29 writeI2C(0x00ca,0x09);
sburg 0:f23c00f688b2 30 writeI2C(0x0198,0x01);
sburg 0:f23c00f688b2 31 writeI2C(0x01b0,0x17);
sburg 0:f23c00f688b2 32 writeI2C(0x01ad,0x00);
sburg 0:f23c00f688b2 33 writeI2C(0x00ff,0x05);
sburg 0:f23c00f688b2 34 writeI2C(0x0100,0x05);
sburg 0:f23c00f688b2 35 writeI2C(0x0199,0x05);
sburg 0:f23c00f688b2 36 writeI2C(0x01a6,0x1b);
sburg 0:f23c00f688b2 37 writeI2C(0x01ac,0x3e);
sburg 0:f23c00f688b2 38 writeI2C(0x01a7,0x1f);
sburg 0:f23c00f688b2 39 writeI2C(0x0030,0x00);
sburg 0:f23c00f688b2 40 //recommended initialization
sburg 0:f23c00f688b2 41 writeI2C(0x0011,0x10);// Enables polling for ‘New Sample ready’ when measurement completes
sburg 0:f23c00f688b2 42 writeI2C(0x010a,0x30);// Set the averaging sample period (compromise between lower noise and increased execution time)
sburg 0:f23c00f688b2 43 writeI2C(0x003f,0x46);// Sets the light and dark gain (upper nibble). Dark gain should not be changed.
dmwahl 5:190457e8c69e 44 writeI2C(0x0031,0xFF);// sets the # of range measurements after which auto calibration of system is performed
sburg 0:f23c00f688b2 45 writeI2C(0x0040,0x63);// Set ALS integration time to 100ms
dmwahl 5:190457e8c69e 46 writeI2C(0x002e,0x01);// perform a single temperature calibration of the ranging sensor
sburg 0:f23c00f688b2 47 //optional initialization
sburg 0:f23c00f688b2 48 writeI2C(0x001b,0x09);// Set default ranging inter-measurement period to 100ms
sburg 0:f23c00f688b2 49 writeI2C(0x003e,0x31);// Set default ALS inter-measurement period to 500ms
sburg 0:f23c00f688b2 50 writeI2C(0x0014,0x24);// Configures interrupt on ‘New Sample Ready threshold event’
dmwahl 5:190457e8c69e 51
sburg 0:f23c00f688b2 52 writeI2C(0x016,0x00);//change fresh out of set status to 0
sburg 0:f23c00f688b2 53 }
sburg 0:f23c00f688b2 54 }
sburg 0:f23c00f688b2 55
dmwahl 5:190457e8c69e 56 float VL6180::read()
dmwahl 5:190457e8c69e 57 {
sburg 0:f23c00f688b2 58 char status;
sburg 0:f23c00f688b2 59 char retn;
dmwahl 5:190457e8c69e 60
sburg 0:f23c00f688b2 61 writeI2C(0x18, 0x01);
dmwahl 5:190457e8c69e 62
sburg 0:f23c00f688b2 63 status = readI2C(0x4F);
sburg 3:5d61f202b1bd 64 while((status & 0x7) != 4) {
sburg 0:f23c00f688b2 65 status = readI2C(0x4F);
sburg 0:f23c00f688b2 66 }
dmwahl 5:190457e8c69e 67
sburg 0:f23c00f688b2 68 retn = readI2C(0x62);
dmwahl 5:190457e8c69e 69
sburg 0:f23c00f688b2 70 writeI2C(0x15, 0x07);
dmwahl 5:190457e8c69e 71
sburg 0:f23c00f688b2 72 return (float)retn / 10.0;
sburg 0:f23c00f688b2 73 }
sburg 0:f23c00f688b2 74
dmwahl 5:190457e8c69e 75 VL6180::operator float()
dmwahl 5:190457e8c69e 76 {
sburg 0:f23c00f688b2 77 return read();
sburg 0:f23c00f688b2 78 }
sburg 0:f23c00f688b2 79
sburg 0:f23c00f688b2 80
dmwahl 5:190457e8c69e 81 void VL6180::writeI2C(int reg, char data)
dmwahl 5:190457e8c69e 82 {
sburg 0:f23c00f688b2 83 char dataout[3];
dmwahl 5:190457e8c69e 84
sburg 0:f23c00f688b2 85 dataout[0] = (reg >> 8) & 0xFF;
sburg 0:f23c00f688b2 86 dataout[1] = reg & 0xFF;
sburg 0:f23c00f688b2 87 dataout[2] = data & 0xFF;
dmwahl 5:190457e8c69e 88
sburg 0:f23c00f688b2 89 _i2c.write(_addr, dataout, 3);
sburg 0:f23c00f688b2 90 }
sburg 0:f23c00f688b2 91
dmwahl 5:190457e8c69e 92 char VL6180::readI2C(int reg)
dmwahl 5:190457e8c69e 93 {
sburg 0:f23c00f688b2 94 char dataout[2];
sburg 0:f23c00f688b2 95 char datain[1];
dmwahl 5:190457e8c69e 96
sburg 0:f23c00f688b2 97 dataout[0] = (reg >> 8) & 0xFF;
sburg 0:f23c00f688b2 98 dataout[1] = reg & 0xFF;
dmwahl 5:190457e8c69e 99
sburg 0:f23c00f688b2 100 _i2c.write(_addr, dataout, 2);
sburg 0:f23c00f688b2 101 _i2c.read(_addr, datain, 1);
dmwahl 5:190457e8c69e 102
sburg 0:f23c00f688b2 103 return datain[0];
dmwahl 5:190457e8c69e 104 }