ToF sensor code by Pololu translated from Arduino to mbed

Revision:
2:bb0cd79ef201
Parent:
1:bc3ff1b884b3
Child:
4:cbf407c53998
--- a/VL53L1X.h	Tue Jul 31 16:03:14 2018 +0000
+++ b/VL53L1X.h	Wed Aug 01 01:30:49 2018 +0000
@@ -1,14 +1,14 @@
 #ifndef VL53L1X_H
 #define VL53L1X_H
-
+ 
 #include "mbed.h"
-
+ 
 class VL53L1X
 {
   public:
     Timer t;
     VL53L1X(PinName SDA, PinName SCL);
-
+ 
     // register addresses from API vl53l1x_register_map.h
     enum regAddr : uint16_t
     {
@@ -1199,67 +1199,67 @@
       SHADOW_PHASECAL_RESULT__REFERENCE_PHASE_HI                                 = 0x0FFE,
       SHADOW_PHASECAL_RESULT__REFERENCE_PHASE_LO                                 = 0x0FFF,
     };
-
+ 
     enum DistanceMode { Short, Medium, Long, Unknown };
-
+ 
     enum RangeStatus : uint8_t
     {
       RangeValid                =   0,
-
+ 
       // "sigma estimator check is above the internal defined threshold"
       // (sigma = standard deviation of measurement)
       SigmaFail                 =   1,
-
+ 
       // "signal value is below the internal defined threshold"
       SignalFail                =   2,
-
+ 
       // "Target is below minimum detection threshold."
       RangeValidMinRangeClipped =   3,
-
+ 
       // "phase is out of bounds"
       // (nothing detected in range; try a longer distance mode if applicable)
       OutOfBoundsFail           =   4,
-
+ 
       // "HW or VCSEL failure"
       HardwareFail              =   5,
-
+ 
       // "The Range is valid but the wraparound check has not been done."
       RangeValidNoWrapCheckFail =   6,
-
+ 
       // "Wrapped target, not matching phases"
       // "no matching phase in other VCSEL period timing."
       WrapTargetFail            =   7,
-
+ 
       // "Internal algo underflow or overflow in lite ranging."
    // ProcessingFail            =   8: not used in API
-
+ 
       // "Specific to lite ranging."
       // should never occur with this lib (which uses low power auto ranging,
       // as the API does)
       XtalkSignalFail           =   9,
-
+ 
       // "1st interrupt when starting ranging in back to back mode. Ignore
       // data."
       // should never occur with this lib
       SynchronizationInt         =  10, // (the API spells this "syncronisation")
-
+ 
       // "All Range ok but object is result of multiple pulses merging together.
       // Used by RQL for merged pulse detection"
    // RangeValid MergedPulse    =  11: not used in API
-
+ 
       // "Used by RQL as different to phase fail."
    // TargetPresentLackOfSignal =  12:
-
+ 
       // "Target is below minimum detection threshold."
       MinRangeFail              =  13,
-
+ 
       // "The reported range is invalid"
    // RangeInvalid              =  14: can't actually be returned by API (range can never become negative, even after correction)
-
+ 
       // "No Update."
       None                      = 255,
     };
-
+ 
     struct RangingData
     {
       uint16_t range_mm;
@@ -1267,58 +1267,58 @@
       float peak_signal_count_rate_MCPS;
       float ambient_count_rate_MCPS;
     };
-
+ 
     RangingData ranging_data;
-
+ 
     uint8_t last_status; // status of last I2C transmission
-
+ 
     VL53L1X();
-
+ 
     void setAddress(uint8_t new_addr);
     uint8_t getAddress() { return address; }
-
+ 
     bool init(bool io_2v8 = true);
-
+ 
     void writeReg(uint16_t registerAddr, uint8_t data);
     void writeReg16Bit(uint16_t registerAddr, uint16_t data);
     void writeReg32Bit(uint16_t registerAddr, uint32_t data);
     uint8_t readReg(uint16_t registerAddr);
     uint16_t readReg16Bit(uint16_t registerAddr);
     uint32_t readReg32Bit(uint16_t registerAddr);
-
+ 
     bool setDistanceMode(DistanceMode mode);
     DistanceMode getDistanceMode() { return distance_mode; }
-
+ 
     bool setMeasurementTimingBudget(uint32_t budget_us);
     uint32_t getMeasurementTimingBudget();
-
+ 
     void startContinuous(uint32_t period_ms);
     void stopContinuous();
     uint16_t read(bool blocking = true);
     uint16_t readRangeContinuousMillimeters(bool blocking = true) { return read(blocking); } // alias of read()
-
+ 
     // check if sensor has new reading available
     // assumes interrupt is active low (GPIO_HV_MUX__CTRL bit 4 is 1)
     bool dataReady() { 
-    int isThereData = readReg(GPIO__TIO_HV_STATUS);
-//    printf("%x\r\n", isThereData);
-    return (readReg(GPIO__TIO_HV_STATUS) & 0x01) == 0; 
+        int isThereData = readReg(GPIO__TIO_HV_STATUS);
+        //printf("Data: %x\r\n", isThereData);            /* Returns 3 */
+        return (isThereData & 0x01) == 0; 
     }
-
+ 
     static const char * rangeStatusToString(RangeStatus status);
-
+ 
     void setTimeout(uint16_t timeout) { io_timeout = timeout; }
     uint16_t getTimeout() { return io_timeout; }
     bool timeoutOccurred();
-
+ 
   private:
     I2C _i2c;
-
-
+ 
+ 
     // The Arduino two-wire interface uses a 7-bit number for the address,
     // and sets the last bit correctly based on reads and writes
     static const uint8_t AddressDefault = 0b0101001;
-
+ 
     // value used in measurement timing budget calculations
     // assumes PresetMode is LOWPOWER_AUTONOMOUS
     //
@@ -1329,11 +1329,11 @@
     //               LOWPOWER_AUTO_OVERHEAD_BETWEEN_A_B_RANGING + vhv
     //             = 1448 + 2100 + 980 = 4528
     static const uint32_t TimingGuard = 4528;
-
+ 
     // value in DSS_CONFIG__TARGET_TOTAL_RATE_MCPS register, used in DSS
     // calculations
     static const uint16_t TargetRate = 0x0A00;
-
+ 
     // for storing values read from RESULT__RANGE_STATUS (0x0089)
     // through RESULT__PEAK_SIGNAL_COUNT_RATE_CROSSTALK_CORRECTED_MCPS_SD0_LOW
     // (0x0099)
@@ -1350,45 +1350,45 @@
       uint16_t final_crosstalk_corrected_range_mm_sd0;
       uint16_t peak_signal_count_rate_crosstalk_corrected_mcps_sd0;
     };
