src/vl53l1x.cpp
bool Vl53l1x::setDistanceMode(DistanceMode mode)
{
  // save existing timing budget
  uint32_t budget_us = getMeasurementTimingBudget();

  switch (mode)
  {
    case Short:
      // from VL53L1_preset_mode_standard_ranging_short_range()

      // timing config
      writeReg(RANGE_CONFIG__VCSEL_PERIOD_A, 0x07);
      writeReg(RANGE_CONFIG__VCSEL_PERIOD_B, 0x05);
      writeReg(RANGE_CONFIG__VALID_PHASE_HIGH, 0x38);

      // dynamic config
      writeReg(SD_CONFIG__WOI_SD0, 0x07);
      writeReg(SD_CONFIG__WOI_SD1, 0x05);
      writeReg(SD_CONFIG__INITIAL_PHASE_SD0, 6); // tuning parm default
      writeReg(SD_CONFIG__INITIAL_PHASE_SD1, 6); // tuning parm default

      break;

    case Medium:
      // from VL53L1_preset_mode_standard_ranging()

      // timing config
      writeReg(RANGE_CONFIG__VCSEL_PERIOD_A, 0x0B);
      writeReg(RANGE_CONFIG__VCSEL_PERIOD_B, 0x09);
      writeReg(RANGE_CONFIG__VALID_PHASE_HIGH, 0x78);

      // dynamic config
      writeReg(SD_CONFIG__WOI_SD0, 0x0B);
      writeReg(SD_CONFIG__WOI_SD1, 0x09);
      writeReg(SD_CONFIG__INITIAL_PHASE_SD0, 10); // tuning parm default
      writeReg(SD_CONFIG__INITIAL_PHASE_SD1, 10); // tuning parm default

      break;

    case Long: // long
      // from VL53L1_preset_mode_standard_ranging_long_range()

      // timing config
      writeReg(RANGE_CONFIG__VCSEL_PERIOD_A, 0x0F);
      writeReg(RANGE_CONFIG__VCSEL_PERIOD_B, 0x0D);
      writeReg(RANGE_CONFIG__VALID_PHASE_HIGH, 0xB8);

      // dynamic config
      writeReg(SD_CONFIG__WOI_SD0, 0x0F);
      writeReg(SD_CONFIG__WOI_SD1, 0x0D);
      writeReg(SD_CONFIG__INITIAL_PHASE_SD0, 14); // tuning parm default
      writeReg(SD_CONFIG__INITIAL_PHASE_SD1, 14); // tuning parm default

      break;

    default:
      // unrecognized mode - do nothing
      return false;
  }

  // reapply timing budget
  setMeasurementTimingBudget(budget_us);

  // save mode so it can be returned by getDistanceMode()
  distance_mode = mode;

  return true;
}
// set distance mode to Short, Medium, or Long
// based on VL53L1_SetDistanceMode()
set distance mode to Short, Medium, or Long
based on VL53L1_SetDistanceMode()
set distance mode to Short Medium or Long based on VL53L1_SetDistanceMode ()
bool Vl53l1x::setDistanceMode(DistanceMode mode)
{
  uint32_t budget_us = getMeasurementTimingBudget();
  switch (mode)
  {
    case Short:
      writeReg(RANGE_CONFIG__VCSEL_PERIOD_A, 0x07);
      writeReg(RANGE_CONFIG__VCSEL_PERIOD_B, 0x05);
      writeReg(RANGE_CONFIG__VALID_PHASE_HIGH, 0x38);
      writeReg(SD_CONFIG__WOI_SD0, 0x07);
      writeReg(SD_CONFIG__WOI_SD1, 0x05);
      writeReg(SD_CONFIG__INITIAL_PHASE_SD0, 6); 
      writeReg(SD_CONFIG__INITIAL_PHASE_SD1, 6); 
      break;
    case Medium:
      writeReg(RANGE_CONFIG__VCSEL_PERIOD_A, 0x0B);
      writeReg(RANGE_CONFIG__VCSEL_PERIOD_B, 0x09);
      writeReg(RANGE_CONFIG__VALID_PHASE_HIGH, 0x78);
      writeReg(SD_CONFIG__WOI_SD0, 0x0B);
      writeReg(SD_CONFIG__WOI_SD1, 0x09);
      writeReg(SD_CONFIG__INITIAL_PHASE_SD0, 10); 
      writeReg(SD_CONFIG__INITIAL_PHASE_SD1, 10); 
      break;
    case Long: 
      writeReg(RANGE_CONFIG__VCSEL_PERIOD_A, 0x0F);
      writeReg(RANGE_CONFIG__VCSEL_PERIOD_B, 0x0D);
      writeReg(RANGE_CONFIG__VALID_PHASE_HIGH, 0xB8);
      writeReg(SD_CONFIG__WOI_SD0, 0x0F);
      writeReg(SD_CONFIG__WOI_SD1, 0x0D);
      writeReg(SD_CONFIG__INITIAL_PHASE_SD0, 14); 
      writeReg(SD_CONFIG__INITIAL_PHASE_SD1, 14); 
      break;
    default:
      return false;
  }
  setMeasurementTimingBudget(budget_us);
  distance_mode = mode;
  return true;
}
bool Vl53l1x :: setDistanceMode ( DistanceMode mode ) { uint32_t budget_us = getMeasurementTimingBudget ( ) ; switch ( mode ) { case Short : writeReg ( RANGE_CONFIG__VCSEL_PERIOD_A , 0x07 ) ; writeReg ( RANGE_CONFIG__VCSEL_PERIOD_B , 0x05 ) ; writeReg ( RANGE_CONFIG__VALID_PHASE_HIGH , 0x38 ) ; writeReg ( SD_CONFIG__WOI_SD0 , 0x07 ) ; writeReg ( SD_CONFIG__WOI_SD1 , 0x05 ) ; writeReg ( SD_CONFIG__INITIAL_PHASE_SD0 , 6 ) ; writeReg ( SD_CONFIG__INITIAL_PHASE_SD1 , 6 ) ; break ; case Medium : writeReg ( RANGE_CONFIG__VCSEL_PERIOD_A , 0x0B ) ; writeReg ( RANGE_CONFIG__VCSEL_PERIOD_B , 0x09 ) ; writeReg ( RANGE_CONFIG__VALID_PHASE_HIGH , 0x78 ) ; writeReg ( SD_CONFIG__WOI_SD0 , 0x0B ) ; writeReg ( SD_CONFIG__WOI_SD1 , 0x09 ) ; writeReg ( SD_CONFIG__INITIAL_PHASE_SD0 , 10 ) ; writeReg ( SD_CONFIG__INITIAL_PHASE_SD1 , 10 ) ; break ; case Long : writeReg ( RANGE_CONFIG__VCSEL_PERIOD_A , 0x0F ) ; writeReg ( RANGE_CONFIG__VCSEL_PERIOD_B , 0x0D ) ; writeReg ( RANGE_CONFIG__VALID_PHASE_HIGH , 0xB8 ) ; writeReg ( SD_CONFIG__WOI_SD0 , 0x0F ) ; writeReg ( SD_CONFIG__WOI_SD1 , 0x0D ) ; writeReg ( SD_CONFIG__INITIAL_PHASE_SD0 , 14 ) ; writeReg ( SD_CONFIG__INITIAL_PHASE_SD1 , 14 ) ; break ; default : return false ; } setMeasurementTimingBudget ( budget_us ) ; distance_mode = mode ; return true ; }
set distance mode to Short, Medium, or Long
based on VL53L1_SetDistanceMode()
set distance mode to Short Medium or Long based on VL53L1_SetDistanceMode ()
// save existing timing budget // from VL53L1_preset_mode_standard_ranging_short_range() // timing config // dynamic config // tuning parm default // tuning parm default // from VL53L1_preset_mode_standard_ranging() // timing config // dynamic config // tuning parm default // tuning parm default // long // from VL53L1_preset_mode_standard_ranging_long_range() // timing config // dynamic config // tuning parm default // tuning parm default // unrecognized mode - do nothing // reapply timing budget // save mode so it can be returned by getDistanceMode()