Skip to content

Commit 402ff06

Browse files
committed
VSC formatting
1 parent d71093b commit 402ff06

2 files changed

Lines changed: 222 additions & 211 deletions

File tree

src/SparkFun_VL6180X.cpp

Lines changed: 119 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,25 @@
44
* Casey Kuhns @ SparkFun Electronics
55
* 10/29/2014
66
* https://github.com/sparkfun/SparkFun_ToF_Range_Finder-VL6180_Arduino_Library
7-
*
7+
*
88
* The VL6180x by ST micro is a time of flight range finder that
99
* uses pulsed IR light to determine distances from object at close
1010
* range. The average range of a sensor is between 0-200mm
11-
*
11+
*
1212
* In this file are the functions in the VL6180x class
13-
*
13+
*
1414
* Resources:
1515
* This library uses the Arduino Wire.h to complete I2C transactions.
16-
*
16+
*
1717
* Development environment specifics:
1818
* IDE: Arduino 1.0.5
1919
* Hardware Platform: Arduino Pro 3.3V/8MHz
2020
* VL6180x Breakout Version: 1.0
2121
* **Updated for Arduino 1.6.4 5/2015**
22-
*
22+
*
2323
* This code is beerware. If you see me (or any other SparkFun employee) at the
2424
* local pub, and you've found our code helpful, please buy us a round!
25-
*
25+
*
2626
* Distributed as-is; no warranty is given.
2727
******************************************************************************/
2828

@@ -32,20 +32,21 @@
3232
VL6180x::VL6180x(uint8_t address)
3333
// Initialize the Library
3434
{
35-
Wire.begin(); // Arduino Wire library initializer
36-
_i2caddress = address; //set default address for communication
35+
Wire.begin(); // Arduino Wire library initializer
36+
_i2caddress = address; // set default address for communication
3737
}
3838

