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.

172 lines
5.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. #if I2C_USE_BRZO
  10. #include <brzo_i2c.h>
  11. #else
  12. #include <Wire.h>
  13. #endif
  14. #define BH1750_CONTINUOUS_HIGH_RES_MODE 0x10 // Start measurement at 1lx resolution. Measurement time is approx 120ms.
  15. #define BH1750_CONTINUOUS_HIGH_RES_MODE_2 0x11 // Start measurement at 0.5lx resolution. Measurement time is approx 120ms.
  16. #define BH1750_CONTINUOUS_LOW_RES_MODE 0x13 // Start measurement at 4lx resolution. Measurement time is approx 16ms.
  17. #define BH1750_ONE_TIME_HIGH_RES_MODE 0x20 // Start measurement at 1lx resolution. Measurement time is approx 120ms.
  18. // Device is automatically set to Power Down after measurement.
  19. #define BH1750_ONE_TIME_HIGH_RES_MODE_2 0x21 // Start measurement at 0.5lx resolution. Measurement time is approx 120ms.
  20. // Device is automatically set to Power Down after measurement.
  21. #define BH1750_ONE_TIME_LOW_RES_MODE 0x23 // Start measurement at 1lx resolution. Measurement time is approx 120ms.
  22. // Device is automatically set to Power Down after measurement.
  23. class BH1750Sensor : public I2CSensor {
  24. public:
  25. // ---------------------------------------------------------------------
  26. // Public
  27. // ---------------------------------------------------------------------
  28. BH1750Sensor(): I2CSensor() {
  29. _sensor_id = SENSOR_BH1750_ID;
  30. _count = 1;
  31. }
  32. // ---------------------------------------------------------------------
  33. void setMode(unsigned char mode) {
  34. if (_mode == mode) return;
  35. _mode = mode;
  36. _dirty = true;
  37. }
  38. // ---------------------------------------------------------------------
  39. unsigned char getMode() {
  40. return _mode;
  41. }
  42. // ---------------------------------------------------------------------
  43. // Sensor API
  44. // ---------------------------------------------------------------------
  45. // Initialization method, must be idempotent
  46. void begin() {
  47. if (!_dirty) return;
  48. _dirty = false;
  49. // I2C auto-discover
  50. unsigned char addresses[] = {0x23, 0x5C};
  51. _address = _begin_i2c(_address, sizeof(addresses), addresses);
  52. if (_address == 0) return;
  53. // Run configuration on next update
  54. _run_configure = true;
  55. }
  56. // Descriptive name of the sensor
  57. String description() {
  58. char buffer[25];
  59. snprintf(buffer, sizeof(buffer), "BH1750 @ I2C (0x%02X)", _address);
  60. return String(buffer);
  61. }
  62. // Type for slot # index
  63. unsigned char type(unsigned char index) {
  64. _error = SENSOR_ERROR_OK;
  65. if (index == 0) return MAGNITUDE_LUX;
  66. _error = SENSOR_ERROR_OUT_OF_RANGE;
  67. return MAGNITUDE_NONE;
  68. }
  69. // Pre-read hook (usually to populate registers with up-to-date data)
  70. void pre() {
  71. _error = SENSOR_ERROR_OK;
  72. _lux = _read();
  73. }
  74. // Current value for slot # index
  75. double value(unsigned char index) {
  76. _error = SENSOR_ERROR_OK;
  77. if (index == 0) return _lux;
  78. _error = SENSOR_ERROR_OUT_OF_RANGE;
  79. return 0;
  80. }
  81. protected:
  82. void _configure() {
  83. #if I2C_USE_BRZO
  84. uint8_t buffer[1] = {_mode};
  85. brzo_i2c_start_transaction(_address, I2C_SCL_FREQUENCY);
  86. brzo_i2c_write(buffer, 1, false);
  87. brzo_i2c_end_transaction();
  88. #else
  89. Wire.beginTransmission(_address);
  90. Wire.write(_mode);
  91. Wire.endTransmission();
  92. #endif
  93. }
  94. double _read() {
  95. double level;
  96. uint8_t buffer[2];
  97. // For one-shot modes reconfigure sensor & wait for conversion
  98. if (_run_configure) {
  99. _configure();
  100. // According to datasheet
  101. // conversion time is ~16ms for low resolution
  102. // and ~120 for high resolution
  103. // but more time is needed
  104. unsigned long wait = (_mode & 0x02) ? 24 : 180;
  105. unsigned long start = millis();
  106. while (millis() - start < wait) delay(1);
  107. // Keep on running configure each time if one-shot mode
  108. _run_configure = _mode & 0x20;
  109. }
  110. #if I2C_USE_BRZO
  111. brzo_i2c_start_transaction(_address, I2C_SCL_FREQUENCY);
  112. brzo_i2c_read(buffer, 2, false);
  113. brzo_i2c_end_transaction();
  114. #else
  115. Wire.beginTransmission(_address);
  116. Wire.requestFrom(_address, (unsigned char) 2);
  117. buffer[0] = Wire.read();
  118. buffer[1] = Wire.read();
  119. Wire.endTransmission();
  120. #endif
  121. // Check data
  122. if (buffer[0] == 0xFF) {
  123. _error = SENSOR_ERROR_CRC;
  124. _run_configure = true;
  125. return 0;
  126. }
  127. level = buffer[0] * 256 + buffer[1];
  128. return level / 1.2;
  129. }
  130. unsigned char _mode;
  131. bool _run_configure = false;
  132. double _lux = 0;
  133. };
  134. #endif // SENSOR_SUPPORT && BH1750_SUPPORT