i2c driver for VL6180x distance sensor

Dependents:   m3Dpi

Revision:
2:2e0406672e67
Parent:
1:6a00afc8dc84
--- a/vl6180x.cpp	Sat Dec 19 10:42:18 2015 +0000
+++ b/vl6180x.cpp	Sat Dec 19 13:27:01 2015 +0000
@@ -48,38 +48,53 @@
     setRegister(0x0030, 0x00);
 
 // Recommended : Public registers - See data sheet for more detail
-    setRegister(0x0011, 0x10); // Enables polling for ‘New Sample ready’
+    setRegister(SYSTEM_MODE_GPIO1, 0x10); // Enables polling for ‘New Sample ready’
 // when measurement completes
-    setRegister(0x010a, 0x30); // Set the averaging sample period
+    setRegister(READOUT_AVERAGING_SAMPLE_PERIOD, 0x30); // Set the averaging sample period
 // (compromise between lower noise and
 // increased execution time)
-    setRegister(0x003f, 0x46); // Sets the light and dark gain (upper
+    setRegister(SYSALS_ANALOGUE_GAIN, 0x46); // Sets the light and dark gain (upper
 // nibble). Dark gain should not be
 // changed.
-    setRegister(0x0031, 0xFF); // sets the # of range measurements after
+    setRegister(SYSRANGE_VHV_REPEAT_RATE, 0xFF); // sets the # of range measurements after
 // which auto calibration of system is
 // performed
-    setRegister(0x0040, 0x63); // Set ALS integration time to 100ms
-    setRegister(0x002e, 0x01); // perform a single temperature calibration
+    setRegister(SYSALS_INTEGRATION_PERIOD, 0x63); // Set ALS integration time to 100ms
+    setRegister(SYSRANGE_VHV_RECALIBRATE, 0x01); // perform a single temperature calibration
 // of the ranging sensor
 
 
-    setRegister(0x001b, 0x09); // Set default ranging inter-measurement
+    setRegister(SYSRANGE_INTERMEASUREMENT_PERIOD, 0x09); // Set default ranging inter-measurement
 // period to 100ms
-    setRegister(0x003e, 0x31); // Set default ALS inter-measurement period
+    setRegister(SYSALS_INTERMEASUREMENT_PERIOD, 0x31); // Set default ALS inter-measurement period
 // to 500ms
-    setRegister(0x0014, 0x24); // Configures interrupt on ‘New Sample
+    setRegister(SYSTEM_INTERRUPT_CONFIG_GPIO, 0x24); // Configures interrupt on ‘New Sample
 // Ready threshold event’
 }
 
+void VL6180x::startContinuousOperation()
+{
+    setRegister(SYSRANGE_START, 0x03);
+    
+}
+
 int VL6180x::getDistance()
 {
     int distance = 0;
-    while(getRegister(0x4D) & 0x01 != 0x01);
-    setRegister(0x018, 0x01); // start measurement
-    while(getRegister(0x4F) & 0x04 != 0x04);
-    distance = getRegister(0x62);
-    setRegister(0x15, 0x07);
+    // should test bit 2  of RESULT_INTERRUPT_STATUS_GPIO ?
+    distance = getRegister(RESULT_RANGE_VAL);
+    // should clear interrupt ?
+    return distance;   
+}
+
+int VL6180x::getSingleDistance()
+{
+    int distance = 0;
+    while(getRegister(RESULT_RANGE_STATUS) & 0x01 != 0x01);
+    setRegister(SYSRANGE_START, 0x01); // start measurement
+    while(getRegister(RESULT_INTERRUPT_STATUS_GPIO) & 0x04 != 0x04);
+    distance = getRegister(RESULT_RANGE_VAL);
+    setRegister(SYSTEM_INTERRUPT_CLEAR, 0x07);
     return distance;
 }
 
@@ -90,7 +105,6 @@
 
 void VL6180x::setRegister(int reg, int value)
 {
-//INTERLEAVED_MODE_ENABLE
     char data_write[3];
     data_write[0] = ((reg &0xff00 ) >> 8);
     data_write[1] = (reg &0x00ff );