Skip to content

Commit 069e331

Browse files
committed
Revert "VSC formatting"
This reverts commit 402ff06.
1 parent b8da600 commit 069e331

2 files changed

Lines changed: 211 additions & 222 deletions

File tree

src/SparkFun_VL6180X.cpp

Lines changed: 103 additions & 119 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,21 +32,20 @@
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-
uint8_t VL6180x::VL6180xInit(void)
40-
{
41-
uint8_t data; // for temp data storage
39+
40+
uint8_t VL6180x::VL6180xInit(void){
41+
uint8_t data; //for temp data storage
4242

4343
data = VL6180x_getRegister(VL6180X_SYSTEM_FRESH_OUT_OF_RESET);
4444

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

48-
// Required by datasheet
49-
// http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf
47+
//Required by datasheet
48+
//http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf
5049
VL6180x_setRegister(0x0207, 0x01);
5150
VL6180x_setRegister(0x0208, 0x01);
5251
VL6180x_setRegister(0x0096, 0x00);
@@ -66,7 +65,7 @@ uint8_t VL6180x::VL6180xInit(void)
6665
VL6180x_setRegister(0x00b7, 0x00);
6766
VL6180x_setRegister(0x00bb, 0x3c);
6867
VL6180x_setRegister(0x00b2, 0x09);
69-
VL6180x_setRegister(0x00ca, 0x09);
68+
VL6180x_setRegister(0x00ca, 0x09);
7069
VL6180x_setRegister(0x0198, 0x01);
7170
VL6180x_setRegister(0x01b0, 0x17);
7271
VL6180x_setRegister(0x01ad, 0x00);
@@ -81,39 +80,38 @@ uint8_t VL6180x::VL6180xInit(void)
8180
return 0;
8281
}
8382

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
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
10099
VL6180x_setRegister(VL6180X_SYSRANGE_INTERMEASUREMENT_PERIOD, 0x09); // Set default ranging inter-measurement period to 100ms
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
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
104103
VL6180x_setRegister(VL6180X_SYSRANGE_MAX_CONVERGENCE_TIME, 0x32);
105104
VL6180x_setRegister(VL6180X_SYSRANGE_RANGE_CHECK_ENABLES, 0x10 | 0x01);
106-
VL6180x_setRegister16bit(VL6180X_SYSRANGE_EARLY_CONVERGENCE_ESTIMATE, 0x7B);
105+
VL6180x_setRegister16bit(VL6180X_SYSRANGE_EARLY_CONVERGENCE_ESTIMATE, 0x7B );
107106
VL6180x_setRegister16bit(VL6180X_SYSALS_INTEGRATION_PERIOD, 0x64);
108107

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);
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);
112111
}
113-
void VL6180x::getIdentification(struct VL6180xIdentification *temp)
114-
{
112+
void VL6180x::getIdentification(struct VL6180xIdentification *temp){
115113

116-
temp->idModel = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODEL_ID);
114+
temp->idModel = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODEL_ID);
117115
temp->idModelRevMajor = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODEL_REV_MAJOR);
118116
temp->idModelRevMinor = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODEL_REV_MINOR);
119117
temp->idModuleRevMajor = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODULE_REV_MAJOR);
@@ -123,25 +121,25 @@ void VL6180x::getIdentification(struct VL6180xIdentification *temp)
123121
temp->idTime = VL6180x_getRegister16bit(VL6180X_IDENTIFICATION_TIME);
124122
}
125123

126-
uint8_t VL6180x::changeAddress(uint8_t old_address, uint8_t new_address)
127-
{
128-
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;
136124

137-
VL6180x_setRegister(VL6180X_I2C_SLAVE_DEVICE_ADDRESS, new_address);
138-
139-
return VL6180x_getRegister(VL6180X_I2C_SLAVE_DEVICE_ADDRESS);
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);
140136
}
137+
138+
141139

142140
uint8_t VL6180x::getDistance()
143141
{
144-
VL6180x_setRegister(VL6180X_SYSRANGE_START, 0x01); // Start Single shot mode
142+
VL6180x_setRegister(VL6180X_SYSRANGE_START, 0x01); //Start Single shot mode
145143
delay(10);
146144
VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CLEAR, 0x07);
147145
return VL6180x_getRegister(VL6180X_RESULT_RANGE_VAL);
@@ -150,59 +148,42 @@ uint8_t VL6180x::getDistance()
150148

151149
float VL6180x::getAmbientLight(vl6180x_als_gain VL6180X_ALS_GAIN)
152150
{
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
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
155153
VL6180x_setRegister(VL6180X_SYSALS_ANALOGUE_GAIN, (0x40 | VL6180X_ALS_GAIN)); // Set the ALS gain
156154

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

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

162160
VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CLEAR, 0x07);
163161

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

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

174172
float alsGain = 0.0;
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;
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;
202183
}
203184

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

208189
return alsCalculated;
@@ -214,12 +195,12 @@ uint8_t VL6180x::VL6180x_getRegister(uint16_t registerAddr)
214195
{
215196
uint8_t data;
216197

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
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
223204

224205
return data;
225206
}
@@ -230,37 +211,40 @@ uint16_t VL6180x::VL6180x_getRegister16bit(uint16_t registerAddr)
230211
uint8_t data_high;
231212
uint16_t data;
232213

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
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
237218

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;
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;
242223

243224
return data;
244225
}
245226

246227
void VL6180x::VL6180x_setRegister(uint16_t registerAddr, uint8_t data)
247228
{
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
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
253234
}
254235

255236
void VL6180x::VL6180x_setRegister16bit(uint16_t registerAddr, uint16_t data)
256237
{
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
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
260241
uint8_t temp;
261242
temp = (data >> 8) & 0xff;
262243
Wire.write(temp); // Data/setting to be sent to device
263244
temp = data & 0xff;
264-
Wire.write(temp); // Data/setting to be sent to device
265-
Wire.endTransmission(); // Send address and register address bytes
245+
Wire.write(temp); // Data/setting to be sent to device
246+
Wire.endTransmission(); //Send address and register address bytes
266247
}
248+
249+
250+

0 commit comments

Comments
 (0)