sdsd
VL6180.cpp@4:aab6582c7456, 2020-04-03 (annotated)
- Committer:
- maazshaikh
- Date:
- Fri Apr 03 15:38:03 2020 +0000
- Revision:
- 4:aab6582c7456
- Parent:
- 3:5d61f202b1bd
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.cpp |
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 functions 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 | * |
maazshaikh | 4:aab6582c7456 | 23 | * This code is beerware. If you see me (or any other SparkFun employee) at the |
maazshaikh | 4:aab6582c7456 | 24 | * local pub, and you've found our code helpful, please buy us a round! |
maazshaikh | 4:aab6582c7456 | 25 | * |
maazshaikh | 4:aab6582c7456 | 26 | * Distributed as-is; no warranty is given. |
maazshaikh | 4:aab6582c7456 | 27 | ******************************************************************************/ |
sburg | 0:f23c00f688b2 | 28 | |
maazshaikh | 4:aab6582c7456 | 29 | #include "VL6180.h" |
maazshaikh | 4:aab6582c7456 | 30 | |
maazshaikh | 4:aab6582c7456 | 31 | // |
maazshaikh | 4:aab6582c7456 | 32 | // Constructors |
maazshaikh | 4:aab6582c7456 | 33 | // |
maazshaikh | 4:aab6582c7456 | 34 | VL6180::VL6180(PinName sda, PinName scl) : i2c(sda, scl) |
maazshaikh | 4:aab6582c7456 | 35 | { |
maazshaikh | 4:aab6582c7456 | 36 | VL6180_i2cAddress = VL6180_DEF_ADDR; |
maazshaikh | 4:aab6582c7456 | 37 | } |
maazshaikh | 4:aab6582c7456 | 38 | |
maazshaikh | 4:aab6582c7456 | 39 | VL6180::VL6180(PinName sda, PinName scl, int i2cAddress) : i2c(sda, scl) |
maazshaikh | 4:aab6582c7456 | 40 | { |
maazshaikh | 4:aab6582c7456 | 41 | VL6180_i2cAddress = i2cAddress; |
maazshaikh | 4:aab6582c7456 | 42 | VL6180_error_no = 0; |
maazshaikh | 4:aab6582c7456 | 43 | } |
maazshaikh | 4:aab6582c7456 | 44 | // |
maazshaikh | 4:aab6582c7456 | 45 | // destructor |
maazshaikh | 4:aab6582c7456 | 46 | // |
maazshaikh | 4:aab6582c7456 | 47 | VL6180::~VL6180() |
maazshaikh | 4:aab6582c7456 | 48 | { |
sburg | 0:f23c00f688b2 | 49 | } |
sburg | 0:f23c00f688b2 | 50 | |
maazshaikh | 4:aab6582c7456 | 51 | int8_t VL6180::VL6180_Init(void) |
maazshaikh | 4:aab6582c7456 | 52 | { |
maazshaikh | 4:aab6582c7456 | 53 | uint8_t data; //for temp data storage |
maazshaikh | 4:aab6582c7456 | 54 | |
maazshaikh | 4:aab6582c7456 | 55 | data = VL6180_getRegister(VL6180_SYSTEM_FRESH_OUT_OF_RESET); |
maazshaikh | 4:aab6582c7456 | 56 | |
maazshaikh | 4:aab6582c7456 | 57 | if(data != 1) return VL6180_FAILURE_RESET; |
maazshaikh | 4:aab6582c7456 | 58 | |
maazshaikh | 4:aab6582c7456 | 59 | //Required by datasheet |
maazshaikh | 4:aab6582c7456 | 60 | //http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf |
maazshaikh | 4:aab6582c7456 | 61 | VL6180_setRegister(0x0207, 0x01); |
maazshaikh | 4:aab6582c7456 | 62 | VL6180_setRegister(0x0208, 0x01); |
maazshaikh | 4:aab6582c7456 | 63 | VL6180_setRegister(0x0096, 0x00); |
maazshaikh | 4:aab6582c7456 | 64 | VL6180_setRegister(0x0097, 0xfd); |
maazshaikh | 4:aab6582c7456 | 65 | VL6180_setRegister(0x00e3, 0x00); |
maazshaikh | 4:aab6582c7456 | 66 | VL6180_setRegister(0x00e4, 0x04); |
maazshaikh | 4:aab6582c7456 | 67 | VL6180_setRegister(0x00e5, 0x02); |
maazshaikh | 4:aab6582c7456 | 68 | VL6180_setRegister(0x00e6, 0x01); |
maazshaikh | 4:aab6582c7456 | 69 | VL6180_setRegister(0x00e7, 0x03); |
maazshaikh | 4:aab6582c7456 | 70 | VL6180_setRegister(0x00f5, 0x02); |
maazshaikh | 4:aab6582c7456 | 71 | VL6180_setRegister(0x00d9, 0x05); |
maazshaikh | 4:aab6582c7456 | 72 | VL6180_setRegister(0x00db, 0xce); |
maazshaikh | 4:aab6582c7456 | 73 | VL6180_setRegister(0x00dc, 0x03); |
maazshaikh | 4:aab6582c7456 | 74 | VL6180_setRegister(0x00dd, 0xf8); |
maazshaikh | 4:aab6582c7456 | 75 | VL6180_setRegister(0x009f, 0x00); |
maazshaikh | 4:aab6582c7456 | 76 | VL6180_setRegister(0x00a3, 0x3c); |
maazshaikh | 4:aab6582c7456 | 77 | VL6180_setRegister(0x00b7, 0x00); |
maazshaikh | 4:aab6582c7456 | 78 | VL6180_setRegister(0x00bb, 0x3c); |
maazshaikh | 4:aab6582c7456 | 79 | VL6180_setRegister(0x00b2, 0x09); |
maazshaikh | 4:aab6582c7456 | 80 | VL6180_setRegister(0x00ca, 0x09); |
maazshaikh | 4:aab6582c7456 | 81 | VL6180_setRegister(0x0198, 0x01); |
maazshaikh | 4:aab6582c7456 | 82 | VL6180_setRegister(0x01b0, 0x17); |
maazshaikh | 4:aab6582c7456 | 83 | VL6180_setRegister(0x01ad, 0x00); |
maazshaikh | 4:aab6582c7456 | 84 | VL6180_setRegister(0x00ff, 0x05); |
maazshaikh | 4:aab6582c7456 | 85 | VL6180_setRegister(0x0100, 0x05); |
maazshaikh | 4:aab6582c7456 | 86 | VL6180_setRegister(0x0199, 0x05); |
maazshaikh | 4:aab6582c7456 | 87 | VL6180_setRegister(0x01a6, 0x1b); |
maazshaikh | 4:aab6582c7456 | 88 | VL6180_setRegister(0x01ac, 0x3e); |
maazshaikh | 4:aab6582c7456 | 89 | VL6180_setRegister(0x01a7, 0x1f); |
maazshaikh | 4:aab6582c7456 | 90 | VL6180_setRegister(0x0030, 0x00); |
maazshaikh | 4:aab6582c7456 | 91 | |
maazshaikh | 4:aab6582c7456 | 92 | return 0; |
sburg | 0:f23c00f688b2 | 93 | } |
sburg | 0:f23c00f688b2 | 94 | |
maazshaikh | 4:aab6582c7456 | 95 | void VL6180::VL6180_DefautSettings(void) |
maazshaikh | 4:aab6582c7456 | 96 | { |
maazshaikh | 4:aab6582c7456 | 97 | //Recommended settings from datasheet |
maazshaikh | 4:aab6582c7456 | 98 | //http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf |
maazshaikh | 4:aab6582c7456 | 99 | |
maazshaikh | 4:aab6582c7456 | 100 | //Enable Interrupts on Conversion Complete (any source) |
maazshaikh | 4:aab6582c7456 | 101 | VL6180_setRegister(VL6180_SYSTEM_INTERRUPT_CONFIG_GPIO, (4 << 3)|(4) ); // Set GPIO1 high when sample complete |
maazshaikh | 4:aab6582c7456 | 102 | |
maazshaikh | 4:aab6582c7456 | 103 | |
maazshaikh | 4:aab6582c7456 | 104 | VL6180_setRegister(VL6180_SYSTEM_MODE_GPIO1, 0x10); // Set GPIO1 high when sample complete |
maazshaikh | 4:aab6582c7456 | 105 | VL6180_setRegister(VL6180_READOUT_AVERAGING_SAMPLE_PERIOD, 0x30); //Set Avg sample period |
maazshaikh | 4:aab6582c7456 | 106 | VL6180_setRegister(VL6180_SYSALS_ANALOGUE_GAIN, 0x46); // Set the ALS gain |
maazshaikh | 4:aab6582c7456 | 107 | VL6180_setRegister(VL6180_SYSRANGE_VHV_REPEAT_RATE, 0xFF); // Set auto calibration period (Max = 255)/(OFF = 0) |
maazshaikh | 4:aab6582c7456 | 108 | VL6180_setRegister(VL6180_SYSALS_INTEGRATION_PERIOD, 0x63); // Set ALS integration time to 100ms |
maazshaikh | 4:aab6582c7456 | 109 | VL6180_setRegister(VL6180_SYSRANGE_VHV_RECALIBRATE, 0x01); // perform a single temperature calibration |
maazshaikh | 4:aab6582c7456 | 110 | //Optional settings from datasheet |
maazshaikh | 4:aab6582c7456 | 111 | //http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf |
maazshaikh | 4:aab6582c7456 | 112 | VL6180_setRegister(VL6180_SYSRANGE_INTERMEASUREMENT_PERIOD, 0x09); // Set default ranging inter-measurement period to 100ms |
maazshaikh | 4:aab6582c7456 | 113 | VL6180_setRegister(VL6180_SYSALS_INTERMEASUREMENT_PERIOD, 0x0A); // Set default ALS inter-measurement period to 100ms |
maazshaikh | 4:aab6582c7456 | 114 | VL6180_setRegister(VL6180_SYSTEM_INTERRUPT_CONFIG_GPIO, 0x24); // Configures interrupt on ‘New Sample Ready threshold event’ |
maazshaikh | 4:aab6582c7456 | 115 | //Additional settings defaults from community |
maazshaikh | 4:aab6582c7456 | 116 | VL6180_setRegister(VL6180_SYSRANGE_MAX_CONVERGENCE_TIME, 0x32); |
maazshaikh | 4:aab6582c7456 | 117 | VL6180_setRegister(VL6180_SYSRANGE_RANGE_CHECK_ENABLES, 0x10 | 0x01); |
maazshaikh | 4:aab6582c7456 | 118 | VL6180_setRegister16bit(VL6180_SYSRANGE_EARLY_CONVERGENCE_ESTIMATE, 0x7B ); |
maazshaikh | 4:aab6582c7456 | 119 | VL6180_setRegister16bit(VL6180_SYSALS_INTEGRATION_PERIOD, 0x64); |
maazshaikh | 4:aab6582c7456 | 120 | |
maazshaikh | 4:aab6582c7456 | 121 | VL6180_setRegister(VL6180_READOUT_AVERAGING_SAMPLE_PERIOD,0x30); |
maazshaikh | 4:aab6582c7456 | 122 | VL6180_setRegister(VL6180_SYSALS_ANALOGUE_GAIN,0x40); |
maazshaikh | 4:aab6582c7456 | 123 | VL6180_setRegister(VL6180_FIRMWARE_RESULT_SCALER,0x01); |
maazshaikh | 4:aab6582c7456 | 124 | } |
maazshaikh | 4:aab6582c7456 | 125 | void VL6180::getIdentification(struct VL6180Identification *temp) |
maazshaikh | 4:aab6582c7456 | 126 | { |
maazshaikh | 4:aab6582c7456 | 127 | |
maazshaikh | 4:aab6582c7456 | 128 | temp->idModel = VL6180_getRegister(VL6180_IDENTIFICATION_MODEL_ID); |
maazshaikh | 4:aab6582c7456 | 129 | temp->idModelRevMajor = VL6180_getRegister(VL6180_IDENTIFICATION_MODEL_REV_MAJOR); |
maazshaikh | 4:aab6582c7456 | 130 | temp->idModelRevMinor = VL6180_getRegister(VL6180_IDENTIFICATION_MODEL_REV_MINOR); |
maazshaikh | 4:aab6582c7456 | 131 | temp->idModuleRevMajor = VL6180_getRegister(VL6180_IDENTIFICATION_MODULE_REV_MAJOR); |
maazshaikh | 4:aab6582c7456 | 132 | temp->idModuleRevMinor = VL6180_getRegister(VL6180_IDENTIFICATION_MODULE_REV_MINOR); |
maazshaikh | 4:aab6582c7456 | 133 | |
maazshaikh | 4:aab6582c7456 | 134 | temp->idDate = VL6180_getRegister16bit(VL6180_IDENTIFICATION_DATE); |
maazshaikh | 4:aab6582c7456 | 135 | temp->idTime = VL6180_getRegister16bit(VL6180_IDENTIFICATION_TIME); |
sburg | 0:f23c00f688b2 | 136 | } |
sburg | 0:f23c00f688b2 | 137 | |
sburg | 0:f23c00f688b2 | 138 | |
maazshaikh | 4:aab6582c7456 | 139 | uint8_t VL6180::changeAddress(uint8_t old_address, uint8_t new_address) |
maazshaikh | 4:aab6582c7456 | 140 | { |
maazshaikh | 4:aab6582c7456 | 141 | |
maazshaikh | 4:aab6582c7456 | 142 | //NOTICE: IT APPEARS THAT CHANGING THE ADDRESS IS NOT STORED IN NON-VOLATILE MEMORY |
maazshaikh | 4:aab6582c7456 | 143 | // POWER CYCLING THE DEVICE REVERTS ADDRESS BACK TO 0X29 |
maazshaikh | 4:aab6582c7456 | 144 | |
maazshaikh | 4:aab6582c7456 | 145 | if( old_address == new_address) return old_address; |
maazshaikh | 4:aab6582c7456 | 146 | if( new_address > 127) return old_address; |
maazshaikh | 4:aab6582c7456 | 147 | |
maazshaikh | 4:aab6582c7456 | 148 | VL6180_setRegister(VL6180_I2C_SLAVE_DEVICE_ADDRESS, new_address); |
maazshaikh | 4:aab6582c7456 | 149 | |
maazshaikh | 4:aab6582c7456 | 150 | return VL6180_getRegister(VL6180_I2C_SLAVE_DEVICE_ADDRESS); |
maazshaikh | 4:aab6582c7456 | 151 | } |
maazshaikh | 4:aab6582c7456 | 152 | |
maazshaikh | 4:aab6582c7456 | 153 | |
maazshaikh | 4:aab6582c7456 | 154 | |
maazshaikh | 4:aab6582c7456 | 155 | uint8_t VL6180::getDistance() |
maazshaikh | 4:aab6582c7456 | 156 | { |
maazshaikh | 4:aab6582c7456 | 157 | VL6180_setRegister(VL6180_SYSRANGE_START, 0x01); //Start Single shot mode |
maazshaikh | 4:aab6582c7456 | 158 | wait(0.01); // 10mS |
maazshaikh | 4:aab6582c7456 | 159 | return VL6180_getRegister(VL6180_RESULT_RANGE_VAL); |
maazshaikh | 4:aab6582c7456 | 160 | // VL6180_setRegister(VL6180_SYSTEM_INTERRUPT_CLEAR, 0x07); |
maazshaikh | 4:aab6582c7456 | 161 | // return distance; |
sburg | 0:f23c00f688b2 | 162 | } |
sburg | 0:f23c00f688b2 | 163 | |
maazshaikh | 4:aab6582c7456 | 164 | float VL6180::getAmbientLight(VL6180_als_gain VL6180_ALS_GAIN) |
maazshaikh | 4:aab6582c7456 | 165 | { |
maazshaikh | 4:aab6582c7456 | 166 | //First load in Gain we are using, do it everytime incase someone changes it on us. |
maazshaikh | 4:aab6582c7456 | 167 | //Note: Upper nibble shoudl be set to 0x4 i.e. for ALS gain of 1.0 write 0x46 |
maazshaikh | 4:aab6582c7456 | 168 | VL6180_setRegister(VL6180_SYSALS_ANALOGUE_GAIN, (0x40 | VL6180_ALS_GAIN)); // Set the ALS gain |
maazshaikh | 4:aab6582c7456 | 169 | |
maazshaikh | 4:aab6582c7456 | 170 | //Start ALS Measurement |
maazshaikh | 4:aab6582c7456 | 171 | VL6180_setRegister(VL6180_SYSALS_START, 0x01); |
maazshaikh | 4:aab6582c7456 | 172 | |
maazshaikh | 4:aab6582c7456 | 173 | wait(0.1); //100Ms |
maazshaikh | 4:aab6582c7456 | 174 | |
maazshaikh | 4:aab6582c7456 | 175 | VL6180_setRegister(VL6180_SYSTEM_INTERRUPT_CLEAR, 0x07); |
maazshaikh | 4:aab6582c7456 | 176 | |
maazshaikh | 4:aab6582c7456 | 177 | //Retrieve the Raw ALS value from the sensoe |
maazshaikh | 4:aab6582c7456 | 178 | unsigned int alsRaw = VL6180_getRegister16bit(VL6180_RESULT_ALS_VAL); |
maazshaikh | 4:aab6582c7456 | 179 | |
maazshaikh | 4:aab6582c7456 | 180 | //Get Integration Period for calculation, we do this everytime incase someone changes it on us. |
maazshaikh | 4:aab6582c7456 | 181 | unsigned int alsIntegrationPeriodRaw = VL6180_getRegister16bit(VL6180_SYSALS_INTEGRATION_PERIOD); |
maazshaikh | 4:aab6582c7456 | 182 | |
maazshaikh | 4:aab6582c7456 | 183 | float alsIntegrationPeriod = 100.0 / alsIntegrationPeriodRaw ; |
maazshaikh | 4:aab6582c7456 | 184 | |
maazshaikh | 4:aab6582c7456 | 185 | //Calculate actual LUX from Appnotes |
maazshaikh | 4:aab6582c7456 | 186 | |
maazshaikh | 4:aab6582c7456 | 187 | float alsGain = 0.0; |
maazshaikh | 4:aab6582c7456 | 188 | |
maazshaikh | 4:aab6582c7456 | 189 | switch (VL6180_ALS_GAIN) { |
maazshaikh | 4:aab6582c7456 | 190 | case GAIN_20: |
maazshaikh | 4:aab6582c7456 | 191 | alsGain = 20.0; |
maazshaikh | 4:aab6582c7456 | 192 | break; |
maazshaikh | 4:aab6582c7456 | 193 | case GAIN_10: |
maazshaikh | 4:aab6582c7456 | 194 | alsGain = 10.32; |
maazshaikh | 4:aab6582c7456 | 195 | break; |
maazshaikh | 4:aab6582c7456 | 196 | case GAIN_5: |
maazshaikh | 4:aab6582c7456 | 197 | alsGain = 5.21; |
maazshaikh | 4:aab6582c7456 | 198 | break; |
maazshaikh | 4:aab6582c7456 | 199 | case GAIN_2_5: |
maazshaikh | 4:aab6582c7456 | 200 | alsGain = 2.60; |
maazshaikh | 4:aab6582c7456 | 201 | break; |
maazshaikh | 4:aab6582c7456 | 202 | case GAIN_1_67: |
maazshaikh | 4:aab6582c7456 | 203 | alsGain = 1.72; |
maazshaikh | 4:aab6582c7456 | 204 | break; |
maazshaikh | 4:aab6582c7456 | 205 | case GAIN_1_25: |
maazshaikh | 4:aab6582c7456 | 206 | alsGain = 1.28; |
maazshaikh | 4:aab6582c7456 | 207 | break; |
maazshaikh | 4:aab6582c7456 | 208 | case GAIN_1: |
maazshaikh | 4:aab6582c7456 | 209 | alsGain = 1.01; |
maazshaikh | 4:aab6582c7456 | 210 | break; |
maazshaikh | 4:aab6582c7456 | 211 | case GAIN_40: |
maazshaikh | 4:aab6582c7456 | 212 | alsGain = 40.0; |
maazshaikh | 4:aab6582c7456 | 213 | break; |
maazshaikh | 4:aab6582c7456 | 214 | } |
maazshaikh | 4:aab6582c7456 | 215 | |
maazshaikh | 4:aab6582c7456 | 216 | //Calculate LUX from formula in AppNotes |
maazshaikh | 4:aab6582c7456 | 217 | |
maazshaikh | 4:aab6582c7456 | 218 | float alsCalculated = (float)0.32 * ((float)alsRaw / alsGain) * alsIntegrationPeriod; |
maazshaikh | 4:aab6582c7456 | 219 | |
maazshaikh | 4:aab6582c7456 | 220 | return alsCalculated; |
maazshaikh | 4:aab6582c7456 | 221 | } |
maazshaikh | 4:aab6582c7456 | 222 | |
maazshaikh | 4:aab6582c7456 | 223 | // --- Private Functions --- // |
maazshaikh | 4:aab6582c7456 | 224 | |
maazshaikh | 4:aab6582c7456 | 225 | uint8_t VL6180::VL6180_getRegister(uint16_t reg_address) |
maazshaikh | 4:aab6582c7456 | 226 | { |
maazshaikh | 4:aab6582c7456 | 227 | char data[2]; |
maazshaikh | 4:aab6582c7456 | 228 | |
maazshaikh | 4:aab6582c7456 | 229 | data[0] = (reg_address >> 8) & 0xFF; //MSB of register address |
maazshaikh | 4:aab6582c7456 | 230 | data[1] = reg_address & 0xFF; //LSB of register address |
maazshaikh | 4:aab6582c7456 | 231 | VL6180_error_no = i2c.write(VL6180_i2cAddress, data, 2); |
maazshaikh | 4:aab6582c7456 | 232 | VL6180_error_no = i2c.read(VL6180_i2cAddress, data, 1, false); |
maazshaikh | 4:aab6582c7456 | 233 | return data[0]; |
maazshaikh | 4:aab6582c7456 | 234 | } |
maazshaikh | 4:aab6582c7456 | 235 | |
maazshaikh | 4:aab6582c7456 | 236 | uint16_t VL6180::VL6180_getRegister16bit(uint16_t reg_address) |
maazshaikh | 4:aab6582c7456 | 237 | { |
maazshaikh | 4:aab6582c7456 | 238 | char data[2]; |
maazshaikh | 4:aab6582c7456 | 239 | |
maazshaikh | 4:aab6582c7456 | 240 | data[0] = (reg_address >> 8) & 0xFF; //MSB of register address |
maazshaikh | 4:aab6582c7456 | 241 | data[1] = reg_address & 0xFF; //LSB of register address |
maazshaikh | 4:aab6582c7456 | 242 | VL6180_error_no = i2c.write(VL6180_i2cAddress, data, 2); |
maazshaikh | 4:aab6582c7456 | 243 | VL6180_error_no = i2c.read(VL6180_i2cAddress, data, 2, false); |
maazshaikh | 4:aab6582c7456 | 244 | return (data[0] + ((data[1] << 8) & 0xFF00)); |
maazshaikh | 4:aab6582c7456 | 245 | } |
maazshaikh | 4:aab6582c7456 | 246 | |
maazshaikh | 4:aab6582c7456 | 247 | void VL6180::VL6180_setRegister(uint16_t reg_address, uint8_t value) |
maazshaikh | 4:aab6582c7456 | 248 | { |
maazshaikh | 4:aab6582c7456 | 249 | char data[3]; |
maazshaikh | 4:aab6582c7456 | 250 | |
maazshaikh | 4:aab6582c7456 | 251 | data[0] = (reg_address >> 8) & 0xFF; //MSB of register address |
maazshaikh | 4:aab6582c7456 | 252 | data[1] = reg_address & 0xFF; //LSB of register address |
maazshaikh | 4:aab6582c7456 | 253 | data[2] = value; |
maazshaikh | 4:aab6582c7456 | 254 | VL6180_error_no = VL6180_error_no = i2c.write(VL6180_i2cAddress, data, 3); |
maazshaikh | 4:aab6582c7456 | 255 | } |
maazshaikh | 4:aab6582c7456 | 256 | |
maazshaikh | 4:aab6582c7456 | 257 | void VL6180::VL6180_setRegister16bit(uint16_t reg_address, uint16_t value) |
maazshaikh | 4:aab6582c7456 | 258 | { |
maazshaikh | 4:aab6582c7456 | 259 | char data[4]; |
maazshaikh | 4:aab6582c7456 | 260 | |
maazshaikh | 4:aab6582c7456 | 261 | data[0] = (reg_address >> 8) & 0xFF; //MSB of register address |
maazshaikh | 4:aab6582c7456 | 262 | data[1] = reg_address & 0xFF; //LSB of register address |
maazshaikh | 4:aab6582c7456 | 263 | data[2] = value & 0xFF; |
maazshaikh | 4:aab6582c7456 | 264 | data[3] = ((value >> 8) & 0xFF); |
maazshaikh | 4:aab6582c7456 | 265 | VL6180_error_no = VL6180_error_no = i2c.write(VL6180_i2cAddress, data, 4); |
maazshaikh | 4:aab6582c7456 | 266 | } |
maazshaikh | 4:aab6582c7456 | 267 | |
maazshaikh | 4:aab6582c7456 | 268 | int VL6180::writeSingleRegister( uint16_t reg_address, uint8_t data ) |
maazshaikh | 4:aab6582c7456 | 269 | { |
maazshaikh | 4:aab6582c7456 | 270 | |
maazshaikh | 4:aab6582c7456 | 271 | char data_write[3]; |
maazshaikh | 4:aab6582c7456 | 272 | data_write[0] = (reg_address >> 8) & 0xFF; //MSB of register address |
maazshaikh | 4:aab6582c7456 | 273 | data_write[1] = reg_address & 0xFF; //LSB of register address |
maazshaikh | 4:aab6582c7456 | 274 | data_write[2] = data & 0xFF; |
maazshaikh | 4:aab6582c7456 | 275 | return i2c.write(VL6180_DEF_ADDR, data_write, 3); |
maazshaikh | 4:aab6582c7456 | 276 | |
maazshaikh | 4:aab6582c7456 | 277 | // char tx[2] = { address | 160, data }; //0d160 = 0b10100000 |
maazshaikh | 4:aab6582c7456 | 278 | // int ack = i2c.write( SLAVE_ADDRESS << 1, tx, 2 ); |
maazshaikh | 4:aab6582c7456 | 279 | // return ack; |
sburg | 0:f23c00f688b2 | 280 | } |
sburg | 0:f23c00f688b2 | 281 |