sdsd
VL6180.h@4:aab6582c7456, 2020-04-03 (annotated)
- Committer:
- maazshaikh
- Date:
- Fri Apr 03 15:38:03 2020 +0000
- Revision:
- 4:aab6582c7456
- Parent:
- 2:595673e4c505
sds
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
maazshaikh | 4:aab6582c7456 | 1 | /****************************************************************************** |
maazshaikh | 4:aab6582c7456 | 2 | * SFE_VL6180.h |
maazshaikh | 4:aab6582c7456 | 3 | * Library for VL6180 time of flight range finder. |
maazshaikh | 4:aab6582c7456 | 4 | * Casey Kuhns @ SparkFun Electronics |
maazshaikh | 4:aab6582c7456 | 5 | * 10/29/2014 |
maazshaikh | 4:aab6582c7456 | 6 | * https://github.com/sparkfun/ |
maazshaikh | 4:aab6582c7456 | 7 | * |
maazshaikh | 4:aab6582c7456 | 8 | * The VL6180 by ST micro is a time of flight range finder that |
maazshaikh | 4:aab6582c7456 | 9 | * uses pulsed IR light to determine distances from object at close |
maazshaikh | 4:aab6582c7456 | 10 | * range. The average range of a sensor is between 0-200mm |
maazshaikh | 4:aab6582c7456 | 11 | * |
maazshaikh | 4:aab6582c7456 | 12 | * In this file are the function prototypes in the VL6180 class |
maazshaikh | 4:aab6582c7456 | 13 | * |
maazshaikh | 4:aab6582c7456 | 14 | * Resources: |
maazshaikh | 4:aab6582c7456 | 15 | * This library uses the Arduino Wire.h to complete I2C transactions. |
maazshaikh | 4:aab6582c7456 | 16 | * |
maazshaikh | 4:aab6582c7456 | 17 | * Development environment specifics: |
maazshaikh | 4:aab6582c7456 | 18 | * IDE: Arduino 1.0.5 |
maazshaikh | 4:aab6582c7456 | 19 | * Hardware Platform: Arduino Pro 3.3V/8MHz |
maazshaikh | 4:aab6582c7456 | 20 | * VL6180 Breakout Version: 1.0 |
maazshaikh | 4:aab6582c7456 | 21 | * |
maazshaikh | 4:aab6582c7456 | 22 | * Some settings and initial values come from code written by Kris Winer |
maazshaikh | 4:aab6582c7456 | 23 | * VL6180_t3 Basic Example Code |
maazshaikh | 4:aab6582c7456 | 24 | * by: Kris Winer |
maazshaikh | 4:aab6582c7456 | 25 | * date: September 1, 2014 |
maazshaikh | 4:aab6582c7456 | 26 | * license: Beerware - Use this code however you'd like. If you |
maazshaikh | 4:aab6582c7456 | 27 | * find it useful you can buy me a beer some time. |
maazshaikh | 4:aab6582c7456 | 28 | * |
maazshaikh | 4:aab6582c7456 | 29 | * This code is beerware. If you see me (or any other SparkFun employee) at the |
maazshaikh | 4:aab6582c7456 | 30 | * local pub, and you've found our code helpful, please buy us a round! |
maazshaikh | 4:aab6582c7456 | 31 | * |
maazshaikh | 4:aab6582c7456 | 32 | * Distributed as-is; no warranty is given. |
maazshaikh | 4:aab6582c7456 | 33 | ******************************************************************************/ |
maazshaikh | 4:aab6582c7456 | 34 | |
maazshaikh | 4:aab6582c7456 | 35 | #ifndef SFE_VL6180_h |
maazshaikh | 4:aab6582c7456 | 36 | #define SFE_VL6180_h |
sburg | 0:f23c00f688b2 | 37 | |
sburg | 0:f23c00f688b2 | 38 | #include "mbed.h" |
maazshaikh | 4:aab6582c7456 | 39 | #include "stdint.h" |
maazshaikh | 4:aab6582c7456 | 40 | |
maazshaikh | 4:aab6582c7456 | 41 | #define VL6180_DEF_ADDR 0x52 |
maazshaikh | 4:aab6582c7456 | 42 | |
maazshaikh | 4:aab6582c7456 | 43 | #define VL6180_FAILURE_RESET -1 |
maazshaikh | 4:aab6582c7456 | 44 | |
maazshaikh | 4:aab6582c7456 | 45 | #define VL6180_IDENTIFICATION_MODEL_ID 0x0000 |
maazshaikh | 4:aab6582c7456 | 46 | #define VL6180_IDENTIFICATION_MODEL_REV_MAJOR 0x0001 |
maazshaikh | 4:aab6582c7456 | 47 | #define VL6180_IDENTIFICATION_MODEL_REV_MINOR 0x0002 |
maazshaikh | 4:aab6582c7456 | 48 | #define VL6180_IDENTIFICATION_MODULE_REV_MAJOR 0x0003 |
maazshaikh | 4:aab6582c7456 | 49 | #define VL6180_IDENTIFICATION_MODULE_REV_MINOR 0x0004 |
maazshaikh | 4:aab6582c7456 | 50 | #define VL6180_IDENTIFICATION_DATE 0x0006 //16bit value |
maazshaikh | 4:aab6582c7456 | 51 | #define VL6180_IDENTIFICATION_TIME 0x0008 //16bit value |
maazshaikh | 4:aab6582c7456 | 52 | |
maazshaikh | 4:aab6582c7456 | 53 | #define VL6180_SYSTEM_MODE_GPIO0 0x0010 |
maazshaikh | 4:aab6582c7456 | 54 | #define VL6180_SYSTEM_MODE_GPIO1 0x0011 |
maazshaikh | 4:aab6582c7456 | 55 | #define VL6180_SYSTEM_HISTORY_CTRL 0x0012 |
maazshaikh | 4:aab6582c7456 | 56 | #define VL6180_SYSTEM_INTERRUPT_CONFIG_GPIO 0x0014 |
maazshaikh | 4:aab6582c7456 | 57 | #define VL6180_SYSTEM_INTERRUPT_CLEAR 0x0015 |
maazshaikh | 4:aab6582c7456 | 58 | #define VL6180_SYSTEM_FRESH_OUT_OF_RESET 0x0016 |
maazshaikh | 4:aab6582c7456 | 59 | #define VL6180_SYSTEM_GROUPED_PARAMETER_HOLD 0x0017 |
maazshaikh | 4:aab6582c7456 | 60 | |
maazshaikh | 4:aab6582c7456 | 61 | #define VL6180_SYSRANGE_START 0x0018 |
maazshaikh | 4:aab6582c7456 | 62 | #define VL6180_SYSRANGE_THRESH_HIGH 0x0019 |
maazshaikh | 4:aab6582c7456 | 63 | #define VL6180_SYSRANGE_THRESH_LOW 0x001A |
maazshaikh | 4:aab6582c7456 | 64 | #define VL6180_SYSRANGE_INTERMEASUREMENT_PERIOD 0x001B |
maazshaikh | 4:aab6582c7456 | 65 | #define VL6180_SYSRANGE_MAX_CONVERGENCE_TIME 0x001C |
maazshaikh | 4:aab6582c7456 | 66 | #define VL6180_SYSRANGE_CROSSTALK_COMPENSATION_RATE 0x001E |
maazshaikh | 4:aab6582c7456 | 67 | #define VL6180_SYSRANGE_CROSSTALK_VALID_HEIGHT 0x0021 |
maazshaikh | 4:aab6582c7456 | 68 | #define VL6180_SYSRANGE_EARLY_CONVERGENCE_ESTIMATE 0x0022 |
maazshaikh | 4:aab6582c7456 | 69 | #define VL6180_SYSRANGE_PART_TO_PART_RANGE_OFFSET 0x0024 |
maazshaikh | 4:aab6582c7456 | 70 | #define VL6180_SYSRANGE_RANGE_IGNORE_VALID_HEIGHT 0x0025 |
maazshaikh | 4:aab6582c7456 | 71 | #define VL6180_SYSRANGE_RANGE_IGNORE_THRESHOLD 0x0026 |
maazshaikh | 4:aab6582c7456 | 72 | #define VL6180_SYSRANGE_MAX_AMBIENT_LEVEL_MULT 0x002C |
maazshaikh | 4:aab6582c7456 | 73 | #define VL6180_SYSRANGE_RANGE_CHECK_ENABLES 0x002D |
maazshaikh | 4:aab6582c7456 | 74 | #define VL6180_SYSRANGE_VHV_RECALIBRATE 0x002E |
maazshaikh | 4:aab6582c7456 | 75 | #define VL6180_SYSRANGE_VHV_REPEAT_RATE 0x0031 |
maazshaikh | 4:aab6582c7456 | 76 | |
maazshaikh | 4:aab6582c7456 | 77 | #define VL6180_SYSALS_START 0x0038 |
maazshaikh | 4:aab6582c7456 | 78 | #define VL6180_SYSALS_THRESH_HIGH 0x003A |
maazshaikh | 4:aab6582c7456 | 79 | #define VL6180_SYSALS_THRESH_LOW 0x003C |
maazshaikh | 4:aab6582c7456 | 80 | #define VL6180_SYSALS_INTERMEASUREMENT_PERIOD 0x003E |
maazshaikh | 4:aab6582c7456 | 81 | #define VL6180_SYSALS_ANALOGUE_GAIN 0x003F |
maazshaikh | 4:aab6582c7456 | 82 | #define VL6180_SYSALS_INTEGRATION_PERIOD 0x0040 |
maazshaikh | 4:aab6582c7456 | 83 | |
maazshaikh | 4:aab6582c7456 | 84 | #define VL6180_RESULT_RANGE_STATUS 0x004D |
maazshaikh | 4:aab6582c7456 | 85 | #define VL6180_RESULT_ALS_STATUS 0x004E |
maazshaikh | 4:aab6582c7456 | 86 | #define VL6180_RESULT_INTERRUPT_STATUS_GPIO 0x004F |
maazshaikh | 4:aab6582c7456 | 87 | #define VL6180_RESULT_ALS_VAL 0x0050 |
maazshaikh | 4:aab6582c7456 | 88 | #define VL6180_RESULT_HISTORY_BUFFER 0x0052 |
maazshaikh | 4:aab6582c7456 | 89 | #define VL6180_RESULT_RANGE_VAL 0x0062 |
maazshaikh | 4:aab6582c7456 | 90 | #define VL6180_RESULT_RANGE_RAW 0x0064 |
maazshaikh | 4:aab6582c7456 | 91 | #define VL6180_RESULT_RANGE_RETURN_RATE 0x0066 |
maazshaikh | 4:aab6582c7456 | 92 | #define VL6180_RESULT_RANGE_REFERENCE_RATE 0x0068 |
maazshaikh | 4:aab6582c7456 | 93 | #define VL6180_RESULT_RANGE_RETURN_SIGNAL_COUNT 0x006C |
maazshaikh | 4:aab6582c7456 | 94 | #define VL6180_RESULT_RANGE_REFERENCE_SIGNAL_COUNT 0x0070 |
maazshaikh | 4:aab6582c7456 | 95 | #define VL6180_RESULT_RANGE_RETURN_AMB_COUNT 0x0074 |
maazshaikh | 4:aab6582c7456 | 96 | #define VL6180_RESULT_RANGE_REFERENCE_AMB_COUNT 0x0078 |
maazshaikh | 4:aab6582c7456 | 97 | #define VL6180_RESULT_RANGE_RETURN_CONV_TIME 0x007C |
maazshaikh | 4:aab6582c7456 | 98 | #define VL6180_RESULT_RANGE_REFERENCE_CONV_TIME 0x0080 |
maazshaikh | 4:aab6582c7456 | 99 | |
maazshaikh | 4:aab6582c7456 | 100 | #define VL6180_READOUT_AVERAGING_SAMPLE_PERIOD 0x010A |
maazshaikh | 4:aab6582c7456 | 101 | #define VL6180_FIRMWARE_BOOTUP 0x0119 |
maazshaikh | 4:aab6582c7456 | 102 | #define VL6180_FIRMWARE_RESULT_SCALER 0x0120 |
maazshaikh | 4:aab6582c7456 | 103 | #define VL6180_I2C_SLAVE_DEVICE_ADDRESS 0x0212 |
maazshaikh | 4:aab6582c7456 | 104 | #define VL6180_INTERLEAVED_MODE_ENABLE 0x02A3 |
maazshaikh | 4:aab6582c7456 | 105 | |
maazshaikh | 4:aab6582c7456 | 106 | |
maazshaikh | 4:aab6582c7456 | 107 | enum VL6180_als_gain { //Data sheet shows gain values as binary list |
maazshaikh | 4:aab6582c7456 | 108 | |
maazshaikh | 4:aab6582c7456 | 109 | GAIN_20 = 0, // Actual ALS Gain of 20 |
maazshaikh | 4:aab6582c7456 | 110 | GAIN_10, // Actual ALS Gain of 10.32 |
maazshaikh | 4:aab6582c7456 | 111 | GAIN_5, // Actual ALS Gain of 5.21 |
maazshaikh | 4:aab6582c7456 | 112 | GAIN_2_5, // Actual ALS Gain of 2.60 |
maazshaikh | 4:aab6582c7456 | 113 | GAIN_1_67, // Actual ALS Gain of 1.72 |
maazshaikh | 4:aab6582c7456 | 114 | GAIN_1_25, // Actual ALS Gain of 1.28 |
maazshaikh | 4:aab6582c7456 | 115 | GAIN_1 , // Actual ALS Gain of 1.01 |
maazshaikh | 4:aab6582c7456 | 116 | GAIN_40, // Actual ALS Gain of 40 |
maazshaikh | 4:aab6582c7456 | 117 | |
maazshaikh | 4:aab6582c7456 | 118 | }; |
maazshaikh | 4:aab6582c7456 | 119 | |
maazshaikh | 4:aab6582c7456 | 120 | struct VL6180Identification { |
maazshaikh | 4:aab6582c7456 | 121 | uint8_t idModel; |
maazshaikh | 4:aab6582c7456 | 122 | uint8_t idModelRevMajor; |
maazshaikh | 4:aab6582c7456 | 123 | uint8_t idModelRevMinor; |
maazshaikh | 4:aab6582c7456 | 124 | uint8_t idModuleRevMajor; |
maazshaikh | 4:aab6582c7456 | 125 | uint8_t idModuleRevMinor; |
maazshaikh | 4:aab6582c7456 | 126 | uint16_t idDate; |
maazshaikh | 4:aab6582c7456 | 127 | uint16_t idTime; |
maazshaikh | 4:aab6582c7456 | 128 | }; |
maazshaikh | 4:aab6582c7456 | 129 | |
maazshaikh | 4:aab6582c7456 | 130 | |
maazshaikh | 4:aab6582c7456 | 131 | class VL6180 |
maazshaikh | 4:aab6582c7456 | 132 | { |
sburg | 0:f23c00f688b2 | 133 | public: |
maazshaikh | 4:aab6582c7456 | 134 | /** Create VL6180 instance |
maazshaikh | 4:aab6582c7456 | 135 | * |
maazshaikh | 4:aab6582c7456 | 136 | * @param sda sda pin for I2C |
maazshaikh | 4:aab6582c7456 | 137 | * @param scl scl pin for I2C |
sburg | 0:f23c00f688b2 | 138 | */ |
maazshaikh | 4:aab6582c7456 | 139 | VL6180( PinName sda, PinName scl ); |
maazshaikh | 4:aab6582c7456 | 140 | VL6180( PinName sda, PinName scl , int i2c_address); |
sburg | 0:f23c00f688b2 | 141 | |
maazshaikh | 4:aab6582c7456 | 142 | /** Destructor |
maazshaikh | 4:aab6582c7456 | 143 | */ |
maazshaikh | 4:aab6582c7456 | 144 | ~VL6180(); |
maazshaikh | 4:aab6582c7456 | 145 | |
maazshaikh | 4:aab6582c7456 | 146 | /** Read red, green, blue and clear values into array |
maazshaikh | 4:aab6582c7456 | 147 | * |
maazshaikh | 4:aab6582c7456 | 148 | * @param readings Array of four integers to store the read data |
sburg | 0:f23c00f688b2 | 149 | */ |
maazshaikh | 4:aab6582c7456 | 150 | //Initalize library with default address |
maazshaikh | 4:aab6582c7456 | 151 | // VL6180(uint8_t address); |
maazshaikh | 4:aab6582c7456 | 152 | //Send manditory settings as stated in ST datasheet. |
maazshaikh | 4:aab6582c7456 | 153 | // http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf (Section 1.3) |
maazshaikh | 4:aab6582c7456 | 154 | int8_t VL6180_Init(void); |
maazshaikh | 4:aab6582c7456 | 155 | // Use default settings from ST data sheet section 9. |
maazshaikh | 4:aab6582c7456 | 156 | // http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf |
maazshaikh | 4:aab6582c7456 | 157 | void VL6180_DefautSettings(void); |
maazshaikh | 4:aab6582c7456 | 158 | |
maazshaikh | 4:aab6582c7456 | 159 | // Get Range distance in (mm) |
maazshaikh | 4:aab6582c7456 | 160 | uint8_t getDistance(); |
maazshaikh | 4:aab6582c7456 | 161 | // Get ALS level in Lux |
maazshaikh | 4:aab6582c7456 | 162 | float getAmbientLight(VL6180_als_gain VL6180_ALS_GAIN); |
maazshaikh | 4:aab6582c7456 | 163 | |
maazshaikh | 4:aab6582c7456 | 164 | //Load structure provided by the user with identification info |
maazshaikh | 4:aab6582c7456 | 165 | //Structure example: |
maazshaikh | 4:aab6582c7456 | 166 | // struct VL6180Identification |
maazshaikh | 4:aab6582c7456 | 167 | // { |
maazshaikh | 4:aab6582c7456 | 168 | // uint8_t idModel; |
maazshaikh | 4:aab6582c7456 | 169 | // uint8_t idModelRevMajor; |
maazshaikh | 4:aab6582c7456 | 170 | // uint8_t idModelRevMinor; |
maazshaikh | 4:aab6582c7456 | 171 | // uint8_t idModuleRevMajor; |
maazshaikh | 4:aab6582c7456 | 172 | // uint8_t idModuleRevMinor; |
maazshaikh | 4:aab6582c7456 | 173 | // uint16_t idDate; |
maazshaikh | 4:aab6582c7456 | 174 | // uint16_t idTime; |
maazshaikh | 4:aab6582c7456 | 175 | // }; |
maazshaikh | 4:aab6582c7456 | 176 | void getIdentification(struct VL6180Identification *temp); |
maazshaikh | 4:aab6582c7456 | 177 | |
maazshaikh | 4:aab6582c7456 | 178 | //Change the default address of the device to allow multiple |
maazshaikh | 4:aab6582c7456 | 179 | //sensors on the bus. Can use up to 127 sensors. New address |
maazshaikh | 4:aab6582c7456 | 180 | //is saved in non-volatile device memory. |
maazshaikh | 4:aab6582c7456 | 181 | uint8_t changeAddress(uint8_t old_address, uint8_t new_address); |
sburg | 0:f23c00f688b2 | 182 | |
maazshaikh | 4:aab6582c7456 | 183 | int writeSingleRegister( uint16_t reg_address, uint8_t data ); |
maazshaikh | 4:aab6582c7456 | 184 | |
maazshaikh | 4:aab6582c7456 | 185 | |
sburg | 0:f23c00f688b2 | 186 | private: |
maazshaikh | 4:aab6582c7456 | 187 | //Store address given when the class is initialized. |
maazshaikh | 4:aab6582c7456 | 188 | //This value can be changed by the changeAddress() function |
maazshaikh | 4:aab6582c7456 | 189 | I2C i2c; |
maazshaikh | 4:aab6582c7456 | 190 | int VL6180_i2cAddress; |
maazshaikh | 4:aab6582c7456 | 191 | uint8_t VL6180_error_no; |
maazshaikh | 4:aab6582c7456 | 192 | |
maazshaikh | 4:aab6582c7456 | 193 | uint8_t VL6180_getRegister(uint16_t registerAddr); |
maazshaikh | 4:aab6582c7456 | 194 | uint16_t VL6180_getRegister16bit(uint16_t registerAddr); |
maazshaikh | 4:aab6582c7456 | 195 | |
maazshaikh | 4:aab6582c7456 | 196 | void VL6180_setRegister(uint16_t registerAddr, uint8_t value); |
maazshaikh | 4:aab6582c7456 | 197 | void VL6180_setRegister16bit(uint16_t registerAddr, uint16_t value); |
maazshaikh | 4:aab6582c7456 | 198 | |
maazshaikh | 4:aab6582c7456 | 199 | |
sburg | 0:f23c00f688b2 | 200 | }; |
sburg | 0:f23c00f688b2 | 201 | |
sburg | 0:f23c00f688b2 | 202 | #endif |