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.

135 lines
4.6 KiB

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