39-
40-
uint8_t VL6180x::VL6180xInit(void){
41-
uint8_t data; //for temp data storage
39+
uint8_t VL6180x::VL6180xInit(void)
40+
{
41+
uint8_t data; // for temp data storage
4242

4343
data = VL6180x_getRegister(VL6180X_SYSTEM_FRESH_OUT_OF_RESET);
4444

45-
if(data != 1) return VL6180x_FAILURE_RESET;
45+
if (data != 1)
46+
return VL6180x_FAILURE_RESET;
4647

47-
//Required by datasheet
48-
//http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf
48+
// Required by datasheet
49+
// http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf
4950
VL6180x_setRegister(0x0207, 0x01);
5051
VL6180x_setRegister(0x0208, 0x01);
5152
VL6180x_setRegister(0x0096, 0x00);
@@ -65,7 +66,7 @@ uint8_t VL6180x::VL6180xInit(void){
6566
VL6180x_setRegister(0x00b7, 0x00);
6667
VL6180x_setRegister(0x00bb, 0x3c);
6768
VL6180x_setRegister(0x00b2, 0x09);
68-
VL6180x_setRegister(0x00ca, 0x09);
69+
VL6180x_setRegister(0x00ca, 0x09);
6970
VL6180x_setRegister(0x0198, 0x01);
7071
VL6180x_setRegister(0x01b0, 0x17);
7172
VL6180x_setRegister(0x01ad, 0x00);
@@ -80,38 +81,39 @@ uint8_t VL6180x::VL6180xInit(void){
8081
return 0;
8182
}
8283

83-
void VL6180x::VL6180xDefautSettings(void){
84-
//Recommended settings from datasheet
85-
//http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf
86-
87-
//Enable Interrupts on Conversion Complete (any source)
88-
VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CONFIG_GPIO, (4 << 3)|(4) ); // Set GPIO1 high when sample complete
89-
90-
91-
VL6180x_setRegister(VL6180X_SYSTEM_MODE_GPIO1, 0x10); // Set GPIO1 high when sample complete
92-
VL6180x_setRegister(VL6180X_READOUT_AVERAGING_SAMPLE_PERIOD, 0x30); //Set Avg sample period
93-
VL6180x_setRegister(VL6180X_SYSALS_ANALOGUE_GAIN, 0x46); // Set the ALS gain
94-
VL6180x_setRegister(VL6180X_SYSRANGE_VHV_REPEAT_RATE, 0xFF); // Set auto calibration period (Max = 255)/(OFF = 0)
95-
VL6180x_setRegister(VL6180X_SYSALS_INTEGRATION_PERIOD, 0x63); // Set ALS integration time to 100ms
96-
VL6180x_setRegister(VL6180X_SYSRANGE_VHV_RECALIBRATE, 0x01); // perform a single temperature calibration
97-
//Optional settings from datasheet
98-
//http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf
84+
void VL6180x::VL6180xDefautSettings(void)
85+
{
86+
// Recommended settings from datasheet
87+
// http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf
88+
89+
// Enable Interrupts on Conversion Complete (any source)
90+
VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CONFIG_GPIO, (4 << 3) | (4)); // Set GPIO1 high when sample complete
91+
92+
VL6180x_setRegister(VL6180X_SYSTEM_MODE_GPIO1, 0x10); // Set GPIO1 high when sample complete
93+
VL6180x_setRegister(VL6180X_READOUT_AVERAGING_SAMPLE_PERIOD, 0x30); // Set Avg sample period
94+
VL6180x_setRegister(VL6180X_SYSALS_ANALOGUE_GAIN, 0x46); // Set the ALS gain
95+
VL6180x_setRegister(VL6180X_SYSRANGE_VHV_REPEAT_RATE, 0xFF); // Set auto calibration period (Max = 255)/(OFF = 0)
96+
VL6180x_setRegister(VL6180X_SYSALS_INTEGRATION_PERIOD, 0x63); // Set ALS integration time to 100ms
97+
VL6180x_setRegister(VL6180X_SYSRANGE_VHV_RECALIBRATE, 0x01); // perform a single temperature calibration
98+
// Optional settings from datasheet
99+
// http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf
99100
VL6180x_setRegister(VL6180X_SYSRANGE_INTERMEASUREMENT_PERIOD, 0x09); // Set default ranging inter-measurement period to 100ms
100-
VL6180x_setRegister(VL6180X_SYSALS_INTERMEASUREMENT_PERIOD, 0x0A); // Set default ALS inter-measurement period to 100ms
101-
VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CONFIG_GPIO, 0x24); // Configures interrupt on ‘New Sample Ready threshold event’
102-
//Additional settings defaults from community
101+
VL6180x_setRegister(VL6180X_SYSALS_INTERMEASUREMENT_PERIOD, 0x0A); // Set default ALS inter-measurement period to 100ms
102+
VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CONFIG_GPIO, 0x24); // Configures interrupt on ‘New Sample Ready threshold event’
103+
// Additional settings defaults from community
103104
VL6180x_setRegister(VL6180X_SYSRANGE_MAX_CONVERGENCE_TIME, 0x32);
104105
VL6180x_setRegister(VL6180X_SYSRANGE_RANGE_CHECK_ENABLES, 0x10 | 0x01);
105-
VL6180x_setRegister16bit(VL6180X_SYSRANGE_EARLY_CONVERGENCE_ESTIMATE, 0x7B );
106+
VL6180x_setRegister16bit(VL6180X_SYSRANGE_EARLY_CONVERGENCE_ESTIMATE, 0x7B);
106107
VL6180x_setRegister16bit(VL6180X_SYSALS_INTEGRATION_PERIOD, 0x64);
107108

108-
VL6180x_setRegister(VL6180X_READOUT_AVERAGING_SAMPLE_PERIOD,0x30);
109-
VL6180x_setRegister(VL6180X_SYSALS_ANALOGUE_GAIN,0x40);
110-
VL6180x_setRegister(VL6180X_FIRMWARE_RESULT_SCALER,0x01);
109+
VL6180x_setRegister(VL6180X_READOUT_AVERAGING_SAMPLE_PERIOD, 0x30);
110+
VL6180x_setRegister(VL6180X_SYSALS_ANALOGUE_GAIN, 0x40);
111+
VL6180x_setRegister(VL6180X_FIRMWARE_RESULT_SCALER, 0x01);
111112
}
112-
void VL6180x::getIdentification(struct VL6180xIdentification *temp){
113+
void VL6180x::getIdentification(struct VL6180xIdentification *temp)
114+
{
113115

114-
temp->idModel = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODEL_ID);
116+
temp->idModel = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODEL_ID);
115117
temp->idModelRevMajor = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODEL_REV_MAJOR);
116118
temp->idModelRevMinor = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODEL_REV_MINOR);
117119
temp->idModuleRevMajor = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODULE_REV_MAJOR);
@@ -121,25 +123,25 @@ void VL6180x::getIdentification(struct VL6180xIdentification *temp){
121123
temp->idTime = VL6180x_getRegister16bit(VL6180X_IDENTIFICATION_TIME);
122124
}
123125

