|
| 1 | + /** |
| 2 | + ****************************************************************************** |
| 3 | + * @file VL53L7CX_HelloWorld.ino |
| 4 | + * @author STMicroelectronics |
| 5 | + * @version V1.0.0 |
| 6 | + * @date 16 January 2023 |
| 7 | + * @brief Arduino test application for STMicroelectronics VL53L7CX |
| 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 | + */ |
| 40 | +/* |
| 41 | + * To use these examples you need to connect the VL53L7CX satellite sensor directly to the Nucleo board with wires as explained below: |
| 42 | + * pin 1 (GND) of the VL53L7CX satellite connected to GND of the Nucleo board |
| 43 | + * pin 2 (IOVDD) of the VL53L7CX satellite connected to 3V3 pin of the Nucleo board |
| 44 | + * pin 3 (AVDD) of the VL53L7CX satellite connected to 5V pin of the Nucleo board |
| 45 | + * pin 4 (PWREN) of the VL53L7CX satellite connected to pin A5 of the Nucleo board |
| 46 | + * pin 5 (LPn) of the VL53L7CX satellite connected to pin A3 of the Nucleo board |
| 47 | + * pin 6 (SCL) of the VL53L7CX satellite connected to pin D15 (SCL) of the Nucleo board |
| 48 | + * pin 7 (SDA) of the VL53L7CX satellite connected to pin D14 (SDA) of the Nucleo board |
| 49 | + * pin 8 (I2C_RST) of the VL53L7CX satellite connected to pin A1 of the Nucleo board |
| 50 | + * pin 9 (INT) of the VL53L7CX satellite connected to pin A2 of the Nucleo board |
| 51 | + */ |
| 52 | + |
| 53 | +/* Includes ------------------------------------------------------------------*/ |
| 54 | +#include <Arduino.h> |
| 55 | +#include <Wire.h> |
| 56 | +#include <vl53l7cx_class.h> |
| 57 | + |
| 58 | + |
| 59 | +#ifdef ARDUINO_SAM_DUE |
| 60 | + #define DEV_I2C Wire1 |
| 61 | +#else |
| 62 | + #define DEV_I2C Wire |
| 63 | +#endif |
| 64 | +#define SerialPort Serial |
| 65 | + |
| 66 | +#define LPN_PIN A3 |
| 67 | +#define I2C_RST_PIN A1 |
| 68 | +#define PWREN_PIN A5 |
| 69 | + |
| 70 | +void print_result(VL53L7CX_ResultsData *Result); |
| 71 | +void clear_screen(void); |
| 72 | +void handle_cmd(uint8_t cmd); |
| 73 | +void display_commands_banner(void); |
| 74 | + |
| 75 | +// Components. |
| 76 | +VL53L7CX sensor_vl53l7cx_top(&DEV_I2C, LPN_PIN, I2C_RST_PIN); |
| 77 | + |
| 78 | +bool EnableAmbient = false; |
| 79 | +bool EnableSignal = false; |
| 80 | +uint8_t res = VL53L7CX_RESOLUTION_4X4; |
| 81 | +char report[256]; |
| 82 | + |
| 83 | +/* Setup ---------------------------------------------------------------------*/ |
| 84 | +void setup() |
| 85 | +{ |
| 86 | + |
| 87 | + // Enable PWREN pin if present |
| 88 | + if (PWREN_PIN >= 0) { |
| 89 | + pinMode(PWREN_PIN, OUTPUT); |
| 90 | + digitalWrite(PWREN_PIN, HIGH); |
| 91 | + delay(10); |
| 92 | + } |
| 93 | + |
| 94 | + // Initialize serial for output. |
| 95 | + SerialPort.begin(460800); |
| 96 | + |
| 97 | + // Initialize I2C bus. |
| 98 | + DEV_I2C.begin(); |
| 99 | + |
| 100 | + // Configure VL53L7CX component. |
| 101 | + sensor_vl53l7cx_top.begin(); |
| 102 | + |
| 103 | + sensor_vl53l7cx_top.init_sensor(); |
| 104 | + |
| 105 | + // Start Measurements |
| 106 | + sensor_vl53l7cx_top.vl53l7cx_start_ranging(); |
| 107 | +} |
| 108 | + |
| 109 | +void loop() |
| 110 | +{ |
| 111 | + VL53L7CX_ResultsData Results; |
| 112 | + uint8_t NewDataReady = 0; |
| 113 | + uint8_t status; |
| 114 | + |
| 115 | + do { |
| 116 | + status = sensor_vl53l7cx_top.vl53l7cx_check_data_ready(&NewDataReady); |
| 117 | + } while (!NewDataReady); |
| 118 | + |
| 119 | + if ((!status) && (NewDataReady != 0)) { |
| 120 | + status = sensor_vl53l7cx_top.vl53l7cx_get_ranging_data(&Results); |
| 121 | + print_result(&Results); |
| 122 | + } |
| 123 | + |
| 124 | + if (Serial.available()>0) |
| 125 | + { |
| 126 | + handle_cmd(Serial.read()); |
| 127 | + } |
| 128 | + delay(1000); |
| 129 | +} |
| 130 | + |
| 131 | +void print_result(VL53L7CX_ResultsData *Result) |
| 132 | +{ |
| 133 | + int8_t i, j, k, l; |
| 134 | + uint8_t zones_per_line; |
| 135 | + uint8_t number_of_zones = res; |
| 136 | + |
| 137 | + zones_per_line = (number_of_zones == 16) ? 4 : 8; |
| 138 | + |
| 139 | + display_commands_banner(); |
| 140 | + |
| 141 | + SerialPort.print("Cell Format :\n\n"); |
| 142 | + |
| 143 | + for (l = 0; l < VL53L7CX_NB_TARGET_PER_ZONE; l++) |
| 144 | + { |
| 145 | + snprintf(report, sizeof(report)," \033[38;5;10m%20s\033[0m : %20s\n", "Distance [mm]", "Status"); |
| 146 | + SerialPort.print(report); |
| 147 | + |
| 148 | + if(EnableAmbient || EnableSignal) |
| 149 | + { |
| 150 | + snprintf(report, sizeof(report)," %20s : %20s\n", "Signal [kcps/spad]", "Ambient [kcps/spad]"); |
| 151 | + SerialPort.print(report); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + SerialPort.print("\n\n"); |
| 156 | + |
| 157 | + for (j = 0; j < number_of_zones; j += zones_per_line) |
| 158 | + { |
| 159 | + for (i = 0; i < zones_per_line; i++) |
| 160 | + SerialPort.print(" -----------------"); |
| 161 | + SerialPort.print("\n"); |
| 162 | + |
| 163 | + for (i = 0; i < zones_per_line; i++) |
| 164 | + SerialPort.print("| "); |
| 165 | + SerialPort.print("|\n"); |
| 166 | + |
| 167 | + for (l = 0; l < VL53L7CX_NB_TARGET_PER_ZONE; l++) |
| 168 | + { |
| 169 | + // Print distance and status |
| 170 | + for (k = (zones_per_line - 1); k >= 0; k--) |
| 171 | + { |
| 172 | + if (Result->nb_target_detected[j+k]>0) |
| 173 | + { |
| 174 | + snprintf(report, sizeof(report),"| \033[38;5;10m%5ld\033[0m : %5ld ", |
| 175 | + (long)Result->distance_mm[(VL53L7CX_NB_TARGET_PER_ZONE * (j+k)) + l], |
| 176 | + (long)Result->target_status[(VL53L7CX_NB_TARGET_PER_ZONE * (j+k)) + l]); |
| 177 | + SerialPort.print(report); |
| 178 | + } |
| 179 | + else |
| 180 | + { |
| 181 | + snprintf(report, sizeof(report),"| %5s : %5s ", "X", "X"); |
| 182 | + SerialPort.print(report); |
| 183 | + } |
| 184 | + } |
| 185 | + SerialPort.print("|\n"); |
| 186 | + |
| 187 | + if (EnableAmbient || EnableSignal ) |
| 188 | + { |
| 189 | + // Print Signal and Ambient |
| 190 | + for (k = (zones_per_line - 1); k >= 0; k--) |
| 191 | + { |
| 192 | + if (Result->nb_target_detected[j+k]>0) |
| 193 | + { |
| 194 | + if (EnableSignal) |
| 195 | + { |
| 196 | + snprintf(report, sizeof(report),"| %5ld : ", (long)Result->signal_per_spad[(VL53L7CX_NB_TARGET_PER_ZONE * (j+k)) + l]); |
| 197 | + SerialPort.print(report); |
| 198 | + } |
| 199 | + else |
| 200 | + { |
| 201 | + snprintf(report, sizeof(report),"| %5s : ", "X"); |
| 202 | + SerialPort.print(report); |
| 203 | + } |
| 204 | + if (EnableAmbient) |
| 205 | + { |
| 206 | + snprintf(report, sizeof(report),"%5ld ", (long)Result->ambient_per_spad[j+k]); |
| 207 | + SerialPort.print(report); |
| 208 | + } |
| 209 | + else |
| 210 | + { |
| 211 | + snprintf(report, sizeof(report),"%5s ", "X"); |
| 212 | + SerialPort.print(report); |
| 213 | + } |
| 214 | + } |
| 215 | + else |
| 216 | + { |
| 217 | + snprintf(report, sizeof(report),"| %5s : %5s ", "X", "X"); |
| 218 | + SerialPort.print(report); |
| 219 | + } |
| 220 | + } |
| 221 | + SerialPort.print("|\n"); |
| 222 | + } |
| 223 | + } |
| 224 | + } |
| 225 | + for (i = 0; i < zones_per_line; i++) |
| 226 | + SerialPort.print(" -----------------"); |
| 227 | + SerialPort.print("\n"); |
| 228 | +} |
| 229 | + |
| 230 | +void toggle_resolution(void) |
| 231 | +{ |
| 232 | + sensor_vl53l7cx_top.vl53l7cx_stop_ranging(); |
| 233 | + |
| 234 | + switch (res) |
| 235 | + { |
| 236 | + case VL53L7CX_RESOLUTION_4X4: |
| 237 | + res = VL53L7CX_RESOLUTION_8X8; |
| 238 | + break; |
| 239 | + |
| 240 | + case VL53L7CX_RESOLUTION_8X8: |
| 241 | + res = VL53L7CX_RESOLUTION_4X4; |
| 242 | + break; |
| 243 | + |
| 244 | + default: |
| 245 | + break; |
| 246 | + } |
| 247 | + sensor_vl53l7cx_top.vl53l7cx_set_resolution(res); |
| 248 | + sensor_vl53l7cx_top.vl53l7cx_start_ranging(); |
| 249 | +} |
| 250 | + |
| 251 | +void toggle_signal_and_ambient(void) |
| 252 | +{ |
| 253 | + EnableAmbient = (EnableAmbient) ? false : true; |
| 254 | + EnableSignal = (EnableSignal) ? false : true; |
| 255 | +} |
| 256 | + |
| 257 | +void clear_screen(void) |
| 258 | +{ |
| 259 | + snprintf(report, sizeof(report),"%c[2J", 27); /* 27 is ESC command */ |
| 260 | + SerialPort.print(report); |
| 261 | +} |
| 262 | + |
| 263 | +void display_commands_banner(void) |
| 264 | +{ |
| 265 | + snprintf(report, sizeof(report),"%c[2H", 27); /* 27 is ESC command */ |
| 266 | + SerialPort.print(report); |
| 267 | + |
| 268 | + Serial.print("53L7A1 Simple Ranging demo application\n"); |
| 269 | + Serial.print("--------------------------------------\n\n"); |
| 270 | + |
| 271 | + Serial.print("Use the following keys to control application\n"); |
| 272 | + Serial.print(" 'r' : change resolution\n"); |
| 273 | + Serial.print(" 's' : enable signal and ambient\n"); |
| 274 | + Serial.print(" 'c' : clear screen\n"); |
| 275 | + Serial.print("\n"); |
| 276 | +} |
| 277 | + |
| 278 | +void handle_cmd(uint8_t cmd) |
| 279 | +{ |
| 280 | + switch (cmd) |
| 281 | + { |
| 282 | + case 'r': |
| 283 | + toggle_resolution(); |
| 284 | + clear_screen(); |
| 285 | + break; |
| 286 | + |
| 287 | + case 's': |
| 288 | + toggle_signal_and_ambient(); |
| 289 | + clear_screen(); |
| 290 | + break; |
| 291 | + |
| 292 | + case 'c': |
| 293 | + clear_screen(); |
| 294 | + break; |
| 295 | + |
| 296 | + default: |
| 297 | + break; |
| 298 | + } |
| 299 | +} |
0 commit comments