Condensed Version of Public VL53L0X

Dependents:   ToF-Only-Tryout

Revision:
5:b95f6951f7d5
Parent:
4:d25c4fa216af
Child:
7:41cbc431e1f4
--- a/VL53L0X.h	Mon Dec 18 08:27:21 2017 +0000
+++ b/VL53L0X.h	Sat Mar 23 21:28:30 2019 +0000
@@ -43,232 +43,102 @@
 
 /* Includes ------------------------------------------------------------------*/
 #include "mbed.h"
-#include "RangeSensor.h"
-#include "DevI2C.h"
+#include "pinmap.h"
 #include "PinNames.h"
 #include "VL53L0X_def.h"
-#include "VL53L0X_platform.h"
-#include "Stmpe1600.h"
-
 
 /**
  * The device model ID
  */
-#define IDENTIFICATION_MODEL_ID                 0x000
-
-
-#define STATUS_OK              0x00
-#define STATUS_FAIL            0x01
-
-#define VL53L0X_OsDelay(...) wait_ms(2) // 2 msec delay. can also use wait(float secs)/wait_us(int)
-
-#ifdef USE_EMPTY_STRING
-#define  VL53L0X_STRING_DEVICE_INFO_NAME                             ""
-#define  VL53L0X_STRING_DEVICE_INFO_NAME_TS0                         ""
-#define  VL53L0X_STRING_DEVICE_INFO_NAME_TS1                         ""
-#define  VL53L0X_STRING_DEVICE_INFO_NAME_TS2                         ""
-#define  VL53L0X_STRING_DEVICE_INFO_NAME_ES1                         ""
-#define  VL53L0X_STRING_DEVICE_INFO_TYPE                             ""
-
-/* PAL ERROR strings */
-#define  VL53L0X_STRING_ERROR_NONE                                   ""
-#define  VL53L0X_STRING_ERROR_CALIBRATION_WARNING                    ""
-#define  VL53L0X_STRING_ERROR_MIN_CLIPPED                            ""
-#define  VL53L0X_STRING_ERROR_UNDEFINED                              ""
-#define  VL53L0X_STRING_ERROR_INVALID_PARAMS                         ""
-#define  VL53L0X_STRING_ERROR_NOT_SUPPORTED                          ""
-#define  VL53L0X_STRING_ERROR_RANGE_ERROR                            ""
-#define  VL53L0X_STRING_ERROR_TIME_OUT                               ""
-#define  VL53L0X_STRING_ERROR_MODE_NOT_SUPPORTED                     ""
-#define  VL53L0X_STRING_ERROR_BUFFER_TOO_SMALL                       ""
-#define  VL53L0X_STRING_ERROR_GPIO_NOT_EXISTING                      ""
-#define  VL53L0X_STRING_ERROR_GPIO_FUNCTIONALITY_NOT_SUPPORTED       ""
-#define  VL53L0X_STRING_ERROR_CONTROL_INTERFACE                      ""
-#define  VL53L0X_STRING_ERROR_INVALID_COMMAND                        ""
-#define  VL53L0X_STRING_ERROR_DIVISION_BY_ZERO                       ""
-#define  VL53L0X_STRING_ERROR_REF_SPAD_INIT                          ""
-#define  VL53L0X_STRING_ERROR_NOT_IMPLEMENTED                        ""
-
-#define  VL53L0X_STRING_UNKNOW_ERROR_CODE                            ""
-
-
+#define IDENTIFICATION_MODEL_ID  0x000
+#define STATUS_OK                0x00
+#define STATUS_FAIL              0x01
 
-/* Range Status */
-#define  VL53L0X_STRING_RANGESTATUS_NONE                             ""
-#define  VL53L0X_STRING_RANGESTATUS_RANGEVALID                       ""
-#define  VL53L0X_STRING_RANGESTATUS_SIGMA                            ""
-#define  VL53L0X_STRING_RANGESTATUS_SIGNAL                           ""
-#define  VL53L0X_STRING_RANGESTATUS_MINRANGE                         ""
-#define  VL53L0X_STRING_RANGESTATUS_PHASE                            ""
-#define  VL53L0X_STRING_RANGESTATUS_HW                               ""
-
-
-/* Range Status */
-#define  VL53L0X_STRING_STATE_POWERDOWN                              ""
-#define  VL53L0X_STRING_STATE_WAIT_STATICINIT                        ""
-#define  VL53L0X_STRING_STATE_STANDBY                                ""
-#define  VL53L0X_STRING_STATE_IDLE                                   ""
-#define  VL53L0X_STRING_STATE_RUNNING                                ""
-#define  VL53L0X_STRING_STATE_UNKNOWN                                ""
-#define  VL53L0X_STRING_STATE_ERROR                                  ""
-
+/** default device address */
+#define VL53L0X_DEFAULT_ADDRESS		0x52 /* (8-bit) */
 
