Ported from sparkfun library

Dependents:   tc_agent Line_Trace_No_collision_Light_control_longer_dis

Committer:
maruta
Date:
Tue Jul 19 03:49:54 2016 +0000
Revision:
0:8aca38272614
Ported to mbed;

Who changed what in which revision?

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