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.

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