Skip to content

Commit

Permalink
Merge pull request #445 from whatisbyandby/add-bh1750-driver
Browse files Browse the repository at this point in the history
Add bh1750 driver
  • Loading branch information
brentru authored Jul 11, 2023
2 parents f029ca7 + 85c9009 commit c6983b1
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ html/*
*.py

# VSCode artifacts
./vscode/*
.vscode/*
src/.vscode/settings.json

.DS_STORE

examples/Wippersnapper_demo/build/

#Platformio artifacts
.pio/
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ paragraph=Arduino client for Adafruit.io WipperSnapper
category=Communication
url=https://github.com/adafruit/Adafruit_IO_Arduino
architectures=*
depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit SleepyDog Library, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit DPS310, Adafruit SCD30, Sensirion I2C SCD4x, Sensirion I2C SEN5X, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit PM25 AQI Sensor, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library
depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit SleepyDog Library, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit DPS310, Adafruit SCD30, Sensirion I2C SCD4x, Sensirion I2C SEN5X, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit PM25 AQI Sensor, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, hp_BH1750
10 changes: 10 additions & 0 deletions src/components/i2c/WipperSnapper_I2C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,16 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
}
_ahtx0->configureDriver(msgDeviceInitReq);
drivers.push_back(_ahtx0);
} else if (strcmp("bh1750", msgDeviceInitReq->i2c_device_name) == 0) {
_bh1750 = new WipperSnapper_I2C_Driver_BH1750(this->_i2c, i2cAddress);
if (!_bh1750->begin()) {
WS_DEBUG_PRINTLN("ERROR: Failed to initialize BH1750 chip!");
_busStatusResponse =
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
return false;
}
_bh1750->configureDriver(msgDeviceInitReq);
drivers.push_back(_bh1750);
} else if (strcmp("bme280", msgDeviceInitReq->i2c_device_name) == 0) {
_bme280 = new WipperSnapper_I2C_Driver_BME280(this->_i2c, i2cAddress);
if (!_bme280->begin()) {
Expand Down
2 changes: 2 additions & 0 deletions src/components/i2c/WipperSnapper_I2C.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "drivers/WipperSnapper_I2C_Driver.h"
#include "drivers/WipperSnapper_I2C_Driver_ADT7410.h"
#include "drivers/WipperSnapper_I2C_Driver_AHTX0.h"
#include "drivers/WipperSnapper_I2C_Driver_BH1750.h"
#include "drivers/WipperSnapper_I2C_Driver_BME280.h"
#include "drivers/WipperSnapper_I2C_Driver_BME680.h"
#include "drivers/WipperSnapper_I2C_Driver_DPS310.h"
Expand Down Expand Up @@ -91,6 +92,7 @@ class WipperSnapper_Component_I2C {
WipperSnapper_I2C_Driver_AHTX0 *_ahtx0 = nullptr;
WipperSnapper_I2C_Driver_DPS310 *_dps310 = nullptr;
WipperSnapper_I2C_Driver_SCD30 *_scd30 = nullptr;
WipperSnapper_I2C_Driver_BH1750 *_bh1750 = nullptr;
WipperSnapper_I2C_Driver_BME280 *_bme280 = nullptr;
WipperSnapper_I2C_Driver_BME680 *_bme680 = nullptr;
WipperSnapper_I2C_Driver_MCP9808 *_mcp9808 = nullptr;
Expand Down
85 changes: 85 additions & 0 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver_BH1750.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#ifndef WipperSnapper_I2C_Driver_BH1750_H
#define WipperSnapper_I2C_Driver_BH1750_H

#include "WipperSnapper_I2C_Driver.h"
#include <hp_BH1750.h> //include the library for the BH1750 sensor

/**************************************************************************/
/*!
@brief Class that provides a driver interface for a BH1750 Light sensor.
This driver uses the H-Resolution Mode and the default measurement
time register (MTreg) of 69. According to the datasheet this is
the recommended mode for most applications. Typical measurement
time in this mode is 120ms
This driver uses the One Time Measurement feature of the BH1750. The
sensor returns to Power Down mode after each reading.
*/
/**************************************************************************/
class WipperSnapper_I2C_Driver_BH1750 : public WipperSnapper_I2C_Driver {
public:
/*******************************************************************************/
/*!
@brief Constructor for a BH1750 sensor.
@param i2c
The I2C interface.
@param sensorAddress
7-bit device address.
*/
/*******************************************************************************/
WipperSnapper_I2C_Driver_BH1750(TwoWire *i2c, uint16_t sensorAddress)
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {
_i2c = i2c;
_sensorAddress = sensorAddress;
}

/*******************************************************************************/
/*!
@brief Destructor for a BH1750 sensor.
*/
/*******************************************************************************/
~WipperSnapper_I2C_Driver_BH1750() {
// Called when a BH1750 component is deleted.
delete _bh1750;
}

/*******************************************************************************/
/*!
@brief Initializes the BH1750 sensor and begins I2C.
The set the quality to the H-Resolution Mode.
@returns True if initialized successfully, False otherwise.
*/
/*******************************************************************************/
bool begin() {
_bh1750 = new hp_BH1750();
// attempt to initialize BH1750
if (!_bh1750->begin(_sensorAddress, _i2c))
return false;
// Set to the recommended quality setting
_bh1750->setQuality(BH1750_QUALITY_HIGH);
return true;
}

/*******************************************************************************/
/*!
@brief Performs a light sensor read using the One Time Measurement
feature of the BH1750. The sensor goes to Power Down mode after
each reading.
@param lightEvent
Light sensor reading, in lux.
@returns True if the sensor event was obtained successfully, False
otherwise.
*/
/*******************************************************************************/
bool getEventLight(sensors_event_t *lightEvent) {
_bh1750->start();
lightEvent->light = _bh1750->getLux();
return true;
}

protected:
hp_BH1750 *_bh1750; ///< Pointer to BH1750 light sensor object
};

#endif // WipperSnapper_I2C_Driver_BH1750

0 comments on commit c6983b1

Please sign in to comment.