|
| 1 | +// SPDX-FileCopyrightText: 2023 Liz Clark for Adafruit Industries |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | +/** |
| 5 | + * PCA9546 I2C Multi Sensor Example |
| 6 | + * |
| 7 | + * Using two VL53L4CD sensors on ports 0 and 1 |
| 8 | + * |
| 9 | + */ |
| 10 | + |
| 11 | +#include <Arduino.h> |
| 12 | +#include <Wire.h> |
| 13 | +#include <vl53l4cd_class.h> |
| 14 | +#include <string.h> |
| 15 | +#include <stdlib.h> |
| 16 | +#include <stdio.h> |
| 17 | +#include <stdint.h> |
| 18 | +#include <assert.h> |
| 19 | + |
| 20 | +#define PCAADDR 0x70 |
| 21 | +#define DEV_I2C Wire |
| 22 | + |
| 23 | +#define SerialPort Serial |
| 24 | + |
| 25 | +// create two instances of the sensor |
| 26 | +VL53L4CD tof1(&DEV_I2C, A1); |
| 27 | +VL53L4CD tof2(&DEV_I2C, A1); |
| 28 | + |
| 29 | +void pcaselect(uint8_t i) { |
| 30 | + if (i > 3) return; |
| 31 | + |
| 32 | + Wire.beginTransmission(PCAADDR); |
| 33 | + Wire.write(1 << i); |
| 34 | + Wire.endTransmission(); |
| 35 | +} |
| 36 | + |
| 37 | +// standard Arduino setup() |
| 38 | +void setup() |
| 39 | +{ |
| 40 | + while (!Serial); |
| 41 | + delay(1000); |
| 42 | + |
| 43 | + Wire.begin(); |
| 44 | + |
| 45 | + Serial.begin(115200); |
| 46 | + Serial.println("\nMultiSensor PCA9546"); |
| 47 | + |
| 48 | + // define the port on the PCA9546 for the first sensor |
| 49 | + pcaselect(0); |
| 50 | + // setup the first sensor |
| 51 | + tof1.begin(); |
| 52 | + tof1.VL53L4CD_Off(); |
| 53 | + tof1.InitSensor(); |
| 54 | + tof1.VL53L4CD_SetRangeTiming(200, 0); |
| 55 | + tof1.VL53L4CD_StartRanging(); |
| 56 | + |
| 57 | + // define the port on the PCA9546 for the 2nd sensor |
| 58 | + pcaselect(1); |
| 59 | + |
| 60 | + // setup the 2nd sensor |
| 61 | + tof2.begin(); |
| 62 | + tof2.VL53L4CD_Off(); |
| 63 | + tof2.InitSensor(); |
| 64 | + tof2.VL53L4CD_SetRangeTiming(200, 0); |
| 65 | + tof2.VL53L4CD_StartRanging(); |
| 66 | + |
| 67 | +} |
| 68 | + |
| 69 | +void loop() |
| 70 | +{ |
| 71 | + uint8_t NewDataReady = 0; |
| 72 | + VL53L4CD_Result_t results; |
| 73 | + uint8_t status; |
| 74 | + char report[64]; |
| 75 | + |
| 76 | + // define port on the PCA9546 |
| 77 | + pcaselect(0); |
| 78 | + |
| 79 | + // loop for time of flight sensor 1 |
| 80 | + do { |
| 81 | + status = tof1.VL53L4CD_CheckForDataReady(&NewDataReady); |
| 82 | + } while (!NewDataReady); |
| 83 | + |
| 84 | + if ((!status) && (NewDataReady != 0)) { |
| 85 | + // (Mandatory) Clear HW interrupt to restart measurements |
| 86 | + tof1.VL53L4CD_ClearInterrupt(); |
| 87 | + |
| 88 | + // Read measured distance. RangeStatus = 0 means valid data |
| 89 | + tof1.VL53L4CD_GetResult(&results); |
| 90 | + snprintf(report, sizeof(report), "ToF 1 - Status = %3u, Distance = %5u mm, Signal = %6u kcps/spad\r\n", |
| 91 | + results.range_status, |
| 92 | + results.distance_mm, |
| 93 | + results.signal_per_spad_kcps); |
| 94 | + SerialPort.println(report); |
| 95 | + } |
| 96 | + |
| 97 | + // define port on PCA9546 |
| 98 | + pcaselect(1); |
| 99 | + |
| 100 | + // loop for time of flight sensor 2 |
| 101 | + do { |
| 102 | + status = tof2.VL53L4CD_CheckForDataReady(&NewDataReady); |
| 103 | + } while (!NewDataReady); |
| 104 | + |
| 105 | + if ((!status) && (NewDataReady != 0)) { |
| 106 | + // (Mandatory) Clear HW interrupt to restart measurements |
| 107 | + tof2.VL53L4CD_ClearInterrupt(); |
| 108 | + |
| 109 | + // Read measured distance. RangeStatus = 0 means valid data |
| 110 | + tof2.VL53L4CD_GetResult(&results); |
| 111 | + snprintf(report, sizeof(report), "ToF 2 - Status = %3u, Distance = %5u mm, Signal = %6u kcps/spad\r\n", |
| 112 | + results.range_status, |
| 113 | + results.distance_mm, |
| 114 | + results.signal_per_spad_kcps); |
| 115 | + SerialPort.println(report); |
| 116 | + } |
| 117 | +} |
0 commit comments