mirror of
https://github.com/claws/BH1750.git
synced 2026-07-28 04:06:05 +00:00
Merge branch 'master' of https://github.com/claws/BH1750 into errorfix
This commit is contained in:
+1
-1
@@ -1,7 +1,7 @@
|
||||
language: cpp
|
||||
env:
|
||||
global:
|
||||
- ARDUINO_PACKAGE_VERSION=1.8.4
|
||||
- ARDUINO_PACKAGE_VERSION=1.8.5
|
||||
- DISPLAY=:1.0
|
||||
|
||||
before_install:
|
||||
|
||||
+55
-60
@@ -59,9 +59,9 @@ BH1750::BH1750(byte addr) {
|
||||
|
||||
/**
|
||||
* Configure sensor
|
||||
* @param mode Measurment mode
|
||||
* @param mode Measurement mode
|
||||
*/
|
||||
bool BH1750::begin(uint8_t mode) {
|
||||
bool BH1750::begin(Mode mode) {
|
||||
|
||||
// I2C is expected to be initialized outside this library
|
||||
|
||||
@@ -72,23 +72,23 @@ bool BH1750::begin(uint8_t mode) {
|
||||
|
||||
|
||||
/**
|
||||
* Configure BH1750 with specified working mode
|
||||
* @param mode Measurment mode
|
||||
* Configure BH1750 with specified mode
|
||||
* @param mode Measurement mode
|
||||
*/
|
||||
bool BH1750::configure(uint8_t mode) {
|
||||
|
||||
bool BH1750::configure(Mode mode) {
|
||||
|
||||
// default transmission result to a value out of normal range
|
||||
byte ack = 5;
|
||||
|
||||
// Check measurment mode is valid
|
||||
|
||||
// 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:
|
||||
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
|
||||
Wire.beginTransmission(BH1750_I2CADDR);
|
||||
@@ -100,38 +100,27 @@ bool BH1750::configure(uint8_t mode) {
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
// Invalid measurement mode
|
||||
#ifdef BH1750_DEBUG
|
||||
Serial.println(F("BH1750: Invalid measurment mode"));
|
||||
#endif
|
||||
|
||||
Serial.println(F("[BH1750] ERROR: Invalid mode"));
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
// Check result code
|
||||
switch (ack) {
|
||||
case 0:
|
||||
BH1750_MODE = mode;
|
||||
case 0:
|
||||
BH1750_MODE = mode;
|
||||
return true;
|
||||
case 1: //too long for transmit buffer
|
||||
#ifdef BH1750_DEBUG
|
||||
Serial.println(F("too long for transmit buffer"));
|
||||
#endif
|
||||
case 2: //received NACK on transmit of address
|
||||
#ifdef BH1750_DEBUG
|
||||
Serial.println(F("received NACK on transmit of address"));
|
||||
#endif
|
||||
case 3: //received NACK on transmit of data
|
||||
#ifdef BH1750_DEBUG
|
||||
Serial.println(F("received NACK on transmit of data"));
|
||||
#endif
|
||||
case 4: //other error
|
||||
#ifdef BH1750_DEBUG
|
||||
Serial.println(F("other error"));
|
||||
#endif
|
||||
default:
|
||||
case 1: // too long for transmit buffer
|
||||
Serial.println(F("[BH1750] ERROR: too long for transmit buffer"));
|
||||
case 2: // received NACK on transmit of address
|
||||
Serial.println(F("[BH1750] ERROR: received NACK on transmit of address"));
|
||||
case 3: // received NACK on transmit of data
|
||||
Serial.println(F("[BH1750] ERROR: received NACK on transmit of data"));
|
||||
case 4: // other error
|
||||
Serial.println(F("[BH1750] ERROR: other error"));
|
||||
default:
|
||||
Serial.println(F("[BH1750] ERROR: undefined error"));
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -146,42 +135,48 @@ bool BH1750::configure(uint8_t mode) {
|
||||
*/
|
||||
uint16_t BH1750::readLightLevel(bool maxWait) {
|
||||
|
||||
// Measurment result will be stored here
|
||||
uint16_t level;
|
||||
if (BH1750_MODE == UNCONFIGURED) {
|
||||
Serial.println(F("[BH1750] Device is not configured!"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Measurement result will be stored here
|
||||
uint16_t level(NAN);
|
||||
|
||||
// Send mode to sensor
|
||||
Wire.beginTransmission(BH1750_I2CADDR);
|
||||
__wire_write((uint8_t)BH1750_MODE);
|
||||
Wire.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
|
||||
// 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.
|
||||
|
||||
// One-Time modes need to be re-applied after power-up. They 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.
|
||||
switch (BH1750_MODE) {
|
||||
|
||||
case BH1750_ONE_TIME_HIGH_RES_MODE:
|
||||
case BH1750_ONE_TIME_HIGH_RES_MODE_2:
|
||||
case BH1750_ONE_TIME_LOW_RES_MODE:
|
||||
case BH1750::ONE_TIME_LOW_RES_MODE:
|
||||
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();
|
||||
|
||||
// wait for measurement time
|
||||
if (BH1750_MODE == BH1750_ONE_TIME_LOW_RES_MODE) {
|
||||
if (BH1750_MODE == BH1750::ONE_TIME_LOW_RES_MODE) {
|
||||
maxWait ? _delay_ms(24) : _delay_ms(16);
|
||||
}
|
||||
else {
|
||||
maxWait ? _delay_ms(180) :_delay_ms(120);
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
// Read two bytes from sensor
|
||||
// Read two bytes from the sensor, which are low and high parts of the sensor
|
||||
// value
|
||||
Wire.requestFrom(BH1750_I2CADDR, 2);
|
||||
|
||||
if (Wire.available() == 2) {
|
||||
// Read two bytes, which are low and high parts of sensor value
|
||||
level = __wire_read();
|
||||
level <<= 8;
|
||||
level |= __wire_read();
|
||||
|
||||
@@ -30,44 +30,41 @@
|
||||
// No active state
|
||||
#define BH1750_POWER_DOWN 0x00
|
||||
|
||||
// Wating for measurment command
|
||||
// Wating for measurement command
|
||||
#define BH1750_POWER_ON 0x01
|
||||
|
||||
// Reset data register value - not accepted in POWER_DOWN mode
|
||||
#define BH1750_RESET 0x07
|
||||
|
||||
// Start measurement at 1lx resolution. Measurement time is approx 120ms.
|
||||
#define BH1750_CONTINUOUS_HIGH_RES_MODE 0x10
|
||||
|
||||
// Start measurement at 0.5lx resolution. Measurement time is approx 120ms.
|
||||
#define BH1750_CONTINUOUS_HIGH_RES_MODE_2 0x11
|
||||
|
||||
// Start measurement at 4lx resolution. Measurement time is approx 16ms.
|
||||
#define BH1750_CONTINUOUS_LOW_RES_MODE 0x13
|
||||
|
||||
// Start measurement at 1lx resolution. Measurement time is approx 120ms.
|
||||
// Device is automatically set to Power Down after measurement.
|
||||
#define BH1750_ONE_TIME_HIGH_RES_MODE 0x20
|
||||
|
||||
// Start measurement at 0.5lx resolution. Measurement time is approx 120ms.
|
||||
// Device is automatically set to Power Down after measurement.
|
||||
#define BH1750_ONE_TIME_HIGH_RES_MODE_2 0x21
|
||||
|
||||
// Start measurement at 1lx resolution. Measurement time is approx 120ms.
|
||||
// Device is automatically set to Power Down after measurement.
|
||||
#define BH1750_ONE_TIME_LOW_RES_MODE 0x23
|
||||
|
||||
class BH1750 {
|
||||
|
||||
public:
|
||||
BH1750 (byte addr = 0x23);
|
||||
bool begin (uint8_t mode = BH1750_CONTINUOUS_HIGH_RES_MODE);
|
||||
bool configure (uint8_t mode);
|
||||
|
||||
enum Mode
|
||||
{
|
||||
UNCONFIGURED = 0,
|
||||
// Measurement at 1lx resolution. Measurement time is approx 120ms.
|
||||
CONTINUOUS_HIGH_RES_MODE = 0x10,
|
||||
// Measurement at 0.5lx resolution. Measurement time is approx 120ms.
|
||||
CONTINUOUS_HIGH_RES_MODE_2 = 0x11,
|
||||
// Measurement at 4lx resolution. Measurement time is approx 16ms.
|
||||
CONTINUOUS_LOW_RES_MODE = 0x13,
|
||||
// Measurement at 1lx resolution. Measurement time is approx 120ms.
|
||||
ONE_TIME_HIGH_RES_MODE = 0x20,
|
||||
// Measurement at 0.5lx resolution. Measurement time is approx 120ms.
|
||||
ONE_TIME_HIGH_RES_MODE_2 = 0x21,
|
||||
// Measurement at 1lx resolution. Measurement time is approx 120ms.
|
||||
ONE_TIME_LOW_RES_MODE = 0x23
|
||||
};
|
||||
|
||||
BH1750(byte addr = 0x23);
|
||||
bool begin(Mode mode = CONTINUOUS_HIGH_RES_MODE);
|
||||
bool configure(Mode mode);
|
||||
uint16_t readLightLevel(bool maxWait = false);
|
||||
|
||||
private:
|
||||
int BH1750_I2CADDR;
|
||||
uint8_t BH1750_MODE;
|
||||
Mode BH1750_MODE = UNCONFIGURED;
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2018 claws
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -17,23 +17,23 @@ A common module containing the BH1750 component is the GY-30 shown below.
|
||||
|
||||
## Overview
|
||||
|
||||
The BH1750 has six different measurment modes which are divided in two groups;
|
||||
continuous and one-time measurments. In continuous mode the sensor
|
||||
The BH1750 has six different measurement modes which are divided in two groups;
|
||||
continuous and one-time measurements. In continuous mode the sensor
|
||||
continuously measures lightness value. In one-time mode, the sensor makes only
|
||||
one measurment and then goes into Power Down mode.
|
||||
one measurement and then goes into Power Down mode.
|
||||
|
||||
Each mode has three different precisions:
|
||||
|
||||
- Low Resolution Mode - (4 lx precision, 16ms measurment time)
|
||||
- High Resolution Mode - (1 lx precision, 120ms measurment time)
|
||||
- High Resolution Mode 2 - (0.5 lx precision, 120ms measurment time)
|
||||
- Low Resolution Mode - (4 lx precision, 16ms measurement time)
|
||||
- High Resolution Mode - (1 lx precision, 120ms measurement time)
|
||||
- High Resolution Mode 2 - (0.5 lx precision, 120ms measurement time)
|
||||
|
||||
By default, this library uses Continuous High Resolution Mode, but you can
|
||||
change this to a different mode by passing the mode argument to
|
||||
BH1750.begin().
|
||||
|
||||
When the One-Time mode is used your sensor will go into Power Down mode when
|
||||
it completes the measurment and you've read it. When the sensor is powered up
|
||||
it completes the measurement and you've read it. When the sensor is powered up
|
||||
again it returns to the default mode which means it needs to be reconfigured
|
||||
back into One-Time mode. This library has been implemented to automatically
|
||||
reconfigure the sensor when you next attempt a measurement so you should not
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
- 0x23 (most common) (if ADD pin had < 0.7VCC voltage)
|
||||
- 0x5C (if ADD pin had > 0.7VCC voltage)
|
||||
|
||||
Library use 0x23 address as default, but you can define any other address.
|
||||
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.
|
||||
|
||||
*/
|
||||
@@ -44,23 +44,23 @@ void setup(){
|
||||
|
||||
/*
|
||||
|
||||
BH1750 has six different measurment modes. They are divided in two groups -
|
||||
continuous and one-time measurments. In continuous mode, sensor continuously
|
||||
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
|
||||
measurment and then goes into Power Down mode.
|
||||
measurement and then goes into Power Down mode.
|
||||
|
||||
Each mode, has three different precisions:
|
||||
|
||||
- Low Resolution Mode - (4 lx precision, 16ms measurment time)
|
||||
- High Resolution Mode - (1 lx precision, 120ms measurment time)
|
||||
- High Resolution Mode 2 - (0.5 lx precision, 120ms measurment time)
|
||||
- Low Resolution Mode - (4 lx precision, 16ms measurement time)
|
||||
- High Resolution Mode - (1 lx precision, 120ms measurement time)
|
||||
- High Resolution Mode 2 - (0.5 lx precision, 120ms measurement time)
|
||||
|
||||
By default, the library uses Continuous High Resolution Mode, but you can
|
||||
set any other mode, by passing it to BH1750.begin() or BH1750.configure()
|
||||
functions.
|
||||
|
||||
[!] Remember, if you use One-Time mode, your sensor will go to Power Down
|
||||
mode each time, when it completes measurment and you've read it.
|
||||
mode each time, when it completes a measurement and you've read it.
|
||||
|
||||
Full mode list:
|
||||
|
||||
@@ -74,8 +74,13 @@ void setup(){
|
||||
|
||||
*/
|
||||
|
||||
lightMeter.begin(BH1750_CONTINUOUS_HIGH_RES_MODE);
|
||||
Serial.println(F("BH1750 Test"));
|
||||
// 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 {
|
||||
Serial.println(F("Error initialising BH1750"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
Example of BH1750 library usage.
|
||||
|
||||
This example initalises the BH1750 object using the high resolution
|
||||
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.
|
||||
@@ -24,7 +24,7 @@ void setup(){
|
||||
Wire.begin();
|
||||
// On esp8266 you can select SCL and SDA pins using Wire.begin(D4, D3);
|
||||
|
||||
lightMeter.begin(BH1750_ONE_TIME_HIGH_RES_MODE);
|
||||
lightMeter.begin(BH1750::ONE_TIME_HIGH_RES_MODE);
|
||||
|
||||
Serial.println(F("BH1750 One-Time Test"));
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
Example of BH1750 library usage.
|
||||
|
||||
This example initalises the BH1750 object using the default high resolution
|
||||
This example initialises the BH1750 object using the default high resolution
|
||||
continuous mode and then makes a light level reading every second.
|
||||
|
||||
Connection:
|
||||
@@ -25,6 +25,7 @@
|
||||
|
||||
BH1750 lightMeter;
|
||||
|
||||
|
||||
void setup(){
|
||||
|
||||
Serial.begin(9600);
|
||||
@@ -35,7 +36,7 @@ void setup(){
|
||||
|
||||
lightMeter.begin();
|
||||
|
||||
Serial.println(F("BH1750 Test"));
|
||||
Serial.println(F("BH1750 Test begin"));
|
||||
|
||||
}
|
||||
|
||||
|
||||
+10
-10
@@ -6,15 +6,15 @@
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
BH1750 KEYWORD1
|
||||
BH1750 KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
begin KEYWORD2
|
||||
configure KEYWORD2
|
||||
readLightLevel KEYWORD2
|
||||
begin KEYWORD2
|
||||
configure KEYWORD2
|
||||
readLightLevel KEYWORD2
|
||||
|
||||
|
||||
#######################################
|
||||
@@ -25,9 +25,9 @@ readLightLevel KEYWORD2
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
#######################################
|
||||
BH1750_CONTINUOUS_HIGH_RES_MODE LITERAL1
|
||||
BH1750_CONTINUOUS_HIGH_RES_MODE_2 LITERAL1
|
||||
BH1750_CONTINUOUS_LOW_RES_MODE LITERAL1
|
||||
BH1750_ONE_TIME_HIGH_RES_MODE LITERAL1
|
||||
BH1750_ONE_TIME_HIGH_RES_MODE_2 LITERAL1
|
||||
BH1750_ONE_TIME_LOW_RES_MODE LITERAL1
|
||||
BH1750_CONTINUOUS_HIGH_RES_MODE LITERAL1
|
||||
BH1750_CONTINUOUS_HIGH_RES_MODE_2 LITERAL1
|
||||
BH1750_CONTINUOUS_LOW_RES_MODE LITERAL1
|
||||
BH1750_ONE_TIME_HIGH_RES_MODE LITERAL1
|
||||
BH1750_ONE_TIME_HIGH_RES_MODE_2 LITERAL1
|
||||
BH1750_ONE_TIME_LOW_RES_MODE LITERAL1
|
||||
|
||||
Reference in New Issue
Block a user