AskSin++
Vl53l0x.h
1 //- -----------------------------------------------------------------------------------------------------------------------
2 // AskSin++
3 // 2018-04-03 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
4 // 2018-11-14 jp112sdl Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
5 //- -----------------------------------------------------------------------------------------------------------------------
6 
7 #ifndef __SENSORS_VL53L0X_h__
8 #define __SENSORS_VL53L0X_h__
9 
10 #include <Sensors.h>
11 #include <Wire.h>
12 #include <VL53L0X.h>
13 
14 namespace as {
15 
16 enum Precision {
17  HIGH_ACCURACY,
18  HIGH_SPEED
19 };
20 
21 enum RangeMode {
22  SHORT_RANGE,
23  LONG_RANGE
24 };
25 
26 //https://github.com/pololu/vl53l0x-arduino
27 template <uint8_t ADDR=0x29, uint16_t TIMEOUT=500, uint8_t RANGEMODE=RangeMode::SHORT_RANGE, uint8_t PRECISION= Precision::HIGH_ACCURACY>
28 class Vl53l0x : public Sensor {
29  VL53L0X _vl53l0x;
30  uint16_t _distance_mm;
31 public:
32  Vl53l0x () {}
33  void init() {
34  _vl53l0x.setAddress(ADDR);
35 
36  _present = _vl53l0x.init();
37 
38  _vl53l0x.setTimeout(TIMEOUT);
39 
40  DPRINTLN(F("VL53L0X SENSOR "));
41  //DPRINT(F("- RANGEMODE = "));
42  if (RANGEMODE == LONG_RANGE) {
43  _vl53l0x.setSignalRateLimit(0.1);
44  _vl53l0x.setVcselPulsePeriod(VL53L0X::VcselPeriodPreRange, 18);
45  _vl53l0x.setVcselPulsePeriod(VL53L0X::VcselPeriodFinalRange, 14);
46  //DPRINTLN(F("LONG"));
47  } else {
48  //DPRINTLN(F("SHORT"));
49  }
50 
51  //DPRINT(F("- PRECISION = "));
52  if (PRECISION == HIGH_SPEED) {
53  _vl53l0x.setMeasurementTimingBudget(20000);
54  //DPRINTLN(F("SPEED"));
55  }
56 
57  if (PRECISION == HIGH_ACCURACY) {
58  _vl53l0x.setMeasurementTimingBudget(200000);
59  //DPRINTLN(F("ACCURACY"));
60  }
61 
62  if (_present) DPRINTLN(F("- OK")); else DPRINTLN(F("- ERROR"));
63  }
64 
65  bool measure(__attribute__((unused)) bool async=false) {
66  if( present() == true ) {
67  _distance_mm = _vl53l0x.readRangeSingleMillimeters();
68  DPRINT("VL53L0X measure distance = ");DDECLN(_distance_mm);
69  bool timeOut = _vl53l0x.timeoutOccurred();
70  if (timeOut) DPRINTLN(F("VL53L0X measure timeout occured"));
71  return !(timeOut);
72  }
73  return false;
74  }
75 
76  uint16_t DistanceMM () { return _distance_mm; }
77 };
78 
79 }
80 
81 #endif
as::Vl53l0x
Definition: Vl53l0x.h:28
as::Sensor
Definition: Sensors.h:11