From 412da7034b512cd93ea01dc0d16ba1d94ccb7913 Mon Sep 17 00:00:00 2001 From: schaeferpm <44257148+schaeferpm@users.noreply.github.com> Date: Sun, 17 May 2020 08:38:05 +0200 Subject: [PATCH 01/16] Update BH1750.cpp The configure method always pushed the old configuration to the device. --- src/BH1750.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BH1750.cpp b/src/BH1750.cpp index dd9c18f..c7469ba 100644 --- a/src/BH1750.cpp +++ b/src/BH1750.cpp @@ -102,7 +102,7 @@ bool BH1750::configure(Mode mode) { // Send mode to sensor I2C->beginTransmission(BH1750_I2CADDR); - __wire_write((uint8_t)BH1750_MODE); + __wire_write((uint8_t)mode); ack = I2C->endTransmission(); // Wait a few moments to wake up From 87ecc0eb694ebb6ce92da9baf539ae634f5b64ee Mon Sep 17 00:00:00 2001 From: Mike Wright <mike-kiwi@users.noreply.github.com> Date: Tue, 11 Aug 2020 01:12:42 +1200 Subject: [PATCH 02/16] Wemos / Lolin D1 Mini Pro uses Wire.begin(D2, D1); --- examples/BH1750test/BH1750test.ino | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/BH1750test/BH1750test.ino b/examples/BH1750test/BH1750test.ino index 7b6b272..4ca5c10 100644 --- a/examples/BH1750test/BH1750test.ino +++ b/examples/BH1750test/BH1750test.ino @@ -34,6 +34,7 @@ void setup(){ // Initialize the I2C bus (BH1750 library doesn't do this automatically) Wire.begin(); // On esp8266 you can select SCL and SDA pins using Wire.begin(D4, D3); + // For Wemos / Lolin D1 Mini Pro and the Ambient Light shield use Wire.begin(D2, D1); lightMeter.begin(); From fbc4981ee25ff73fde28713821b813cb8a1375f8 Mon Sep 17 00:00:00 2001 From: coelner <waenger@gmail.com> Date: Tue, 23 Oct 2018 18:46:13 +0200 Subject: [PATCH 03/16] fix begin() for MTreg default value --- src/BH1750.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/BH1750.cpp b/src/BH1750.cpp index c7469ba..04f485f 100644 --- a/src/BH1750.cpp +++ b/src/BH1750.cpp @@ -75,6 +75,8 @@ bool BH1750::begin(Mode mode, byte addr, TwoWire *i2c) { BH1750_I2CADDR = addr; } + //return to a known value + setMTreg(BH1750_DEFAULT_MTREG); // Configure sensor in specified mode return configure(mode); From b8f7d70bc14f0283e4966d0552fd7a4fba6b6c15 Mon Sep 17 00:00:00 2001 From: coelner <waenger@googlemail.com> Date: Wed, 14 Aug 2019 19:37:18 +0200 Subject: [PATCH 04/16] fix continuous mode reset --- src/BH1750.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/BH1750.cpp b/src/BH1750.cpp index 04f485f..8821213 100644 --- a/src/BH1750.cpp +++ b/src/BH1750.cpp @@ -229,11 +229,6 @@ float BH1750::readLightLevel(bool maxWait) { // Measurement result will be stored here float level = -1.0; - // Send mode to sensor - I2C->beginTransmission(BH1750_I2CADDR); - __wire_write((uint8_t)BH1750_MODE); - I2C->endTransmission(); - // Wait for measurement to be taken. // Measurements have a maximum measurement time and a typical measurement // time. The maxWait argument determines which measurement wait time is @@ -247,10 +242,18 @@ float BH1750::readLightLevel(bool maxWait) { switch (BH1750_MODE) { case BH1750::ONE_TIME_LOW_RES_MODE: + // Send mode to sensor + Wire.beginTransmission(BH1750_I2CADDR); + __wire_write((uint8_t)BH1750_MODE); + Wire.endTransmission(); maxWait ? _delay_ms(24 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG) : _delay_ms(16 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG); break; case BH1750::ONE_TIME_HIGH_RES_MODE: case BH1750::ONE_TIME_HIGH_RES_MODE_2: + // Send mode to sensor + Wire.beginTransmission(BH1750_I2CADDR); + __wire_write((uint8_t)BH1750_MODE); + Wire.endTransmission(); maxWait ? _delay_ms(180 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG) :_delay_ms(120 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG); break; default: From 8fa366cba60db84c58fe961f2f9b633dbcd97faa Mon Sep 17 00:00:00 2001 From: coelner <waenger@googlemail.com> Date: Fri, 14 Aug 2020 10:35:31 +0200 Subject: [PATCH 05/16] MTReg fix #53 add new I2C address example (see #71), [fix error in my master] --- src/BH1750.cpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/BH1750.cpp b/src/BH1750.cpp index 8821213..1578b8a 100644 --- a/src/BH1750.cpp +++ b/src/BH1750.cpp @@ -75,10 +75,8 @@ bool BH1750::begin(Mode mode, byte addr, TwoWire *i2c) { BH1750_I2CADDR = addr; } - //return to a known value - setMTreg(BH1750_DEFAULT_MTREG); - // Configure sensor in specified mode - return configure(mode); + // Configure sensor in specified mode and set default MTreg + return (configure(mode) && setMTreg(BH1750_DEFAULT_MTREG)); } @@ -243,17 +241,17 @@ float BH1750::readLightLevel(bool maxWait) { case BH1750::ONE_TIME_LOW_RES_MODE: // Send mode to sensor - Wire.beginTransmission(BH1750_I2CADDR); + I2C->beginTransmission(BH1750_I2CADDR); __wire_write((uint8_t)BH1750_MODE); - Wire.endTransmission(); + I2C->endTransmission(); maxWait ? _delay_ms(24 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG) : _delay_ms(16 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG); break; case BH1750::ONE_TIME_HIGH_RES_MODE: case BH1750::ONE_TIME_HIGH_RES_MODE_2: // Send mode to sensor - Wire.beginTransmission(BH1750_I2CADDR); + I2C->beginTransmission(BH1750_I2CADDR); __wire_write((uint8_t)BH1750_MODE); - Wire.endTransmission(); + I2C->.endTransmission(); maxWait ? _delay_ms(180 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG) :_delay_ms(120 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG); break; default: From 877e2687e078e01e741495ed72b025b520cbcc1f Mon Sep 17 00:00:00 2001 From: coelner <waenger@googlemail.com> Date: Sun, 1 Nov 2020 16:11:38 +0100 Subject: [PATCH 06/16] add new example, fix error in my master --- .../BH1750differentI2Caddress.ino | 63 +++++++++++++++++++ src/BH1750.cpp | 7 +-- 2 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 examples/BH1750differentI2Caddress/BH1750differentI2Caddress.ino diff --git a/examples/BH1750differentI2Caddress/BH1750differentI2Caddress.ino b/examples/BH1750differentI2Caddress/BH1750differentI2Caddress.ino new file mode 100644 index 0000000..6686ea8 --- /dev/null +++ b/examples/BH1750differentI2Caddress/BH1750differentI2Caddress.ino @@ -0,0 +1,63 @@ +/* + + Different I2C address BH1750 library usage example + + This example has some comments about advanced usage features. + + Connection: + + VCC -> 3V3 or 5V + GND -> GND + SCL -> SCL (A5 on Arduino Uno, Leonardo, etc or 21 on Mega and Due, on esp8266 free selectable) + SDA -> SDA (A4 on Arduino Uno, Leonardo, etc or 20 on Mega and Due, on esp8266 free selectable) + ADD -> VCC + + ADD pin is used to set sensor I2C address. If it has voltage greater or equal to + 0.7VCC voltage (e.g. you've connected it to VCC) the sensor address will be + 0x5C. In other case (if ADD voltage less than 0.7 * VCC) the sensor address will + be 0x23 (by default). + +*/ + +#include <Wire.h> +#include <BH1750.h> + +/* + BH1750 can be physically configured to use two I2C addresses: + - 0x23 (most common) (if ADD pin had < 0.7VCC voltage) + - 0x5C (if ADD pin had > 0.7VCC voltage) + + Library uses 0x23 address as default, but you can define any other address. + If you had troubles with default value - try to change it to 0x5C. + +*/ +BH1750 lightMeter(); + +void setup(){ + + Serial.begin(9600); + + // Initialize the I2C bus (BH1750 library doesn't do this automatically) + Wire.begin(); + // On esp8266 you can select SCL and SDA pins using Wire.begin(D4, D3); + + // begin returns a boolean that can be used to detect setup problems. + if (lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE, 0x5c)) { + Serial.println(F("BH1750 different I2C address begin")); + } + else { + Serial.println(F("Error initialising BH1750")); + } + +} + + +void loop() { + + float lux = lightMeter.readLightLevel(); + Serial.print("Light: "); + Serial.print(lux); + Serial.println(" lx"); + delay(1000); + +} diff --git a/src/BH1750.cpp b/src/BH1750.cpp index 1578b8a..f08c626 100644 --- a/src/BH1750.cpp +++ b/src/BH1750.cpp @@ -46,14 +46,13 @@ /** * Constructor - * @params addr Sensor address (0x76 or 0x72, see datasheet) + * @params addr Sensor address (0x23 or 0x5C, see datasheet) * - * On most sensor boards, it was 0x76 + * On most sensor boards, it was 0x23 */ BH1750::BH1750(byte addr) { BH1750_I2CADDR = addr; - // Allows user to change TwoWire instance I2C = &Wire; } @@ -251,7 +250,7 @@ float BH1750::readLightLevel(bool maxWait) { // Send mode to sensor I2C->beginTransmission(BH1750_I2CADDR); __wire_write((uint8_t)BH1750_MODE); - I2C->.endTransmission(); + I2C->endTransmission(); maxWait ? _delay_ms(180 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG) :_delay_ms(120 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG); break; default: From 88282991a35792a95dd50e5d3f89711789148466 Mon Sep 17 00:00:00 2001 From: coelner <waenger@googlemail.com> Date: Wed, 14 Aug 2019 19:37:18 +0200 Subject: [PATCH 07/16] fix continuous mode reset --- src/BH1750.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/BH1750.cpp b/src/BH1750.cpp index f08c626..9ebd7ff 100644 --- a/src/BH1750.cpp +++ b/src/BH1750.cpp @@ -240,17 +240,29 @@ float BH1750::readLightLevel(bool maxWait) { case BH1750::ONE_TIME_LOW_RES_MODE: // Send mode to sensor +<<<<<<< HEAD:src/BH1750.cpp I2C->beginTransmission(BH1750_I2CADDR); __wire_write((uint8_t)BH1750_MODE); I2C->endTransmission(); +======= + _wire->beginTransmission(BH1750_I2CADDR); + _wire->write((uint8_t)BH1750_MODE); + _wire->endTransmission(); +>>>>>>> ea80121 (fix continuous mode reset):BH1750.cpp maxWait ? _delay_ms(24 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG) : _delay_ms(16 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG); break; case BH1750::ONE_TIME_HIGH_RES_MODE: case BH1750::ONE_TIME_HIGH_RES_MODE_2: // Send mode to sensor +<<<<<<< HEAD:src/BH1750.cpp I2C->beginTransmission(BH1750_I2CADDR); __wire_write((uint8_t)BH1750_MODE); I2C->endTransmission(); +======= + _wire->beginTransmission(BH1750_I2CADDR); + _wire->write((uint8_t)BH1750_MODE); + _wire->endTransmission(); +>>>>>>> ea80121 (fix continuous mode reset):BH1750.cpp maxWait ? _delay_ms(180 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG) :_delay_ms(120 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG); break; default: From 11db606497dbddaf387b6f4c69ff601ce11e3667 Mon Sep 17 00:00:00 2001 From: coelner <waenger@googlemail.com> Date: Wed, 14 Aug 2019 20:31:57 +0200 Subject: [PATCH 08/16] backport slipped in twi changes --- src/BH1750.cpp | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/BH1750.cpp b/src/BH1750.cpp index 9ebd7ff..f08c626 100644 --- a/src/BH1750.cpp +++ b/src/BH1750.cpp @@ -240,29 +240,17 @@ float BH1750::readLightLevel(bool maxWait) { case BH1750::ONE_TIME_LOW_RES_MODE: // Send mode to sensor -<<<<<<< HEAD:src/BH1750.cpp I2C->beginTransmission(BH1750_I2CADDR); __wire_write((uint8_t)BH1750_MODE); I2C->endTransmission(); -======= - _wire->beginTransmission(BH1750_I2CADDR); - _wire->write((uint8_t)BH1750_MODE); - _wire->endTransmission(); ->>>>>>> ea80121 (fix continuous mode reset):BH1750.cpp maxWait ? _delay_ms(24 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG) : _delay_ms(16 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG); break; case BH1750::ONE_TIME_HIGH_RES_MODE: case BH1750::ONE_TIME_HIGH_RES_MODE_2: // Send mode to sensor -<<<<<<< HEAD:src/BH1750.cpp I2C->beginTransmission(BH1750_I2CADDR); __wire_write((uint8_t)BH1750_MODE); I2C->endTransmission(); -======= - _wire->beginTransmission(BH1750_I2CADDR); - _wire->write((uint8_t)BH1750_MODE); - _wire->endTransmission(); ->>>>>>> ea80121 (fix continuous mode reset):BH1750.cpp maxWait ? _delay_ms(180 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG) :_delay_ms(120 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG); break; default: From 9cf2fe14b4599f2f420fed72f2cc16b4b242042b Mon Sep 17 00:00:00 2001 From: coelner <waenger@googlemail.com> Date: Fri, 14 Aug 2020 18:01:33 +0200 Subject: [PATCH 09/16] upstream related #61 , #59 , #53 --- README.md | 3 +- examples/BH1750advanced/BH1750advanced.ino | 12 ++-- .../BH1750autoadjust/BH1750autoadjust.ino | 68 ++++++++++--------- examples/BH1750onetime/BH1750onetime.ino | 6 +- examples/BH1750two_i2c/BH1750two_i2c.ino | 6 +- keywords.txt | 2 +- src/BH1750.cpp | 67 ++++++++++++------ src/BH1750.h | 5 +- 8 files changed, 103 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index d536b03..876004e 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,8 @@ This is needed if you use an overlay windows or compensate environmental influen This time frame is defined by a register which is called MTreg. Therefore you could choose a value between 32 and 254. The default value is 69; keep in mind that the measurement time is changed accordingly. -The datasheet for the BH1750 chip can be obtained [here](http://www.elechouse.com/elechouse/images/product/Digital%20light%20Sensor/bh1750fvi-e.pdf) +The datasheet for the BH1750 chip can be obtained +[here](https://www.mouser.de/datasheet/2/348/Rohm_11162017_ROHMS34826-1-1279292.pdf)[2011.11 Rev.D] ## Installation [](https://www.ardu-badge.com/BH1750) diff --git a/examples/BH1750advanced/BH1750advanced.ino b/examples/BH1750advanced/BH1750advanced.ino index 0d9851e..d03f051 100644 --- a/examples/BH1750advanced/BH1750advanced.ino +++ b/examples/BH1750advanced/BH1750advanced.ino @@ -85,11 +85,11 @@ void setup(){ void loop() { - - float lux = lightMeter.readLightLevel(); - Serial.print("Light: "); - Serial.print(lux); - Serial.println(" lx"); - delay(1000); + if (lightMeter.measurementReady()) { + float lux = lightMeter.readLightLevel(); + Serial.print("Light: "); + Serial.print(lux); + Serial.println(" lx"); + } } diff --git a/examples/BH1750autoadjust/BH1750autoadjust.ino b/examples/BH1750autoadjust/BH1750autoadjust.ino index d256f7c..8ac7e71 100644 --- a/examples/BH1750autoadjust/BH1750autoadjust.ino +++ b/examples/BH1750autoadjust/BH1750autoadjust.ino @@ -49,48 +49,50 @@ void setup(){ void loop() { //we use here the maxWait option due fail save - float lux = lightMeter.readLightLevel(true); - Serial.print(F("Light: ")); - Serial.print(lux); - Serial.println(F(" lx")); - - if (lux < 0) { - Serial.println(F("Error condition detected")); - } - else { - if (lux > 40000.0) { - // reduce measurement time - needed in direct sun light - if (lightMeter.setMTreg(32)) { - Serial.println(F("Setting MTReg to low value for high light environment")); - } - else { - Serial.println(F("Error setting MTReg to low value for high light environment")); - } + if (lightMeter.measurementReady(true)) { + float lux = lightMeter.readLightLevel(); + Serial.print(F("Light: ")); + Serial.print(lux); + Serial.println(F(" lx")); + + if (lux < 0) { + Serial.println(F("Error condition detected")); } else { - if (lux > 10.0) { - // typical light environment - if (lightMeter.setMTreg(69)) { - Serial.println(F("Setting MTReg to default value for normal light environment")); - } - else { - Serial.println(F("Error setting MTReg to default value for normal light environment")); - } + if (lux > 40000.0) { + // reduce measurement time - needed in direct sun light + if (lightMeter.setMTreg(32)) { + Serial.println(F("Setting MTReg to low value for high light environment")); } else { - if (lux <= 10.0) { - //very low light environment - if (lightMeter.setMTreg(138)) { - Serial.println(F("Setting MTReg to high value for low light environment")); + Serial.println(F("Error setting MTReg to low value for high light environment")); + } + } + else { + if (lux > 10.0) { + // typical light environment + if (lightMeter.setMTreg(69)) { + Serial.println(F("Setting MTReg to default value for normal light environment")); } else { - Serial.println(F("Error setting MTReg to high value for low light environment")); + Serial.println(F("Error setting MTReg to default value for normal light environment")); } } - } - } + else { + if (lux <= 10.0) { + //very low light environment + if (lightMeter.setMTreg(138)) { + Serial.println(F("Setting MTReg to high value for low light environment")); + } + else { + Serial.println(F("Error setting MTReg to high value for low light environment")); + } + } + } + } + } + Serial.println(F("--------------------------------------")); } - Serial.println(F("--------------------------------------")); delay(5000); } diff --git a/examples/BH1750onetime/BH1750onetime.ino b/examples/BH1750onetime/BH1750onetime.ino index 2988a85..c22ff9a 100644 --- a/examples/BH1750onetime/BH1750onetime.ino +++ b/examples/BH1750onetime/BH1750onetime.ino @@ -33,10 +33,12 @@ void setup(){ void loop() { + while (!lightMeter.measurementReady(true)) { + yield(); + } float lux = lightMeter.readLightLevel(); Serial.print("Light: "); Serial.print(lux); Serial.println(" lx"); - delay(1000); - + lightMeter.configure(BH1750::ONE_TIME_HIGH_RES_MODE); } diff --git a/examples/BH1750two_i2c/BH1750two_i2c.ino b/examples/BH1750two_i2c/BH1750two_i2c.ino index e336ca5..49bcd8e 100644 --- a/examples/BH1750two_i2c/BH1750two_i2c.ino +++ b/examples/BH1750two_i2c/BH1750two_i2c.ino @@ -49,8 +49,10 @@ int error_counter_1_b = 0; int error_counter_2_b = 0; void loop() { - const float light_level_a = bh1750_a.readLightLevel(); - const float light_level_b = bh1750_b.readLightLevel(); + float light_level_a; + if (bh1750_a.measurementReady()) { light_level_a = bh1750_a.readLightLevel(); } + float light_level_b; + if (bh1750_b.measurementReady()) { light_level_b = bh1750_b.readLightLevel(); } if (lround(light_level_a) == -1) { error_counter_1_a++; diff --git a/keywords.txt b/keywords.txt index fa9a921..b7c12da 100644 --- a/keywords.txt +++ b/keywords.txt @@ -16,7 +16,7 @@ begin KEYWORD2 configure KEYWORD2 setMTreg KEYWORD2 readLightLevel KEYWORD2 - +measurementReady KEYWORD2 ####################################### # Instances (KEYWORD2) ####################################### diff --git a/src/BH1750.cpp b/src/BH1750.cpp index f08c626..592e5dc 100644 --- a/src/BH1750.cpp +++ b/src/BH1750.cpp @@ -76,7 +76,6 @@ bool BH1750::begin(Mode mode, byte addr, TwoWire *i2c) { // Configure sensor in specified mode and set default MTreg return (configure(mode) && setMTreg(BH1750_DEFAULT_MTREG)); - } @@ -119,6 +118,7 @@ bool BH1750::configure(Mode mode) { switch (ack) { case 0: BH1750_MODE = mode; + lastReadTimestamp = millis(); return true; case 1: // too long for transmit buffer Serial.println(F("[BH1750] ERROR: too long for transmit buffer")); @@ -175,18 +175,6 @@ bool BH1750::setMTreg(byte MTreg) { switch (ack) { case 0: BH1750_MTreg = MTreg; - // Delay for specific continuous mode to get valid values - switch (BH1750_MODE) { - case BH1750::CONTINUOUS_LOW_RES_MODE: - _delay_ms(24 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG); - break; - case BH1750::CONTINUOUS_HIGH_RES_MODE: - case BH1750::CONTINUOUS_HIGH_RES_MODE_2: - _delay_ms(180 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG); - break; - default: - break; - } return true; case 1: // too long for transmit buffer Serial.println(F("[BH1750] ERROR: too long for transmit buffer")); @@ -208,6 +196,44 @@ bool BH1750::setMTreg(byte MTreg) { return false; } +/** + * Checks whether enough time has gone to read a new value + * @param maxWait a boolean if to wait for typical or maximum delay + * @return a boolean if a new measurement is possible + * + */ +bool BH1750::measurementReady(bool maxWait) { + unsigned long delaytime = 0; + switch (BH1750_MODE) { + case BH1750::CONTINUOUS_HIGH_RES_MODE: + case BH1750::CONTINUOUS_HIGH_RES_MODE_2: + case BH1750::ONE_TIME_HIGH_RES_MODE: + case BH1750::ONE_TIME_HIGH_RES_MODE_2: + maxWait ? delaytime = (180 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG) : delaytime = (120 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG); + break; + case BH1750::CONTINUOUS_LOW_RES_MODE: + case BH1750::ONE_TIME_LOW_RES_MODE: + // Send mode to sensor + maxWait ? delaytime = (24 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG) : delaytime = (16 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG); + break; + default: + break; + } + // Wait for new measurement to be possible. + // Measurements have a maximum measurement time and a typical measurement + // time. The maxWait argument determines which measurement wait time is + // used when a one-time mode is being used. The typical (shorter) + // measurement time is used by default and if maxWait is set to True then + // the maximum measurement time will be used. See data sheet pages 2, 5 + // and 7 for more details. + unsigned long currentTimestamp = millis(); + if (currentTimestamp - lastReadTimestamp >= delaytime) { + return true; + } + else + return false; +} + /** * Read light level from sensor * The return value range differs if the MTreg value is changed. The global @@ -216,7 +242,7 @@ bool BH1750::setMTreg(byte MTreg) { * -1 : no valid return value * -2 : sensor not configured */ -float BH1750::readLightLevel(bool maxWait) { +float BH1750::readLightLevel() { if (BH1750_MODE == UNCONFIGURED) { Serial.println(F("[BH1750] Device is not configured!")); @@ -266,6 +292,7 @@ float BH1750::readLightLevel(bool maxWait) { tmp |= __wire_read(); level = tmp; } + lastReadTimestamp = millis(); if (level != -1.0) { // Print raw value if debug enabled @@ -275,12 +302,12 @@ float BH1750::readLightLevel(bool maxWait) { #endif if (BH1750_MTreg != BH1750_DEFAULT_MTREG) { - level *= (float)((byte)BH1750_DEFAULT_MTREG/(float)BH1750_MTreg); - // Print MTreg factor if debug enabled - #ifdef BH1750_DEBUG - Serial.print(F("[BH1750] MTreg factor: ")); - Serial.println( String((float)((byte)BH1750_DEFAULT_MTREG/(float)BH1750_MTreg)) ); - #endif + level *= (float)((byte)BH1750_DEFAULT_MTREG/(float)BH1750_MTreg); + // Print MTreg factor if debug enabled + #ifdef BH1750_DEBUG + Serial.print(F("[BH1750] MTreg factor: ")); + Serial.println( String((float)((byte)BH1750_DEFAULT_MTREG/(float)BH1750_MTreg)) ); + #endif } if (BH1750_MODE == BH1750::ONE_TIME_HIGH_RES_MODE_2 || BH1750_MODE == BH1750::CONTINUOUS_HIGH_RES_MODE_2) { level /= 2; diff --git a/src/BH1750.h b/src/BH1750.h index b788769..dd4575c 100644 --- a/src/BH1750.h +++ b/src/BH1750.h @@ -44,6 +44,7 @@ class BH1750 { enum Mode { + // same as Power Down UNCONFIGURED = 0, // Measurement at 1 lux resolution. Measurement time is approx 120ms. CONTINUOUS_HIGH_RES_MODE = 0x10, @@ -64,7 +65,8 @@ class BH1750 { TwoWire* i2c = nullptr); bool configure(Mode mode); bool setMTreg(byte MTreg); - float readLightLevel(bool maxWait = false); + bool measurementReady(bool maxWait = false); + float readLightLevel(); private: byte BH1750_I2CADDR; @@ -75,6 +77,7 @@ class BH1750 { const float BH1750_CONV_FACTOR = 1.2; Mode BH1750_MODE = UNCONFIGURED; TwoWire* I2C; + unsigned long lastReadTimestamp; }; #endif From e1f37bcfd3a30a34936b5ad7b8d777a6ed7993d7 Mon Sep 17 00:00:00 2001 From: Chris Laws <clawsicus@gmail.com> Date: Sun, 3 Jan 2021 13:16:11 +1030 Subject: [PATCH 10/16] bump version for new release --- README.md | 25 +++++++++++++------------ library.json | 4 ++-- library.properties | 4 ++-- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 876004e..8d6e0b6 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ As we use float, if an error occurs you will get a negative value. Otherwise the measured counts are converted to lux and returned. If no advanced parameters are changed the maximum lux value is 54612.5 lx. As the sensor counts impact of light in a specific time frame you could change this time frame. -This is needed if you use an overlay windows or compensate environmental influence like darkness. +This is needed if you use an overlay window or compensate for environmental influence like darkness. This time frame is defined by a register which is called MTreg. Therefore you could choose a value between 32 and 254. The default value is 69; keep in mind that the measurement time is changed accordingly. @@ -62,23 +62,24 @@ The datasheet for the BH1750 chip can be obtained ## Installation [](https://www.ardu-badge.com/BH1750) -Click "Clone or download" -> "Download ZIP" button. +- **(For Arduino >= 1.5.x)** Install this package by searching for it in the + Arduino Library Manager and then clicking ``install``. Alternatively, this + library can be installed manually by clicking "Clone or download" -> "Download ZIP" + button. Then open Arduino IDE, click `Sketch -> Include library -> Add .ZIP library` + and select the downloaded archive. - - **(For Arduino >= 1.5.x)** Use the way above, or Library Manager. Open Arduino - IDE, click `Sketch -> Include library -> Add .ZIP library ` and select the - downloaded archive. - - - **(For Arduino < 1.5.x)** Extract the archive to - ``<Your User Directory>/My Documents/Arduino/libraries/`` folder and rename it - to `BH1750`. Restart IDE. +- **(For Arduino < 1.5.x)** Download this package as an archive by clicking + "Clone or download" -> "Download ZIP" button. Then extract the archive to + ``<Your User Directory>/My Documents/Arduino/libraries/`` folder and rename + it to `BH1750`. Restart IDE. The following YouTube [video](https://youtu.be/ACTMQvPVMLs) (specifically from -7:20 onwards) provides a good overview of installing this library and loading -an example using the Arduino IDE. +7:20 onwards) provides a good overview of manually installing this library and +loading an example using the Arduino IDE. [](https://youtu.be/ACTMQvPVMLs?t=437) -Additional info, about library installation process - https://www.arduino.cc/en/Guide/Libraries +Information about the library installation process - https://www.arduino.cc/en/Guide/Libraries ## Example diff --git a/library.json b/library.json index 01bc18a..d575750 100644 --- a/library.json +++ b/library.json @@ -7,6 +7,6 @@ "url": "https://github.com/claws/BH1750.git" }, "frameworks": "arduino", - "platforms": ["atmelavr", "atmelsam", "espressif8266", "espressif32"], - "version": "1.1.4" + "platforms": ["atmelavr", "atmelsam", "espressif8266", "espressif32", "stm32"], + "version": "1.2.0" } diff --git a/library.properties b/library.properties index 76ff54c..142a0d4 100644 --- a/library.properties +++ b/library.properties @@ -1,10 +1,10 @@ name=BH1750 -version=1.1.4 +version=1.2.0 author=Christopher Laws maintainer=Christopher Laws sentence=Arduino library for the digital light sensor breakout boards containing the BH1750FVI IC paragraph=Pretty simple and robust BH1750 library. Arduino, ESP8266 & ESP32 compatible. category=Sensors url=https://github.com/claws/BH1750 -architectures=avr,sam,esp8266,esp32 +architectures=avr,sam,esp8266,esp32,stm32 includes=BH1750.h From ff0e4b2e15bc9d22fe9a3a0b722866a7aee92d15 Mon Sep 17 00:00:00 2001 From: coelner <waenger@googlemail.com> Date: Tue, 5 Jan 2021 11:01:46 +0100 Subject: [PATCH 11/16] remove I2C from constructor, default pointer to Wire, fix travis links, add travis jobs --- .travis.yml | 2 ++ README.md | 2 +- examples/BH1750advanced/BH1750advanced.ino | 11 +--------- .../BH1750differentI2Caddress.ino | 2 +- src/BH1750.cpp | 20 ++++--------------- src/BH1750.h | 6 +++--- 6 files changed, 12 insertions(+), 31 deletions(-) diff --git a/.travis.yml b/.travis.yml index f1bb044..15798b7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,6 +25,8 @@ script: - arduino --verify --board arduino:avr:uno $PWD/examples/BH1750advanced/BH1750advanced.ino - arduino --verify --board arduino:avr:uno $PWD/examples/BH1750onetime/BH1750onetime.ino - arduino --verify --board arduino:avr:uno $PWD/examples/BH1750autoadjust/BH1750autoadjust.ino + - arduino --verify --board arduino:avr:uno $PWD/examples/BH1750differentI2Caddress/BH1750differentI2Caddress.ino + - arduino --verify --board arduino:avr:uno $PWD/examples/BH1750two_i2c/BH1750two_i2c.ino # esp8266 # - arduino --verify --board esp8266:esp8266:generic $PWD/examples/BH1570test/BH1750test.ino # - arduino --verify --board esp8266:esp8266:generic $PWD/examples/BH1570advanced/BH1750advanced.ino diff --git a/README.md b/README.md index 8d6e0b6..a717e8c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # BH1750 -[](https://travis-ci.org/claws/BH1750)<br> +[](https://travis-ci.com/claws/BH1750)<br> This package contains an Arduino library for digital light sensor breakout boards containing the BH1750FVI IC. diff --git a/examples/BH1750advanced/BH1750advanced.ino b/examples/BH1750advanced/BH1750advanced.ino index d03f051..bc426c1 100644 --- a/examples/BH1750advanced/BH1750advanced.ino +++ b/examples/BH1750advanced/BH1750advanced.ino @@ -22,16 +22,7 @@ #include <Wire.h> #include <BH1750.h> -/* - BH1750 can be physically configured to use two I2C addresses: - - 0x23 (most common) (if ADD pin had < 0.7VCC voltage) - - 0x5C (if ADD pin had > 0.7VCC voltage) - - Library uses 0x23 address as default, but you can define any other address. - If you had troubles with default value - try to change it to 0x5C. - -*/ -BH1750 lightMeter(0x23); +BH1750 lightMeter; void setup(){ diff --git a/examples/BH1750differentI2Caddress/BH1750differentI2Caddress.ino b/examples/BH1750differentI2Caddress/BH1750differentI2Caddress.ino index 6686ea8..25614c3 100644 --- a/examples/BH1750differentI2Caddress/BH1750differentI2Caddress.ino +++ b/examples/BH1750differentI2Caddress/BH1750differentI2Caddress.ino @@ -31,7 +31,7 @@ If you had troubles with default value - try to change it to 0x5C. */ -BH1750 lightMeter(); +BH1750 lightMeter; void setup(){ diff --git a/src/BH1750.cpp b/src/BH1750.cpp index 592e5dc..c3309ab 100644 --- a/src/BH1750.cpp +++ b/src/BH1750.cpp @@ -46,39 +46,27 @@ /** * Constructor - * @params addr Sensor address (0x23 or 0x5C, see datasheet) - * - * On most sensor boards, it was 0x23 */ -BH1750::BH1750(byte addr) { - - BH1750_I2CADDR = addr; - I2C = &Wire; +BH1750::BH1750() { } - /** * Configure sensor * @param mode Measurement mode - * @param addr Address of the sensor + * @param addr Address of the sensor (0x23 or 0x5C, see datasheet) * @param i2c TwoWire instance connected to I2C bus */ bool BH1750::begin(Mode mode, byte addr, TwoWire *i2c) { // I2C is expected to be initialized outside this library // But, allows a different address and TwoWire instance to be used - if(i2c) { - I2C = i2c; - } - if(addr) { - BH1750_I2CADDR = addr; - } + I2C = i2c; + BH1750_I2CADDR = addr; // Configure sensor in specified mode and set default MTreg return (configure(mode) && setMTreg(BH1750_DEFAULT_MTREG)); } - /** * Configure BH1750 with specified mode * @param mode Measurement mode diff --git a/src/BH1750.h b/src/BH1750.h index dd4575c..96fc3ee 100644 --- a/src/BH1750.h +++ b/src/BH1750.h @@ -60,16 +60,16 @@ class BH1750 { ONE_TIME_LOW_RES_MODE = 0x23 }; - BH1750(byte addr = 0x23); + BH1750(); bool begin(Mode mode = CONTINUOUS_HIGH_RES_MODE, byte addr = 0x23, - TwoWire* i2c = nullptr); + TwoWire* i2c = &Wire); bool configure(Mode mode); bool setMTreg(byte MTreg); bool measurementReady(bool maxWait = false); float readLightLevel(); private: - byte BH1750_I2CADDR; + byte BH1750_I2CADDR = 0x23; byte BH1750_MTreg = (byte)BH1750_DEFAULT_MTREG; // Correction factor used to calculate lux. Typical value is 1.2 but can // range from 0.96 to 1.44. See the data sheet (p.2, Measurement Accuracy) From c48fc2344615b03858891ab1c88685722169fef0 Mon Sep 17 00:00:00 2001 From: coelner <waenger@googlemail.com> Date: Tue, 5 Jan 2021 12:43:04 +0100 Subject: [PATCH 12/16] remove failing travis task --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 15798b7..f28b9c4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,7 +26,6 @@ script: - arduino --verify --board arduino:avr:uno $PWD/examples/BH1750onetime/BH1750onetime.ino - arduino --verify --board arduino:avr:uno $PWD/examples/BH1750autoadjust/BH1750autoadjust.ino - arduino --verify --board arduino:avr:uno $PWD/examples/BH1750differentI2Caddress/BH1750differentI2Caddress.ino - - arduino --verify --board arduino:avr:uno $PWD/examples/BH1750two_i2c/BH1750two_i2c.ino # esp8266 # - arduino --verify --board esp8266:esp8266:generic $PWD/examples/BH1570test/BH1750test.ino # - arduino --verify --board esp8266:esp8266:generic $PWD/examples/BH1570advanced/BH1750advanced.ino From ac4bb5189384a84d2fecf7e7ca97bf7a0ac0cce3 Mon Sep 17 00:00:00 2001 From: Chris Laws <clawsicus@gmail.com> Date: Sat, 16 Jan 2021 21:37:10 +1030 Subject: [PATCH 13/16] Setup Github Actions --- .clang-format | 3 + .github/workflows/ci.yml | 68 ++++++ README.md | 88 +++++-- build-examples.bash | 36 --- ci/code-format.bash | 42 ++++ ci/compile-examples.bash | 30 +++ examples/BH1750advanced/BH1750advanced.ino | 42 ++-- .../BH1750autoadjust/BH1750autoadjust.ino | 102 ++++----- examples/BH1750onetime/BH1750onetime.ino | 18 +- examples/BH1750test/BH1750test.ino | 42 ++-- examples/BH1750two_i2c/BH1750two_i2c.ino | 52 +++-- src/BH1750.cpp | 215 +++++++++--------- src/BH1750.h | 7 +- 13 files changed, 456 insertions(+), 289 deletions(-) create mode 100644 .clang-format create mode 100644 .github/workflows/ci.yml delete mode 100755 build-examples.bash create mode 100755 ci/code-format.bash create mode 100755 ci/compile-examples.bash diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..be05977 --- /dev/null +++ b/.clang-format @@ -0,0 +1,3 @@ +Language: Cpp +IndentPPDirectives: AfterHash +PointerAlignment: Left diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..0124dea --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,68 @@ +name: checks +on: [push, pull_request] +jobs: + lint: + runs-on: ubuntu-latest + steps: + - name: Checkout Repo + uses: actions/checkout@v2 + - name: Check Lint + uses: arduino/arduino-lint-action@v1.0.0 + with: + library-manager: update + compliance: strict + - name: Check Code Format + run: | + sudo apt install clang-format + clang-format --version + bash ./ci/code-format.bash + compile: + runs-on: ubuntu-latest + strategy: + matrix: + arduino-platform: ["arduino:avr", "esp8266:esp8266", "esp32:esp32"] + # In addition to the platform we also need to define the FQBN of the + # boards we want to test for each platform. + include: + - arduino-platform: "arduino:avr" + fqbn: "arduino:avr:uno" + additional-urls-arg: "" + - arduino-platform: "esp8266:esp8266" + fqbn: "esp8266:esp8266:generic" + additional-urls-arg: "--additional-urls http://arduino.esp8266.com/stable/package_esp8266com_index.json" + - arduino-platform: "esp32:esp32" + fqbn: "esp32:esp32:esp32" + additional-urls-arg: "--additional-urls https://dl.espressif.com/dl/package_esp32_index.json" + steps: + - name: Checkout Repo + uses: actions/checkout@master + - name: Setup Arduino CLI + uses: arduino/setup-arduino-cli@v1.1.1 + - name: Setup Python for esptool used during ESP32 compile + uses: actions/setup-python@v2 + if: ${{ matrix.arduino-platform == 'esp32:esp32' }} + with: + python-version: '3.9' + - name: Install Python Dependencies + if: ${{ matrix.arduino-platform == 'esp32:esp32' }} + run: | + python -m pip install --upgrade pip + pip install esptool + - name: Install Repo as Library + run: | + mkdir -p "$HOME/Arduino/libraries" + ln -s "$PWD" "$HOME/Arduino/libraries/." + - name: Install Arduino core and platform + run: | + arduino-cli core update-index ${{ matrix.additional-urls-arg }} + arduino-cli core install ${{ matrix.arduino-platform }} ${{ matrix.additional-urls-arg }} + - name: Compile Platform Common Sketches + run: | + arduino-cli compile --fqbn ${{ matrix.fqbn }} ./examples/BH1750test/BH1750test.ino + arduino-cli compile --fqbn ${{ matrix.fqbn }} ./examples/BH1750advanced/BH1750advanced.ino + arduino-cli compile --fqbn ${{ matrix.fqbn }} ./examples/BH1750onetime/BH1750onetime.ino + arduino-cli compile --fqbn ${{ matrix.fqbn }} ./examples/BH1750autoadjust/BH1750autoadjust.ino + - name: Compile ESP32 Platform Specific Sketches + if: ${{ matrix.arduino-platform == 'esp32:esp32' }} + run: | + arduino-cli compile --fqbn ${{ matrix.fqbn }} ./examples/BH1750two_i2c/BH1750two_i2c.ino diff --git a/README.md b/README.md index a717e8c..fdaa909 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # BH1750 -[](https://travis-ci.com/claws/BH1750)<br> +[](https://github.com/claws/BH1750/actions)<br> This package contains an Arduino library for digital light sensor breakout boards containing the BH1750FVI IC. @@ -44,14 +44,14 @@ Usually you will get an integer value which represent the lux equivalent. - High Resolution Mode - (generic range: 0.0 up to 54612.5 lux) - High Resolution Mode 2 - (generic range: 0.0 up to 27306.25 lux) -The sensor itself returns a 16 bit unsigned integer. Therefore the maximum value is limited in general. +The sensor itself returns a 16 bit unsigned integer. Therefore the maximum value is limited in general. The standard conversion between the so called 'counts' to lux is 1/1.2, that means you get a smaller value. As we use float, if an error occurs you will get a negative value. - -1 no valid data was transmitted from the sensor - -2 device is not configured -Otherwise the measured counts are converted to lux and returned. If no advanced parameters are changed the maximum lux value is 54612.5 lx. - -As the sensor counts impact of light in a specific time frame you could change this time frame. +Otherwise the measured counts are converted to lux and returned. If no advanced parameters are changed the maximum lux value is 54612.5 lx. + +As the sensor counts impact of light in a specific time frame you could change this time frame. This is needed if you use an overlay window or compensate for environmental influence like darkness. This time frame is defined by a register which is called MTreg. Therefore you could choose a value between 32 and 254. The default value is 69; keep in mind that the measurement time is changed accordingly. @@ -62,19 +62,19 @@ The datasheet for the BH1750 chip can be obtained ## Installation [](https://www.ardu-badge.com/BH1750) -- **(For Arduino >= 1.5.x)** Install this package by searching for it in the +- **(For Arduino >= 1.5.x)** Install this package by searching for it in the Arduino Library Manager and then clicking ``install``. Alternatively, this - library can be installed manually by clicking "Clone or download" -> "Download ZIP" - button. Then open Arduino IDE, click `Sketch -> Include library -> Add .ZIP library` + library can be installed manually by clicking "Clone or download" -> "Download ZIP" + button. Then open Arduino IDE, click `Sketch -> Include library -> Add .ZIP library` and select the downloaded archive. -- **(For Arduino < 1.5.x)** Download this package as an archive by clicking - "Clone or download" -> "Download ZIP" button. Then extract the archive to - ``<Your User Directory>/My Documents/Arduino/libraries/`` folder and rename +- **(For Arduino < 1.5.x)** Download this package as an archive by clicking + "Clone or download" -> "Download ZIP" button. Then extract the archive to + ``<Your User Directory>/My Documents/Arduino/libraries/`` folder and rename it to `BH1750`. Restart IDE. The following YouTube [video](https://youtu.be/ACTMQvPVMLs) (specifically from -7:20 onwards) provides a good overview of manually installing this library and +7:20 onwards) provides a good overview of manually installing this library and loading an example using the Arduino IDE. [](https://youtu.be/ACTMQvPVMLs?t=437) @@ -159,7 +159,69 @@ Light: 335.0 lx Light: 332.0 lx ``` -## More Examples +### More Examples The ``examples`` directory contains more advanced use cases such as using different modes, I2C addresses and multiple Wire instances. +## Developers + +The following information is for developers of this library. +### Code Format + +The code in this project is formatted using ``clang-format`` tool. + +Good instructions for installing ``clang-format`` can be found +[here](https://learn.adafruit.com/the-well-automated-arduino-library/formatting-with-clang-format) + +Once the ``clang-format`` tool has been install you can then run the +convenience script (``ci/code-format.bash``) to check or apply the code +formatting. The script should be run from the repo's top level directory. + +```shell +$ ./ci/code-format.bash +``` +This script is also run as part of the project's continuous integration +checks. + +If you make changes to code files then the code format can be applied +by simply passing *apply* as an argument to the script. + +```shell +$ ./ci/code-format.bash apply +``` + +### Code Linting + +The code in this project is linted using ``arduino-lint``. The tool can be +installed using the instructions [here](https://arduino.github.io/arduino-lint/latest/installation/). + +To run the linter over the project use the command below. + +```shell +$ arduino-lint --library-manager update --compliance strict +``` + +The same command is run as part of the project's continuous integration +checks. + +If errors or warnings are reported then fix them and re-run the script +until they are resolved. + +### Build Locally + +The code in this project can be built locally using the ``arduino-cli`` tool. +The tool can be installed using the instructions [here](https://github.com/arduino/arduino-cli#quickstart). Once you have the tool installed you can compile the +examples scripts using the convenience script (``ci/compile-examples.bash``). + +```shell +$ ./ci/compile-examples.bash +``` + +This script performs the same actions as the project's continuous integration +compile checks. + +### Release Process + +- Update ``library.json`` and ``library.properties`` version strings. +- Create a new project release and use the new version number as tag. Click Publish. +- Now wait about an hour for it to appear in the Arduino library manager. diff --git a/build-examples.bash b/build-examples.bash deleted file mode 100755 index c2916bc..0000000 --- a/build-examples.bash +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env bash -# -# A simple script to automate building BH1750 examples. -# -# Example (MacOSX): -# $ ARDUINO_IDE_PATH=/Applications/Arduino.app/Contents/Java ./build-examples.bash -# - -# Path to script directory. -SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )" -LIBNAME="$(basename "$SCRIPTPATH")" - -if [[ -z "${ARDUINO_IDE_PATH}" ]]; then - echo "ARDUINO_IDE_PATH env var is not set" - exit 1 -fi - -# Link BH1750 library into Arduino libraries directory -ln -s $SCRIPTPATH $ARDUINO_IDE_PATH/libraries/ - -cd $ARDUINO_IDE_PATH - -for sketch in `find $SCRIPTPATH/examples -name '*.ino'` -do - echo "Compiling $sketch" - ./arduino-builder -hardware ./hardware -tools ./hardware/tools/avr -tools ./tools-builder -libraries ./libraries -fqbn arduino:avr:uno --prefs "compiler.warning_level=all" $sketch - # ./arduino-builder -hardware ./hardware -tools ./hardware/tools/avr -tools ./tools-builder -libraries ./libraries -fqbn esp8266:esp8266:generic --prefs "compiler.warning_level=all" $sketch - if [ $? -ne 0 ]; then - echo -e "\xe2\x9c\x96" # check icon - else - echo -e "\xe2\x9c\x93" # tick icon - fi -done - -# Unlink BH1750 library from Arduino libraries directory -unlink $ARDUINO_IDE_PATH/libraries/$LIBNAME diff --git a/ci/code-format.bash b/ci/code-format.bash new file mode 100755 index 0000000..2d07a27 --- /dev/null +++ b/ci/code-format.bash @@ -0,0 +1,42 @@ +#!/bin/bash + +# Exit immediately if a command exits with a non-zero status. +# set -e + +# Define colors +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NORMAL='\033[0m' + +apply=0 +action="Checking" +if [ $# -eq 1 ]; then + if [ "$1" == "apply" ]; then + apply=1 + action="Applying" + else + echo "'apply' is the only argument supported" + exit 1 + fi +fi + +files=$(find . -type f -name "*.c" -or -name "*.cpp" -or -name "*.h" -or -name "*.hpp" -or -name "*.ino") + +# Check clang-format output +for f in $files ; do + if [ -f "$f" ]; then + echo "${action} code format for ${f}" + if [ $apply -eq 1 ]; then + clang-format -i $f + else + diff $f <(clang-format --assume-filename=main.cpp $f) --minimal + fi + if [ $? -ne 0 ]; then + echo -e "${RED}\xe2\x9c\x96${NORMAL}"; # Cross + exit 1 + else + echo -e "${GREEN}\xe2\x9c\x93${NORMAL}"; # Tick + fi + fi +done diff --git a/ci/compile-examples.bash b/ci/compile-examples.bash new file mode 100755 index 0000000..d17a3c7 --- /dev/null +++ b/ci/compile-examples.bash @@ -0,0 +1,30 @@ +#!/bin/bash +# +# A convenience script to automate building BH1750 examples. +# + +# AVR +echo "Compiling examples using FQBN arduino:avr:uno" +arduino-cli compile --fqbn arduino:avr:uno ./examples/BH1750test/BH1750test.ino +arduino-cli compile --fqbn arduino:avr:uno ./examples/BH1750advanced/BH1750advanced.ino +arduino-cli compile --fqbn arduino:avr:uno ./examples/BH1750onetime/BH1750onetime.ino +arduino-cli compile --fqbn arduino:avr:uno ./examples/BH1750autoadjust/BH1750autoadjust.ino + +# ESP8266 +echo "Compiling examples using FQBN esp8266:esp8266:generic" +arduino-cli core update-index --additional-urls http://arduino.esp8266.com/stable/package_esp8266com_index.json +arduino-cli core install esp8266:esp8266 --additional-urls http://arduino.esp8266.com/stable/package_esp8266com_index.json +arduino-cli compile --fqbn esp8266:esp8266:generic ./examples/BH1750test/BH1750test.ino +arduino-cli compile --fqbn esp8266:esp8266:generic ./examples/BH1750advanced/BH1750advanced.ino +arduino-cli compile --fqbn esp8266:esp8266:generic ./examples/BH1750onetime/BH1750onetime.ino +arduino-cli compile --fqbn esp8266:esp8266:generic ./examples/BH1750autoadjust/BH1750autoadjust.ino + +# ESP32 +echo "Compiling examples using FQBN esp32:esp32:esp32" +arduino-cli core update-index --additional-urls https://dl.espressif.com/dl/package_esp32_index.json +arduino-cli core install esp32:esp32 --additional-urls https://dl.espressif.com/dl/package_esp32_index.json +arduino-cli compile --fqbn esp32:esp32:esp32 ./examples/BH1750test/BH1750test.ino +arduino-cli compile --fqbn esp32:esp32:esp32 ./examples/BH1750advanced/BH1750advanced.ino +arduino-cli compile --fqbn esp32:esp32:esp32 ./examples/BH1750onetime/BH1750onetime.ino +arduino-cli compile --fqbn esp32:esp32:esp32 ./examples/BH1750autoadjust/BH1750autoadjust.ino +arduino-cli compile --fqbn esp32:esp32:esp32 ./examples/BH1750two_i2c/BH1750two_i2c.ino diff --git a/examples/BH1750advanced/BH1750advanced.ino b/examples/BH1750advanced/BH1750advanced.ino index bc426c1..2e2a625 100644 --- a/examples/BH1750advanced/BH1750advanced.ino +++ b/examples/BH1750advanced/BH1750advanced.ino @@ -1,30 +1,32 @@ /* - Advanced BH1750 library usage example +Advanced BH1750 library usage example - This example has some comments about advanced usage features. +This example has some comments about advanced usage features. - Connection: +Connections - VCC -> 3V3 or 5V - GND -> GND - SCL -> SCL (A5 on Arduino Uno, Leonardo, etc or 21 on Mega and Due, on esp8266 free selectable) - SDA -> SDA (A4 on Arduino Uno, Leonardo, etc or 20 on Mega and Due, on esp8266 free selectable) - ADD -> (not connected) or GND + - VCC to 3V3 or 5V + - GND to GND + - SCL to SCL (A5 on Arduino Uno, Leonardo, etc or 21 on Mega and Due, on + esp8266 free selectable) + - SDA to SDA (A4 on Arduino Uno, Leonardo, etc or 20 on Mega and Due, on + esp8266 free selectable) + - ADD to (not connected) or GND - ADD pin is used to set sensor I2C address. If it has voltage greater or equal to - 0.7VCC voltage (e.g. you've connected it to VCC) the sensor address will be - 0x5C. In other case (if ADD voltage less than 0.7 * VCC) the sensor address will - be 0x23 (by default). +ADD pin is used to set sensor I2C address. If it has voltage greater or equal +to 0.7VCC voltage (e.g. you've connected it to VCC) the sensor address will be +0x5C. In other case (if ADD voltage less than 0.7 * VCC) the sensor address +will be 0x23 (by default). */ -#include <Wire.h> #include <BH1750.h> +#include <Wire.h> BH1750 lightMeter; -void setup(){ +void setup() { Serial.begin(9600); @@ -35,9 +37,9 @@ void setup(){ /* BH1750 has six different measurement modes. They are divided in two groups; - continuous and one-time measurements. In continuous mode, sensor continuously - measures lightness value. In one-time mode the sensor makes only one - measurement and then goes into Power Down mode. + continuous and one-time measurements. In continuous mode, sensor + continuously measures lightness value. In one-time mode the sensor makes + only one measurement and then goes into Power Down mode. Each mode, has three different precisions: @@ -67,14 +69,11 @@ void setup(){ // begin returns a boolean that can be used to detect setup problems. if (lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) { Serial.println(F("BH1750 Advanced begin")); - } - else { + } else { Serial.println(F("Error initialising BH1750")); } - } - void loop() { if (lightMeter.measurementReady()) { float lux = lightMeter.readLightLevel(); @@ -82,5 +81,4 @@ void loop() { Serial.print(lux); Serial.println(" lx"); } - } diff --git a/examples/BH1750autoadjust/BH1750autoadjust.ino b/examples/BH1750autoadjust/BH1750autoadjust.ino index 8ac7e71..ea57988 100644 --- a/examples/BH1750autoadjust/BH1750autoadjust.ino +++ b/examples/BH1750autoadjust/BH1750autoadjust.ino @@ -1,38 +1,40 @@ /* - Example of BH1750 library usage. +Example of BH1750 library usage. - This example initialises the BH1750 object using the default high resolution - one shot mode and then makes a light level reading every five seconds. +This example initialises the BH1750 object using the default high resolution +one shot mode and then makes a light level reading every five seconds. - After the measurement the MTreg value is changed according to the result: - lux > 40000 ==> MTreg = 32 - lux < 40000 ==> MTreg = 69 (default) - lux < 10 ==> MTreg = 138 - Remember to test your specific sensor! Maybe the MTreg value range from 32 - up to 254 is not applicable to your unit. +After the measurement the MTreg value is changed according to the result: +lux > 40000 ==> MTreg = 32 +lux < 40000 ==> MTreg = 69 (default) +lux < 10 ==> MTreg = 138 +Remember to test your specific sensor! Maybe the MTreg value range from 32 +up to 254 is not applicable to your unit. - Connection: +Connections - VCC -> 3V3 or 5V - GND -> GND - SCL -> SCL (A5 on Arduino Uno, Leonardo, etc or 21 on Mega and Due, on esp8266 free selectable) - SDA -> SDA (A4 on Arduino Uno, Leonardo, etc or 20 on Mega and Due, on esp8266 free selectable) - ADD -> (not connected) or GND + - VCC to 3V3 or 5V + - GND to GND + - SCL to SCL (A5 on Arduino Uno, Leonardo, etc or 21 on Mega and Due, on + esp8266 free selectable) + - SDA to SDA (A4 on Arduino Uno, Leonardo, etc or 20 on Mega and Due, on + esp8266 free selectable) + - ADD to (not connected) or GND - ADD pin is used to set sensor I2C address. If it has voltage greater or equal to - 0.7VCC voltage (e.g. you've connected it to VCC) the sensor address will be - 0x5C. In other case (if ADD voltage less than 0.7 * VCC) the sensor address will - be 0x23 (by default). +ADD pin is used to set sensor I2C address. If it has voltage greater or equal to +0.7VCC voltage (e.g. you've connected it to VCC) the sensor address will be +0x5C. In other case (if ADD voltage less than 0.7 * VCC) the sensor address will +be 0x23 (by default). */ -#include <Wire.h> #include <BH1750.h> +#include <Wire.h> BH1750 lightMeter; -void setup(){ +void setup() { Serial.begin(9600); @@ -41,15 +43,14 @@ void setup(){ // On esp8266 you can select SCL and SDA pins using Wire.begin(D4, D3); lightMeter.begin(BH1750::ONE_TIME_HIGH_RES_MODE); - //lightMeter.setMTreg(69); // not needed, only mentioning it + // lightMeter.setMTreg(69); // not needed, only mentioning it Serial.println(F("BH1750 Test begin")); - } void loop() { - //we use here the maxWait option due fail save - if (lightMeter.measurementReady(true)) { + // we use here the maxWait option due fail save + if (lightMeter.measurementReady(true)) { float lux = lightMeter.readLightLevel(); Serial.print(F("Light: ")); Serial.print(lux); @@ -57,40 +58,39 @@ void loop() { if (lux < 0) { Serial.println(F("Error condition detected")); - } - else { + } else { if (lux > 40000.0) { // reduce measurement time - needed in direct sun light if (lightMeter.setMTreg(32)) { - Serial.println(F("Setting MTReg to low value for high light environment")); - } - else { - Serial.println(F("Error setting MTReg to low value for high light environment")); + Serial.println( + F("Setting MTReg to low value for high light environment")); + } else { + Serial.println( + F("Error setting MTReg to low value for high light environment")); } - } - else { - if (lux > 10.0) { - // typical light environment - if (lightMeter.setMTreg(69)) { - Serial.println(F("Setting MTReg to default value for normal light environment")); - } - else { - Serial.println(F("Error setting MTReg to default value for normal light environment")); - } + } else { + if (lux > 10.0) { + // typical light environment + if (lightMeter.setMTreg(69)) { + Serial.println(F( + "Setting MTReg to default value for normal light environment")); + } else { + Serial.println(F("Error setting MTReg to default value for normal " + "light environment")); } - else { - if (lux <= 10.0) { - //very low light environment - if (lightMeter.setMTreg(138)) { - Serial.println(F("Setting MTReg to high value for low light environment")); - } - else { - Serial.println(F("Error setting MTReg to high value for low light environment")); - } + } else { + if (lux <= 10.0) { + // very low light environment + if (lightMeter.setMTreg(138)) { + Serial.println( + F("Setting MTReg to high value for low light environment")); + } else { + Serial.println(F("Error setting MTReg to high value for low " + "light environment")); } + } } } - } Serial.println(F("--------------------------------------")); } diff --git a/examples/BH1750onetime/BH1750onetime.ino b/examples/BH1750onetime/BH1750onetime.ino index c22ff9a..ba7b71f 100644 --- a/examples/BH1750onetime/BH1750onetime.ino +++ b/examples/BH1750onetime/BH1750onetime.ino @@ -1,22 +1,22 @@ /* - Example of BH1750 library usage. +Example of BH1750 library usage. - This example initialises the BH1750 object using the high resolution - one-time mode and then makes a light level reading every second. +This example initialises the BH1750 object using the high resolution +one-time mode and then makes a light level reading every second. - The BH1750 component starts up in default mode when it next powers up. - The BH1750 library automatically reconfigures the one-time mode in - preparation for the next measurement. +The BH1750 component starts up in default mode when it next powers up. +The BH1750 library automatically reconfigures the one-time mode in +preparation for the next measurement. */ -#include <Wire.h> #include <BH1750.h> +#include <Wire.h> BH1750 lightMeter; -void setup(){ +void setup() { Serial.begin(9600); @@ -27,10 +27,8 @@ void setup(){ lightMeter.begin(BH1750::ONE_TIME_HIGH_RES_MODE); Serial.println(F("BH1750 One-Time Test")); - } - void loop() { while (!lightMeter.measurementReady(true)) { diff --git a/examples/BH1750test/BH1750test.ino b/examples/BH1750test/BH1750test.ino index 4ca5c10..63cd611 100644 --- a/examples/BH1750test/BH1750test.ino +++ b/examples/BH1750test/BH1750test.ino @@ -1,54 +1,50 @@ /* - Example of BH1750 library usage. +Example of BH1750 library usage. - This example initialises the BH1750 object using the default high resolution - continuous mode and then makes a light level reading every second. +This example initialises the BH1750 object using the default high resolution +continuous mode and then makes a light level reading every second. - Connection: +Connections - VCC -> 3V3 or 5V - GND -> GND - SCL -> SCL (A5 on Arduino Uno, Leonardo, etc or 21 on Mega and Due, on esp8266 free selectable) - SDA -> SDA (A4 on Arduino Uno, Leonardo, etc or 20 on Mega and Due, on esp8266 free selectable) - ADD -> (not connected) or GND + - VCC to 3V3 or 5V + - GND to GND + - SCL to SCL (A5 on Arduino Uno, Leonardo, etc or 21 on Mega and Due, on + esp8266 free selectable) + - SDA to SDA (A4 on Arduino Uno, Leonardo, etc or 20 on Mega and Due, on + esp8266 free selectable) + - ADD to (not connected) or GND - ADD pin is used to set sensor I2C address. If it has voltage greater or equal to - 0.7VCC voltage (e.g. you've connected it to VCC) the sensor address will be - 0x5C. In other case (if ADD voltage less than 0.7 * VCC) the sensor address will - be 0x23 (by default). +ADD pin is used to set sensor I2C address. If it has voltage greater or equal +to 0.7VCC voltage (e.g. you've connected it to VCC) the sensor address will be +0x5C. In other case (if ADD voltage less than 0.7 * VCC) the sensor address +will be 0x23 (by default). */ - -#include <Wire.h> #include <BH1750.h> +#include <Wire.h> BH1750 lightMeter; - -void setup(){ - +void setup() { Serial.begin(9600); // Initialize the I2C bus (BH1750 library doesn't do this automatically) Wire.begin(); // On esp8266 you can select SCL and SDA pins using Wire.begin(D4, D3); - // For Wemos / Lolin D1 Mini Pro and the Ambient Light shield use Wire.begin(D2, D1); + // For Wemos / Lolin D1 Mini Pro and the Ambient Light shield use + // Wire.begin(D2, D1); lightMeter.begin(); Serial.println(F("BH1750 Test begin")); - } - void loop() { - float lux = lightMeter.readLightLevel(); Serial.print("Light: "); Serial.print(lux); Serial.println(" lx"); delay(1000); - } diff --git a/examples/BH1750two_i2c/BH1750two_i2c.ino b/examples/BH1750two_i2c/BH1750two_i2c.ino index 49bcd8e..6a17fb9 100644 --- a/examples/BH1750two_i2c/BH1750two_i2c.ino +++ b/examples/BH1750two_i2c/BH1750two_i2c.ino @@ -1,31 +1,31 @@ /* - Example of BH1750 library usage. +Example of BH1750 library usage. - This example initialises two BH1750 objects using different TwoWire - instances (Wire and Wire1) and then makes a light level reading every second. - This is the case for boards such as the ESP8266 and ESP32 +This example initialises two BH1750 objects using different TwoWire +instances (Wire and Wire1) and then makes a light level reading every second. +This is the case for boards such as the ESP8266 and ESP32 - Connection: +Connections: - BH1750 A: - VCC -> 3V3 or 5V - GND -> GND - SCL -> SCL (19 in this example) - SDA -> SDA (18 in this example) - ADD -> (not connected) or GND + BH1750 A: + VCC -> 3V3 or 5V + GND -> GND + SCL -> SCL (19 in this example) + SDA -> SDA (18 in this example) + ADD -> (not connected) or GND - BH1750 B: - VCC -> 3V3 or 5V - GND -> GND - SCL -> SCL (22 in this example) - SDA -> SDA (21 in this example) - ADD -> (not connected) or GND + BH1750 B: + VCC -> 3V3 or 5V + GND -> GND + SCL -> SCL (22 in this example) + SDA -> SDA (21 in this example) + ADD -> (not connected) or GND - ADD pin is used to set sensor I2C address. If it has voltage greater or equal to - 0.7VCC voltage (e.g. you've connected it to VCC) the sensor address will be - 0x5C. In other case (if ADD voltage less than 0.7 * VCC) the sensor address will - be 0x23 (by default). +ADD pin is used to set sensor I2C address. If it has voltage greater or equal +to 0.7VCC voltage (e.g. you've connected it to VCC) the sensor address will be +0x5C. In other case (if ADD voltage less than 0.7 * VCC) the sensor address +will be 0x23 (by default). */ @@ -50,9 +50,13 @@ int error_counter_2_b = 0; void loop() { float light_level_a; - if (bh1750_a.measurementReady()) { light_level_a = bh1750_a.readLightLevel(); } - float light_level_b; - if (bh1750_b.measurementReady()) { light_level_b = bh1750_b.readLightLevel(); } + if (bh1750_a.measurementReady()) { + light_level_a = bh1750_a.readLightLevel(); + } + float light_level_b; + if (bh1750_b.measurementReady()) { + light_level_b = bh1750_b.readLightLevel(); + } if (lround(light_level_a) == -1) { error_counter_1_a++; diff --git a/src/BH1750.cpp b/src/BH1750.cpp index c3309ab..dc2e701 100644 --- a/src/BH1750.cpp +++ b/src/BH1750.cpp @@ -15,35 +15,32 @@ // Define milliseconds delay for ESP8266 platform #if defined(ESP8266) - #include <pgmspace.h> - #define _delay_ms(ms) delayMicroseconds((ms) * 1000) +# include <pgmspace.h> +# define _delay_ms(ms) delayMicroseconds((ms)*1000) // Use _delay_ms from utils for AVR-based platforms #elif defined(__avr__) - #include <util/delay.h> +# include <util/delay.h> // Use Wiring's delay for compability with another platforms #else - #define _delay_ms(ms) delay(ms) +# define _delay_ms(ms) delay(ms) #endif - // Legacy Wire.write() function fix #if (ARDUINO >= 100) - #define __wire_write(d) I2C->write(d) +# define __wire_write(d) I2C->write(d) #else - #define __wire_write(d) I2C->send(d) +# define __wire_write(d) I2C->send(d) #endif - // Legacy Wire.read() function fix #if (ARDUINO >= 100) - #define __wire_read() I2C->read() +# define __wire_read() I2C->read() #else - #define __wire_read() I2C->receive() +# define __wire_read() I2C->receive() #endif - /** * Constructor */ @@ -56,12 +53,16 @@ BH1750::BH1750() { * @param addr Address of the sensor (0x23 or 0x5C, see datasheet) * @param i2c TwoWire instance connected to I2C bus */ -bool BH1750::begin(Mode mode, byte addr, TwoWire *i2c) { +bool BH1750::begin(Mode mode, byte addr, TwoWire* i2c) { // I2C is expected to be initialized outside this library // But, allows a different address and TwoWire instance to be used - I2C = i2c; - BH1750_I2CADDR = addr; + if (i2c) { + I2C = i2c; + } + if (addr) { + BH1750_I2CADDR = addr; + } // Configure sensor in specified mode and set default MTreg return (configure(mode) && setMTreg(BH1750_DEFAULT_MTREG)); @@ -79,54 +80,52 @@ bool BH1750::configure(Mode mode) { // Check measurement mode is valid switch (mode) { - case BH1750::CONTINUOUS_HIGH_RES_MODE: - case BH1750::CONTINUOUS_HIGH_RES_MODE_2: - case BH1750::CONTINUOUS_LOW_RES_MODE: - case BH1750::ONE_TIME_HIGH_RES_MODE: - case BH1750::ONE_TIME_HIGH_RES_MODE_2: - case BH1750::ONE_TIME_LOW_RES_MODE: - - // Send mode to sensor - I2C->beginTransmission(BH1750_I2CADDR); - __wire_write((uint8_t)mode); - ack = I2C->endTransmission(); - - // Wait a few moments to wake up - _delay_ms(10); - break; - - default: - // Invalid measurement mode - Serial.println(F("[BH1750] ERROR: Invalid mode")); - break; - + case BH1750::CONTINUOUS_HIGH_RES_MODE: + case BH1750::CONTINUOUS_HIGH_RES_MODE_2: + case BH1750::CONTINUOUS_LOW_RES_MODE: + case BH1750::ONE_TIME_HIGH_RES_MODE: + case BH1750::ONE_TIME_HIGH_RES_MODE_2: + case BH1750::ONE_TIME_LOW_RES_MODE: + + // Send mode to sensor + I2C->beginTransmission(BH1750_I2CADDR); + __wire_write((uint8_t)mode); + ack = I2C->endTransmission(); + + // Wait a few moments to wake up + _delay_ms(10); + break; + + default: + // Invalid measurement mode + Serial.println(F("[BH1750] ERROR: Invalid mode")); + break; } // Check result code switch (ack) { - case 0: - BH1750_MODE = mode; - lastReadTimestamp = millis(); - return true; - case 1: // too long for transmit buffer - Serial.println(F("[BH1750] ERROR: too long for transmit buffer")); - break; - case 2: // received NACK on transmit of address - Serial.println(F("[BH1750] ERROR: received NACK on transmit of address")); - break; - case 3: // received NACK on transmit of data - Serial.println(F("[BH1750] ERROR: received NACK on transmit of data")); - break; - case 4: // other error - Serial.println(F("[BH1750] ERROR: other error")); - break; - default: - Serial.println(F("[BH1750] ERROR: undefined error")); - break; + case 0: + BH1750_MODE = mode; + lastReadTimestamp = millis(); + return true; + case 1: // too long for transmit buffer + Serial.println(F("[BH1750] ERROR: too long for transmit buffer")); + break; + case 2: // received NACK on transmit of address + Serial.println(F("[BH1750] ERROR: received NACK on transmit of address")); + break; + case 3: // received NACK on transmit of data + Serial.println(F("[BH1750] ERROR: received NACK on transmit of data")); + break; + case 4: // other error + Serial.println(F("[BH1750] ERROR: other error")); + break; + default: + Serial.println(F("[BH1750] ERROR: undefined error")); + break; } return false; - } /** @@ -137,7 +136,7 @@ bool BH1750::configure(Mode mode) { * false if MTreg not changed or parameter out of range */ bool BH1750::setMTreg(byte MTreg) { - //Bug: lowest value seems to be 32! + // Bug: lowest value seems to be 32! if (MTreg <= 31 || MTreg > 254) { Serial.println(F("[BH1750] ERROR: MTreg out of range")); return false; @@ -150,7 +149,7 @@ bool BH1750::setMTreg(byte MTreg) { __wire_write((0b01000 << 3) | (MTreg >> 5)); ack = I2C->endTransmission(); I2C->beginTransmission(BH1750_I2CADDR); - __wire_write((0b011 << 5 ) | (MTreg & 0b11111)); + __wire_write((0b011 << 5) | (MTreg & 0b11111)); ack = ack | I2C->endTransmission(); I2C->beginTransmission(BH1750_I2CADDR); __wire_write(BH1750_MODE); @@ -161,24 +160,24 @@ bool BH1750::setMTreg(byte MTreg) { // Check result code switch (ack) { - case 0: - BH1750_MTreg = MTreg; - return true; - case 1: // too long for transmit buffer - Serial.println(F("[BH1750] ERROR: too long for transmit buffer")); - break; - case 2: // received NACK on transmit of address - Serial.println(F("[BH1750] ERROR: received NACK on transmit of address")); - break; - case 3: // received NACK on transmit of data - Serial.println(F("[BH1750] ERROR: received NACK on transmit of data")); - break; - case 4: // other error - Serial.println(F("[BH1750] ERROR: other error")); - break; - default: - Serial.println(F("[BH1750] ERROR: undefined error")); - break; + case 0: + BH1750_MTreg = MTreg; + return true; + case 1: // too long for transmit buffer + Serial.println(F("[BH1750] ERROR: too long for transmit buffer")); + break; + case 2: // received NACK on transmit of address + Serial.println(F("[BH1750] ERROR: received NACK on transmit of address")); + break; + case 3: // received NACK on transmit of data + Serial.println(F("[BH1750] ERROR: received NACK on transmit of data")); + break; + case 4: // other error + Serial.println(F("[BH1750] ERROR: other error")); + break; + default: + Serial.println(F("[BH1750] ERROR: undefined error")); + break; } return false; @@ -188,24 +187,26 @@ bool BH1750::setMTreg(byte MTreg) { * Checks whether enough time has gone to read a new value * @param maxWait a boolean if to wait for typical or maximum delay * @return a boolean if a new measurement is possible - * + * */ bool BH1750::measurementReady(bool maxWait) { unsigned long delaytime = 0; switch (BH1750_MODE) { - case BH1750::CONTINUOUS_HIGH_RES_MODE: - case BH1750::CONTINUOUS_HIGH_RES_MODE_2: - case BH1750::ONE_TIME_HIGH_RES_MODE: - case BH1750::ONE_TIME_HIGH_RES_MODE_2: - maxWait ? delaytime = (180 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG) : delaytime = (120 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG); - break; - case BH1750::CONTINUOUS_LOW_RES_MODE: - case BH1750::ONE_TIME_LOW_RES_MODE: - // Send mode to sensor - maxWait ? delaytime = (24 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG) : delaytime = (16 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG); - break; - default: - break; + case BH1750::CONTINUOUS_HIGH_RES_MODE: + case BH1750::CONTINUOUS_HIGH_RES_MODE_2: + case BH1750::ONE_TIME_HIGH_RES_MODE: + case BH1750::ONE_TIME_HIGH_RES_MODE_2: + maxWait ? delaytime = (180 * BH1750_MTreg / (byte)BH1750_DEFAULT_MTREG) + : delaytime = (120 * BH1750_MTreg / (byte)BH1750_DEFAULT_MTREG); + break; + case BH1750::CONTINUOUS_LOW_RES_MODE: + case BH1750::ONE_TIME_LOW_RES_MODE: + // Send mode to sensor + maxWait ? delaytime = (24 * BH1750_MTreg / (byte)BH1750_DEFAULT_MTREG) + : delaytime = (16 * BH1750_MTreg / (byte)BH1750_DEFAULT_MTREG); + break; + default: + break; } // Wait for new measurement to be possible. // Measurements have a maximum measurement time and a typical measurement @@ -215,11 +216,10 @@ bool BH1750::measurementReady(bool maxWait) { // the maximum measurement time will be used. See data sheet pages 2, 5 // and 7 for more details. unsigned long currentTimestamp = millis(); - if (currentTimestamp - lastReadTimestamp >= delaytime) { - return true; - } - else - return false; + if (currentTimestamp - lastReadTimestamp >= delaytime) { + return true; + } else + return false; } /** @@ -283,33 +283,34 @@ float BH1750::readLightLevel() { lastReadTimestamp = millis(); if (level != -1.0) { - // Print raw value if debug enabled - #ifdef BH1750_DEBUG +// Print raw value if debug enabled +#ifdef BH1750_DEBUG Serial.print(F("[BH1750] Raw value: ")); Serial.println(level); - #endif +#endif if (BH1750_MTreg != BH1750_DEFAULT_MTREG) { - level *= (float)((byte)BH1750_DEFAULT_MTREG/(float)BH1750_MTreg); - // Print MTreg factor if debug enabled - #ifdef BH1750_DEBUG + level *= (float)((byte)BH1750_DEFAULT_MTREG / (float)BH1750_MTreg); +// Print MTreg factor if debug enabled +#ifdef BH1750_DEBUG Serial.print(F("[BH1750] MTreg factor: ")); - Serial.println( String((float)((byte)BH1750_DEFAULT_MTREG/(float)BH1750_MTreg)) ); - #endif + Serial.println( + String((float)((byte)BH1750_DEFAULT_MTREG / (float)BH1750_MTreg))); +#endif } - if (BH1750_MODE == BH1750::ONE_TIME_HIGH_RES_MODE_2 || BH1750_MODE == BH1750::CONTINUOUS_HIGH_RES_MODE_2) { + if (BH1750_MODE == BH1750::ONE_TIME_HIGH_RES_MODE_2 || + BH1750_MODE == BH1750::CONTINUOUS_HIGH_RES_MODE_2) { level /= 2; } // Convert raw value to lux level /= BH1750_CONV_FACTOR; - // Print converted value if debug enabled - #ifdef BH1750_DEBUG +// Print converted value if debug enabled +#ifdef BH1750_DEBUG Serial.print(F("[BH1750] Converted float value: ")); Serial.println(level); - #endif +#endif } return level; - } diff --git a/src/BH1750.h b/src/BH1750.h index 96fc3ee..a849973 100644 --- a/src/BH1750.h +++ b/src/BH1750.h @@ -6,7 +6,8 @@ interface to the device. Configuring the I2C bus is expected to be done in user code. The BH1750 library doesn't do this automatically. - Datasheet: http://www.elechouse.com/elechouse/images/product/Digital%20light%20Sensor/bh1750fvi-e.pdf + Datasheet: + http://www.elechouse.com/elechouse/images/product/Digital%20light%20Sensor/bh1750fvi-e.pdf Written by Christopher Laws, March, 2013. @@ -16,9 +17,9 @@ #define BH1750_h #if (ARDUINO >= 100) - #include <Arduino.h> +# include <Arduino.h> #else - #include <WProgram.h> +# include <WProgram.h> #endif #include "Wire.h" From 6bc6a7faac17c2945dc3209d98c492de3e648b41 Mon Sep 17 00:00:00 2001 From: coelner <waenger@googlemail.com> Date: Wed, 13 Oct 2021 20:42:49 +0200 Subject: [PATCH 14/16] fix broken code due merge --- .../BH1750differentI2Caddress.ino | 24 ++++++++----------- src/BH1750.cpp | 11 +++++++-- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/examples/BH1750differentI2Caddress/BH1750differentI2Caddress.ino b/examples/BH1750differentI2Caddress/BH1750differentI2Caddress.ino index 25614c3..a9bb18c 100644 --- a/examples/BH1750differentI2Caddress/BH1750differentI2Caddress.ino +++ b/examples/BH1750differentI2Caddress/BH1750differentI2Caddress.ino @@ -8,19 +8,19 @@ VCC -> 3V3 or 5V GND -> GND - SCL -> SCL (A5 on Arduino Uno, Leonardo, etc or 21 on Mega and Due, on esp8266 free selectable) - SDA -> SDA (A4 on Arduino Uno, Leonardo, etc or 20 on Mega and Due, on esp8266 free selectable) - ADD -> VCC + SCL -> SCL (A5 on Arduino Uno, Leonardo, etc or 21 on Mega and Due, on + esp8266 free selectable) SDA -> SDA (A4 on Arduino Uno, Leonardo, etc or 20 on + Mega and Due, on esp8266 free selectable) ADD -> VCC - ADD pin is used to set sensor I2C address. If it has voltage greater or equal to - 0.7VCC voltage (e.g. you've connected it to VCC) the sensor address will be - 0x5C. In other case (if ADD voltage less than 0.7 * VCC) the sensor address will - be 0x23 (by default). + ADD pin is used to set sensor I2C address. If it has voltage greater or equal + to 0.7VCC voltage (e.g. you've connected it to VCC) the sensor address will be + 0x5C. In other case (if ADD voltage less than 0.7 * VCC) the sensor address + will be 0x23 (by default). */ -#include <Wire.h> #include <BH1750.h> +#include <Wire.h> /* BH1750 can be physically configured to use two I2C addresses: @@ -33,7 +33,7 @@ */ BH1750 lightMeter; -void setup(){ +void setup() { Serial.begin(9600); @@ -44,14 +44,11 @@ void setup(){ // begin returns a boolean that can be used to detect setup problems. if (lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE, 0x5c)) { Serial.println(F("BH1750 different I2C address begin")); - } - else { + } else { Serial.println(F("Error initialising BH1750")); } - } - void loop() { float lux = lightMeter.readLightLevel(); @@ -59,5 +56,4 @@ void loop() { Serial.print(lux); Serial.println(" lx"); delay(1000); - } diff --git a/src/BH1750.cpp b/src/BH1750.cpp index dc2e701..6550bf2 100644 --- a/src/BH1750.cpp +++ b/src/BH1750.cpp @@ -43,14 +43,21 @@ /** * Constructor + * @params addr Sensor address (0x76 or 0x72, see datasheet) + * + * On most sensor boards, it was 0x76 */ -BH1750::BH1750() { +BH1750::BH1750(byte addr) { + + BH1750_I2CADDR = addr; + // Allows user to change TwoWire instance + I2C = &Wire; } /** * Configure sensor * @param mode Measurement mode - * @param addr Address of the sensor (0x23 or 0x5C, see datasheet) + * @param addr Address of the sensor * @param i2c TwoWire instance connected to I2C bus */ bool BH1750::begin(Mode mode, byte addr, TwoWire* i2c) { From fdd8420a4ee5422c8c90b524e74b488af75c67cd Mon Sep 17 00:00:00 2001 From: coelner <waenger@googlemail.com> Date: Wed, 13 Oct 2021 21:26:17 +0200 Subject: [PATCH 15/16] code format --- src/BH1750.cpp | 36 ++++++++++++------------ src/BH1750.h | 74 ++++++++++++++++++++++++-------------------------- 2 files changed, 55 insertions(+), 55 deletions(-) diff --git a/src/BH1750.cpp b/src/BH1750.cpp index 6550bf2..bbed3ac 100644 --- a/src/BH1750.cpp +++ b/src/BH1750.cpp @@ -259,23 +259,25 @@ float BH1750::readLightLevel() { switch (BH1750_MODE) { - case BH1750::ONE_TIME_LOW_RES_MODE: - // Send mode to sensor - I2C->beginTransmission(BH1750_I2CADDR); - __wire_write((uint8_t)BH1750_MODE); - I2C->endTransmission(); - maxWait ? _delay_ms(24 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG) : _delay_ms(16 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG); - break; - case BH1750::ONE_TIME_HIGH_RES_MODE: - case BH1750::ONE_TIME_HIGH_RES_MODE_2: - // Send mode to sensor - I2C->beginTransmission(BH1750_I2CADDR); - __wire_write((uint8_t)BH1750_MODE); - I2C->endTransmission(); - maxWait ? _delay_ms(180 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG) :_delay_ms(120 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG); - break; - default: - break; + case BH1750::ONE_TIME_LOW_RES_MODE: + // Send mode to sensor + I2C->beginTransmission(BH1750_I2CADDR); + __wire_write((uint8_t)BH1750_MODE); + I2C->endTransmission(); + maxWait ? _delay_ms(24 * BH1750_MTreg / (byte)BH1750_DEFAULT_MTREG) + : _delay_ms(16 * BH1750_MTreg / (byte)BH1750_DEFAULT_MTREG); + break; + case BH1750::ONE_TIME_HIGH_RES_MODE: + case BH1750::ONE_TIME_HIGH_RES_MODE_2: + // Send mode to sensor + I2C->beginTransmission(BH1750_I2CADDR); + __wire_write((uint8_t)BH1750_MODE); + I2C->endTransmission(); + maxWait ? _delay_ms(180 * BH1750_MTreg / (byte)BH1750_DEFAULT_MTREG) + : _delay_ms(120 * BH1750_MTreg / (byte)BH1750_DEFAULT_MTREG); + break; + default: + break; } // Read two bytes from the sensor, which are low and high parts of the sensor diff --git a/src/BH1750.h b/src/BH1750.h index a849973..6d3b1de 100644 --- a/src/BH1750.h +++ b/src/BH1750.h @@ -41,44 +41,42 @@ class BH1750 { - public: - - enum Mode - { - // same as Power Down - UNCONFIGURED = 0, - // Measurement at 1 lux resolution. Measurement time is approx 120ms. - CONTINUOUS_HIGH_RES_MODE = 0x10, - // Measurement at 0.5 lux resolution. Measurement time is approx 120ms. - CONTINUOUS_HIGH_RES_MODE_2 = 0x11, - // Measurement at 4 lux resolution. Measurement time is approx 16ms. - CONTINUOUS_LOW_RES_MODE = 0x13, - // Measurement at 1 lux resolution. Measurement time is approx 120ms. - ONE_TIME_HIGH_RES_MODE = 0x20, - // Measurement at 0.5 lux resolution. Measurement time is approx 120ms. - ONE_TIME_HIGH_RES_MODE_2 = 0x21, - // Measurement at 4 lux resolution. Measurement time is approx 16ms. - ONE_TIME_LOW_RES_MODE = 0x23 - }; - - BH1750(); - bool begin(Mode mode = CONTINUOUS_HIGH_RES_MODE, byte addr = 0x23, - TwoWire* i2c = &Wire); - bool configure(Mode mode); - bool setMTreg(byte MTreg); - bool measurementReady(bool maxWait = false); - float readLightLevel(); - - private: - byte BH1750_I2CADDR = 0x23; - byte BH1750_MTreg = (byte)BH1750_DEFAULT_MTREG; - // Correction factor used to calculate lux. Typical value is 1.2 but can - // range from 0.96 to 1.44. See the data sheet (p.2, Measurement Accuracy) - // for more information. - const float BH1750_CONV_FACTOR = 1.2; - Mode BH1750_MODE = UNCONFIGURED; - TwoWire* I2C; - unsigned long lastReadTimestamp; +public: + enum Mode { + // same as Power Down + UNCONFIGURED = 0, + // Measurement at 1 lux resolution. Measurement time is approx 120ms. + CONTINUOUS_HIGH_RES_MODE = 0x10, + // Measurement at 0.5 lux resolution. Measurement time is approx 120ms. + CONTINUOUS_HIGH_RES_MODE_2 = 0x11, + // Measurement at 4 lux resolution. Measurement time is approx 16ms. + CONTINUOUS_LOW_RES_MODE = 0x13, + // Measurement at 1 lux resolution. Measurement time is approx 120ms. + ONE_TIME_HIGH_RES_MODE = 0x20, + // Measurement at 0.5 lux resolution. Measurement time is approx 120ms. + ONE_TIME_HIGH_RES_MODE_2 = 0x21, + // Measurement at 4 lux resolution. Measurement time is approx 16ms. + ONE_TIME_LOW_RES_MODE = 0x23 + }; + + BH1750(); + bool begin(Mode mode = CONTINUOUS_HIGH_RES_MODE, byte addr = 0x23, + TwoWire* i2c = &Wire); + bool configure(Mode mode); + bool setMTreg(byte MTreg); + bool measurementReady(bool maxWait = false); + float readLightLevel(); + +private: + byte BH1750_I2CADDR = 0x23; + byte BH1750_MTreg = (byte)BH1750_DEFAULT_MTREG; + // Correction factor used to calculate lux. Typical value is 1.2 but can + // range from 0.96 to 1.44. See the data sheet (p.2, Measurement Accuracy) + // for more information. + const float BH1750_CONV_FACTOR = 1.2; + Mode BH1750_MODE = UNCONFIGURED; + TwoWire* I2C; + unsigned long lastReadTimestamp; }; #endif From 08a4ab5bd67de646797cab5edcf406ccb6114344 Mon Sep 17 00:00:00 2001 From: coelner <waenger@googlemail.com> Date: Wed, 13 Oct 2021 21:39:49 +0200 Subject: [PATCH 16/16] fix merge error --- src/BH1750.cpp | 33 --------------------------------- 1 file changed, 33 deletions(-) diff --git a/src/BH1750.cpp b/src/BH1750.cpp index bbed3ac..1b3a05b 100644 --- a/src/BH1750.cpp +++ b/src/BH1750.cpp @@ -247,39 +247,6 @@ float BH1750::readLightLevel() { // Measurement result will be stored here float level = -1.0; - // Wait for measurement to be taken. - // Measurements have a maximum measurement time and a typical measurement - // time. The maxWait argument determines which measurement wait time is - // used when a one-time mode is being used. The typical (shorter) - // measurement time is used by default and if maxWait is set to True then - // the maximum measurement time will be used. See data sheet pages 2, 5 - // and 7 for more details. - // A continuous mode measurement can be read immediately after re-sending - // the mode command. - - switch (BH1750_MODE) { - - case BH1750::ONE_TIME_LOW_RES_MODE: - // Send mode to sensor - I2C->beginTransmission(BH1750_I2CADDR); - __wire_write((uint8_t)BH1750_MODE); - I2C->endTransmission(); - maxWait ? _delay_ms(24 * BH1750_MTreg / (byte)BH1750_DEFAULT_MTREG) - : _delay_ms(16 * BH1750_MTreg / (byte)BH1750_DEFAULT_MTREG); - break; - case BH1750::ONE_TIME_HIGH_RES_MODE: - case BH1750::ONE_TIME_HIGH_RES_MODE_2: - // Send mode to sensor - I2C->beginTransmission(BH1750_I2CADDR); - __wire_write((uint8_t)BH1750_MODE); - I2C->endTransmission(); - maxWait ? _delay_ms(180 * BH1750_MTreg / (byte)BH1750_DEFAULT_MTREG) - : _delay_ms(120 * BH1750_MTreg / (byte)BH1750_DEFAULT_MTREG); - break; - default: - break; - } - // Read two bytes from the sensor, which are low and high parts of the sensor // value if (2 == I2C->requestFrom((int)BH1750_I2CADDR, (int)2)) {