-
+ 
     // making this static would save RAM for multiple instances as long as there
     // aren't multiple sensors being read at the same time (e.g. on separate
     // I2C buses)
     ResultBuffer results;
-
+ 
     uint8_t address;
-
+ 
     uint16_t io_timeout;
     bool did_timeout;
     uint16_t timeout_start_ms;
-
+ 
     uint16_t fast_osc_frequency;
     uint16_t osc_calibrate_val;
-
+ 
     bool calibrated;
     uint8_t saved_vhv_init;
     uint8_t saved_vhv_timeout;
-
+ 
     DistanceMode distance_mode;
-
+ 
     // Record the current time to check an upcoming timeout against
-    void startTimeout() { timeout_start_ms = t.read()*1000; }
-
+    void startTimeout() { timeout_start_ms = t.read()/10000; }
+ 
     // Check if timeout is enabled (set to nonzero value) and has expired
-    bool checkTimeoutExpired() {return (io_timeout > 0) && ((uint16_t)(t.read()*1000 - timeout_start_ms) > io_timeout); }
-
+    bool checkTimeoutExpired() {return (io_timeout > 0) && ((uint16_t)(t.read()/10000 - timeout_start_ms) > io_timeout); }
+ 
     void setupManualCalibration();
     void readResults();
     void updateDSS();
     void getRangingData();
-
+ 
     static uint32_t decodeTimeout(uint16_t reg_val);
     static uint16_t encodeTimeout(uint32_t timeout_mclks);
     static uint32_t timeoutMclksToMicroseconds(uint32_t timeout_mclks, uint32_t macro_period_us);
     static uint32_t timeoutMicrosecondsToMclks(uint32_t timeout_us, uint32_t macro_period_us);
     uint32_t calcMacroPeriod(uint8_t vcsel_period);
-
+ 
     // Convert count rate from fixed point 9.7 format to float
     float countRateFixedToFloat(uint16_t count_rate_fixed) { return (float)count_rate_fixed / (1 << 7); }
 };
-#endif
\ No newline at end of file
+#endif