pub struct VL53L4cd<I2C, D> { /* private fields */ }Expand description
VL53L4CD ultra-low-power time-of-flight distance sensor driver.
This struct provides an async interface to control and read data from the VL53L4CD sensor. It manages the I2C communication, sensor configuration, and ranging operations.
The driver is generic over the I2C and delay implementations, allowing it to work with any embedded-hal-async compatible hardware.
Implementations§
Source§impl<I2C, E, D> VL53L4cd<I2C, D>
impl<I2C, E, D> VL53L4cd<I2C, D>
Sourcepub fn new(i2c: I2C, delay: D) -> Self
pub fn new(i2c: I2C, delay: D) -> Self
Creates a new VL53L4CD sensor driver instance.
This function initializes a new sensor driver with the default I2C address (0x29)
and the provided I2C and delay implementations. The sensor is not yet initialized
and must be configured using sensor_init before use.
§Arguments
i2c- I2C interface implementation for sensor communicationdelay- Delay implementation for timing operations
§Returns
A new VL53L4cd instance with default configuration
§Examples
use vl53l4cd_ulp::VL53L4cd;
use embedded_hal::{i2c::I2c, delay::DelayNs};
let i2c = embedded_hal_mock::eh1::i2c::Mock::new(&[]);
let delay = embedded_hal_mock::eh1::delay::NoopDelay;
let mut sensor = VL53L4cd::new(i2c, delay);§Default Configuration
- I2C Address: 0x29 (default sensor address)
- Sensor State: Uninitialized (must call
sensor_init()) - Ranging Mode: Stopped
Sourcepub fn set_i2c_address(&mut self, address: u8) -> Result<(), Error<E>>
pub fn set_i2c_address(&mut self, address: u8) -> Result<(), Error<E>>
Sets the I2C address of the sensor.
This function writes a new I2C slave address to the sensor’s internal register. The new address will take effect after the sensor is reset or reinitialized.
Note: The address change only takes effect when the sensor is in a reset state. According to the VL53L4CD Application Note, to change the I2C address, the host must:
- Put the device in HW standby by setting the XSHUT pin low
- Raise the XSHUT pin
- Call
set_i2c_address(new_address)to program the new address, - call
sensor_init()to initialize the sensor on the new address
The current driver instance will continue to use the old address until reinitialization.
§Arguments
address- The new 7-bit I2C address
§Returns
Ok(())- If the address was set successfully
§Errors
Err(Error::I2cError(E))- If there was an I2C communication error
§Examples
use vl53l4cd_ulp::VL53L4cd;
let i2c = embedded_hal_mock::eh1::i2c::Mock::new(&[]);
let delay = embedded_hal_mock::eh1::delay::NoopDelay;
let mut sensor = VL53L4cd::new(i2c, delay);
// Change sensor address to 0x30
sensor.set_i2c_address(0x30).unwrap();
// Reinitialize to use new address
sensor.sensor_init().unwrap();Sourcepub fn get_sensor_id(&mut self) -> Result<u16, Error<E>>
pub fn get_sensor_id(&mut self) -> Result<u16, Error<E>>
Retrieves the sensor identification model ID.
This function reads the sensor’s model identification register to verify that the correct sensor is connected and responding. The VL53L4CD should return a specific model ID value.
§Returns
Ok(u16)- The sensor model ID (expected value for VL53L4CD)
§Errors
Err(Error::I2cError(E))- If there was an I2C communication error
§Examples
use vl53l4cd_ulp::VL53L4cd;
let i2c = embedded_hal_mock::eh1::i2c::Mock::new(&[]);
let delay = embedded_hal_mock::eh1::delay::NoopDelay;
let mut sensor = VL53L4cd::new(i2c, delay);
sensor.sensor_init().unwrap();
let sensor_id = sensor.get_sensor_id().unwrap();
println!("Sensor ID: 0x{:04X}", sensor_id);
// Verify it's the correct sensor
if sensor_id == 0xEACC { // Expected VL53L4CD model ID
println!("VL53L4CD sensor detected");
} else {
println!("Unexpected sensor ID: 0x{:04X}", sensor_id);
}Sourcepub fn sensor_init(&mut self) -> Result<(), Error<E>>
pub fn sensor_init(&mut self) -> Result<(), Error<E>>
Initializes the VL53L4CD sensor for operation.
This function performs the complete sensor initialization sequence.
Important: This function must be called before any ranging operations. The sensor will not function correctly without proper initialization.
§Returns
Ok(())- If the sensor was initialized successfully
§Errors
Err(Error::Timeout)- If the sensor did not boot within 1 secondErr(Error::I2cError(E))- If there was an I2C communication error
§Examples
use vl53l4cd_ulp::VL53L4cd;
let i2c = embedded_hal_mock::eh1::i2c::Mock::new(&[]);
let delay = embedded_hal_mock::eh1::delay::NoopDelay;
let mut sensor = VL53L4cd::new(i2c, delay);
// Initialize the sensor
sensor.sensor_init().unwrap();
println!("Sensor initialized successfully");
// Now the sensor is ready for ranging operations
sensor.start_ranging().unwrap();Sourcepub fn check_for_data_ready(&mut self) -> Result<bool, Error<E>>
pub fn check_for_data_ready(&mut self) -> Result<bool, Error<E>>
Checks if new ranging data is ready for retrieval.
This function determines whether the sensor has completed a ranging measurement and new data is available. It checks the interrupt status by first determining the interrupt polarity configuration, then reading the actual interrupt status.
Note: This function only checks the status - it does not clear the interrupt.
Use clear_interrupt after reading the data to reset
the interrupt condition.
§Returns
Ok(true)- New ranging data is availableOk(false)- No new data available yet
§Errors
Err(Error::I2cError(E))- If there was an I2C communication error
§Examples
use vl53l4cd_ulp::VL53L4cd;
let i2c = embedded_hal_mock::eh1::i2c::Mock::new(&[]);
let delay = embedded_hal_mock::eh1::delay::NoopDelay;
let mut sensor = VL53L4cd::new(i2c, delay);
sensor.sensor_init().unwrap();
// Start ranging
sensor.start_ranging().unwrap();
// Poll for data ready
loop {
if sensor.check_for_data_ready().unwrap() {
let measurement = sensor.get_estimated_measurement().unwrap();
println!("Distance: {} mm", measurement.estimated_distance_mm);
sensor.clear_interrupt().unwrap();
break;
}
}Sourcepub fn clear_interrupt(&mut self) -> Result<(), Error<E>>
pub fn clear_interrupt(&mut self) -> Result<(), Error<E>>
Clears the sensor interrupt flag.
This function clears the interrupt condition by writing to the interrupt clear register. It should be called after reading measurement data to reset the interrupt state and prepare for the next measurement.
Important: Always call this function after reading data when using interrupt-driven operation. Failure to clear the interrupt will prevent new interrupts from being generated.
§Returns
Ok(())- If the interrupt was cleared successfully
§Errors
Err(Error::I2cError(E))- If there was an I2C communication error
§Examples
use vl53l4cd_ulp::VL53L4cd;
let i2c = embedded_hal_mock::eh1::i2c::Mock::new(&[]);
let delay = embedded_hal_mock::eh1::delay::NoopDelay;
let mut sensor = VL53L4cd::new(i2c, delay);
sensor.sensor_init().unwrap();
// Check if data is ready
if sensor.check_for_data_ready().unwrap() {
// Read the measurement data
let measurement = sensor.get_estimated_measurement().unwrap();
println!("Distance: {} mm", measurement.estimated_distance_mm);
// Clear the interrupt to prepare for next measurement
sensor.clear_interrupt().unwrap();
}Sourcepub fn start_ranging_single_shot(&mut self) -> Result<(), Error<E>>
pub fn start_ranging_single_shot(&mut self) -> Result<(), Error<E>>
Starts a single-shot ranging measurement.
This function initiates a single ranging measurement. The sensor will perform one complete ranging cycle and then automatically stop. This mode is useful for applications that need occasional distance measurements without continuous operation.
Note: The sensor will automatically stop ranging after completing the measurement.
No need to call stop_ranging for single-shot mode.
§Returns
Ok(())- If ranging was started successfully
§Errors
Err(Error::I2cError(E))- If there was an I2C communication error
§Examples
use vl53l4cd_ulp::VL53L4cd;
let i2c = embedded_hal_mock::eh1::i2c::Mock::new(&[]);
let delay = embedded_hal_mock::eh1::delay::NoopDelay;
let mut sensor = VL53L4cd::new(i2c, delay);
sensor.sensor_init().unwrap();
// Start single-shot ranging
sensor.start_ranging_single_shot().unwrap();
// Wait for data to be ready
loop {
if sensor.check_for_data_ready().unwrap() {
let measurement = sensor.get_estimated_measurement().unwrap();
println!("Single-shot distance: {} mm", measurement.estimated_distance_mm);
sensor.clear_interrupt().unwrap();
break;
}
}Sourcepub fn start_ranging(&mut self) -> Result<(), Error<E>>
pub fn start_ranging(&mut self) -> Result<(), Error<E>>
Starts continuous ranging measurements.
This function initiates continuous ranging mode where the sensor performs
measurements continuously at the configured inter-measurement interval.
The sensor will continue ranging until explicitly stopped with
stop_ranging.
§Returns
Ok(())- If ranging was started successfully
§Errors
Err(Error::I2cError(E))- If there was an I2C communication error
§Examples
use vl53l4cd_ulp::VL53L4cd;
let i2c = embedded_hal_mock::eh1::i2c::Mock::new(&[]);
let delay = embedded_hal_mock::eh1::delay::NoopDelay;
let mut sensor = VL53L4cd::new(i2c, delay);
sensor.sensor_init().unwrap();
// Start continuous ranging
sensor.start_ranging().unwrap();
// Continuous measurement loop
for _ in 0..10 {
if sensor.check_for_data_ready().unwrap() {
let measurement = sensor.get_estimated_measurement().unwrap();
println!("Distance: {} mm", measurement.estimated_distance_mm);
sensor.clear_interrupt().unwrap();
}
}
// Stop ranging when done
sensor.stop_ranging().unwrap();Sourcepub fn stop_ranging(&mut self) -> Result<(), Error<E>>
pub fn stop_ranging(&mut self) -> Result<(), Error<E>>
Stops ranging measurements.
This function stops the sensor from performing ranging measurements. It should be called when ranging is no longer needed to conserve power and prepare the sensor for low-power modes.
§Returns
Ok(())- If ranging was stopped successfully
§Errors
Err(Error::I2cError(E))- If there was an I2C communication error
§Examples
use vl53l4cd_ulp::VL53L4cd;
let i2c = embedded_hal_mock::eh1::i2c::Mock::new(&[]);
let delay = embedded_hal_mock::eh1::delay::NoopDelay;
let mut sensor = VL53L4cd::new(i2c, delay);
sensor.sensor_init().unwrap();
// Start ranging
sensor.start_ranging().unwrap();
// Perform some measurements
for _ in 0..5 {
if sensor.check_for_data_ready().unwrap() {
let measurement = sensor.get_estimated_measurement().unwrap();
println!("Distance: {} mm", measurement.estimated_distance_mm);
sensor.clear_interrupt().unwrap();
}
}
// Stop ranging when done
sensor.stop_ranging().unwrap();Sourcepub fn get_estimated_measurement(
&mut self,
) -> Result<EstimatedMeasurement, Error<E>>
pub fn get_estimated_measurement( &mut self, ) -> Result<EstimatedMeasurement, Error<E>>
Get the estimated measurement from the sensor.
This function reads all the measurement data from the sensor’s result registers and returns a comprehensive measurement result including distance, quality indicators, and environmental data.
Note: This function should only be called after confirming that new data
is available using check_for_data_ready or
using an interrupt pin to detect when new data is available.
§Returns
Ok(EstimatedMeasurement)- The complete measurement data
§Errors
Err(Error::I2cError(E))- If there was an I2C communication error
§Examples
use vl53l4cd_ulp::VL53L4cd;
let i2c = embedded_hal_mock::eh1::i2c::Mock::new(&[]);
let delay = embedded_hal_mock::eh1::delay::NoopDelay;
let mut sensor = VL53L4cd::new(i2c, delay);
sensor.sensor_init().unwrap();
if sensor.check_for_data_ready().unwrap() {
let measurement = sensor.get_estimated_measurement().unwrap();
if measurement.measurement_status == 0 {
println!("Distance: {} mm", measurement.estimated_distance_mm);
println!("Signal strength: {} kcps", measurement.signal_kcps);
println!("Ambient light: {} kcps", measurement.ambient_kcps);
println!("Measurement precision: ±{} mm", measurement.sigma_mm);
} else {
println!("Measurement failed with status: {}", measurement.measurement_status);
}
sensor.clear_interrupt().unwrap();
}Sourcepub fn set_macro_timing(&mut self, macro_timing: u16) -> Result<(), Error<E>>
pub fn set_macro_timing(&mut self, macro_timing: u16) -> Result<(), Error<E>>
Sets the macro timing for ranging measurements.
This function configures the timing parameters that control the duration and precision of ranging measurements. Higher values provide better precision but increase measurement time and power consumption.
§Arguments
macro_timing- Macro timing value (1-255)
§Returns
Ok(())- If the timing was set successfully
§Errors
Err(Error::InvalidArgument)- If the value is outside valid rangeErr(Error::I2cError(E))- If there was an I2C communication error
§Examples
use vl53l4cd_ulp::VL53L4cd;
let i2c = embedded_hal_mock::eh1::i2c::Mock::new(&[]);
let delay = embedded_hal_mock::eh1::delay::NoopDelay;
let mut sensor = VL53L4cd::new(i2c, delay);
sensor.sensor_init().unwrap();
// Set macro timing for high precision
sensor.set_macro_timing(200).unwrap();
// Start ranging with new timing
sensor.start_ranging().unwrap();Sourcepub fn get_macro_timing(&mut self) -> Result<u16, Error<E>>
pub fn get_macro_timing(&mut self) -> Result<u16, Error<E>>
Gets the current macro timing configuration.
This function reads the current macro timing value from the sensor. The macro timing controls the duration and precision of ranging measurements, with higher values providing better precision but longer measurement times.
§Returns
Ok(u16)- Current macro timing value (1-255)
§Errors
Err(Error::I2cError(E))- If there was an I2C communication error
§Examples
use vl53l4cd_ulp::VL53L4cd;
let i2c = embedded_hal_mock::eh1::i2c::Mock::new(&[]);
let delay = embedded_hal_mock::eh1::delay::NoopDelay;
let mut sensor = VL53L4cd::new(i2c, delay);
sensor.sensor_init().unwrap();
// Get current macro timing
let current_timing = sensor.get_macro_timing().unwrap();
println!("Current macro timing: {}", current_timing);
// Adjust timing if needed
if current_timing < 100 {
println!("Timing is low, consider increasing for better precision");
sensor.set_macro_timing(150).unwrap();
}Sourcepub fn set_inter_measurement_in_ms(
&mut self,
inter_measurement_ms: u32,
) -> Result<(), Error<E>>
pub fn set_inter_measurement_in_ms( &mut self, inter_measurement_ms: u32, ) -> Result<(), Error<E>>
Sets the inter-measurement period in milliseconds.
This function configures the time interval between consecutive ranging measurements in continuous mode. The sensor will wait this duration after completing one measurement before starting the next.
Note: This setting only affects continuous ranging mode. Single-shot mode ignores this setting and performs one measurement immediately.
§Arguments
inter_measurement_ms- Time between measurements in milliseconds (1-65535)
§Returns
Ok(())- If the interval was set successfully
§Errors
Err(Error::InvalidArgument)- If the value is outside valid rangeErr(Error::I2cError(E))- If there was an I2C communication error
§Examples
use vl53l4cd_ulp::VL53L4cd;
let i2c = embedded_hal_mock::eh1::i2c::Mock::new(&[]);
let delay = embedded_hal_mock::eh1::delay::NoopDelay;
let mut sensor = VL53L4cd::new(i2c, delay);
sensor.sensor_init().unwrap();
// Set measurement interval to 100ms for high-frequency updates
sensor.set_inter_measurement_in_ms(100).unwrap();
// Start continuous ranging with 100ms interval
sensor.start_ranging().unwrap();
// Now measurements will occur every 100msSourcepub fn get_inter_measurement_in_ms(&mut self) -> Result<u32, Error<E>>
pub fn get_inter_measurement_in_ms(&mut self) -> Result<u32, Error<E>>
Gets the current inter-measurement period.
This function reads the current inter-measurement interval from the sensor. The inter-measurement period defines the time between consecutive ranging measurements in continuous mode, measured in milliseconds.
§Returns
Ok(u32)- Current inter-measurement period in milliseconds
§Errors
Err(Error::I2cError(E))- If there was an I2C communication error
§Examples
use vl53l4cd_ulp::VL53L4cd;
let i2c = embedded_hal_mock::eh1::i2c::Mock::new(&[]);
let delay = embedded_hal_mock::eh1::delay::NoopDelay;
let mut sensor = VL53L4cd::new(i2c, delay);
sensor.sensor_init().unwrap();
// Get current measurement interval
let current_interval = sensor.get_inter_measurement_in_ms().unwrap();
println!("Current measurement interval: {} ms", current_interval);
// Adjust interval if needed for power optimization
if current_interval < 500 {
println!("Interval is quite short, consider increasing for battery life");
sensor.set_inter_measurement_in_ms(1000).unwrap();
}Sourcepub fn set_roi(&mut self, roi_width: u8) -> Result<(), Error<E>>
pub fn set_roi(&mut self, roi_width: u8) -> Result<(), Error<E>>
Sets the Region of Interest (ROI) for the sensor.
This function configures the number of SPADs (Single Photon Avalanche Diodes) used for ranging measurements. The ROI function allows some SPADs to be disabled, which affects both power consumption and maximum ranging distance.
Important Notes:
- Changing the SPAD number has no effect on the field of view.
- The sensor defaults to 16x16 mode (maximum SPADs)
- ST recommends changing SPAD number only if current consumption below 75 μA is desired
- Using minimum ROI (4x4) typically reduces current consumption by -10 μA during ranging
- Maximum ranging distance impact depends on reflectance, ambient light, and macroperiod
- In some conditions, minimum ROI can reduce maximum ranging distance by up to -50%
§Arguments
roi_width- The ROI width in pixels (4-16, where 4x4 = minimum, 16x16 = maximum)
§Returns
Ok(())- If the ROI was set successfully
§Errors
Err(Error::I2cError(E))- If there was an I2C communication error or invalid argument
§Examples
use vl53l4cd_ulp::VL53L4cd;
let i2c = embedded_hal_mock::eh1::i2c::Mock::new(&[]);
let delay = embedded_hal_mock::eh1::delay::NoopDelay;
let mut sensor = VL53L4cd::new(i2c, delay);
sensor.sensor_init().unwrap();
// Set ROI to 4x4 for minimum power consumption (may reduce max range by up to 50%)
sensor.set_roi(4).unwrap();
// Or set to 8x8 for balanced power and range performance
sensor.set_roi(8).unwrap();Sourcepub fn get_roi(&mut self) -> Result<u8, Error<E>>
pub fn get_roi(&mut self) -> Result<u8, Error<E>>
Gets the current Region of Interest (ROI) width setting.
This function reads the current ROI width configuration from the sensor. The returned value represents the ROI width in pixels, which affects the sensor’s measurement area and field of view.
§Returns
Ok(u8)- The current ROI width in pixels (4-16)
§Errors
Err(Error::I2cError(E))- If there was an I2C communication error
§Examples
use vl53l4cd_ulp::VL53L4cd;
let i2c = embedded_hal_mock::eh1::i2c::Mock::new(&[]);
let delay = embedded_hal_mock::eh1::delay::NoopDelay;
let mut sensor = VL53L4cd::new(i2c, delay);
sensor.sensor_init().unwrap();
// Get current ROI width
let roi_width = sensor.get_roi().unwrap();
println!("Current ROI width: {} pixels", roi_width);Sourcepub fn set_interrupt_configuration(
&mut self,
low_threshold_mm: u16,
high_threshold_mm: u16,
interrupt_on: InterruptOn,
) -> Result<(), Error<E>>
pub fn set_interrupt_configuration( &mut self, low_threshold_mm: u16, high_threshold_mm: u16, interrupt_on: InterruptOn, ) -> Result<(), Error<E>>
Sets the interrupt configuration and distance thresholds.
This function configures the sensor’s interrupt behavior and sets distance thresholds that determine when interrupts are generated. The interrupt can be configured to trigger on new sample ready, when distance is within a threshold window, or when distance is outside the threshold window.
Note: The low threshold must be less than or equal to the high threshold for proper operation. Invalid threshold combinations may result in unexpected interrupt behavior.
§Arguments
low_threshold_mm- Lower distance threshold in millimeters (0-4000)high_threshold_mm- Upper distance threshold in millimeters (0-4000)interrupt_on- Interrupt trigger condition (InterruptOn)
§Returns
Ok(())- If the interrupt configuration was set successfully
§Errors
Err(Error::I2cError(E))- If there was an I2C communication error
§Examples
use vl53l4cd_ulp::{VL53L4cd, InterruptOn};
let i2c = embedded_hal_mock::eh1::i2c::Mock::new(&[]);
let delay = embedded_hal_mock::eh1::delay::NoopDelay;
let mut sensor = VL53L4cd::new(i2c, delay);
sensor.sensor_init().unwrap();
// Configure interrupt to trigger when distance is between 100-500mm
sensor.set_interrupt_configuration(100, 500, InterruptOn::InWindow).unwrap();Sourcepub fn get_interrupt_configuration(
&mut self,
) -> Result<(u16, InterruptOn), Error<E>>
pub fn get_interrupt_configuration( &mut self, ) -> Result<(u16, InterruptOn), Error<E>>
Gets the current interrupt configuration and threshold settings.
This function reads the current interrupt configuration and high distance threshold from the sensor. It returns both the threshold value and the interrupt mode in a single call.
§Returns
Ok((u16, InterruptOn))- Tuple of (high_threshold_mm,interrupt_mode)
§Errors
Err(Error::I2cError(E))- If there was an I2C communication error
§Examples
use vl53l4cd_ulp::{VL53L4cd, InterruptOn};
let i2c = embedded_hal_mock::eh1::i2c::Mock::new(&[]);
let delay = embedded_hal_mock::eh1::delay::NoopDelay;
let mut sensor = VL53L4cd::new(i2c, delay);
sensor.sensor_init().unwrap();
// Get current interrupt configuration and threshold
let (threshold, mode) = sensor.get_interrupt_configuration().unwrap();
println!("High threshold: {} mm", threshold);
match mode {
InterruptOn::NewSampleReady => println!("Interrupt on new sample ready"),
InterruptOn::InWindow => println!("Interrupt when distance in threshold window"),
InterruptOn::OutOfWindow => println!("Interrupt when distance outside threshold window"),
_ => println!("Other interrupt mode: {:?}", mode),
}Sourcepub fn set_signal_threshold(&mut self, signal_kcps: u16) -> Result<(), Error<E>>
pub fn set_signal_threshold(&mut self, signal_kcps: u16) -> Result<(), Error<E>>
Sets the signal rate threshold for ranging measurements.
This function configures the minimum signal rate required for a valid ranging measurement. Measurements with signal rates below this threshold will be considered unreliable or invalid.
Note: The signal threshold helps filter out weak or noisy measurements, improving overall ranging reliability at the cost of potentially missing some distant or low-reflectivity targets.
§Arguments
signal_kcps- Minimum signal rate in kilocounts per second (0-65535)
§Returns
Ok(())- If the threshold was set successfully
§Errors
Err(Error::I2cError(E))- If there was an I2C communication error
§Examples
use vl53l4cd_ulp::VL53L4cd;
let i2c = embedded_hal_mock::eh1::i2c::Mock::new(&[]);
let delay = embedded_hal_mock::eh1::delay::NoopDelay;
let mut sensor = VL53L4cd::new(i2c, delay);
sensor.sensor_init().unwrap();
// Set signal threshold for reliable measurements
sensor.set_signal_threshold(100).unwrap(); // 100 kcps minimum
// Start ranging with signal threshold
sensor.start_ranging().unwrap();Sourcepub fn get_signal_threshold(&mut self) -> Result<u16, Error<E>>
pub fn get_signal_threshold(&mut self) -> Result<u16, Error<E>>
Gets the current signal rate threshold setting.
This function reads the current signal rate threshold from the sensor. The signal threshold defines the minimum signal rate required for valid ranging measurements, helping filter out weak or noisy readings.
§Returns
Ok(u16)- Current signal threshold in kilocounts per second
§Errors
Err(Error::I2cError(E))- If there was an I2C communication error
§Examples
use vl53l4cd_ulp::VL53L4cd;
let i2c = embedded_hal_mock::eh1::i2c::Mock::new(&[]);
let delay = embedded_hal_mock::eh1::delay::NoopDelay;
let mut sensor = VL53L4cd::new(i2c, delay);
sensor.sensor_init().unwrap();
// Get current signal threshold
let current_threshold = sensor.get_signal_threshold().unwrap();
println!("Current signal threshold: {} kcps", current_threshold);
// Adjust threshold if needed
if current_threshold < 50 {
println!("Threshold is quite low, consider increasing for reliability");
sensor.set_signal_threshold(100).unwrap();
}Sourcepub fn set_sigma_threshold(&mut self, sigma_mm: u16) -> Result<(), Error<E>>
pub fn set_sigma_threshold(&mut self, sigma_mm: u16) -> Result<(), Error<E>>
Sets the sigma threshold for ranging measurements.
This function configures the maximum acceptable measurement uncertainty (sigma) for valid ranging results. Measurements with sigma values above this threshold will be considered unreliable due to poor precision.
Note: The sigma threshold helps filter out imprecise measurements, improving overall ranging accuracy at the cost of potentially rejecting some valid but noisy measurements.
§Arguments
sigma_mm- Maximum acceptable sigma value in millimeters (0-65535)
§Returns
Ok(())- If the threshold was set successfully
§Errors
Err(Error::I2cError(E))- If there was an I2C communication error
§Examples
use vl53l4cd_ulp::VL53L4cd;
let i2c = embedded_hal_mock::eh1::i2c::Mock::new(&[]);
let delay = embedded_hal_mock::eh1::delay::NoopDelay;
let mut sensor = VL53L4cd::new(i2c, delay);
sensor.sensor_init().unwrap();
// Set sigma threshold for high-precision measurements
sensor.set_sigma_threshold(50).unwrap(); // 50mm maximum uncertainty
// Start ranging with precision threshold
sensor.start_ranging().unwrap();Sourcepub fn get_sigma_threshold(&mut self) -> Result<u16, Error<E>>
pub fn get_sigma_threshold(&mut self) -> Result<u16, Error<E>>
Gets the current sigma threshold setting.
This function reads the current sigma threshold from the sensor. The sigma threshold defines the maximum acceptable measurement uncertainty for valid ranging results, helping filter out imprecise measurements.
§Returns
Ok(u16)- Current sigma threshold in millimeters
§Errors
Err(Error::I2cError(E))- If there was an I2C communication error
§Examples
use vl53l4cd_ulp::VL53L4cd;
let i2c = embedded_hal_mock::eh1::i2c::Mock::new(&[]);
let delay = embedded_hal_mock::eh1::delay::NoopDelay;
let mut sensor = VL53L4cd::new(i2c, delay);
sensor.sensor_init().unwrap();
// Get current sigma threshold
let current_threshold = sensor.get_sigma_threshold().unwrap();
println!("Current sigma threshold: {} mm", current_threshold);
// Adjust threshold if needed
if current_threshold > 100 {
println!("Threshold is quite high, consider decreasing for precision");
sensor.set_sigma_threshold(50).unwrap();
}Sourcepub fn write_byte<R>(
&mut self,
register_address: R,
value: u8,
) -> Result<(), Error<E>>
pub fn write_byte<R>( &mut self, register_address: R, value: u8, ) -> Result<(), Error<E>>
Writes a single byte to a sensor register.
This is a low-level function that writes an 8-bit value to a specific
register address on the sensor. The function is generic over the register
address type, accepting any type that can be converted to u16.
Note: This function performs direct I2C communication with the sensor. Most applications should use the higher-level configuration functions instead of calling this directly.
§Arguments
register_address- The register address to write to (implementsInto<u16>)value- The 8-bit value to write to the register
§Returns
Ok(())- If the write operation was successful
§Errors
Err(Error::I2cError(E))- If there was an I2C communication error
§Examples
use vl53l4cd_ulp::{VL53L4cd, Register};
let i2c = embedded_hal_mock::eh1::i2c::Mock::new(&[]);
let delay = embedded_hal_mock::eh1::delay::NoopDelay;
let mut sensor = VL53L4cd::new(i2c, delay);
sensor.sensor_init().unwrap();
// Write to a specific register using the Register enum
sensor.write_byte(Register::SystemInterrupt, 0x01).unwrap();
// Or write to a hardcoded address
sensor.write_byte(0x0046u16, 0x01).unwrap();Sourcepub fn read_byte<R>(&mut self, register_address: R) -> Result<u8, Error<E>>
pub fn read_byte<R>(&mut self, register_address: R) -> Result<u8, Error<E>>
Reads a single byte from a sensor register.
This is a low-level function that reads an 8-bit value from a specific
register address on the sensor. The function is generic over the register
address type, accepting any type that can be converted to u16.
Note: This function performs direct I2C communication with the sensor. Most applications should use the higher-level data reading functions instead of calling this directly.
§Arguments
register_address- The register address to read from (implementsInto<u16>)
§Returns
Ok(u8)- The 8-bit value read from the register
§Errors
Err(Error::I2cError(E))- If there was an I2C communication error
§Examples
use vl53l4cd_ulp::{VL53L4cd, Register};
let i2c = embedded_hal_mock::eh1::i2c::Mock::new(&[]);
let delay = embedded_hal_mock::eh1::delay::NoopDelay;
let mut sensor = VL53L4cd::new(i2c, delay);
sensor.sensor_init().unwrap();
// Read from a specific register using the Register enum
let status = sensor.read_byte(Register::SystemInterrupt).unwrap();
println!("System interrupt status: 0x{:02X}", status);
// Or read from a hardcoded address
let value = sensor.read_byte(0x0046u16).unwrap();Sourcepub fn write_word<R>(
&mut self,
register_address: R,
value: u16,
) -> Result<(), Error<E>>
pub fn write_word<R>( &mut self, register_address: R, value: u16, ) -> Result<(), Error<E>>
Writes a 16-bit word to a sensor register.
This is a low-level function that writes a 16-bit value to a specific
register address on the sensor. The function is generic over the register
address type, accepting any type that can be converted to u16.
Note: This function performs direct I2C communication with the sensor. Most applications should use the higher-level configuration functions instead of calling this directly.
§Arguments
register_address- The register address to write to (implementsInto<u16>)value- The 16-bit value to write to the register
§Returns
Ok(())- If the write operation was successful
§Errors
Err(Error::I2cError(E))- If there was an I2C communication error
§Examples
use vl53l4cd_ulp::{VL53L4cd, Register};
let i2c = embedded_hal_mock::eh1::i2c::Mock::new(&[]);
let delay = embedded_hal_mock::eh1::delay::NoopDelay;
let mut sensor = VL53L4cd::new(i2c, delay);
sensor.sensor_init().unwrap();
// Write a 16-bit value to a specific register using the Register enum
sensor.write_word(Register::RangeConfigA, 0x0123).unwrap();
// Or write to a hardcoded address
sensor.write_word(0x005Eu16, 0x0123).unwrap();Sourcepub fn read_word<R>(&mut self, register_address: R) -> Result<u16, Error<E>>
pub fn read_word<R>(&mut self, register_address: R) -> Result<u16, Error<E>>
Reads a 16-bit word from a sensor register.
This is a low-level function that reads a 16-bit value from a specific
register address on the sensor. The function is generic over the register
address type, accepting any type that can be converted to u16.
Note: This function performs direct I2C communication with the sensor. Most applications should use the higher-level data reading functions instead of calling this directly.
§Arguments
register_address- The register address to read from (implementsInto<u16>)
§Returns
Ok(u16)- The 16-bit value read from the register
§Errors
Err(Error::I2cError(E))- If there was an I2C communication error
§Examples
use vl53l4cd_ulp::{VL53L4cd, Register};
let i2c = embedded_hal_mock::eh1::i2c::Mock::new(&[]);
let delay = embedded_hal_mock::eh1::delay::NoopDelay;
let mut sensor = VL53L4cd::new(i2c, delay);
sensor.sensor_init().unwrap();
// Read a 16-bit value from a specific register using the Register enum
let config = sensor.read_word(Register::RangeConfigA).unwrap();
println!("Range config A: 0x{:04X}", config);
// Or read from a hardcoded address
let value = sensor.read_word(0x005Eu16).unwrap();Sourcepub fn write_dword<R>(
&mut self,
register_address: R,
value: u32,
) -> Result<(), Error<E>>
pub fn write_dword<R>( &mut self, register_address: R, value: u32, ) -> Result<(), Error<E>>
Writes a 32-bit double word to a sensor register.
This is a low-level function that writes a 32-bit value to a specific
register address on the sensor. The function is generic over the register
address type, accepting any type that can be converted to u16.
Note: This function performs direct I2C communication with the sensor. Most applications should use the higher-level configuration functions instead of calling this directly.
§Arguments
register_address- The register address to write to (implementsInto<u16>)value- The 32-bit value to write to the register
§Returns
Ok(())- If the write operation was successful
§Errors
Err(Error::I2cError(E))- If there was an I2C communication error
§Examples
use vl53l4cd_ulp::{VL53L4cd, Register};
let i2c = embedded_hal_mock::eh1::i2c::Mock::new(&[]);
let delay = embedded_hal_mock::eh1::delay::NoopDelay;
let mut sensor = VL53L4cd::new(i2c, delay);
sensor.sensor_init().unwrap();
// Write a 32-bit value to a specific register using the Register enum
sensor.write_dword(Register::IntermeasurementMs, 1000).unwrap();
// Or write to a hardcoded address
sensor.write_dword(0x006Cu16, 1000).unwrap();Sourcepub fn read_dword<R>(&mut self, register_address: R) -> Result<u32, Error<E>>
pub fn read_dword<R>(&mut self, register_address: R) -> Result<u32, Error<E>>
Reads a 32-bit double word from a sensor register.
This is a low-level function that reads a 32-bit value from a specific
register address on the sensor. The function is generic over the register
address type, accepting any type that can be converted to u16.
Note: This function performs direct I2C communication with the sensor. Most applications should use the higher-level data reading functions instead of calling this directly.
§Arguments
register_address- The register address to read from (implementsInto<u16>)
§Returns
Ok(u32)- The 32-bit value read from the register
§Errors
Err(Error<E>::I2cError(e))- If there was an I2C communication error
§Examples
use vl53l4cd_ulp::{VL53L4cd, Register};
let i2c = embedded_hal_mock::eh1::i2c::Mock::new(&[]);
let delay = embedded_hal_mock::eh1::delay::NoopDelay;
let mut sensor = VL53L4cd::new(i2c, delay);
sensor.sensor_init().unwrap();
// Read a 32-bit value from a specific register using the Register enum
let interval = sensor.read_dword(Register::IntermeasurementMs).unwrap();
println!("Inter-measurement interval: {} ms", interval);
// Or read from a hardcoded address
let value = sensor.read_dword(0x006Cu16).unwrap();