AS_BH1750A.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. This is a (Arduino) library for the BH1750FVI Digital Light Sensor.
  3. Description:
  4. http://www.rohm.com/web/global/products/-/product/BH1750FVI
  5. Datasheet:
  6. http://rohmfs.rohm.com/en/products/databook/datasheet/ic/sensor/light/bh1750fvi-e.pdf
  7. Copyright (c) 2013 Alexander Schulz. All right reserved.
  8. This library is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU Lesser General Public
  10. License as published by the Free Software Foundation; either
  11. version 2.1 of the License, or (at your option) any later version.
  12. This library is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. Lesser General Public License for more details.
  16. You should have received a copy of the GNU Lesser General Public
  17. License along with this library; if not, write to the Free Software
  18. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #ifndef AS_BH1750A_h
  21. #define AS_BH1750A_h
  22. #if (ARDUINO >= 100)
  23. #include <Arduino.h>
  24. #else
  25. #include <WProgram.h>
  26. #endif
  27. #include "Wire.h"
  28. // Mögliche I2C Adressen
  29. #define BH1750_DEFAULT_I2CADDR 0x23
  30. #define BH1750_SECOND_I2CADDR 0x5C
  31. // MTreg Werte
  32. // Default
  33. #define BH1750_MTREG_DEFAULT 69
  34. // Sensitivity : default = 0.45
  35. #define BH1750_MTREG_MIN 31
  36. // Sensitivity : default = 3.68
  37. #define BH1750_MTREG_MAX 254
  38. // Hardware Modi
  39. // No active state
  40. #define BH1750_POWER_DOWN 0x00
  41. // Wating for measurment command
  42. #define BH1750_POWER_ON 0x01
  43. // Reset data register value - not accepted in POWER_DOWN mode
  44. #define BH1750_RESET 0x07
  45. // Start measurement at 1lx resolution. Measurement time is approx 120ms.
  46. #define BH1750_CONTINUOUS_HIGH_RES_MODE 0x10
  47. // Start measurement at 0.5lx resolution. Measurement time is approx 120ms.
  48. #define BH1750_CONTINUOUS_HIGH_RES_MODE_2 0x11
  49. // Start measurement at 4lx resolution. Measurement time is approx 16ms.
  50. #define BH1750_CONTINUOUS_LOW_RES_MODE 0x13
  51. // Start measurement at 1lx resolution. Measurement time is approx 120ms.
  52. // Device is automatically set to Power Down after measurement.
  53. #define BH1750_ONE_TIME_HIGH_RES_MODE 0x20
  54. // Start measurement at 0.5lx resolution. Measurement time is approx 120ms.
  55. // Device is automatically set to Power Down after measurement.
  56. #define BH1750_ONE_TIME_HIGH_RES_MODE_2 0x21
  57. // Start measurement at 1lx resolution. Measurement time is approx 120ms.
  58. // Device is automatically set to Power Down after measurement.
  59. #define BH1750_ONE_TIME_LOW_RES_MODE 0x23
  60. #define MAX_U_LONG 4294967295;
  61. /** Virtual Modi */
  62. typedef enum
  63. {
  64. RESOLUTION_LOW = (1), /** 4lx resolution. Measurement time is approx 16ms. */
  65. RESOLUTION_NORMAL = (2), /** 1lx resolution. Measurement time is approx 120ms. */
  66. RESOLUTION_HIGH = (3), /** 0,5lx resolution. Measurement time is approx 120ms. */
  67. RESOLUTION_AUTO_HIGH = (99) /** 0,11-1lx resolution. Measurement time is above 250ms. */
  68. }
  69. sensors_resolution_t;
  70. typedef void (*DelayFuncPtr)(unsigned long);
  71. typedef unsigned long (*TimeFuncPtr)(void);
  72. /**
  73. * BH1750 driver class.
  74. */
  75. class AS_BH1750A {
  76. public:
  77. /**
  78. * Constructor.
  79. * Erlaubt die I2C-Adresse des Sensors zu ändern.
  80. * Standardadresse: 0x23, Alternativadresse: 0x5C.
  81. * Es sind entsprechende Konstanten definiert: BH1750_DEFAULT_I2CADDR und BH1750_SECOND_I2CADDR.
  82. * Bei Nichtangabe wird die Standardadresse verwendet.
  83. * Um die Alternativadresse zu nutzen, muss der Sensorpin 'ADR' des Chips auf VCC gelegt werden.
  84. */
  85. AS_BH1750A(uint8_t address = BH1750_DEFAULT_I2CADDR);
  86. /**
  87. * Führt die anfängliche Initialisierung des Sensors.
  88. * Mögliche Parameter:
  89. * - Modus für die Sensorauflösung:
  90. * -- RESOLUTION_LOW: Physische Sensormodus mit 4 lx Auflösung. Messzeit ca. 16ms. Bereich 0-54612.
  91. * -- RESOLUTION_NORMAL: Physische Sensormodus mit 1 lx Auflösung. Messzeit ca. 120ms. Bereich 0-54612.
  92. * -- RESOLUTION_HIGH: Physische Sensormodus mit 0,5 lx Auflösung. Messzeit ca. 120ms. Bereich 0-54612.
  93. * (Die Messbereiche können durch Änderung des MTreg verschoben werden.)
  94. * -- RESOLUTION_AUTO_HIGH: Die Werte im MTreg werden je nach Helligkeit automatisch so angepasst,
  95. * dass eine maximalmögliche Auflösung und Messbereich erziehlt werden.
  96. * Die messbaren Werte fangen von 0,11 lx und gehen bis über 100000 lx.
  97. * (ich weis nicht, wie genau die Werte in Grenzbereichen sind,
  98. * besonders bei hohen Werte habe ich meine Zweifel.)
  99. * Auflösung im Unteren Bereich 0,13 lx, im mittleren 0,5 lx, im oberen 1-2 lx.
  100. * Die Messzeiten verlängern sich durch mehrfache Messungen und
  101. * die Änderungen von Measurement Time (MTreg) bis max. ca. 500 ms.
  102. *
  103. * - AutoPowerDown: true = Der Sensor wird nach der Messung in den Stromsparmodus versetzt.
  104. * Das spätere Aufwecken wird ggf. automatisch vorgenommen, braucht jedoch geringfügig mehr Zeit.
  105. *
  106. * Defaultwerte: RESOLUTION_AUTO_HIGH, true, delay()
  107. *
  108. */
  109. bool begin(sensors_resolution_t mode = RESOLUTION_AUTO_HIGH, bool autoPowerDown = true);
  110. /**
  111. * Erlaub eine Prüfung, ob ein (ansprechbarer) BH1750-Sensor vorhanden ist.
  112. */
  113. bool isPresent(void);
  114. /**
  115. * Liefert aktuell gemessenen Wert für Helligkeit in lux (lx).
  116. * Falls sich der Sensorf in Stromsparmodus befindet, wird er automatisch geweckt.
  117. *
  118. * Wurde der Sensor (noch) nicht initialisiert (begin), wird der Wert -1 geliefert.
  119. *
  120. * Mögliche Parameter:
  121. *
  122. * - DelayFuncPtr: delay(n) Möglichkeit, eigene Delay-Funktion mitzugeben (z.B. um sleep-Modus zu verwenden).
  123. *
  124. * Defaultwerte: delay()
  125. *
  126. */
  127. float readLightLevel(DelayFuncPtr fDelayPtr = &delay, TimeFuncPtr fTimePtr = &millis);
  128. bool startMeasurementAsync(TimeFuncPtr fTimePtr = &millis);
  129. bool isMeasurementReady(void);
  130. float readLightLevelAsync();
  131. //float checkAndReadLightLevelAsync(TimeFuncPtr fTimePtr);
  132. //void reset(void);
  133. unsigned long nextDelay(void);
  134. /**
  135. * Schickt den Sensor in Stromsparmodus.
  136. * Funktionier nur, wenn der Sensor bereits initialisiert wurde.
  137. */
  138. void powerDown(void);
  139. //bool delayExpired(); // TEST
  140. private:
  141. int _address;
  142. uint8_t _hardwareMode;
  143. uint8_t _MTreg;
  144. float _MTregFactor;
  145. sensors_resolution_t _virtualMode;
  146. bool _autoPowerDown;
  147. bool _valueReaded;
  148. bool selectResolutionMode(uint8_t mode);
  149. void defineMTReg(uint8_t val);
  150. void powerOn();
  151. //void reset(void);
  152. uint16_t readRawLevel(void);
  153. float convertRawValue(uint16_t raw);
  154. bool isInitialized();
  155. bool write8(uint8_t data);
  156. TimeFuncPtr _fTimePtr;
  157. int _stage = 0;
  158. int _nextDelay = 0;
  159. int _lastTimestamp = 0;
  160. float _lastResult = -100;
  161. bool delayExpired();
  162. void selectAutoMode();
  163. // TEST
  164. //float readLightLevel_alt(DelayFuncPtr fDelayPtr = &delay);
  165. unsigned long getModeDelay();
  166. };
  167. #endif