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.

134 lines
4.6 KiB

  1. // -----------------------------------------------------------------------------
  2. // BH1750 Liminosity sensor over I2C
  3. // Copyright (C) 2017-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && BH1750_SUPPORT
  6. #pragma once
  7. #include "I2CSensor.h"
  8. #define BH1750_CONTINUOUS_HIGH_RES_MODE 0x10 // Start measurement at 1lx resolution. Measurement time is approx 120ms.
  9. #define BH1750_CONTINUOUS_HIGH_RES_MODE_2 0x11 // Start measurement at 0.5lx resolution. Measurement time is approx 120ms.
  10. #define BH1750_CONTINUOUS_LOW_RES_MODE 0x13 // Start measurement at 4lx resolution. Measurement time is approx 16ms.
  11. #define BH1750_ONE_TIME_HIGH_RES_MODE 0x20 // Start measurement at 1lx resolution. Measurement time is approx 120ms.
  12. // Device is automatically set to Power Down after measurement.
  13. #define BH1750_ONE_TIME_HIGH_RES_MODE_2 0x21 // Start measurement at 0.5lx resolution. Measurement time is approx 120ms.
  14. // Device is automatically set to Power Down after measurement.
  15. #define BH1750_ONE_TIME_LOW_RES_MODE 0x23 // Start measurement at 1lx resolution. Measurement time is approx 120ms.
  16. // Device is automatically set to Power Down after measurement.
  17. class BH1750Sensor : public I2CSensor<> {
  18. public:
  19. // ---------------------------------------------------------------------
  20. // Public
  21. // ---------------------------------------------------------------------
  22. BH1750Sensor() {
  23. _sensor_id = SENSOR_BH1750_ID;
  24. _count = 1;
  25. }
  26. // ---------------------------------------------------------------------
  27. void setMode(unsigned char mode) {
  28. if (_mode == mode) return;
  29. _mode = mode;
  30. _dirty = true;
  31. }
  32. // ---------------------------------------------------------------------
  33. unsigned char getMode() {
  34. return _mode;
  35. }
  36. // ---------------------------------------------------------------------
  37. // Sensor API
  38. // ---------------------------------------------------------------------
  39. // Initialization method, must be idempotent
  40. void begin() {
  41. if (!_dirty) return;
  42. // I2C auto-discover
  43. unsigned char addresses[] = {0x23, 0x5C};
  44. _address = _begin_i2c(_address, sizeof(addresses), addresses);
  45. if (_address == 0) return;
  46. // Run configuration on next update
  47. _run_configure = true;
  48. _ready = true;
  49. _dirty = false;
  50. }
  51. // Descriptive name of the sensor
  52. String description() {
  53. char buffer[25];
  54. snprintf(buffer, sizeof(buffer), "BH1750 @ I2C (0x%02X)", _address);
  55. return String(buffer);
  56. }
  57. // Type for slot # index
  58. unsigned char type(unsigned char index) {
  59. if (index == 0) return MAGNITUDE_LUX;
  60. return MAGNITUDE_NONE;
  61. }
  62. // Pre-read hook (usually to populate registers with up-to-date data)
  63. void pre() {
  64. _error = SENSOR_ERROR_OK;
  65. _lux = _read();
  66. }
  67. // Current value for slot # index
  68. double value(unsigned char index) {
  69. if (index == 0) return _lux;
  70. return 0;
  71. }
  72. protected:
  73. double _read() {
  74. // For one-shot modes reconfigure sensor & wait for conversion
  75. if (_run_configure) {
  76. // Configure mode
  77. i2c_write_uint8(_address, _mode);
  78. // According to datasheet
  79. // conversion time is ~16ms for low resolution
  80. // and ~120 for high resolution
  81. // but more time is needed
  82. unsigned long wait = (_mode & 0x02) ? 24 : 180;
  83. unsigned long start = millis();
  84. while (millis() - start < wait) delay(1);
  85. // Keep on running configure each time if one-shot mode
  86. _run_configure = _mode & 0x20;
  87. }
  88. double level = (double) i2c_read_uint16(_address);
  89. if (level == 0xFFFF) {
  90. _error = SENSOR_ERROR_CRC;
  91. _run_configure = true;
  92. return 0;
  93. }
  94. return level / 1.2;
  95. }
  96. unsigned char _mode;
  97. bool _run_configure = false;
  98. double _lux = 0;
  99. };
  100. #endif // SENSOR_SUPPORT && BH1750_SUPPORT