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-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. _dirty = false;
  44. // I2C auto-discover
  45. unsigned char addresses[] = {0x23, 0x5C};
  46. _address = _begin_i2c(_address, sizeof(addresses), addresses);
  47. if (_address == 0) return;
  48. // Run configuration on next update
  49. _run_configure = true;
  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