1
- /* *
2
- ******************************************************************************
3
- * @file VL53L8CX_HelloWorld_I2C.ino
4
- * @author STMicroelectronics
5
- * @version V1 .0.0
6
- * @date 12 June 2023
7
- * @brief Arduino test application for STMicroelectronics VL53L8CX
8
- * proximity sensor satellite based on FlightSense.
9
- * This application makes use of C++ classes obtained from the C
10
- * components' drivers.
11
- ******************************************************************************
12
- * @attention
13
- *
14
- * <h2><center>© COPYRIGHT(c) 2021 STMicroelectronics</center></h2>
15
- *
16
- * Redistribution and use in source and binary forms, with or without modification,
17
- * are permitted provided that the following conditions are met:
18
- * 1. Redistributions of source code must retain the above copyright notice,
19
- * this list of conditions and the following disclaimer.
20
- * 2. Redistributions in binary form must reproduce the above copyright notice,
21
- * this list of conditions and the following disclaimer in the documentation
22
- * and/or other materials provided with the distribution.
23
- * 3. Neither the name of STMicroelectronics nor the names of its contributors
24
- * may be used to endorse or promote products derived from this software
25
- * without specific prior written permission.
26
- *
27
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
31
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
- *
38
- ******************************************************************************
39
- */
1
+ /* *
2
+ ******************************************************************************
3
+ * @file VL53L8CX_HelloWorld_I2C.ino
4
+ * @author STMicroelectronics
5
+ * @version V2 .0.0
6
+ * @date 27 June 2024
7
+ * @brief Arduino test application for STMicroelectronics VL53L8CX
8
+ * proximity sensor satellite based on FlightSense.
9
+ * This application makes use of C++ classes obtained from the C
10
+ * components' drivers.
11
+ ******************************************************************************
12
+ * @attention
13
+ *
14
+ * <h2><center>© COPYRIGHT(c) 2024 STMicroelectronics</center></h2>
15
+ *
16
+ * Redistribution and use in source and binary forms, with or without modification,
17
+ * are permitted provided that the following conditions are met:
18
+ * 1. Redistributions of source code must retain the above copyright notice,
19
+ * this list of conditions and the following disclaimer.
20
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ * 3. Neither the name of STMicroelectronics nor the names of its contributors
24
+ * may be used to endorse or promote products derived from this software
25
+ * without specific prior written permission.
26
+ *
27
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
31
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
+ *
38
+ ******************************************************************************
39
+ */
40
40
/*
41
41
* To use these examples you need to connect the VL53L8CX satellite sensor directly to the Nucleo board with wires as explained below:
42
- * pin 1 (SPI_I2C_n ) of the VL53L8CX satellite connected to pin GND of the Nucleo board
42
+ * pin 1 (SPI_I2C_N ) of the VL53L8CX satellite connected to pin GND of the Nucleo board
43
43
* pin 2 (LPn) of the VL53L8CX satellite connected to pin A3 of the Nucleo board
44
44
* pin 3 (NCS) not connected
45
45
* pin 4 (MISO) not connected
49
49
* pin 8 (I0VDD) of the VL53L8CX satellite not connected
50
50
* pin 9 (3V3) of the VL53L8CX satellite connected to 3V3 of the Nucleo board
51
51
* pin 10 (1V8) of the VL53L8CX satellite not connected
52
- * pin 11 (5V) of the VL53L8CX satellite not connected
52
+ * pin 11 (5V) of the VL53L8CX satellite not connected
53
53
* GPIO1 of VL53L8CX satellite connected to A2 pin of the Nucleo board (not used)
54
54
* GND of the VL53L8CX satellite connected to GND of the Nucleo board
55
55
*/
56
-
56
+
57
57
/* Includes ------------------------------------------------------------------*/
58
- #include < Arduino.h>
59
- #include < Wire.h>
60
- #include < vl53l8cx_class.h>
58
+
59
+ #include < vl53l8cx.h>
61
60
62
61
63
62
#ifdef ARDUINO_SAM_DUE
68
67
#define SerialPort Serial
69
68
70
69
#define LPN_PIN A3
71
- #define I2C_RST_PIN -1
72
70
#define PWREN_PIN 11
73
71
74
72
void print_result (VL53L8CX_ResultsData *Result);
@@ -77,12 +75,13 @@ void handle_cmd(uint8_t cmd);
77
75
void display_commands_banner (void );
78
76
79
77
// Components.
80
- VL53L8CX sensor_vl53l8cx_top (&DEV_I2C, LPN_PIN, I2C_RST_PIN );
78
+ VL53L8CX sensor_vl53l8cx_top (&DEV_I2C, LPN_PIN);
81
79
82
80
bool EnableAmbient = false ;
83
81
bool EnableSignal = false ;
84
82
uint8_t res = VL53L8CX_RESOLUTION_4X4;
85
83
char report[256 ];
84
+ uint8_t status;
86
85
87
86
/* Setup ---------------------------------------------------------------------*/
88
87
void setup ()
@@ -100,32 +99,30 @@ void setup()
100
99
101
100
// Initialize I2C bus.
102
101
DEV_I2C.begin ();
103
-
102
+
104
103
// Configure VL53L8CX component.
105
104
sensor_vl53l8cx_top.begin ();
106
- sensor_vl53l8cx_top.init_sensor ();
107
-
105
+ status = sensor_vl53l8cx_top.init ();
106
+
108
107
// Start Measurements
109
- sensor_vl53l8cx_top.vl53l8cx_start_ranging ();
108
+ status = sensor_vl53l8cx_top.start_ranging ();
110
109
}
111
110
112
111
void loop ()
113
112
{
114
113
VL53L8CX_ResultsData Results;
115
114
uint8_t NewDataReady = 0 ;
116
- uint8_t status;
117
115
118
116
do {
119
- status = sensor_vl53l8cx_top.vl53l8cx_check_data_ready (&NewDataReady);
117
+ status = sensor_vl53l8cx_top.check_data_ready (&NewDataReady);
120
118
} while (!NewDataReady);
121
119
122
120
if ((!status) && (NewDataReady != 0 )) {
123
- status = sensor_vl53l8cx_top.vl53l8cx_get_ranging_data (&Results);
121
+ status = sensor_vl53l8cx_top.get_ranging_data (&Results);
124
122
print_result (&Results);
125
123
}
126
124
127
- if (Serial.available ()>0 )
128
- {
125
+ if (Serial.available () > 0 ) {
129
126
handle_cmd (Serial.read ());
130
127
}
131
128
delay (100 );
@@ -142,100 +139,83 @@ void print_result(VL53L8CX_ResultsData *Result)
142
139
display_commands_banner ();
143
140
144
141
SerialPort.print (" Cell Format :\n\n " );
145
-
146
- for (l = 0 ; l < VL53L8CX_NB_TARGET_PER_ZONE; l++)
147
- {
148
- snprintf (report, sizeof (report)," \033 [38;5;10m%20s\033 [0m : %20s\n " , " Distance [mm]" , " Status" );
142
+
143
+ for (l = 0 ; l < VL53L8CX_NB_TARGET_PER_ZONE; l++) {
144
+ snprintf (report, sizeof (report), " \033 [38;5;10m%20s\033 [0m : %20s\n " , " Distance [mm]" , " Status" );
149
145
SerialPort.print (report);
150
146
151
- if (EnableAmbient || EnableSignal)
152
- {
153
- snprintf (report, sizeof (report)," %20s : %20s\n " , " Signal [kcps/spad]" , " Ambient [kcps/spad]" );
147
+ if (EnableAmbient || EnableSignal) {
148
+ snprintf (report, sizeof (report), " %20s : %20s\n " , " Signal [kcps/spad]" , " Ambient [kcps/spad]" );
154
149
SerialPort.print (report);
155
150
}
156
151
}
157
152
158
153
SerialPort.print (" \n\n " );
159
154
160
- for (j = 0 ; j < number_of_zones; j += zones_per_line)
161
- {
162
- for (i = 0 ; i < zones_per_line; i++)
155
+ for (j = 0 ; j < number_of_zones; j += zones_per_line) {
156
+ for (i = 0 ; i < zones_per_line; i++) {
163
157
SerialPort.print (" -----------------" );
158
+ }
164
159
SerialPort.print (" \n " );
165
-
166
- for (i = 0 ; i < zones_per_line; i++)
160
+
161
+ for (i = 0 ; i < zones_per_line; i++) {
167
162
SerialPort.print (" | " );
163
+ }
168
164
SerialPort.print (" |\n " );
169
-
170
- for (l = 0 ; l < VL53L8CX_NB_TARGET_PER_ZONE; l++)
171
- {
172
- // Print distance and status
173
- for (k = (zones_per_line - 1 ); k >= 0 ; k--)
174
- {
175
- if (Result->nb_target_detected [j+k]>0 )
176
- {
177
- snprintf (report, sizeof (report)," | \033 [38;5;10m%5ld\033 [0m : %5ld " ,
178
- (long )Result->distance_mm [(VL53L8CX_NB_TARGET_PER_ZONE * (j+k)) + l],
179
- (long )Result->target_status [(VL53L8CX_NB_TARGET_PER_ZONE * (j+k)) + l]);
180
- SerialPort.print (report);
181
- }
182
- else
183
- {
184
- snprintf (report, sizeof (report)," | %5s : %5s " , " X" , " X" );
165
+
166
+ for (l = 0 ; l < VL53L8CX_NB_TARGET_PER_ZONE; l++) {
167
+ // Print distance and status
168
+ for (k = (zones_per_line - 1 ); k >= 0 ; k--) {
169
+ if (Result->nb_target_detected [j + k] > 0 ) {
170
+ snprintf (report, sizeof (report), " | \033 [38;5;10m%5ld\033 [0m : %5ld " ,
171
+ (long )Result->distance_mm [(VL53L8CX_NB_TARGET_PER_ZONE * (j + k)) + l],
172
+ (long )Result->target_status [(VL53L8CX_NB_TARGET_PER_ZONE * (j + k)) + l]);
173
+ SerialPort.print (report);
174
+ } else {
175
+ snprintf (report, sizeof (report), " | %5s : %5s " , " X" , " X" );
185
176
SerialPort.print (report);
186
177
}
187
178
}
188
179
SerialPort.print (" |\n " );
189
180
190
- if (EnableAmbient || EnableSignal )
191
- {
192
- // Print Signal and Ambient
193
- for (k = (zones_per_line - 1 ); k >= 0 ; k--)
194
- {
195
- if (Result->nb_target_detected [j+k]>0 )
196
- {
197
- if (EnableSignal)
198
- {
199
- snprintf (report, sizeof (report)," | %5ld : " , (long )Result->signal_per_spad [(VL53L8CX_NB_TARGET_PER_ZONE * (j+k)) + l]);
181
+ if (EnableAmbient || EnableSignal) {
182
+ // Print Signal and Ambient
183
+ for (k = (zones_per_line - 1 ); k >= 0 ; k--) {
184
+ if (Result->nb_target_detected [j + k] > 0 ) {
185
+ if (EnableSignal) {
186
+ snprintf (report, sizeof (report), " | %5ld : " , (long )Result->signal_per_spad [(VL53L8CX_NB_TARGET_PER_ZONE * (j + k)) + l]);
200
187
SerialPort.print (report);
201
- }
202
- else
203
- {
204
- snprintf (report, sizeof (report)," | %5s : " , " X" );
188
+ } else {
189
+ snprintf (report, sizeof (report), " | %5s : " , " X" );
205
190
SerialPort.print (report);
206
191
}
207
- if (EnableAmbient)
208
- {
209
- snprintf (report, sizeof (report)," %5ld " , (long )Result->ambient_per_spad [j+k]);
192
+ if (EnableAmbient) {
193
+ snprintf (report, sizeof (report), " %5ld " , (long )Result->ambient_per_spad [j + k]);
210
194
SerialPort.print (report);
211
- }
212
- else
213
- {
214
- snprintf (report, sizeof (report)," %5s " , " X" );
195
+ } else {
196
+ snprintf (report, sizeof (report), " %5s " , " X" );
215
197
SerialPort.print (report);
216
198
}
217
- }
218
- else
219
- {
220
- snprintf (report, sizeof (report)," | %5s : %5s " , " X" , " X" );
199
+ } else {
200
+ snprintf (report, sizeof (report), " | %5s : %5s " , " X" , " X" );
221
201
SerialPort.print (report);
222
202
}
223
203
}
224
204
SerialPort.print (" |\n " );
225
205
}
226
206
}
227
207
}
228
- for (i = 0 ; i < zones_per_line; i++)
229
- SerialPort.print (" -----------------" );
208
+ for (i = 0 ; i < zones_per_line; i++) {
209
+ SerialPort.print (" -----------------" );
210
+ }
230
211
SerialPort.print (" \n " );
231
212
}
232
213
233
214
void toggle_resolution (void )
234
215
{
235
- sensor_vl53l8cx_top.vl53l8cx_stop_ranging ();
216
+ status = sensor_vl53l8cx_top.stop_ranging ();
236
217
237
- switch (res)
238
- {
218
+ switch (res) {
239
219
case VL53L8CX_RESOLUTION_4X4:
240
220
res = VL53L8CX_RESOLUTION_8X8;
241
221
break ;
@@ -247,8 +227,8 @@ void toggle_resolution(void)
247
227
default :
248
228
break ;
249
229
}
250
- sensor_vl53l8cx_top.vl53l8cx_set_resolution (res);
251
- sensor_vl53l8cx_top.vl53l8cx_start_ranging ();
230
+ status = sensor_vl53l8cx_top.set_resolution (res);
231
+ status = sensor_vl53l8cx_top.start_ranging ();
252
232
}
253
233
254
234
void toggle_signal_and_ambient (void )
@@ -259,13 +239,13 @@ void toggle_signal_and_ambient(void)
259
239
260
240
void clear_screen (void )
261
241
{
262
- snprintf (report, sizeof (report)," %c[2J" , 27 ); /* 27 is ESC command */
242
+ snprintf (report, sizeof (report), " %c[2J" , 27 ); /* 27 is ESC command */
263
243
SerialPort.print (report);
264
244
}
265
245
266
246
void display_commands_banner (void )
267
247
{
268
- snprintf (report, sizeof (report)," %c[2H" , 27 ); /* 27 is ESC command */
248
+ snprintf (report, sizeof (report), " %c[2H" , 27 ); /* 27 is ESC command */
269
249
SerialPort.print (report);
270
250
271
251
Serial.print (" 53L8A1 Simple Ranging demo application\n " );
@@ -280,8 +260,7 @@ void display_commands_banner(void)
280
260
281
261
void handle_cmd (uint8_t cmd)
282
262
{
283
- switch (cmd)
284
- {
263
+ switch (cmd) {
285
264
case ' r' :
286
265
toggle_resolution ();
287
266
clear_screen ();
0 commit comments