VL53L1X sensor class, for ARM Mbed platform. Based on Ultra-lite, Mass-market C Driver.

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Files at this revision

API Documentation at this revision

Comitter:
dmathew
Date:
Fri May 17 09:07:55 2019 +0000
Parent:
5:f16727052990
Commit message:
Changes to class to allow interrupts

Changed in this revision

vl53l1x_class.cpp Show annotated file Show diff for this revision Revisions of this file
vl53l1x_class.h Show annotated file Show diff for this revision Revisions of this file
--- a/vl53l1x_class.cpp	Mon Jan 28 10:14:03 2019 +0000
+++ b/vl53l1x_class.cpp	Fri May 17 09:07:55 2019 +0000
@@ -62,8 +62,8 @@
 {
     VL53L1X_ERROR status = 0;
 
+    status = VL53L1_WrByte(Device, VL53L1_I2C_SLAVE__DEVICE_ADDRESS, new_address >> 1);
     Device->I2cDevAddr = new_address;
-    status = VL53L1_WrByte(Device, VL53L1_I2C_SLAVE__DEVICE_ADDRESS, new_address >> 1);    
     return status;
 }
 
@@ -152,13 +152,7 @@
 VL53L1X_ERROR VL53L1X::VL53L1X_StartRanging()
 {
     VL53L1X_ERROR status = 0;
-/*
-    uint8_t Addr = 0x00;
 
-    for (Addr = 0x2D; Addr <= 0x87; Addr++){
-        status = VL53L1_WrByte(Device, Addr, VL51L1X_DEFAULT_CONFIGURATION[Addr - 0x2D]);
-    }
-*/
     status = VL53L1_WrByte(Device, SYSTEM__MODE_START, 0x40);   /* Enable VL53L1X */
     return status;
 }
