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.

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