-
Notifications
You must be signed in to change notification settings - Fork 0
/
TSL2581.cpp
194 lines (162 loc) · 5.36 KB
/
TSL2581.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <Wire.h>
#include "TSL2581.h"
/**************************************************************************
Writes a register and an 8 bit value over I2C
**************************************************************************/
void WaveShare_TSL2581::Write8(byte reg, unsigned long value)
{
Wire.beginTransmission(I2C_addr);
Wire.write(reg);
Wire.write(value & 0xFF);
Wire.endTransmission();
}
/**************************************************************************
Read a register and an 8 bit value over I2C
**************************************************************************/
byte WaveShare_TSL2581::Read8(byte reg )
{
Wire.beginTransmission(I2C_addr);
Wire.write(reg);
Wire.endTransmission();
Wire.requestFrom(I2C_addr, 1);
if (Wire.available()) {
return Wire.read();
}
return 0;
}
/**************************************************************************
Constructor
**************************************************************************/
WaveShare_TSL2581::WaveShare_TSL2581() {
I2C_addr = ADDR_FLOAT; //ADDR --> float
}
WaveShare_TSL2581::WaveShare_TSL2581(byte addr) {
I2C_addr = addr;
}
/**************************************************************************
//power on and off
**************************************************************************/
void WaveShare_TSL2581::TSL2581_power_on(void) {
Write8(COMMAND_CMD | CONTROL, CONTROL_POWERON);
}
/**************************************************************************
//set timer and INTERRUPT
**************************************************************************/
void WaveShare_TSL2581::TSL2581_config(void) {
Write8(COMMAND_CMD | TIMING, INTEGRATIONTIME_400MS); //400MS
Write8(COMMAND_CMD | CONTROL, ADC_EN | CONTROL_POWERON); //
Write8(COMMAND_CMD | INTERRUPT, INTR_INTER_MODE);//Every ADC cycle generates interrupt
Write8(COMMAND_CMD | ANALOG, GAIN_16X);//GAIN = 16
}
void WaveShare_TSL2581::Reload_register(void)
{
Write8(COMMAND_CMD | TRANSACTION_SPECIAL | SPECIAL_FUN_INTCLEAR, SPECIAL_FUN_INTCLEAR);
Write8(COMMAND_CMD | CONTROL, ADC_EN | CONTROL_POWERON);
}
/**************************************************************************
//read i2c ID || ID = 80 ||
**************************************************************************/
byte WaveShare_TSL2581::TSL2581_Read_ID(void) {
return Read8(COMMAND_CMD | TRANSACTION | ID);
}
/*********************************************************
//SET_Interrupt_Threshold
**********************************************************/
void WaveShare_TSL2581::SET_Interrupt_Threshold(unsigned long low, unsigned long high)
{
unsigned int DataLLow, DataLHigh, DataHLow, DataHHigh;
DataLLow = low % 256;
DataLHigh = low / 256;
Write8(COMMAND_CMD | THLLOW, DataLLow);
Write8(COMMAND_CMD | THLHIGH, DataLHigh);
DataHLow = high % 256;
DataHHigh = high / 256;
Write8( COMMAND_CMD | THHLOW, DataHLow);
Write8(COMMAND_CMD | THHHIGH, DataHHigh);
}
void WaveShare_TSL2581::TSL2581_Read_Channel()
{
delay(450);
word low, high;
low = Read8( COMMAND_CMD | TRANSACTION | DATA0LOW);
high = Read8( COMMAND_CMD | TRANSACTION | DATA0HIGH);
ch0 = high * 256 + low;
low = Read8( COMMAND_CMD | TRANSACTION | DATA1LOW);
high = Read8( COMMAND_CMD | TRANSACTION | DATA1HIGH);
ch1 = high * 256 + low;
}
// Arguments: unsigned int iGain - gain, where 0:1X, 1:8X, 2:16X, 3:128X
// unsigned int tIntCycles - INTEG_CYCLES defined in Timing Register
unsigned long WaveShare_TSL2581::calculateLux(unsigned int iGain, unsigned int tIntCycles)
{
unsigned long chScale0;
unsigned long chScale1;
unsigned long channel1;
unsigned long channel0;
unsigned long temp;
unsigned long ratio1 = 0;
unsigned long ratio ;
unsigned long lux_temp;
unsigned int b, m;
// No scaling if nominal integration (148 cycles or 400 ms) is used
if (tIntCycles == NOM_INTEG_CYCLE)
chScale0 = 65536;
//chScale0 = (1 << (CH_SCALE));
else
chScale0 = (NOM_INTEG_CYCLE << CH_SCALE) / tIntCycles;
switch (iGain)
{
case 0: // 1x gain
chScale1 = chScale0; // No scale. Nominal setting
break;
case 1: // 8x gain
chScale0 = chScale0 >> 3; // Scale/multiply value by 1/8
chScale1 = chScale0;
break;
case 2: // 16x gain
chScale0 = chScale0 >> 4; // Scale/multiply value by 1/16
chScale1 = chScale0;
break;
case 3: // 128x gain
chScale1 = chScale0 / CH1GAIN128X;
chScale0 = chScale0 / CH0GAIN128X;
break;
}
// scale the channel values
channel0 = (ch0 * chScale0) >> CH_SCALE;
channel1 = (ch1 * chScale1) >> CH_SCALE;
// find the ratio of the channel values (Channel1/Channel0)
if (channel0 != 0)
ratio1 = (channel1 << (RATIO_SCALE + 1)) / channel0;
ratio = (ratio1 + 1) >> 1; // round the ratio value
if ((ratio >= 0) && (ratio <= K1C))
{
b = B1C;
m = M1C;
}
else if (ratio <= K2C)
{
b = B2C;
m = M2C;
}
else if (ratio <= K3C)
{
b = B3C;
m = M3C;
}
else if (ratio <= K4C)//276
{
b = B4C;
m = M4C;
}
else if (ratio > K5C)//276
{
b = B5C;
m = M5C;
}
temp = ((channel0 * b) - (channel1 * m));
// temp += (1 << (LUX_SCALE - 1));
temp = temp + 32768; // round lsb (2^(LUX_SCALE-1))
lux_temp = temp >> LUX_SCALE; // strip off fractional portion
return (lux_temp); // Signal I2C had no errors
}