-/* Device Specific */
-#define  VL53L0X_STRING_DEVICEERROR_NONE                             ""
-#define  VL53L0X_STRING_DEVICEERROR_VCSELCONTINUITYTESTFAILURE       ""
-#define  VL53L0X_STRING_DEVICEERROR_VCSELWATCHDOGTESTFAILURE         ""
-#define  VL53L0X_STRING_DEVICEERROR_NOVHVVALUEFOUND                  ""
-#define  VL53L0X_STRING_DEVICEERROR_MSRCNOTARGET                     ""
-#define  VL53L0X_STRING_DEVICEERROR_SNRCHECK                         ""
-#define  VL53L0X_STRING_DEVICEERROR_RANGEPHASECHECK                  ""
-#define  VL53L0X_STRING_DEVICEERROR_SIGMATHRESHOLDCHECK              ""
-#define  VL53L0X_STRING_DEVICEERROR_TCC                              ""
-#define  VL53L0X_STRING_DEVICEERROR_PHASECONSISTENCY                 ""
-#define  VL53L0X_STRING_DEVICEERROR_MINCLIP                          ""
-#define  VL53L0X_STRING_DEVICEERROR_RANGECOMPLETE                    ""
-#define  VL53L0X_STRING_DEVICEERROR_ALGOUNDERFLOW                    ""
-#define  VL53L0X_STRING_DEVICEERROR_ALGOOVERFLOW                     ""
-#define  VL53L0X_STRING_DEVICEERROR_RANGEIGNORETHRESHOLD             ""
-#define  VL53L0X_STRING_DEVICEERROR_UNKNOWN                          ""
-
-/* Check Enable */
-#define  VL53L0X_STRING_CHECKENABLE_SIGMA_FINAL_RANGE                ""
-#define  VL53L0X_STRING_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE          ""
-#define  VL53L0X_STRING_CHECKENABLE_SIGNAL_REF_CLIP                  ""
-#define  VL53L0X_STRING_CHECKENABLE_RANGE_IGNORE_THRESHOLD           ""
-
-/* Sequence Step */
-#define  VL53L0X_STRING_SEQUENCESTEP_TCC                             ""
-#define  VL53L0X_STRING_SEQUENCESTEP_DSS                             ""
-#define  VL53L0X_STRING_SEQUENCESTEP_MSRC                            ""
-#define  VL53L0X_STRING_SEQUENCESTEP_PRE_RANGE                       ""
-#define  VL53L0X_STRING_SEQUENCESTEP_FINAL_RANGE                     ""
-#else
-#define  VL53L0X_STRING_DEVICE_INFO_NAME          "VL53L0X cut1.0"
 #define  VL53L0X_STRING_DEVICE_INFO_NAME_TS0      "VL53L0X TS0"
 #define  VL53L0X_STRING_DEVICE_INFO_NAME_TS1      "VL53L0X TS1"
 #define  VL53L0X_STRING_DEVICE_INFO_NAME_TS2      "VL53L0X TS2"
 #define  VL53L0X_STRING_DEVICE_INFO_NAME_ES1      "VL53L0X ES1 or later"
 #define  VL53L0X_STRING_DEVICE_INFO_TYPE          "VL53L0X"
 
