Simple VL6180x demo for VL6180x Explorer shield with Nucleo F401 adapted from SparkFun example.
Dependents: VL6180X VL6180X_Explorer VL6180X_Explorer VL6180X ... more
VL6180x.h@0:4a5d4b4a40c4, 2015-05-10 (annotated)
- Committer:
- highroads
- Date:
- Sun May 10 07:43:39 2015 +0000
- Revision:
- 0:4a5d4b4a40c4
- Child:
- 1:3ebf8893b7e0
First version that works
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
highroads | 0:4a5d4b4a40c4 | 1 | /****************************************************************************** |
highroads | 0:4a5d4b4a40c4 | 2 | * SFE_VL6180X.h |
highroads | 0:4a5d4b4a40c4 | 3 | * Library for VL6180x time of flight range finder. |
highroads | 0:4a5d4b4a40c4 | 4 | * Casey Kuhns @ SparkFun Electronics |
highroads | 0:4a5d4b4a40c4 | 5 | * 10/29/2014 |
highroads | 0:4a5d4b4a40c4 | 6 | * https://github.com/sparkfun/ |
highroads | 0:4a5d4b4a40c4 | 7 | * |
highroads | 0:4a5d4b4a40c4 | 8 | * The VL6180x by ST micro is a time of flight range finder that |
highroads | 0:4a5d4b4a40c4 | 9 | * uses pulsed IR light to determine distances from object at close |
highroads | 0:4a5d4b4a40c4 | 10 | * range. The average range of a sensor is between 0-200mm |
highroads | 0:4a5d4b4a40c4 | 11 | * |
highroads | 0:4a5d4b4a40c4 | 12 | * In this file are the function prototypes in the VL6180x class |
highroads | 0:4a5d4b4a40c4 | 13 | * |
highroads | 0:4a5d4b4a40c4 | 14 | * Resources: |
highroads | 0:4a5d4b4a40c4 | 15 | * This library uses the Arduino Wire.h to complete I2C transactions. |
highroads | 0:4a5d4b4a40c4 | 16 | * |
highroads | 0:4a5d4b4a40c4 | 17 | * Development environment specifics: |
highroads | 0:4a5d4b4a40c4 | 18 | * IDE: Arduino 1.0.5 |
highroads | 0:4a5d4b4a40c4 | 19 | * Hardware Platform: Arduino Pro 3.3V/8MHz |
highroads | 0:4a5d4b4a40c4 | 20 | * VL6180x Breakout Version: 1.0 |
highroads | 0:4a5d4b4a40c4 | 21 | * |
highroads | 0:4a5d4b4a40c4 | 22 | * Some settings and initial values come from code written by Kris Winer |
highroads | 0:4a5d4b4a40c4 | 23 | * VL6180X_t3 Basic Example Code |
highroads | 0:4a5d4b4a40c4 | 24 | * by: Kris Winer |
highroads | 0:4a5d4b4a40c4 | 25 | * date: September 1, 2014 |
highroads | 0:4a5d4b4a40c4 | 26 | * license: Beerware - Use this code however you'd like. If you |
highroads | 0:4a5d4b4a40c4 | 27 | * find it useful you can buy me a beer some time. |
highroads | 0:4a5d4b4a40c4 | 28 | * |
highroads | 0:4a5d4b4a40c4 | 29 | * This code is beerware. If you see me (or any other SparkFun employee) at the |
highroads | 0:4a5d4b4a40c4 | 30 | * local pub, and you've found our code helpful, please buy us a round! |
highroads | 0:4a5d4b4a40c4 | 31 | * |
highroads | 0:4a5d4b4a40c4 | 32 | * Distributed as-is; no warranty is given. |
highroads | 0:4a5d4b4a40c4 | 33 | ******************************************************************************/ |
highroads | 0:4a5d4b4a40c4 | 34 | #include "mbed.h" |
highroads | 0:4a5d4b4a40c4 | 35 | #ifndef VL6180x_h |
highroads | 0:4a5d4b4a40c4 | 36 | #define VL6180x_h |
highroads | 0:4a5d4b4a40c4 | 37 | |
highroads | 0:4a5d4b4a40c4 | 38 | #define VL6180x_FAILURE_RESET -1 |
highroads | 0:4a5d4b4a40c4 | 39 | |
highroads | 0:4a5d4b4a40c4 | 40 | #define VL6180X_IDENTIFICATION_MODEL_ID 0x0000 |
highroads | 0:4a5d4b4a40c4 | 41 | #define VL6180X_IDENTIFICATION_MODEL_REV_MAJOR 0x0001 |
highroads | 0:4a5d4b4a40c4 | 42 | #define VL6180X_IDENTIFICATION_MODEL_REV_MINOR 0x0002 |
highroads | 0:4a5d4b4a40c4 | 43 | #define VL6180X_IDENTIFICATION_MODULE_REV_MAJOR 0x0003 |
highroads | 0:4a5d4b4a40c4 | 44 | #define VL6180X_IDENTIFICATION_MODULE_REV_MINOR 0x0004 |
highroads | 0:4a5d4b4a40c4 | 45 | #define VL6180X_IDENTIFICATION_DATE 0x0006 //16bit value |
highroads | 0:4a5d4b4a40c4 | 46 | #define VL6180X_IDENTIFICATION_TIME 0x0008 //16bit value |
highroads | 0:4a5d4b4a40c4 | 47 | |
highroads | 0:4a5d4b4a40c4 | 48 | #define VL6180X_SYSTEM_MODE_GPIO0 0x0010 |
highroads | 0:4a5d4b4a40c4 | 49 | #define VL6180X_SYSTEM_MODE_GPIO1 0x0011 |
highroads | 0:4a5d4b4a40c4 | 50 | #define VL6180X_SYSTEM_HISTORY_CTRL 0x0012 |
highroads | 0:4a5d4b4a40c4 | 51 | #define VL6180X_SYSTEM_INTERRUPT_CONFIG_GPIO 0x0014 |
highroads | 0:4a5d4b4a40c4 | 52 | #define VL6180X_SYSTEM_INTERRUPT_CLEAR 0x0015 |
highroads | 0:4a5d4b4a40c4 | 53 | #define VL6180X_SYSTEM_FRESH_OUT_OF_RESET 0x0016 |
highroads | 0:4a5d4b4a40c4 | 54 | #define VL6180X_SYSTEM_GROUPED_PARAMETER_HOLD 0x0017 |
highroads | 0:4a5d4b4a40c4 | 55 | |
highroads | 0:4a5d4b4a40c4 | 56 | #define VL6180X_SYSRANGE_START 0x0018 |
highroads | 0:4a5d4b4a40c4 | 57 | #define VL6180X_SYSRANGE_THRESH_HIGH 0x0019 |
highroads | 0:4a5d4b4a40c4 | 58 | #define VL6180X_SYSRANGE_THRESH_LOW 0x001A |
highroads | 0:4a5d4b4a40c4 | 59 | #define VL6180X_SYSRANGE_INTERMEASUREMENT_PERIOD 0x001B |
highroads | 0:4a5d4b4a40c4 | 60 | #define VL6180X_SYSRANGE_MAX_CONVERGENCE_TIME 0x001C |
highroads | 0:4a5d4b4a40c4 | 61 | #define VL6180X_SYSRANGE_CROSSTALK_COMPENSATION_RATE 0x001E |
highroads | 0:4a5d4b4a40c4 | 62 | #define VL6180X_SYSRANGE_CROSSTALK_VALID_HEIGHT 0x0021 |
highroads | 0:4a5d4b4a40c4 | 63 | #define VL6180X_SYSRANGE_EARLY_CONVERGENCE_ESTIMATE 0x0022 |
highroads | 0:4a5d4b4a40c4 | 64 | #define VL6180X_SYSRANGE_PART_TO_PART_RANGE_OFFSET 0x0024 |
highroads | 0:4a5d4b4a40c4 | 65 | #define VL6180X_SYSRANGE_RANGE_IGNORE_VALID_HEIGHT 0x0025 |
highroads | 0:4a5d4b4a40c4 | 66 | #define VL6180X_SYSRANGE_RANGE_IGNORE_THRESHOLD 0x0026 |
highroads | 0:4a5d4b4a40c4 | 67 | #define VL6180X_SYSRANGE_MAX_AMBIENT_LEVEL_MULT 0x002C |
highroads | 0:4a5d4b4a40c4 | 68 | #define VL6180X_SYSRANGE_RANGE_CHECK_ENABLES 0x002D |
highroads | 0:4a5d4b4a40c4 | 69 | #define VL6180X_SYSRANGE_VHV_RECALIBRATE 0x002E |
highroads | 0:4a5d4b4a40c4 | 70 | #define VL6180X_SYSRANGE_VHV_REPEAT_RATE 0x0031 |
highroads | 0:4a5d4b4a40c4 | 71 | |
highroads | 0:4a5d4b4a40c4 | 72 | #define VL6180X_SYSALS_START 0x0038 |
highroads | 0:4a5d4b4a40c4 | 73 | #define VL6180X_SYSALS_THRESH_HIGH 0x003A |
highroads | 0:4a5d4b4a40c4 | 74 | #define VL6180X_SYSALS_THRESH_LOW 0x003C |
highroads | 0:4a5d4b4a40c4 | 75 | #define VL6180X_SYSALS_INTERMEASUREMENT_PERIOD 0x003E |
highroads | 0:4a5d4b4a40c4 | 76 | #define VL6180X_SYSALS_ANALOGUE_GAIN 0x003F |
highroads | 0:4a5d4b4a40c4 | 77 | #define VL6180X_SYSALS_INTEGRATION_PERIOD 0x0040 |
highroads | 0:4a5d4b4a40c4 | 78 | |
highroads | 0:4a5d4b4a40c4 | 79 | #define VL6180X_RESULT_RANGE_STATUS 0x004D |
highroads | 0:4a5d4b4a40c4 | 80 | #define VL6180X_RESULT_ALS_STATUS 0x004E |
highroads | 0:4a5d4b4a40c4 | 81 | #define VL6180X_RESULT_INTERRUPT_STATUS_GPIO 0x004F |
highroads | 0:4a5d4b4a40c4 | 82 | #define VL6180X_RESULT_ALS_VAL 0x0050 |
highroads | 0:4a5d4b4a40c4 | 83 | #define VL6180X_RESULT_HISTORY_BUFFER 0x0052 |
highroads | 0:4a5d4b4a40c4 | 84 | #define VL6180X_RESULT_RANGE_VAL 0x0062 |
highroads | 0:4a5d4b4a40c4 | 85 | #define VL6180X_RESULT_RANGE_RAW 0x0064 |
highroads | 0:4a5d4b4a40c4 | 86 | #define VL6180X_RESULT_RANGE_RETURN_RATE 0x0066 |
highroads | 0:4a5d4b4a40c4 | 87 | #define VL6180X_RESULT_RANGE_REFERENCE_RATE 0x0068 |
highroads | 0:4a5d4b4a40c4 | 88 | #define VL6180X_RESULT_RANGE_RETURN_SIGNAL_COUNT 0x006C |
highroads | 0:4a5d4b4a40c4 | 89 | #define VL6180X_RESULT_RANGE_REFERENCE_SIGNAL_COUNT 0x0070 |
highroads | 0:4a5d4b4a40c4 | 90 | #define VL6180X_RESULT_RANGE_RETURN_AMB_COUNT 0x0074 |
highroads | 0:4a5d4b4a40c4 | 91 | #define VL6180X_RESULT_RANGE_REFERENCE_AMB_COUNT 0x0078 |
highroads | 0:4a5d4b4a40c4 | 92 | #define VL6180X_RESULT_RANGE_RETURN_CONV_TIME 0x007C |
highroads | 0:4a5d4b4a40c4 | 93 | #define VL6180X_RESULT_RANGE_REFERENCE_CONV_TIME 0x0080 |
highroads | 0:4a5d4b4a40c4 | 94 | |
highroads | 0:4a5d4b4a40c4 | 95 | #define VL6180X_READOUT_AVERAGING_SAMPLE_PERIOD 0x010A |
highroads | 0:4a5d4b4a40c4 | 96 | #define VL6180X_FIRMWARE_BOOTUP 0x0119 |
highroads | 0:4a5d4b4a40c4 | 97 | #define VL6180X_FIRMWARE_RESULT_SCALER 0x0120 |
highroads | 0:4a5d4b4a40c4 | 98 | #define VL6180X_I2C_SLAVE_DEVICE_ADDRESS 0x0212 |
highroads | 0:4a5d4b4a40c4 | 99 | #define VL6180X_INTERLEAVED_MODE_ENABLE 0x02A3 |
highroads | 0:4a5d4b4a40c4 | 100 | |
highroads | 0:4a5d4b4a40c4 | 101 | |
highroads | 0:4a5d4b4a40c4 | 102 | enum vl6180x_als_gain { //Data sheet shows gain values as binary list |
highroads | 0:4a5d4b4a40c4 | 103 | |
highroads | 0:4a5d4b4a40c4 | 104 | GAIN_20 = 0, // Actual ALS Gain of 20 |
highroads | 0:4a5d4b4a40c4 | 105 | GAIN_10, // Actual ALS Gain of 10.32 |
highroads | 0:4a5d4b4a40c4 | 106 | GAIN_5, // Actual ALS Gain of 5.21 |
highroads | 0:4a5d4b4a40c4 | 107 | GAIN_2_5, // Actual ALS Gain of 2.60 |
highroads | 0:4a5d4b4a40c4 | 108 | GAIN_1_67, // Actual ALS Gain of 1.72 |
highroads | 0:4a5d4b4a40c4 | 109 | GAIN_1_25, // Actual ALS Gain of 1.28 |
highroads | 0:4a5d4b4a40c4 | 110 | GAIN_1 , // Actual ALS Gain of 1.01 |
highroads | 0:4a5d4b4a40c4 | 111 | GAIN_40, // Actual ALS Gain of 40 |
highroads | 0:4a5d4b4a40c4 | 112 | |
highroads | 0:4a5d4b4a40c4 | 113 | }; |
highroads | 0:4a5d4b4a40c4 | 114 | |
highroads | 0:4a5d4b4a40c4 | 115 | struct VL6180xIdentification |
highroads | 0:4a5d4b4a40c4 | 116 | { |
highroads | 0:4a5d4b4a40c4 | 117 | uint8_t idModel; |
highroads | 0:4a5d4b4a40c4 | 118 | uint8_t idModelRevMajor; |
highroads | 0:4a5d4b4a40c4 | 119 | uint8_t idModelRevMinor; |
highroads | 0:4a5d4b4a40c4 | 120 | uint8_t idModuleRevMajor; |
highroads | 0:4a5d4b4a40c4 | 121 | uint8_t idModuleRevMinor; |
highroads | 0:4a5d4b4a40c4 | 122 | uint16_t idDate; |
highroads | 0:4a5d4b4a40c4 | 123 | uint16_t idTime; |
highroads | 0:4a5d4b4a40c4 | 124 | }; |
highroads | 0:4a5d4b4a40c4 | 125 | |
highroads | 0:4a5d4b4a40c4 | 126 | class VL6180x |
highroads | 0:4a5d4b4a40c4 | 127 | { |
highroads | 0:4a5d4b4a40c4 | 128 | public: |
highroads | 0:4a5d4b4a40c4 | 129 | //Initalize library with default address |
highroads | 0:4a5d4b4a40c4 | 130 | /** |
highroads | 0:4a5d4b4a40c4 | 131 | * VL6180x constructor |
highroads | 0:4a5d4b4a40c4 | 132 | * |
highroads | 0:4a5d4b4a40c4 | 133 | * @param sda SDA pin |
highroads | 0:4a5d4b4a40c4 | 134 | * @param sdl SCL pin |
highroads | 0:4a5d4b4a40c4 | 135 | * @param addr addr of the I2C peripheral |
highroads | 0:4a5d4b4a40c4 | 136 | */ |
highroads | 0:4a5d4b4a40c4 | 137 | VL6180x(PinName sda, PinName scl, uint8_t addr); |
highroads | 0:4a5d4b4a40c4 | 138 | /** |
highroads | 0:4a5d4b4a40c4 | 139 | * VL6180x destructor |
highroads | 0:4a5d4b4a40c4 | 140 | */ |
highroads | 0:4a5d4b4a40c4 | 141 | ~VL6180x(); |
highroads | 0:4a5d4b4a40c4 | 142 | //Send mandatory settings as stated in ST datasheet. |
highroads | 0:4a5d4b4a40c4 | 143 | // http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf (Section 1.3) |
highroads | 0:4a5d4b4a40c4 | 144 | uint8_t VL6180xInit(void); |
highroads | 0:4a5d4b4a40c4 | 145 | // Use default settings from ST data sheet section 9. |
highroads | 0:4a5d4b4a40c4 | 146 | // http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf |
highroads | 0:4a5d4b4a40c4 | 147 | void VL6180xDefautSettings(void); |
highroads | 0:4a5d4b4a40c4 | 148 | |
highroads | 0:4a5d4b4a40c4 | 149 | // Get Range distance in (mm) |
highroads | 0:4a5d4b4a40c4 | 150 | uint8_t getDistance(); |
highroads | 0:4a5d4b4a40c4 | 151 | // Get ALS level in Lux |
highroads | 0:4a5d4b4a40c4 | 152 | float getAmbientLight(vl6180x_als_gain VL6180X_ALS_GAIN); |
highroads | 0:4a5d4b4a40c4 | 153 | |
highroads | 0:4a5d4b4a40c4 | 154 | //Load structure provided by the user with identification info |
highroads | 0:4a5d4b4a40c4 | 155 | //Structure example: |
highroads | 0:4a5d4b4a40c4 | 156 | // struct VL6180xIdentification |
highroads | 0:4a5d4b4a40c4 | 157 | // { |
highroads | 0:4a5d4b4a40c4 | 158 | // uint8_t idModel; |
highroads | 0:4a5d4b4a40c4 | 159 | // uint8_t idModelRevMajor; |
highroads | 0:4a5d4b4a40c4 | 160 | // uint8_t idModelRevMinor; |
highroads | 0:4a5d4b4a40c4 | 161 | // uint8_t idModuleRevMajor; |
highroads | 0:4a5d4b4a40c4 | 162 | // uint8_t idModuleRevMinor; |
highroads | 0:4a5d4b4a40c4 | 163 | // uint16_t idDate; |
highroads | 0:4a5d4b4a40c4 | 164 | // uint16_t idTime; |
highroads | 0:4a5d4b4a40c4 | 165 | // }; |
highroads | 0:4a5d4b4a40c4 | 166 | void getIdentification(struct VL6180xIdentification *temp); |
highroads | 0:4a5d4b4a40c4 | 167 | |
highroads | 0:4a5d4b4a40c4 | 168 | //Change the default address of the device to allow multiple |
highroads | 0:4a5d4b4a40c4 | 169 | //sensors on the bus. Can use up to 127 sensors. New address |
highroads | 0:4a5d4b4a40c4 | 170 | //is saved in non-volatile device memory. |
highroads | 0:4a5d4b4a40c4 | 171 | uint8_t changeAddress(uint8_t old_address, uint8_t new_address); |
highroads | 0:4a5d4b4a40c4 | 172 | |
highroads | 0:4a5d4b4a40c4 | 173 | |
highroads | 0:4a5d4b4a40c4 | 174 | private: |
highroads | 0:4a5d4b4a40c4 | 175 | //Store address given when the class is initialized. |
highroads | 0:4a5d4b4a40c4 | 176 | //This value can be changed by the changeAddress() function |
highroads | 0:4a5d4b4a40c4 | 177 | I2C m_i2c; |
highroads | 0:4a5d4b4a40c4 | 178 | int m_addr; |
highroads | 0:4a5d4b4a40c4 | 179 | uint8_t VL6180x_getRegister(uint16_t registerAddr); |
highroads | 0:4a5d4b4a40c4 | 180 | uint16_t VL6180x_getRegister16bit(uint16_t registerAddr); |
highroads | 0:4a5d4b4a40c4 | 181 | |
highroads | 0:4a5d4b4a40c4 | 182 | void VL6180x_setRegister(uint16_t registerAddr, uint8_t data); |
highroads | 0:4a5d4b4a40c4 | 183 | void VL6180x_setRegister16bit(uint16_t registerAddr, uint16_t data); |
highroads | 0:4a5d4b4a40c4 | 184 | |
highroads | 0:4a5d4b4a40c4 | 185 | |
highroads | 0:4a5d4b4a40c4 | 186 | }; |
highroads | 0:4a5d4b4a40c4 | 187 | |
highroads | 0:4a5d4b4a40c4 | 188 | #endif |