forked from claws/BH1750
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BH1750.cpp
97 lines (71 loc) · 1.79 KB
/
BH1750.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
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.
Written by Christopher Laws, March, 2013.
*/
#include "BH1750.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();
}