-
Notifications
You must be signed in to change notification settings - Fork 3
/
isl29023.cpp
92 lines (67 loc) · 2.48 KB
/
isl29023.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
#include <Wire.h>
#include <isl29023.h>
#define ISL29023_ADDR 0x44
#define CMD1 0
#define CMD2 1
#define DATALSB 2
#define DATAMSB 3
unsigned char dev_address;
ISL29023::ISL29023(){
dev_address = ISL29023_ADDR;
}
ISL29023::ISL29023(unsigned char address){
dev_address = address;
}
void ISL29023::init()
{
unsigned char i2cdata[2];
i2cdata[0] = CMD1;
i2cdata[1] = 0b10100000;
Wire.beginTransmission(dev_address); // start transmission to device
Wire.write(i2cdata[0]); // send register address
Wire.write(i2cdata[1]); // send data to write
Wire.endTransmission(); // end transmission
i2cdata[0] = CMD2;
i2cdata[1] = 0b00000011;
Wire.beginTransmission(dev_address); // start transmission to device
Wire.write(i2cdata[0]); // send register address
Wire.write(i2cdata[1]); // send data to write
Wire.endTransmission(); // end transmission
}
/***********************************************************************/
*
* Reads the lux value back from the sensor.
*
/**********************************************************************/
float ISL29023::read()
{
unsigned char i2cdata[6];
unsigned int light;
float lux;
i2cdata[0] = DATAMSB;
Wire.beginTransmission(dev_address); // start transmission to device
Wire.write(i2cdata[0]); // send register address
Wire.endTransmission(); // end transmission
Wire.beginTransmission(dev_address); // start transmission to device
Wire.requestFrom((int)dev_address, 1); // send data n-bytes read/
while (Wire.available()) {
i2cdata[0] = Wire.read(); // receive DATA
}
Wire.endTransmission(); // end transmission
light = ((unsigned int)i2cdata[0])<<8;
i2cdata[0] = DATALSB;
Wire.beginTransmission(dev_address); // start transmission to device
Wire.write(i2cdata[0]); // send register address
Wire.endTransmission();
Wire.beginTransmission(dev_address); // start transmission to device
Wire.requestFrom((int)dev_address, 1);
while (Wire.available()) {
i2cdata[0] = Wire.read(); // receive DATA
}
Wire.endTransmission(); // end transmission
light |= i2cdata[0];
// this is a bit lame, ideally use data read back from the device
// to scale accordingly
lux = (64000 * (float)light)/65536;
return lux;
}