126+
uint8_t VL6180x::changeAddress(uint8_t old_address, uint8_t new_address)
127+
{
124128

125-
uint8_t VL6180x::changeAddress(uint8_t old_address, uint8_t new_address){
126-
127-
//NOTICE: IT APPEARS THAT CHANGING THE ADDRESS IS NOT STORED IN NON-VOLATILE MEMORY
128-
// POWER CYCLING THE DEVICE REVERTS ADDRESS BACK TO 0X29
129-
130-
if( old_address == new_address) return old_address;
131-
if( new_address > 127) return old_address;
132-
133-
VL6180x_setRegister(VL6180X_I2C_SLAVE_DEVICE_ADDRESS, new_address);
134-
135-
return VL6180x_getRegister(VL6180X_I2C_SLAVE_DEVICE_ADDRESS);
136-
}
137-
129+
// NOTICE: IT APPEARS THAT CHANGING THE ADDRESS IS NOT STORED IN NON-VOLATILE MEMORY
130+
// POWER CYCLING THE DEVICE REVERTS ADDRESS BACK TO 0X29
131+
132+
if (old_address == new_address)
133+
return old_address;
134+
if (new_address > 127)
135+
return old_address;
138136

137+
VL6180x_setRegister(VL6180X_I2C_SLAVE_DEVICE_ADDRESS, new_address);
138+
139+
return VL6180x_getRegister(VL6180X_I2C_SLAVE_DEVICE_ADDRESS);
140+
}
139141

140142
uint8_t VL6180x::getDistance()
141143
{
142-
VL6180x_setRegister(VL6180X_SYSRANGE_START, 0x01); //Start Single shot mode
144+
VL6180x_setRegister(VL6180X_SYSRANGE_START, 0x01); // Start Single shot mode
143145
delay(10);
144146
VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CLEAR, 0x07);
145147
return VL6180x_getRegister(VL6180X_RESULT_RANGE_VAL);
@@ -148,42 +150,59 @@ uint8_t VL6180x::getDistance()
148150

149151
float VL6180x::getAmbientLight(vl6180x_als_gain VL6180X_ALS_GAIN)
150152
{
151-
//First load in Gain we are using, do it everytime incase someone changes it on us.
152-
//Note: Upper nibble shoudl be set to 0x4 i.e. for ALS gain of 1.0 write 0x46
153+
// First load in Gain we are using, do it everytime incase someone changes it on us.
154+
// Note: Upper nibble shoudl be set to 0x4 i.e. for ALS gain of 1.0 write 0x46
153155
VL6180x_setRegister(VL6180X_SYSALS_ANALOGUE_GAIN, (0x40 | VL6180X_ALS_GAIN)); // Set the ALS gain
154156

155-
//Start ALS Measurement
157+
// Start ALS Measurement
156158
VL6180x_setRegister(VL6180X_SYSALS_START, 0x01);
157159

158-
delay(100); //give it time...
160+
delay(100); // give it time...
159161

160162
VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CLEAR, 0x07);
161163

162-
//Retrieve the Raw ALS value from the sensoe
164+
// Retrieve the Raw ALS value from the sensoe
163165
unsigned int alsRaw = VL6180x_getRegister16bit(VL6180X_RESULT_ALS_VAL);
164-
165-
//Get Integration Period for calculation, we do this everytime incase someone changes it on us.
166+
167+
// Get Integration Period for calculation, we do this everytime incase someone changes it on us.
166168
unsigned int alsIntegrationPeriodRaw = VL6180x_getRegister16bit(VL6180X_SYSALS_INTEGRATION_PERIOD);
167-
168-
float alsIntegrationPeriod = 100.0 / alsIntegrationPeriodRaw ;
169169

170-
//Calculate actual LUX from Appnotes
170+
float alsIntegrationPeriod = 100.0 / alsIntegrationPeriodRaw;
171+
172+
// Calculate actual LUX from Appnotes
171173

