i2c driver for VL6180x distance sensor
Revision 2:2e0406672e67, committed 2015-12-19
- Comitter:
- sillevl
- Date:
- Sat Dec 19 13:27:01 2015 +0000
- Parent:
- 1:6a00afc8dc84
- Commit message:
- add registernames and continuous mode
Changed in this revision
vl6180x.cpp | Show annotated file Show diff for this revision Revisions of this file |
vl6180x.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 6a00afc8dc84 -r 2e0406672e67 vl6180x.cpp --- 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 );
diff -r 6a00afc8dc84 -r 2e0406672e67 vl6180x.h --- a/vl6180x.h Sat Dec 19 10:42:18 2015 +0000 +++ b/vl6180x.h Sat Dec 19 13:27:01 2015 +0000 @@ -21,9 +21,11 @@ void initialize(); int getDistance(); + int getSingleDistance(); float getAmbientLight(); Identification getIdentification(); static void printIdentification(Identification id); + void startContinuousOperation(); void setRegister(int register, int value); int getRegister(int reg); @@ -34,11 +36,69 @@ int address; I2C i2c; - - static const int I2C_SLAVE_DEFAULT_ADDRESS = 0x52; + + static const int IDENTIFICATION_MODEL_ID = 0x000; + static const int IDENTIFICATION_MODEL_REV_MAJOR = 0x001; + static const int IDENTIFICATION_MODEL_REV_MINOR = 0x002; + static const int IDENTIFICATION_MODULE_REV_MAJOR = 0x003; + static const int IDENTIFICATION_MODULE_REV_MINOR = 0x004; + static const int IDENTIFICATION_DATE_HI = 0x006; + static const int IDENTIFICATION_DATE_LO = 0x007; + static const int IDENTIFICATION_TIME = 0x008; + static const int SYSTEM_MODE_GPIO0 = 0x010; + static const int SYSTEM_MODE_GPIO1 = 0x011; + static const int SYSTEM_HISTORY_CTRL = 0x012; + static const int SYSTEM_INTERRUPT_CONFIG_GPIO = 0x014; + static const int SYSTEM_INTERRUPT_CLEAR = 0x015; + static const int SYSTEM_FRESH_OUT_OF_RESET = 0x016; + static const int SYSTEM_GROUPED_PARAMETER_HOLD = 0x017; + static const int SYSRANGE_START = 0x018; + static const int SYSRANGE_THRESH_HIGH = 0x019; + static const int SYSRANGE_THRESH_LOW = 0x01A; + static const int SYSRANGE_INTERMEASUREMENT_PERIOD = 0x01B; + static const int SYSRANGE_MAX_CONVERGENCE_TIME = 0x01C; + static const int SYSRANGE_CROSSTALK_COMPENSATION_RATE = 0x01E; + static const int SYSRANGE_CROSSTALK_VALID_HEIGHT = 0x021; + static const int SYSRANGE_EARLY_CONVERGENCE_ESTIMATE = 0x022; + static const int SYSRANGE_PART_TO_PART_RANGE_OFFSET = 0x024; + static const int SYSRANGE_RANGE_IGNORE_VALID_HEIGHT = 0x025; + static const int SYSRANGE_RANGE_IGNORE_THRESHOLD = 0x026; + static const int SYSRANGE_MAX_AMBIENT_LEVEL_MULT = 0x02C; + static const int SYSRANGE_RANGE_CHECK_ENABLES = 0x02D; + static const int SYSRANGE_VHV_RECALIBRATE = 0x02E; + static const int SYSRANGE_VHV_REPEAT_RATE = 0x031; + static const int SYSALS_START = 0x038; + static const int SYSALS_THRESH_HIGH = 0x03A; + static const int SYSALS_THRESH_LOW = 0x03C; + static const int SYSALS_INTERMEASUREMENT_PERIOD = 0x03E; + static const int SYSALS_ANALOGUE_GAIN = 0x03F; + static const int SYSALS_INTEGRATION_PERIOD = 0x040; + static const int RESULT_RANGE_STATUS = 0x04D; + static const int RESULT_ALS_STATUS = 0x04E; + static const int RESULT_INTERRUPT_STATUS_GPIO = 0x04F; + static const int RESULT_ALS_VAL = 0x050; + static const int RESULT_HISTORY_BUFFER_0 = 0x052; + static const int RESULT_HISTORY_BUFFER_1 = 0x054; + static const int RESULT_HISTORY_BUFFER_2 = 0x056; + static const int RESULT_HISTORY_BUFFER_3 = 0x058; + static const int RESULT_HISTORY_BUFFER_4 = 0x05A; + static const int RESULT_HISTORY_BUFFER_5 = 0x05C; + static const int RESULT_HISTORY_BUFFER_6 = 0x05E; + static const int RESULT_HISTORY_BUFFER_7 = 0x060; + static const int RESULT_RANGE_VAL = 0x062; + static const int RESULT_RANGE_RAW = 0x064; + static const int RESULT_RANGE_RETURN_RATE = 0x066; + static const int RESULT_RANGE_REFERENCE_RATE = 0x068; + static const int RESULT_RANGE_RETURN_SIGNAL_COUNT = 0x06C; + static const int RESULT_RANGE_REFERENCE_SIGNAL_COUNT = 0x070; + static const int RESULT_RANGE_RETURN_AMB_COUNT = 0x074; + static const int RESULT_RANGE_REFERENCE_AMB_COUNT = 0x078; + static const int RESULT_RANGE_RETURN_CONV_TIME = 0x07C; + static const int RESULT_RANGE_REFERENCE_CONV_TIME = 0x080; + static const int READOUT_AVERAGING_SAMPLE_PERIOD = 0x10A; + static const int FIRMWARE_BOOTUP = 0x119; + static const int FIRMWARE_RESULT_SCALER = 0x120; static const int I2C_SLAVE_DEVICE_ADDRESS = 0x212; - static const int INTERLEAVED_MODE_ENABLE = 0x2A3; - }; \ No newline at end of file