@@ -592,7 +586,7 @@
 
 VL53L1X_ERROR VL53L1X::VL53L1X_SetXtalk(uint16_t XtalkValue)
 {
-/* XTalkValue in count per second to avoid float type */
+    /* XTalkValue in count per second to avoid float type */
     VL53L1X_ERROR status = 0;
 
     status = VL53L1_WrWord(Device,
@@ -955,7 +949,6 @@
     int ret;
 
     ret = dev_i2c->v53l1x_i2c_read(pBuffer, DeviceAddr, RegisterAddr, NumByteToRead);
-    //ret = dev_i2c->i2c_read(pBuffer, DeviceAddr, RegisterAddr, NumByteToRead);
 
     if (ret) {
         return -1;
@@ -972,7 +965,6 @@
 
     VL53L1X_ERROR status  = VL53L1_ERROR_NONE;
 
-    //*ptick_count_ms = timeGetTime();
     *ptick_count_ms = 0;
 
     return status;
@@ -982,16 +974,12 @@
 
 VL53L1X_ERROR VL53L1X::VL53L1_WaitUs(VL53L1_Dev_t *pdev, int32_t wait_us)
 {
-    //(void)pdev;
-    //wait_ms(wait_us/1000);
     return VL53L1_ERROR_NONE;
 }
 
 
 VL53L1X_ERROR VL53L1X::VL53L1_WaitMs(VL53L1_Dev_t *pdev, int32_t wait_ms)
 {
-    //(void)pdev;
-    //wait_ms(wait_ms);
     return VL53L1_ERROR_NONE;
 }
 
@@ -1070,5 +1058,58 @@
     return status;
 }
 
+int VL53L1X::handle_irq(uint16_t *distance)
+{
+    int status;
+    status = get_measurement(distance);
+    enable_interrupt_measure_detection_irq();
+    return status;
+}
 
+int VL53L1X::get_measurement(uint16_t *distance)
+{
+    int status = 0;
 
+    status = VL53L1X_GetDistance(distance);
+    status = VL53L1X_ClearInterrupt();
+
+    return status;
+}
+
+int VL53L1X::start_measurement(void (*fptr)(void))
+{
+    int status = 0;
+
+    if (_gpio1Int == NULL) {
+        printf("GPIO1 Error\r\n");
+        return 1;
+    }
+
+    status = VL53L1X_StopRanging(); // it is safer to do this while sensor is stopped
+
+    if (status == 0) {
+        attach_interrupt_measure_detection_irq(fptr);
+        enable_interrupt_measure_detection_irq();
+    }
+
+    if (status == 0) {
+        status = VL53L1X_StartRanging();
+    }
+
+    return status;
+}
+
+int VL53L1X::stop_measurement()
+{
+    int status = 0;
+
+    if (status == 0) {
+        printf("Call of VL53L0X_StopMeasurement\n");
+        status = VL53L1X_StopRanging();
+    }
+
+    if (status == 0)
+        status = VL53L1X_ClearInterrupt();
+
+    return status;
+}
--- a/vl53l1x_class.h	Mon Jan 28 10:14:03 2019 +0000
+++ b/vl53l1x_class.h	Fri May 17 09:07:55 2019 +0000
@@ -5,7 +5,7 @@
  * @date    15-January-2019
  * @brief   Header file for VL53L1 sensor component
  ******************************************************************************
- Copyright © 2019, STMicroelectronics International N.V.
+ Copyright © 2019, STMicroelectronics International N.V.
  All rights reserved.
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:
@@ -50,7 +50,6 @@
 #include "PinNames.h"
 #include "RangeSensor.h"
 #include "vl53l1x_error_codes.h"
-//#include "DevI2C.h"
 #include "vl53L1x_I2c.h"
 #include "Stmpe1600.h"
 
@@ -62,7 +61,6 @@
 
 typedef int8_t VL53L1X_ERROR;
 
-//#define SOFT_RESET                                          0x0000
 #define VL53L1_I2C_SLAVE__DEVICE_ADDRESS                    0x0001
 #define VL53L1_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND        0x0008
 #define ALGO__CROSSTALK_COMPENSATION_PLANE_OFFSET_KCPS      0x0016
@@ -271,18 +269,18 @@
 
 
 
-/**
- *
- * @brief One time device initialization
- * @param void
- * @return     0 on success,  @a #CALIBRATION_WARNING if failed
- */
+    /**
+     *
+     * @brief One time device initialization
+     * @param void
+     * @return     0 on success,  @a #CALIBRATION_WARNING if failed
+     */
     virtual int init(void *init)
     {
        return VL53L1X_SensorInit();
+
     }
 
-
     /**
      * @brief       Initialize the sensor with default values
      * @return      "0" on success
@@ -318,13 +316,64 @@
         return -1;
     }
 
-
+    /**
+     * @brief       Interrupt handling func to be called by user after an INT is occurred
+     * @param[out]  Data pointer to the distance to read data in to
+     * @return      0 on Success
+     */
+    int handle_irq(uint16_t *distance);
 
-/**
- * @brief Get ranging result and only that
- * @param pRange_mm  Pointer to range distance
- * @return           0 on success
- */
+    /**
+     * @brief       Start the measure indicated by operating mode
+     * @param[in]   fptr specifies call back function must be !NULL in case of interrupt measure
+     * @return      0 on Success
+     */
+    int start_measurement(void (*fptr)(void));
+    /**
+     * @brief       Stop the currently running measure indicate by operating_mode
+     * @return      0 on Success
+     */
+    int stop_measurement();
+    /**
+     * @brief       Get results for the measure
+     * @param[out]  Data pointer to the distance_data to read data in to
+     * @return      0 on Success
+     */
+    int get_measurement(uint16_t *distance);
+    /**
+     * @brief       Enable interrupt measure IRQ
+     * @return      0 on Success
+     */
+    void enable_interrupt_measure_detection_irq(void)
+    {
+        if (_gpio1Int != NULL)
+            _gpio1Int->enable_irq();
+    }
+
+    /**
+     * @brief       Disable interrupt measure IRQ
+     * @return      0 on Success
+     */
+    void disable_interrupt_measure_detection_irq(void)
+    {
+        if (_gpio1Int != NULL)
+            _gpio1Int->disable_irq();
+    }
+    /**
+     * @brief       Attach a function to call when an interrupt is detected, i.e. measurement is ready
+     * @param[in]   fptr pointer to call back function to be called whenever an interrupt occours
+     * @return      0 on Success
+     */
+    void attach_interrupt_measure_detection_irq(void (*fptr)(void))
+    {
+        if (_gpio1Int != NULL)
+            _gpio1Int->rise(fptr);
+    }
+    /**
+     * @brief Get ranging result and only that
+     * @param pRange_mm  Pointer to range distance
+     * @return           0 on success
+     */
     virtual int get_distance(uint32_t *piData)
     {
     int status;
@@ -335,9 +384,7 @@
     }
 
 
-/* VL53L1X_api.h functions */
-
-
+    /* VL53L1X_api.h functions */
 
     /**
      * @brief This function returns the SW driver version
@@ -628,13 +675,10 @@
     /* Measure detection IRQ */
     InterruptIn *_gpio1Int;
  
-    ///* Digital out pin */
-    //int gpio0;
-    //int gpio1Int;
     /* Device data */
     VL53L1_Dev_t MyDevice;
     VL53L1_DEV Device;
 };
 
 
-#endif /* _VL53L1X_CLASS_H_ */
\ No newline at end of file
+#endif /* _VL53L1X_CLASS_H_ */