-/* PAL ERROR strings */
-#define  VL53L0X_STRING_ERROR_NONE \
-			"No Error"
-#define  VL53L0X_STRING_ERROR_CALIBRATION_WARNING \
-			"Calibration Warning Error"
-#define  VL53L0X_STRING_ERROR_MIN_CLIPPED \
-			"Min clipped error"
-#define  VL53L0X_STRING_ERROR_UNDEFINED \
-			"Undefined error"
-#define  VL53L0X_STRING_ERROR_INVALID_PARAMS \
-			"Invalid parameters error"
-#define  VL53L0X_STRING_ERROR_NOT_SUPPORTED \
-			"Not supported error"
-#define  VL53L0X_STRING_ERROR_RANGE_ERROR \
-			"Range error"
-#define  VL53L0X_STRING_ERROR_TIME_OUT \
-			"Time out error"
-#define  VL53L0X_STRING_ERROR_MODE_NOT_SUPPORTED \
-			"Mode not supported error"
-#define  VL53L0X_STRING_ERROR_BUFFER_TOO_SMALL \
-			"Buffer too small"
-#define  VL53L0X_STRING_ERROR_GPIO_NOT_EXISTING \
-			"GPIO not existing"
-#define  VL53L0X_STRING_ERROR_GPIO_FUNCTIONALITY_NOT_SUPPORTED \
-			"GPIO funct not supported"
-#define  VL53L0X_STRING_ERROR_INTERRUPT_NOT_CLEARED \
-			"Interrupt not Cleared"
-#define  VL53L0X_STRING_ERROR_CONTROL_INTERFACE \
-			"Control Interface Error"
-#define  VL53L0X_STRING_ERROR_INVALID_COMMAND \
-			"Invalid Command Error"
-#define  VL53L0X_STRING_ERROR_DIVISION_BY_ZERO \
-			"Division by zero Error"
-#define  VL53L0X_STRING_ERROR_REF_SPAD_INIT \
-			"Reference Spad Init Error"
-#define  VL53L0X_STRING_ERROR_NOT_IMPLEMENTED \
-			"Not implemented error"
+
+/* Classes -------------------------------------------------------------------*/
+/** Helper class DevI2C providing functions for multi-register I2C communication
+ *  common for a series of I2C devices
+ */
+class DevI2C : public I2C
+{
+private:
+    static const unsigned int TEMP_BUF_SIZE = 64;
 
-#define  VL53L0X_STRING_UNKNOW_ERROR_CODE \
-			"Unknown Error Code"
-
-
+public:
+    /** Create a DevI2C Master interface, connected to the specified pins
+     *
+     *  @param sda I2C data line pin
+     *  @param scl I2C clock line pin
+     */
+    DevI2C(PinName sda, PinName scl) : I2C(sda, scl) {}
+    
+    /**
+     * @brief  Writes a buffer towards the I2C peripheral device.
+     * @param  pBuffer pointer to the byte-array data to send
+     * @param  DeviceAddr specifies the peripheral device slave address.
+     * @param  RegisterAddr specifies the internal address register
+     *         where to start writing to (must be correctly masked).
+     * @param  NumByteToWrite number of bytes to be written.
+     * @retval 0 if ok,
+     * @retval -1 if an I2C error has occured, or
+     * @retval -2 on temporary buffer overflow (i.e. NumByteToWrite was too high)
+     * @note   On some devices if NumByteToWrite is greater
+     *         than one, the RegisterAddr must be masked correctly!
+     */
+    int i2c_write(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr,
+                  uint16_t NumByteToWrite) {
+        int ret;
+        uint8_t tmp[TEMP_BUF_SIZE];
 
-/* Range Status */
-#define  VL53L0X_STRING_RANGESTATUS_NONE                 "No Update"
-#define  VL53L0X_STRING_RANGESTATUS_RANGEVALID           "Range Valid"
-#define  VL53L0X_STRING_RANGESTATUS_SIGMA                "Sigma Fail"
-#define  VL53L0X_STRING_RANGESTATUS_SIGNAL               "Signal Fail"
-#define  VL53L0X_STRING_RANGESTATUS_MINRANGE             "Min Range Fail"
-#define  VL53L0X_STRING_RANGESTATUS_PHASE                "Phase Fail"
-#define  VL53L0X_STRING_RANGESTATUS_HW                   "Hardware Fail"
-
+        if(NumByteToWrite >= TEMP_BUF_SIZE) return -2;
 
-/* Range Status */
-#define  VL53L0X_STRING_STATE_POWERDOWN               "POWERDOWN State"
-#define  VL53L0X_STRING_STATE_WAIT_STATICINIT \
-			"Wait for staticinit State"
-#define  VL53L0X_STRING_STATE_STANDBY                 "STANDBY State"
-#define  VL53L0X_STRING_STATE_IDLE                    "IDLE State"
-#define  VL53L0X_STRING_STATE_RUNNING                 "RUNNING State"
-#define  VL53L0X_STRING_STATE_UNKNOWN                 "UNKNOWN State"
-#define  VL53L0X_STRING_STATE_ERROR                   "ERROR State"
+        /* First, send device address. Then, send data and STOP condition */
+        tmp[0] = RegisterAddr;
+        memcpy(tmp+1, pBuffer, NumByteToWrite);
 
+        ret = write(DeviceAddr, (const char*)tmp, NumByteToWrite+1, false);
+
+        if(ret) return -1;
+        return 0;
+    }
 
-/* Device Specific */
-#define  VL53L0X_STRING_DEVICEERROR_NONE              "No Update"
-#define  VL53L0X_STRING_DEVICEERROR_VCSELCONTINUITYTESTFAILURE \
-			"VCSEL Continuity Test Failure"
-#define  VL53L0X_STRING_DEVICEERROR_VCSELWATCHDOGTESTFAILURE \
-			"VCSEL Watchdog Test Failure"
-#define  VL53L0X_STRING_DEVICEERROR_NOVHVVALUEFOUND \
-			"No VHV Value found"
-#define  VL53L0X_STRING_DEVICEERROR_MSRCNOTARGET \
-			"MSRC No Target Error"
-#define  VL53L0X_STRING_DEVICEERROR_SNRCHECK \
-			"SNR Check Exit"
-#define  VL53L0X_STRING_DEVICEERROR_RANGEPHASECHECK \
-			"Range Phase Check Error"
-#define  VL53L0X_STRING_DEVICEERROR_SIGMATHRESHOLDCHECK \
-			"Sigma Threshold Check Error"
-#define  VL53L0X_STRING_DEVICEERROR_TCC \
-			"TCC Error"
-#define  VL53L0X_STRING_DEVICEERROR_PHASECONSISTENCY \
-			"Phase Consistency Error"
-#define  VL53L0X_STRING_DEVICEERROR_MINCLIP \
-			"Min Clip Error"
-#define  VL53L0X_STRING_DEVICEERROR_RANGECOMPLETE \
-			"Range Complete"
-#define  VL53L0X_STRING_DEVICEERROR_ALGOUNDERFLOW \
-			"Range Algo Underflow Error"
-#define  VL53L0X_STRING_DEVICEERROR_ALGOOVERFLOW \
-			"Range Algo Overlow Error"
-#define  VL53L0X_STRING_DEVICEERROR_RANGEIGNORETHRESHOLD \
-			"Range Ignore Threshold Error"
-#define  VL53L0X_STRING_DEVICEERROR_UNKNOWN \
-			"Unknown error code"
+    /**
+     * @brief  Reads a buffer from the I2C peripheral device.
+     * @param  pBuffer pointer to the byte-array to read data in to
+     * @param  DeviceAddr specifies the peripheral device slave address.
+     * @param  RegisterAddr specifies the internal address register
+     *         where to start reading from (must be correctly masked).
+     * @param  NumByteToRead number of bytes to be read.
+     * @retval 0 if ok,
+     * @retval -1 if an I2C error has occured
+     * @note   On some devices if NumByteToWrite is greater
+     *         than one, the RegisterAddr must be masked correctly!
+     */
+    int i2c_read(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr,
+                 uint16_t NumByteToRead) {
+        int ret;
 
-/* Check Enable */
-#define  VL53L0X_STRING_CHECKENABLE_SIGMA_FINAL_RANGE \
-			"SIGMA FINAL RANGE"
-#define  VL53L0X_STRING_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE \
-			"SIGNAL RATE FINAL RANGE"
-#define  VL53L0X_STRING_CHECKENABLE_SIGNAL_REF_CLIP \
-			"SIGNAL REF CLIP"
-#define  VL53L0X_STRING_CHECKENABLE_RANGE_IGNORE_THRESHOLD \
-			"RANGE IGNORE THRESHOLD"
-#define  VL53L0X_STRING_CHECKENABLE_SIGNAL_RATE_MSRC \
-			"SIGNAL RATE MSRC"
-#define  VL53L0X_STRING_CHECKENABLE_SIGNAL_RATE_PRE_RANGE \
-			"SIGNAL RATE PRE RANGE"
+        /* Send device address, with no STOP condition */
+        ret = write(DeviceAddr, (const char*)&RegisterAddr, 1, true);
+        if(!ret) {
+            /* Read data, with STOP condition  */
+            ret = read(DeviceAddr, (char*)pBuffer, NumByteToRead, false);
+        }
 
-/* Sequence Step */
-#define  VL53L0X_STRING_SEQUENCESTEP_TCC                   "TCC"
-#define  VL53L0X_STRING_SEQUENCESTEP_DSS                   "DSS"
-#define  VL53L0X_STRING_SEQUENCESTEP_MSRC                  "MSRC"
-#define  VL53L0X_STRING_SEQUENCESTEP_PRE_RANGE             "PRE RANGE"
-#define  VL53L0X_STRING_SEQUENCESTEP_FINAL_RANGE           "FINAL RANGE"
-#endif /* USE_EMPTY_STRING */
+        if(ret) return -1;
+        return 0;
+    }
+
+};
 
 /* sensor operating modes */
 typedef enum {
@@ -283,13 +153,10 @@
     range_continuous_interrupt_out_of_window,
 } OperatingMode;
 
