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.7 KiB

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