172174
float alsGain = 0.0;
173-
174-
switch (VL6180X_ALS_GAIN){
175-
case GAIN_20: alsGain = 20.0; break;
176-
case GAIN_10: alsGain = 10.32; break;
177-
case GAIN_5: alsGain = 5.21; break;
178-
case GAIN_2_5: alsGain = 2.60; break;
179-
case GAIN_1_67: alsGain = 1.72; break;
180-
case GAIN_1_25: alsGain = 1.28; break;
181-
case GAIN_1: alsGain = 1.01; break;
182-
case GAIN_40: alsGain = 40.0; break;
175+
176+
switch (VL6180X_ALS_GAIN)
177+
{
178+
case GAIN_20:
179+
alsGain = 20.0;
180+
break;
181+
case GAIN_10:
182+
alsGain = 10.32;
183+
break;
184+
case GAIN_5:
185+
alsGain = 5.21;
186+
break;
187+
case GAIN_2_5:
188+
alsGain = 2.60;
189+
break;
190+
case GAIN_1_67:
191+
alsGain = 1.72;
192+
break;
193+
case GAIN_1_25:
194+
alsGain = 1.28;
195+
break;
196+
case GAIN_1:
197+
alsGain = 1.01;
198+
break;
199+
case GAIN_40:
200+
alsGain = 40.0;
201+
break;
183202
}
184203

185-
//Calculate LUX from formula in AppNotes
186-
204+
// Calculate LUX from formula in AppNotes
205+
187206
float alsCalculated = (float)0.32 * ((float)alsRaw / alsGain) * alsIntegrationPeriod;
188207

189208
return alsCalculated;
@@ -195,12 +214,12 @@ uint8_t VL6180x::VL6180x_getRegister(uint16_t registerAddr)
195214
{
196215
uint8_t data;
197216

198-
Wire.beginTransmission( _i2caddress ); // Address set on class instantiation
199-
Wire.write((registerAddr >> 8) & 0xFF); //MSB of register address
200-
Wire.write(registerAddr & 0xFF); //LSB of register address
201-
Wire.endTransmission(false); //Send address and register address bytes
202-
Wire.requestFrom( _i2caddress , 1);
203-
data = Wire.read(); //Read Data from selected register
217+
Wire.beginTransmission(_i2caddress); // Address set on class instantiation
218+
Wire.write((registerAddr >> 8) & 0xFF); // MSB of register address
219+
Wire.write(registerAddr & 0xFF); // LSB of register address
220+
Wire.endTransmission(false); // Send address and register address bytes
221+
Wire.requestFrom(_i2caddress, 1);
222+
data = Wire.read(); // Read Data from selected register
204223

205224
return data;
206225
}
@@ -211,40 +230,37 @@ uint16_t VL6180x::VL6180x_getRegister16bit(uint16_t registerAddr)
211230
uint8_t data_high;
212231
uint16_t data;
213232

214-
Wire.beginTransmission( _i2caddress ); // Address set on class instantiation
215-
Wire.write((registerAddr >> 8) & 0xFF); //MSB of register address
216-
Wire.write(registerAddr & 0xFF); //LSB of register address
217-
Wire.endTransmission(false); //Send address and register address bytes
233+
Wire.beginTransmission(_i2caddress); // Address set on class instantiation
234+
Wire.write((registerAddr >> 8) & 0xFF); // MSB of register address
235+
Wire.write(registerAddr & 0xFF); // LSB of register address
236+
Wire.endTransmission(false); // Send address and register address bytes
218237

219-
Wire.requestFrom( _i2caddress, 2);
220-
data_high = Wire.read(); //Read Data from selected register
221-
data_low = Wire.read(); //Read Data from selected register
222-
data = (data_high << 8)|data_low;
238+
Wire.requestFrom(_i2caddress, 2);
239+
data_high = Wire.read(); // Read Data from selected register
240+
data_low = Wire.read(); // Read Data from selected register
241+
data = (data_high << 8) | data_low;
223242

224243
return data;
225244
}
226245

227246
void VL6180x::VL6180x_setRegister(uint16_t registerAddr, uint8_t data)
228247
{
229-
Wire.beginTransmission( _i2caddress ); // Address set on class instantiation
230-
Wire.write((registerAddr >> 8) & 0xFF); //MSB of register address
231-
Wire.write(registerAddr & 0xFF); //LSB of register address
232-
Wire.write(data); // Data/setting to be sent to device.
233-
Wire.endTransmission(); //Send address and register address bytes
248+
Wire.beginTransmission(_i2caddress); // Address set on class instantiation
249+
Wire.write((registerAddr >> 8) & 0xFF); // MSB of register address
250+
Wire.write(registerAddr & 0xFF); // LSB of register address
251+
Wire.write(data); // Data/setting to be sent to device.
252+
Wire.endTransmission(); // Send address and register address bytes
234253
}
235254

236255
void VL6180x::VL6180x_setRegister16bit(uint16_t registerAddr, uint16_t data)
237256
{
238-
Wire.beginTransmission( _i2caddress ); // Address set on class instantiation
239-
Wire.write((registerAddr >> 8) & 0xFF); //MSB of register address
240-
Wire.write(registerAddr & 0xFF); //LSB of register address
257+
Wire.beginTransmission(_i2caddress); // Address set on class instantiation
258+
Wire.write((registerAddr >> 8) & 0xFF); // MSB of register address
259+
Wire.write(registerAddr & 0xFF); // LSB of register address
241260
uint8_t temp;
242261
temp = (data >> 8) & 0xff;
243262
Wire.write(temp); // Data/setting to be sent to device
244263
temp = data & 0xff;
245-
Wire.write(temp); // Data/setting to be sent to device
246-
Wire.endTransmission(); //Send address and register address bytes
264+
Wire.write(temp); // Data/setting to be sent to device
265+
Wire.endTransmission(); // Send address and register address bytes
247266
}
248-
249-
250-

0 commit comments

Comments
 (0)