IoT-MBBR (masterthesis)
Masterthesis upgrading a Moving Bed BioReactor with extra sensors and cloud connectivity.
VL53L1X.h File Reference

Code for the VL53L1X sensor. More...

#include <stdint.h>
#include <stdbool.h>

Go to the source code of this file.

Functions

int8_t initVL53L1X (void)
 Initialize the sensor. More...
 
uint16_t measureVL53L1X (void)
 Get a sensor reading. More...
 

Detailed Description

Code for the VL53L1X sensor.

Version
1.0
Author
Brecht Van Eeckhoudt

License

Copyright (C) 2020 - Brecht Van Eeckhoudt

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

A copy of the GNU General Public License can be found in the LICENSE file along with this source code.


Some methods use code obtained from examples from Silicon Labs' GitHub. These sections are licensed under the Silabs License Agreement. See the file "Silabs_License_Agreement.txt" for details. Before using this software for any purpose, you must agree to the terms of that agreement.

Definition in file VL53L1X.h.

Function Documentation

◆ initVL53L1X()

int8_t initVL53L1X ( void  )

Initialize the sensor.

This method wait until the sensor is booted, initializes it and gets offset values.

Returns
  • 0 - Calibration successful.
  • -1 - Calibration failed.

Definition at line 69 of file VL43L1X.c.

70 {
71  /* Returned status */
72  int8_t status;
73 
74  /* Wait for device booted */
75  uint8_t bootState = 0;
76  uint8_t exitWhile = 0;
77 
78  while ((bootState != 1) && (exitWhile < 5))
79  {
80  status = VL53L1X_BootState(0x52, &bootState);
81 
82 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
83  if (status != 0) dbcritInt("VL53L1X_BootState status = ", status, "");
84  dbinfoInt("VL53L1X_BootState = ", bootState, "");
85 #endif /* DEBUG_DBPRINT */
86 
87  /* 1s delay */
88  delay(1000);
89 
90  /* Functionality so we don't end up forever in this loop */
91  exitWhile++;
92  }
93 
94  /* Initialize sensor */
95  status = VL53L1X_SensorInit(0x52);
96 
97 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
98  if (status != 0) dbcritInt("VL53L1X_SensorInit status = ", status, "");
99 #endif /* DEBUG_DBPRINT */
100 
101  /* Get offset */
102  status = VL53L1X_GetOffset(0x52, &offset);
103 
104 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
105  if (status != 0) dbcritInt("VL53L1X_GetOffset status = ", status, "");
106  dbwarnInt("VL53L1X_GetOffset = ", offset, " mm");
107 #endif /* DEBUG_DBPRINT */
108 
109  /* Get Xtalk */
110  status = VL53L1X_GetXtalk(0x52, &xtalk);
111 
112 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
113  if (status != 0) dbcritInt("VL53L1X_GetXtalk status = ", status, "");
114  dbwarnInt("VL53L1X_GetXtalk = ", xtalk, " cps");
115 #endif /* DEBUG_DBPRINT */
116 
117  /* Configure distance mode to short (1.3m), better ambient immunity */
118  status = VL53L1X_SetDistanceMode(0x52, 2); /* default = long (4m in the dark with 200 ms timing budget) */
119 
120 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
121 #if DBPRINT_VERBOSE == 1 /* DBPRINT_VERBOSE */
122  if (status != 0) dbcritInt("VL53L1X_SetDistanceMode status = ", status, "");
123 #endif /* DBPRINT_VERBOSE */
124 #endif /* DEBUG_DBPRINT */
125 
126  /* Configure inter-measurement period to 500 ms (IMP, has to be the same or greater than the timing budget) */
127  status = VL53L1X_SetInterMeasurementInMs(0x52, 500); /* default = 100 ms */
128 
129 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
130  if (status != 0) dbcritInt("VL53L1X_SetInterMeasurementInMs status = ", status, "");
131 #endif /* DEBUG_DBPRINT */
132 
133  /* Configure timing budget to 200 ms */
134  status = VL53L1X_SetTimingBudgetInMs(0x52, 200); /* default = 100 ms */
135 
136 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
137  if (status != 0) dbcritInt("VL53L1X_SetTimingBudgetInMs status = ", status, "");
138 #endif /* DEBUG_DBPRINT */
139 
140  /* Get sensor ID */
141  uint16_t id; // must be 0xEEAC ??
142  status = VL53L1X_GetSensorId(0x52, &id);
143 
144 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
145  if (status != 0) dbcritInt("VL53L1X_GetSensorId status = ", status, "");
146  dbinfoInt_hex("VL53L1X_GetSensorId = ", id, "");
147 #endif /* DEBUG_DBPRINT */
148 
149  return (status);
150 }
void dbinfoInt_hex(char *message1, int32_t value, char *message2)
Print an info value surrounded by two strings (char array) to USARTx.
Definition: dbprint.c:661
void dbinfoInt(char *message1, int32_t value, char *message2)
Print an info value surrounded by two strings (char array) to USARTx.
Definition: dbprint.c:569
int16_t offset
Definition: VL43L1X.c:52
signed char int8_t
Typedef defining 8 bit char type. The developer should modify this to suit the platform being deploye...
Definition: vl53l1_types.h:103
uint16_t xtalk
Definition: VL43L1X.c:53
void dbcritInt(char *message1, int32_t value, char *message2)
Print a critical value surrounded by two strings (char array) to USARTx.
Definition: dbprint.c:631
void dbwarnInt(char *message1, int32_t value, char *message2)
Print a warning value surrounded by two strings (char array) to USARTx.
Definition: dbprint.c:600
VL53L1X_ERROR VL53L1X_GetXtalk(uint16_t dev, uint16_t *xtalk)
This function returns the current programmed xtalk correction value in cps.
Definition: VL53L1X_api.c:685
VL53L1X_ERROR VL53L1X_SetDistanceMode(uint16_t dev, uint16_t DM)
This function programs the distance mode (1=short, 2=long(default)). Short mode max distance is limit...
Definition: VL53L1X_api.c:452
VL53L1X_ERROR VL53L1X_GetOffset(uint16_t dev, int16_t *offset)
This function returns the programmed offset correction value in mm.
Definition: VL53L1X_api.c:658
VL53L1X_ERROR VL53L1X_GetSensorId(uint16_t dev, uint16_t *sensorId)
This function returns the sensor id, sensor Id must be 0xEEAC.
Definition: VL53L1X_api.c:536
void delay(uint32_t msDelay)
Wait for a certain amount of milliseconds in EM2/3.
Definition: delay.c:124
VL53L1X_ERROR VL53L1X_SensorInit(uint16_t dev)
This function loads the 135 bytes default values to initialize the sensor.
Definition: VL53L1X_api.c:225
VL53L1X_ERROR VL53L1X_SetTimingBudgetInMs(uint16_t dev, uint16_t TimingBudgetInMs)
This function programs the timing budget in ms. Predefined values = 15, 20, 33, 50, 100(default), 200, 500.
Definition: VL53L1X_api.c:309
VL53L1X_ERROR VL53L1X_SetInterMeasurementInMs(uint16_t dev, uint32_t InterMeasMs)
This function programs the Intermeasurement period in ms Intermeasurement period must be >/= timing b...
Definition: VL53L1X_api.c:499
unsigned short uint16_t
Typedef defining 16 bit unsigned short type. The developer should modify this to suit the platform be...
Definition: vl53l1_types.h:88
unsigned char uint8_t
Typedef defining 8 bit unsigned char type. The developer should modify this to suit the platform bein...
Definition: vl53l1_types.h:98
VL53L1X_ERROR VL53L1X_BootState(uint16_t dev, uint8_t *state)
This function returns the boot state of the device (1:booted, 0:not booted)
Definition: VL53L1X_api.c:526