-/** default device address */
-#define VL53L0X_DEFAULT_ADDRESS		0x52 /* (8-bit) */
-
 /* Classes -------------------------------------------------------------------*/
 /** Class representing a VL53L0 sensor component
  */
-class VL53L0X : public RangeSensor
+class VL53L0X 
 {
 public:
     /** Constructor
@@ -304,7 +171,6 @@
         _my_device.comms_type = 1; // VL53L0X_COMMS_I2C
         _my_device.comms_speed_khz = 400;
         _device = &_my_device;
-        _expgpio0 = NULL;
         if (pin_gpio1 != NC) {
             _gpio1Int = new InterruptIn(pin_gpio1);
         } else {
@@ -312,34 +178,11 @@
         }
     }
 
-    /** Constructor 2 (STMPE1600DigiOut)
-     * @param[in] i2c device I2C to be used for communication
-     * @param[in] &pin Gpio Expander STMPE1600DigiOut pin to be used as component GPIO_0 CE
-     * @param[in] pin_gpio1 pin Mbed InterruptIn PinName to be used as component GPIO_1 INT
-     * @param[in] device address, 0x29 by default
-     */
-    VL53L0X(DevI2C *i2c, Stmpe1600DigiOut *pin, PinName pin_gpio1,
-            uint8_t dev_addr = VL53L0X_DEFAULT_ADDRESS) : _dev_i2c(i2c), _expgpio0(pin)
-    {
-        _my_device.I2cDevAddr = dev_addr;
-        _my_device.comms_type = 1; // VL53L0X_COMMS_I2C
-        _my_device.comms_speed_khz = 400;
-        _device = &_my_device;
-        _gpio0 = NULL;
-        if (pin_gpio1 != NC) {
-            _gpio1Int = new InterruptIn(pin_gpio1);
-        } else {
-            _gpio1Int = NULL;
-        }
-    }
 
     /** Destructor
      */
     virtual ~VL53L0X()
-    {
-        if (_gpio1Int != NULL) {
-            delete _gpio1Int;
-        }
+    {  if (_gpio1Int != NULL) { delete _gpio1Int; }
     }
 
     /*** Interface Methods ***/
@@ -351,13 +194,7 @@
     /* turns on the sensor */
     void VL53L0X_on(void)
     {
-        if (_gpio0) {
-            *_gpio0 = 1;
-        } else {
-            if (_expgpio0) {
-                *_expgpio0 = 1;
-            }
-        }
+        if (_gpio0) { *_gpio0 = 1; }
         wait_ms(10);
     }
 
@@ -368,17 +205,10 @@
     /* turns off the sensor */
     void VL53L0X_off(void)
     {
-        if (_gpio0) {
-            *_gpio0 = 0;
-        } else {
-            if (_expgpio0) {
-                *_expgpio0 = 0;
-            }
-        }
+        if (_gpio0) { *_gpio0 = 0; } 
         wait_ms(10);
     }
 
-
     /**
      * @brief       Initialize the sensor with default values
      * @return      "0" on success
@@ -2107,9 +1937,7 @@
         uint8_t id = 0;
 
         status = read_id(&id);
-        if (status) {
-            VL53L0X_ErrLog("Failed to read ID device. Device not present!\n\r");
-        }
+
         return status;
     }
 
@@ -2784,8 +2612,6 @@
     DevI2C *_dev_i2c;
     /* Digital out pin */
     DigitalOut *_gpio0;
-    /* GPIO expander */
-    Stmpe1600DigiOut *_expgpio0;
     /* Measure detection IRQ */
     InterruptIn *_gpio1Int;
     /* Device data */