ToF sensor code by Pololu translated from Arduino to mbed

Revision:
1:bc3ff1b884b3
Parent:
0:76ea242a637f
Child:
2:bb0cd79ef201
--- a/VL53L1X.cpp	Mon Jul 30 18:45:53 2018 +0000
+++ b/VL53L1X.cpp	Tue Jul 31 16:03:14 2018 +0000
@@ -39,6 +39,7 @@
 // mode.
 bool VL53L1X::init(bool io_2v8)
 {
+  t.start();
   // check model ID and module type registers (values specified in datasheet)
   int tempRegister = readReg16Bit(IDENTIFICATION__MODEL_ID);
   printf("temporary %x\r\n", tempRegister);
@@ -82,7 +83,7 @@
   // store oscillator info for later use
   fast_osc_frequency = readReg16Bit(OSC_MEASURED__FAST_OSC__FREQUENCY);
   osc_calibrate_val = readReg16Bit(RESULT__OSC_CALIBRATE_VAL);
-
+  printf("fast_osc_frequency %d, osc_calibrate_val %d\r\n",fast_osc_frequency, osc_calibrate_val);
   // VL53L1_DataInit() end
 
   // VL53L1_StaticInit() begin
@@ -193,14 +194,6 @@
     data_write[4] = (data >> 8) & 0xFF;;
     data_write[5] =  data & 0xFF;
     _i2c.write(address, data_write, 6); 
- // _i2c.beginTransmission(address);
- /* _i2c.write((reg >> 8) & 0xFF); // reg high byte
-  _i2c.write( reg       & 0xFF); // reg low byte
-  _i2c.write((value >> 24) & 0xFF); // value highest byte
-  _i2c.write((value >> 16) & 0xFF);
-  _i2c.write((value >>  8) & 0xFF);
-  _i2c.write( value        & 0xFF); // value lowest byte
-  //last_status = _i2c.endTransmission();*/
 }
 
 // Read an 8-bit register
@@ -240,18 +233,7 @@
 uint32_t VL53L1X::readReg32Bit(uint16_t reg)
 {
   uint32_t value;
-/*
-  _i2c.beginTransmission(address);
-  _i2c.write((reg >> 8) & 0xFF); // reg high byte
-  _i2c.write( reg       & 0xFF); // reg low byte
-  last_status = _i2c.endTransmission();
 
-  _i2c.requestFrom(address, (uint8_t)4);
-  value  = (uint32_t)_i2c.read() << 24; // value highest byte
-  value |= (uint32_t)_i2c.read() << 16;
-  value |= (uint16_t)_i2c.read() <<  8;
-  value |=           _i2c.read();       // value lowest byte
-*/
   return value;
 }
 
@@ -261,7 +243,7 @@
 {
   // save existing timing budget
   uint32_t budget_us = getMeasurementTimingBudget();
-  printf("budget_us = %d", budget_us);
+
   switch (mode)
   {
     case Short:
@@ -325,7 +307,6 @@
 
   return true;
 }
-
 // Set the measurement timing budget in microseconds, which is the time allowed
 // for one measurement. A longer timing budget allows for more accurate
 // measurements.
@@ -380,7 +361,6 @@
   // "Update Range Timing B timeout"
   writeReg16Bit(RANGE_CONFIG__TIMEOUT_MACROP_B, encodeTimeout(
     timeoutMicrosecondsToMclks(range_config_timeout_us, macro_period_us)));
-    printf("it is true\r\n");
   // VL53L1_calc_timeout_register_values() end
 
   return true;
@@ -456,6 +436,7 @@
     {
       if (checkTimeoutExpired())
       {
+        printf("i timed out\r\n");
         did_timeout = true;
         ranging_data.range_status = None;
         ranging_data.range_mm = 0;
@@ -465,7 +446,6 @@
       }
     }
   }
-
   readResults();
 
   if (!calibrated)
@@ -571,13 +551,13 @@
 //  char infoToWrite[2];
   char infoToRead[17];
   //_i2c.beginTransmission(address);
-  _i2c.write((RESULT__RANGE_STATUS >> 8) & 0xFF); // reg high byte
-  _i2c.write( RESULT__RANGE_STATUS       & 0xFF); // reg low byte
-//  last_status = _i2c.endTransmission();
+    char data_write[2];
+    data_write[0] = (RESULT__RANGE_STATUS >> 8) & 0xFF; //MSB of register address 
+    data_write[1] = RESULT__RANGE_STATUS & 0xFF; //LSB of register address 
 
-//  _i2c.requestFrom(address, (uint8_t)17);
+    _i2c.write(address, data_write, 2); 
+
   _i2c.read(address, infoToRead, 17, 0);
-
   results.range_status = infoToRead[0];
 
 //  infoToRead[1]; // report_status: not used