◆ measureVL53L1X()

uint16_t measureVL53L1X ( void  )

Get a sensor reading.

Returns
The measured distance in mm.

Definition at line 160 of file VL43L1X.c.

161 {
162  /* Start ranging */
163  int8_t status = VL53L1X_StartRanging(0x52);
164 
165 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
166  if (status != 0) dbcritInt("VL53L1X_StartRanging status = ", status, "");
167 #endif /* DEBUG_DBPRINT */
168 
169  uint8_t dataReady;
170  uint16_t distance;
171 
172  /* Wait for data ready */
173  while (dataReady != 1)
174  {
175  status = VL53L1X_CheckForDataReady(0x52, &dataReady);
176 
177 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
178  if (status != 0) dbcritInt("VL53L1X_CheckForDataReady status = ", status, "");
179 #endif /* DEBUG_DBPRINT */
180 
181  }
182 
183  /* Get range status */
184  uint8_t state;
185  status = VL53L1X_GetRangeStatus(0x52, &state);
186 
187 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
188  if (status != 0) dbcritInt("VL53L1X_GetRangeStatus status = ", status, "");
189  if (state != 0) dbcritInt("VL53L1X_GetRangeStatus = ", state, "");
190 #endif /* DEBUG_DBPRINT */
191 
192  /* Get distance */
193  status = VL53L1X_GetDistance(0x52, &distance);
194 
195 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
196  if (status != 0) dbcritInt("VL53L1X_GetDistance status = ", status, "");
197 #endif /* DEBUG_DBPRINT */
198 
199  /* Clear interrupt */
200  status = VL53L1X_ClearInterrupt(0x52);
201 
202 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
203  if (status != 0) dbcritInt("VL53L1X_ClearInterrupt status = ", status, "");
204 #endif /* DEBUG_DBPRINT */
205 
206  return (distance);
207 }
VL53L1X_ERROR VL53L1X_StartRanging(uint16_t dev)
This function starts the ranging distance operation The ranging operation is continuous. The clear interrupt has to be done after each get data to allow the interrupt to raise when the next data is ready 1=active high (default), 0=active low, use SetInterruptPolarity() to change the interrupt polarity if required.
Definition: VL53L1X_api.c:275
signed char int8_t
Typedef defining 8 bit char type. The developer should modify this to suit the platform being deploye...
Definition: vl53l1_types.h:103
void dbcritInt(char *message1, int32_t value, char *message2)
Print a critical value surrounded by two strings (char array) to USARTx.
Definition: dbprint.c:631
VL53L1X_ERROR VL53L1X_GetDistance(uint16_t dev, uint16_t *distance)
This function returns the distance measured by the sensor in mm.
Definition: VL53L1X_api.c:546
VL53L1X_ERROR VL53L1X_GetRangeStatus(uint16_t dev, uint8_t *rangeStatus)
This function returns the ranging status error (0:no error, 1:sigma failed, 2:signal failed...
Definition: VL53L1X_api.c:613
VL53L1X_ERROR VL53L1X_CheckForDataReady(uint16_t dev, uint8_t *isDataReady)
This function checks if the new ranging data is available by polling the dedicated register...
Definition: VL53L1X_api.c:291
VL53L1X_ERROR VL53L1X_ClearInterrupt(uint16_t dev)
This function clears the interrupt, to be called after a ranging data reading to arm the interrupt fo...
Definition: VL53L1X_api.c:245
unsigned short uint16_t
Typedef defining 16 bit unsigned short type. The developer should modify this to suit the platform be...
Definition: vl53l1_types.h:88
unsigned char uint8_t
Typedef defining 8 bit unsigned char type. The developer should modify this to suit the platform bein...
Definition: vl53l1_types.h:98