Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.2 KiB

  1. // -----------------------------------------------------------------------------
  2. // VL53L1X Sensor over I2C
  3. // Copyright (C) 2017-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && VL53L1X_SUPPORT
  6. #pragma once
  7. #undef I2C_SUPPORT
  8. #define I2C_SUPPORT 1 // Explicitly request I2C support.
  9. #include "Arduino.h"
  10. #include "I2CSensor.h"
  11. #include "VL53L1X.h"
  12. class VL53L1XSensor : public I2CSensor {
  13. public:
  14. // ---------------------------------------------------------------------
  15. // Public
  16. // ---------------------------------------------------------------------
  17. VL53L1XSensor(): I2CSensor() {
  18. _count = 1;
  19. _sensor_id = SENSOR_VL53L1X_ID;
  20. _vl53l1x = new VL53L1X();
  21. }
  22. ~VL53L1XSensor() {
  23. delete _vl53l1x;
  24. }
  25. // ---------------------------------------------------------------------
  26. void setDistanceMode(VL53L1X::DistanceMode mode) {
  27. _vl53l1x->setDistanceMode(mode);
  28. }
  29. void setMeasurementTimingBudget(uint32_t budget_us) {
  30. _vl53l1x->setMeasurementTimingBudget(budget_us);
  31. }
  32. void setInterMeasurementPeriod(unsigned int period) {
  33. if (_inter_measurement_period == period) return;
  34. _inter_measurement_period = period;
  35. _dirty = true;
  36. }
  37. // ---------------------------------------------------------------------
  38. // Sensor API
  39. // ---------------------------------------------------------------------
  40. void begin() {
  41. if (!_dirty) {
  42. return;
  43. }
  44. // I2C auto-discover
  45. unsigned char addresses[] = {0x29};
  46. _address = _begin_i2c(_address, sizeof(addresses), addresses);
  47. if (_address == 0) return;
  48. _vl53l1x->setAddress(_address);
  49. if (!_vl53l1x->init()) {
  50. return;
  51. };
  52. _vl53l1x->startContinuous(_inter_measurement_period);
  53. _ready = true;
  54. _dirty = false;
  55. }
  56. // Descriptive name of the sensor
  57. String description() {
  58. char buffer[21];
  59. snprintf(buffer, sizeof(buffer), "VL53L1X @ I2C (0x%02X)", _address);
  60. return String(buffer);
  61. }
  62. // Descriptive name of the slot # index
  63. String slot(unsigned char index) {
  64. return description();
  65. };
  66. // Type for slot # index
  67. unsigned char type(unsigned char index) {
  68. if (index == 0) return MAGNITUDE_DISTANCE;
  69. return MAGNITUDE_NONE;
  70. }
  71. // Pre-read hook (usually to populate registers with up-to-date data)
  72. void pre() {
  73. if (!_vl53l1x->dataReady()) {
  74. return;
  75. }
  76. _distance = (double) _vl53l1x->read(false) / 1000.00;
  77. }
  78. // Current value for slot # index
  79. double value(unsigned char index) {
  80. if (index != 0) return 0;
  81. return _distance;
  82. }
  83. protected:
  84. VL53L1X * _vl53l1x = NULL;
  85. unsigned int _inter_measurement_period;
  86. double _distance = 0;
  87. };
  88. #endif // SENSOR_SUPPORT && VL53L1X_SUPPORT