Skip to content
Snippets Groups Projects
Commit 09c34092 authored by Rene Ebeling's avatar Rene Ebeling
Browse files

reworked arduino library folder structure

parent ff5c1f96
No related branches found
No related tags found
No related merge requests found
Showing
with 0 additions and 341 deletions
/**
******************************************************************************
* @file VL53L7CX_Sat_HelloWorld.ino
* @author STMicroelectronics
* @version V1.0.0
* @date 11 November 2021
* @brief Arduino test application for the STMicrolectronics VL53L7CX
* proximity sensor satellite based on FlightSense.
* This application makes use of C++ classes obtained from the C
* components' drivers.
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT(c) 2021 STMicroelectronics</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
#include <Arduino.h>
#include <Wire.h>
#include <ArduinoJson.h>
#include <vl53l7cx_class.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <stdlib.h>
#include <PCF8575.h>
#include <cmath>
#define DEV_I2C1 Wire1
#define DEV_I2C0 Wire
#define LedPin LED_BUILTIN
#define FAKE_I2C_RST 99
#define LPN_PIN0 0
#define LPN_PIN1 1
#define LPN_PIN2 2
#define LPN_PIN3 3
#define LPN_PIN4 4
#define LPN_PIN5 5
#define LPN_PIN6 6
#define LPN_PIN7 7
#define LPN_PIN8 8
uint16_t sensoraddress0 = 0x30;
uint16_t sensoraddress1 = 0x31;
uint16_t sensoraddress2 = 0x32;
uint16_t sensoraddress3 = 0x33;
uint16_t sensoraddress4 = 0x34;
uint16_t sensoraddress5 = 0x35;
uint16_t sensoraddress6 = 0x36;
uint16_t sensoraddress7 = 0x37;
uint16_t sensoraddress8 = 0x38;
uint16_t wait_for_i2c = 50;
uint16_t imageWidth = 8;
uint16_t ranging_frequency = 5;
uint32_t i2c_freq_hz = 600000;
uint32_t baudrate = 1000000;
pin_size_t SDA_PIN0 = 4;
pin_size_t SCL_PIN0 = 5;
pin_size_t SDA_PIN1 = 26;
pin_size_t SCL_PIN1 = 27;
int sensor_count = 0;
bool sensorInitializedI2C1[8] = { false }; // global oder static
bool sensorInitializedI2C0[8] = { false }; // global oder static
// adjust addresses if needed
PCF8575 PCF1(0x21, &DEV_I2C1); // or Wire2 if supported
PCF8575 PCF0(0x20, &DEV_I2C0); // or Wire2 if supported
JsonDocument doc;
// Components.
VL53L7CX sensor0(&DEV_I2C1, LPN_PIN0, FAKE_I2C_RST);
VL53L7CX sensor1(&DEV_I2C1, LPN_PIN1, FAKE_I2C_RST);
VL53L7CX sensor2(&DEV_I2C1, LPN_PIN2, FAKE_I2C_RST);
VL53L7CX sensor3(&DEV_I2C1, LPN_PIN3, FAKE_I2C_RST);
VL53L7CX sensor4(&DEV_I2C1, LPN_PIN4, FAKE_I2C_RST);
VL53L7CX sensor5(&DEV_I2C1, LPN_PIN5, FAKE_I2C_RST);
VL53L7CX sensor6(&DEV_I2C1, LPN_PIN6, FAKE_I2C_RST);
VL53L7CX sensor7(&DEV_I2C1, LPN_PIN7, FAKE_I2C_RST);
VL53L7CX sensor8(&DEV_I2C1, LPN_PIN8, FAKE_I2C_RST);
VL53L7CX sensor9(&DEV_I2C0, LPN_PIN0, FAKE_I2C_RST);
VL53L7CX sensor10(&DEV_I2C0, LPN_PIN1, FAKE_I2C_RST);
VL53L7CX sensor11(&DEV_I2C0, LPN_PIN2, FAKE_I2C_RST);
VL53L7CX sensor12(&DEV_I2C0, LPN_PIN3, FAKE_I2C_RST);
VL53L7CX sensor13(&DEV_I2C0, LPN_PIN4, FAKE_I2C_RST);
VL53L7CX sensor14(&DEV_I2C0, LPN_PIN5, FAKE_I2C_RST);
VL53L7CX sensor15(&DEV_I2C0, LPN_PIN6, FAKE_I2C_RST);
VL53L7CX sensor16(&DEV_I2C0, LPN_PIN7, FAKE_I2C_RST);
VL53L7CX sensor17(&DEV_I2C0, LPN_PIN8, FAKE_I2C_RST);
void blink_led_loop(void);
void blink_led_loop(void) {
do {
// Blink the led forever
digitalWrite(LedPin, HIGH);
delay(wait_for_i2c);
digitalWrite(LedPin, LOW);
} while (1);
}
/* Setup ---------------------------------------------------------------------*/
void setup() {
delay(3000);
// Led.
pinMode(LedPin, OUTPUT);
digitalWrite(LedPin, HIGH);
// Initialize serial for output.
Serial.begin(baudrate);
Serial.println();
Serial.println("Please wait, it may take few seconds...");
// Initialize I2C bus.
DEV_I2C0.begin();
DEV_I2C0.setClock(i2c_freq_hz);
DEV_I2C1.setSCL(SCL_PIN1);
DEV_I2C1.setSDA(SDA_PIN1);
DEV_I2C1.begin();
DEV_I2C1.setClock(i2c_freq_hz);
setupPCF(PCF0, DEV_I2C0, 0);
setupPCF(PCF1, DEV_I2C1, 1);
// Initialize and configure sensors
initializeSensor(sensor9, sensoraddress0, LPN_PIN0, PCF0, "PCF0");
initializeSensor(sensor10, sensoraddress1, LPN_PIN1, PCF0, "PCF0");
initializeSensor(sensor11, sensoraddress2, LPN_PIN2, PCF0, "PCF0");
initializeSensor(sensor12, sensoraddress3, LPN_PIN3, PCF0, "PCF0");
initializeSensor(sensor13, sensoraddress4, LPN_PIN4, PCF0, "PCF0");
initializeSensor(sensor14, sensoraddress5, LPN_PIN5, PCF0, "PCF0");
initializeSensor(sensor15, sensoraddress6, LPN_PIN6, PCF0, "PCF0");
initializeSensor(sensor16, sensoraddress7, LPN_PIN7, PCF0, "PCF0");
initializeSensor(sensor17, sensoraddress8, LPN_PIN8, PCF0, "PCF0");
Serial.println("Initialized " + String(sensor_count) + " Sensors");
initializeSensor(sensor0, sensoraddress0, LPN_PIN0, PCF1, "PCF1");
initializeSensor(sensor1, sensoraddress1, LPN_PIN1, PCF1, "PCF1");
initializeSensor(sensor2, sensoraddress2, LPN_PIN2, PCF1, "PCF1");
initializeSensor(sensor3, sensoraddress3, LPN_PIN3, PCF1, "PCF1");
initializeSensor(sensor4, sensoraddress4, LPN_PIN4, PCF1, "PCF1");
initializeSensor(sensor5, sensoraddress5, LPN_PIN5, PCF1, "PCF1");
initializeSensor(sensor6, sensoraddress6, LPN_PIN6, PCF1, "PCF1");
initializeSensor(sensor7, sensoraddress7, LPN_PIN7, PCF1, "PCF1");
initializeSensor(sensor8, sensoraddress8, LPN_PIN8, PCF1, "PCF1");
Serial.println("Initialized " + String(sensor_count) + " Sensors");
}
void loop() {
// Declare the result data variables for each sensor
VL53L7CX_ResultsData Results0;
VL53L7CX_ResultsData Results1;
VL53L7CX_ResultsData Results2;
VL53L7CX_ResultsData Results3;
VL53L7CX_ResultsData Results4;
VL53L7CX_ResultsData Results5;
VL53L7CX_ResultsData Results6;
VL53L7CX_ResultsData Results7;
VL53L7CX_ResultsData Results8;
VL53L7CX_ResultsData Results9;
VL53L7CX_ResultsData Results10;
VL53L7CX_ResultsData Results11;
VL53L7CX_ResultsData Results12;
VL53L7CX_ResultsData Results13;
VL53L7CX_ResultsData Results14;
VL53L7CX_ResultsData Results15;
VL53L7CX_ResultsData Results16;
VL53L7CX_ResultsData Results17;
// Process each sensor data and save to respective JSON arrays
processSensorData(sensor0, Results0, doc["sensor0"].to<JsonArray>(), "sensor0", LPN_PIN0);
processSensorData(sensor1, Results1, doc["sensor1"].to<JsonArray>(), "sensor1", LPN_PIN1);
processSensorData(sensor2, Results2, doc["sensor2"].to<JsonArray>(), "sensor2", LPN_PIN2);
processSensorData(sensor3, Results3, doc["sensor3"].to<JsonArray>(), "sensor3", LPN_PIN3);
processSensorData(sensor4, Results4, doc["sensor4"].to<JsonArray>(), "sensor4", LPN_PIN4);
processSensorData(sensor5, Results5, doc["sensor5"].to<JsonArray>(), "sensor5", LPN_PIN5);
processSensorData(sensor6, Results6, doc["sensor6"].to<JsonArray>(), "sensor6", LPN_PIN6);
processSensorData(sensor7, Results7, doc["sensor7"].to<JsonArray>(), "sensor7", LPN_PIN7);
processSensorData(sensor8, Results8, doc["sensor8"].to<JsonArray>(), "sensor8", LPN_PIN8);
processSensorData(sensor9, Results9, doc["sensor9"].to<JsonArray>(), "sensor9", LPN_PIN0);
processSensorData(sensor10, Results10, doc["sensor10"].to<JsonArray>(), "sensor10", LPN_PIN1);
processSensorData(sensor11, Results11, doc["sensor11"].to<JsonArray>(), "sensor11", LPN_PIN2);
processSensorData(sensor12, Results12, doc["sensor12"].to<JsonArray>(), "sensor12", LPN_PIN3);
processSensorData(sensor13, Results13, doc["sensor13"].to<JsonArray>(), "sensor13", LPN_PIN4);
processSensorData(sensor14, Results14, doc["sensor14"].to<JsonArray>(), "sensor14", LPN_PIN5);
processSensorData(sensor15, Results15, doc["sensor15"].to<JsonArray>(), "sensor15", LPN_PIN6);
processSensorData(sensor16, Results16, doc["sensor16"].to<JsonArray>(), "sensor16", LPN_PIN7);
processSensorData(sensor17, Results17, doc["sensor17"].to<JsonArray>(), "sensor17", LPN_PIN8);
// Serialize the JSON document and print to Serial
serializeJson(doc, Serial);
Serial.println();
}
void i2cScanner(TwoWire DEV_I2C) {
for (byte address = 1; address < 127; address++) {
DEV_I2C.beginTransmission(address);
if (DEV_I2C.endTransmission() == 0) {
Serial.print("Device found at address 0x");
if (address < 0x10)
Serial.print("0");
Serial.println(address, HEX);
}
}
}
void initializeSensor(VL53L7CX &sensor, uint16_t sensorAddress, int lpnPin, PCF8575 &PCF, String PCF_Name) {
uint8_t status = VL53L7CX_STATUS_OK;
uint8_t isAlive = 0;
PCF.write(lpnPin, HIGH);
double x = PCF.read16();
x = log2(x);
Serial.println("Starting to initialize Sensor " + String((int)round(x)) + " on the " + PCF_Name + " half.");
// Set I2C address
status = sensor.vl53l7cx_set_i2c_address(sensorAddress << 1);
if (status != VL53L7CX_STATUS_OK) {
Serial.print("Failed to initialize Sensor " + String((int)round(x)) + " and received I2C Error:");
Serial.println(VL53L7CX_STATUS_ERROR);
PCF.write16(0x0000);
return;
}
status = sensor.vl53l7cx_is_alive(&isAlive);
if (!isAlive || status != VL53L7CX_STATUS_OK) {
Serial.print("Failed to initialize Sensor " + String((int)round(x)) + " and received is_alive Error:");
Serial.println(VL53L7CX_STATUS_ERROR);
PCF.write16(0x0000);
return;
}
// Init VL53L7CX sensor
status = sensor.vl53l7cx_init();
if (status != VL53L7CX_STATUS_OK) {
Serial.print("Failed to initialize Sensor " + String((int)round(x)) + " and received init Error:");
Serial.println(VL53L7CX_STATUS_ERROR);
PCF.write16(0x0000);
return;
}
delay(wait_for_i2c);
// Set resolution and frequency
sensor.vl53l7cx_set_resolution(VL53L7CX_RESOLUTION_8X8);
delay(wait_for_i2c);
sensor.vl53l7cx_set_ranging_frequency_hz(ranging_frequency);
delay(wait_for_i2c);
// Start ranging
sensor.vl53l7cx_start_ranging();
delay(wait_for_i2c);
sensorInitializedI2C1[lpnPin] = true;
sensor_count++;
// Activate sensor power
PCF.write16(0x0000);
}
void processSensorData(VL53L7CX &sensor, VL53L7CX_ResultsData &results, const JsonArray &data, const char *sensorKey, int lpn_pin) {
if (!sensorInitializedI2C1[lpn_pin]) return;
uint8_t NewDataReady = 0;
uint8_t status;
// Wait for data to be ready
do {
status = sensor.vl53l7cx_check_data_ready(&NewDataReady);
} while (!NewDataReady);
// Turn LED on to indicate data processing
digitalWrite(LedPin, HIGH);
// If data is ready, get the ranging data and store it in the JSON array
if ((!status) && (NewDataReady != 0)) {
status = sensor.vl53l7cx_get_ranging_data(&results);
JsonArray sensorData = doc[sensorKey].to<JsonArray>();
// Process the results and add them to the JSON array
for (int y = imageWidth * (imageWidth - 1); y >= 0; y -= imageWidth) {
for (int x = 0; x <= imageWidth - 1; x++) {
sensorData.add(results.distance_mm[VL53L7CX_NB_TARGET_PER_ZONE * (x + y)]);
}
}
}
// Turn LED off to indicate processing is done
digitalWrite(LedPin, LOW);
}
void setupPCF(PCF8575 &PCF, TwoWire DEV_I2C, int num) {
if (!PCF.begin(0x0000)) {
Serial.println("could not initialize...");
}
while (!PCF.isConnected()) {
i2cScanner(DEV_I2C);
delay(500);
PCF.begin(0x0000);
int x = PCF.lastError();
Serial.println("Error from I2C " + String(num) + " and Fehlercode: " + String(x));
}
PCF.write16(0xFFFF);
delay(wait_for_i2c);
PCF.write16(0x0000);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment