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.

155 lines
5.2 KiB

  1. // -----------------------------------------------------------------------------
  2. // BH1750 Liminosity sensor over I2C
  3. // Copyright (C) 2017 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. // Configure
  54. _configure();
  55. delay(10);
  56. }
  57. // Descriptive name of the sensor
  58. String description() {
  59. char buffer[25];
  60. snprintf(buffer, sizeof(buffer), "BH1750 @ I2C (0x%02X)", _address);
  61. return String(buffer);
  62. }
  63. // Type for slot # index
  64. unsigned char type(unsigned char index) {
  65. _error = SENSOR_ERROR_OK;
  66. if (index == 0) return MAGNITUDE_LUX;
  67. _error = SENSOR_ERROR_OUT_OF_RANGE;
  68. return MAGNITUDE_NONE;
  69. }
  70. // Current value for slot # index
  71. double value(unsigned char index) {
  72. _error = SENSOR_ERROR_OK;
  73. if (index == 0) return _read();
  74. _error = SENSOR_ERROR_OUT_OF_RANGE;
  75. return 0;
  76. }
  77. protected:
  78. void _configure() {
  79. #if I2C_USE_BRZO
  80. uint8_t buffer[1] = {_mode};
  81. brzo_i2c_start_transaction(_address, I2C_SCL_FREQUENCY);
  82. brzo_i2c_write(buffer, 1, false);
  83. brzo_i2c_end_transaction();
  84. #else
  85. Wire.beginTransmission(_address);
  86. Wire.write(_mode);
  87. Wire.endTransmission();
  88. #endif
  89. }
  90. double _read() {
  91. double level;
  92. uint8_t buffer[2];
  93. // For one-shot modes reconfigure sensor & wait for conversion
  94. if (_mode & 0x20) {
  95. _configure();
  96. // According to datasheet
  97. // conversion time is ~16ms for low resolution
  98. // and ~120 for high resolution
  99. // but more time is needed
  100. unsigned long wait = (_mode & 0x02) ? 24 : 180;
  101. unsigned long start = millis();
  102. while (millis() - start < wait) delay(1);
  103. }
  104. #if I2C_USE_BRZO
  105. brzo_i2c_start_transaction(_address, I2C_SCL_FREQUENCY);
  106. brzo_i2c_read(buffer, 2, false);
  107. brzo_i2c_end_transaction();
  108. #else
  109. Wire.beginTransmission(_address);
  110. Wire.requestFrom(_address, (unsigned char) 2);
  111. buffer[0] = Wire.read();
  112. buffer[1] = Wire.read();
  113. Wire.endTransmission();
  114. #endif
  115. level = buffer[0] * 256 + buffer[1];
  116. return level / 1.2;
  117. }
  118. unsigned char _mode;
  119. };
  120. #endif // SENSOR_SUPPORT && SI7021_SUPPORT