Initial structure for HalMultiSensor
Former-commit-id: 22bc809d9e6de8a2994f1df699ae74bb7245378f
This commit is contained in:
parent
70b1608dd8
commit
a6f8cf872a
14 changed files with 612 additions and 0 deletions
93
arduino/HalMultiSensor/HardwareBH1750.cpp
Executable file
93
arduino/HalMultiSensor/HardwareBH1750.cpp
Executable file
|
|
@ -0,0 +1,93 @@
|
|||
/*
|
||||
|
||||
This is a library for the BH1750FVI Digital Light Sensor
|
||||
breakout board.
|
||||
|
||||
The board uses I2C for communication. 2 pins are required to
|
||||
interface to the device.
|
||||
|
||||
|
||||
based on Christopher Laws, March, 2013 code.
|
||||
|
||||
*/
|
||||
|
||||
#include "HardwareBH1750.h"
|
||||
#include <util/delay.h>
|
||||
|
||||
|
||||
BH1750::BH1750() {}
|
||||
|
||||
void BH1750::begin(uint8_t mode) {
|
||||
Wire.begin();
|
||||
//write8(mode);
|
||||
configure(mode);
|
||||
}
|
||||
|
||||
|
||||
void BH1750::configure(uint8_t mode) {
|
||||
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:
|
||||
// apply a valid mode change
|
||||
write8(mode);
|
||||
_delay_ms(10);
|
||||
break;
|
||||
default:
|
||||
// Invalid measurement mode
|
||||
#if BH1750_DEBUG == 1
|
||||
Serial.println("Invalid measurement mode");
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
uint16_t BH1750::readLightLevel(void) {
|
||||
uint16_t level;
|
||||
|
||||
Wire.beginTransmission(BH1750_I2CADDR);
|
||||
Wire.requestFrom(BH1750_I2CADDR, 2);
|
||||
#if (ARDUINO >= 100)
|
||||
level = Wire.read();
|
||||
level <<= 8;
|
||||
level |= Wire.read();
|
||||
#else
|
||||
level = Wire.receive();
|
||||
level <<= 8;
|
||||
level |= Wire.receive();
|
||||
#endif
|
||||
Wire.endTransmission();
|
||||
|
||||
#if BH1750_DEBUG == 1
|
||||
Serial.print("Raw light level: ");
|
||||
Serial.println(level);
|
||||
#endif
|
||||
|
||||
level = level/1.2; // convert to lux
|
||||
|
||||
#if BH1750_DEBUG == 1
|
||||
Serial.print("Light level: ");
|
||||
Serial.println(level);
|
||||
#endif
|
||||
return level;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*********************************************************************/
|
||||
|
||||
|
||||
void BH1750::write8(uint8_t d) {
|
||||
Wire.beginTransmission(BH1750_I2CADDR);
|
||||
#if (ARDUINO >= 100)
|
||||
Wire.write(d);
|
||||
#else
|
||||
Wire.send(d);
|
||||
#endif
|
||||
Wire.endTransmission();
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue