Fix One-Time use case

This commit is contained in:
Chris Laws
2017-08-27 17:29:49 +09:30
parent 47b0c77b81
commit 509ee6a6bf
5 changed files with 64 additions and 15 deletions
+25 -8
View File
@@ -56,6 +56,7 @@ BH1750::BH1750(byte addr) {
}
/**
* Begin I2C and configure sensor
* @param SDA Date SCL Clock mode Measurment mode
@@ -64,13 +65,15 @@ BH1750::BH1750(byte addr) {
void BH1750::begin(uint8_t SDA, uint8_t SCL, uint8_t mode) {
// allow config of pins
Wire.begin(SDA,SCL);
Wire.begin(SDA, SCL);
// Configure sensor in specified mode
configure(mode);
}
#endif
/**
* Begin I2C and configure sensor
* @param mode Measurment mode
@@ -87,12 +90,13 @@ void BH1750::begin(uint8_t mode) {
/**
* Configurate BH1750 with specified working mode
* Configure BH1750 with specified working mode
* @param mode Measurment mode
*/
void BH1750::configure(uint8_t mode) {
// Check, is measurment mode exist
// Check measurment mode is valid
switch (mode) {
case BH1750_CONTINUOUS_HIGH_RES_MODE:
@@ -102,12 +106,15 @@ void BH1750::configure(uint8_t mode) {
case BH1750_ONE_TIME_HIGH_RES_MODE_2:
case BH1750_ONE_TIME_LOW_RES_MODE:
// Save mode so it can be restored when One-Time modes are used.
BH1750_MODE = mode;
// Send mode to sensor
Wire.beginTransmission(BH1750_I2CADDR);
__wire_write((uint8_t)mode);
__wire_write((uint8_t)BH1750_MODE);
Wire.endTransmission();
// Wait few moments for waking up
// Wait a few moments to wake up
_delay_ms(10);
break;
@@ -127,13 +134,23 @@ void BH1750::configure(uint8_t mode) {
/**
* Read light level from sensor
* @return Lightness in lux (0 ~ 65535)
* @return Light level in lux (0 ~ 65535)
*/
uint16_t BH1750::readLightLevel(void) {
// Measurment result will be stored here
uint16_t level;
// One-Time modes apparently need to be re-applied after power-up
// to ensure the chip is awake.
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:
__wire_write((uint8_t)BH1750_MODE);
break;
}
// Read two bytes from sensor
Wire.requestFrom(BH1750_I2CADDR, 2);
@@ -142,7 +159,7 @@ uint16_t BH1750::readLightLevel(void) {
level <<= 8;
level |= __wire_read();
// Send raw value if debug enabled
// Print raw value if debug enabled
#ifdef BH1750_DEBUG
Serial.print(F("[BH1750] Raw value: "));
Serial.println(level);
@@ -151,7 +168,7 @@ uint16_t BH1750::readLightLevel(void) {
// Convert raw value to lux
level /= 1.2;
// Send converted value, if debug enabled
// Print converted value if debug enabled
#ifdef BH1750_DEBUG
Serial.print(F("[BH1750] Converted value: "));
Serial.println(level);
+1
View File
@@ -70,6 +70,7 @@ class BH1750 {
private:
int BH1750_I2CADDR;
uint8_t BH1750_MODE;
};
+31
View File
@@ -0,0 +1,31 @@
#!/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 $sketch
# ./arduino-builder -hardware ./hardware -tools ./hardware/tools/avr -tools ./tools-builder -libraries ./libraries -fqbn esp8266:esp8266:generic $sketch
done
# Unlink BH1750 library from Arduino libraries directory
unlink $ARDUINO_IDE_PATH/libraries/$LIBNAME
+3 -3
View File
@@ -12,9 +12,9 @@
SDA -> SDA (A4 on Arduino Uno, Leonardo, etc or 20 on Mega and Due)
ADD -> GND or VCC (see below)
ADD pin uses to set sensor I2C address. If it has voltage greater or equal to
0.7VCC voltage (as example, you've connected it to VCC) - sensor address will be
0x5C. In other case (if ADD voltage less than 0.7 * VCC) - sensor address will
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).
*/
+4 -4
View File
@@ -13,9 +13,9 @@
SDA -> SDA (A4 on Arduino Uno, Leonardo, etc or 20 on Mega and Due on esp8266 free selectable)
ADD -> (not connected) or GND
ADD pin uses to set sensor I2C address. If it has voltage greater or equal to
0.7VCC voltage (as example, you've connected it to VCC) - sensor address will be
0x5C. In other case (if ADD voltage less than 0.7 * VCC) - sensor address will
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).
*/
@@ -29,7 +29,7 @@ void setup(){
Serial.begin(9600);
lightMeter.begin();
//lightMeter.begin(begin(D4, D3)); //use this line on a esp8266
//lightMeter.begin(begin(D4, D3)); // use this line on a esp8266 to modify pins
Serial.println(F("BH1750 Test"));
}