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.

138 lines
4.8 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. _error = SENSOR_ERROR_OK;
  60. if (index == 0) return MAGNITUDE_LUX;
  61. _error = SENSOR_ERROR_OUT_OF_RANGE;
  62. return MAGNITUDE_NONE;
  63. }
  64. // Pre-read hook (usually to populate registers with up-to-date data)
  65. void pre() {
  66. _error = SENSOR_ERROR_OK;
  67. _lux = _read();
  68. }
  69. // Current value for slot # index
  70. double value(unsigned char index) {
  71. _error = SENSOR_ERROR_OK;
  72. if (index == 0) return _lux;
  73. _error = SENSOR_ERROR_OUT_OF_RANGE;
  74. return 0;
  75. }
  76. protected:
  77. double _read() {
  78. // For one-shot modes reconfigure sensor & wait for conversion
  79. if (_run_configure) {
  80. // Configure mode
  81. i2c_write_uint8(_address, _mode);
  82. // According to datasheet
  83. // conversion time is ~16ms for low resolution
  84. // and ~120 for high resolution
  85. // but more time is needed
  86. unsigned long wait = (_mode & 0x02) ? 24 : 180;
  87. unsigned long start = millis();
  88. while (millis() - start < wait) delay(1);
  89. // Keep on running configure each time if one-shot mode
  90. _run_configure = _mode & 0x20;
  91. }
  92. double level = (double) i2c_read_uint16(_address);
  93. if (level == 0xFFFF) {
  94. _error = SENSOR_ERROR_CRC;
  95. _run_configure = true;
  96. return 0;
  97. }
  98. return level / 1.2;
  99. }
  100. unsigned char _mode;
  101. bool _run_configure = false;
  102. double _lux = 0;
  103. };
  104. #endif // SENSOR_SUPPORT && BH1750_SUPPORT