-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathDigital_Light_TSL2561.cpp
223 lines (199 loc) · 7.19 KB
/
Digital_Light_TSL2561.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
Digital_Light_TSL2561.cpp
A library for TSL2561
Copyright (c) 2012 seeed technology inc.
Website : www.seeed.cc
Author : zhangkun
Create Time:
Change Log : Jack Shao, Nov 2014, bug fix and update for user experience
The MIT License (MIT)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <Digital_Light_TSL2561.h>
#include <Arduino.h>
#include <Wire.h>
TSL2561_CalculateLux::TSL2561_CalculateLux(uint32_t read_timeout) : read_timeout(read_timeout) {
}
uint8_t TSL2561_CalculateLux::readRegister(int deviceAddress, int address) {
uint8_t value;
Wire.beginTransmission(deviceAddress);
Wire.write(address); // register to read
Wire.endTransmission();
Wire.requestFrom(deviceAddress, 1); // read a byte
uint32_t readStart = micros();
while (!Wire.available()) {
if (read_timeout != TSL2561_NO_READ_TIMEOUT && micros() - readStart >= read_timeout) {
hadTimeout = true;
break;
}
}
value = Wire.read();
//delay(100);
return value;
}
void TSL2561_CalculateLux::writeRegister(int deviceAddress, int address, uint8_t val) {
Wire.beginTransmission(deviceAddress); // start transmission to device
Wire.write(address); // send register address
Wire.write(val); // send value to write
Wire.endTransmission(); // end transmission
//delay(100);
}
void TSL2561_CalculateLux::getLux(void) {
CH0_LOW = readRegister(TSL2561_Address, TSL2561_Channal0L);
CH0_HIGH = readRegister(TSL2561_Address, TSL2561_Channal0H);
//read two bytes from registers 0x0E and 0x0F
CH1_LOW = readRegister(TSL2561_Address, TSL2561_Channal1L);
CH1_HIGH = readRegister(TSL2561_Address, TSL2561_Channal1H);
ch0 = (CH0_HIGH << 8) | CH0_LOW;
ch1 = (CH1_HIGH << 8) | CH1_LOW;
}
void TSL2561_CalculateLux::init() {
writeRegister(TSL2561_Address, TSL2561_Control, 0x03); // POWER UP
writeRegister(TSL2561_Address, TSL2561_Timing, 0x00); //No High Gain (1x), integration time of 13ms
writeRegister(TSL2561_Address, TSL2561_Interrupt, 0x00);
writeRegister(TSL2561_Address, TSL2561_Control, 0x00); // POWER Down
}
uint16_t TSL2561_CalculateLux::readIRLuminosity() { // read Infrared channel value only, not convert to lux.
writeRegister(TSL2561_Address, TSL2561_Control, 0x03); // POWER UP
delay(14);
getLux();
writeRegister(TSL2561_Address, TSL2561_Control, 0x00); // POWER Down
if (ch1 == 0) {
return 0;
}
if (ch0 / ch1 < 2 && ch0 > 4900) {
return -1; //ch0 out of range, but ch1 not. the lux is not valid in this situation.
}
return ch1;
}
uint16_t TSL2561_CalculateLux::readFSpecLuminosity() { //read Full Spectrum channel value only, not convert to lux.
writeRegister(TSL2561_Address, TSL2561_Control, 0x03); // POWER UP
delay(14);
getLux();
writeRegister(TSL2561_Address, TSL2561_Control, 0x00); // POWER Down
if (ch1 == 0) {
return 0;
}
if (ch0 / ch1 < 2 && ch0 > 4900) {
return -1; //ch0 out of range, but ch1 not. the lux is not valid in this situation.
}
return ch0;
}
signed long TSL2561_CalculateLux::readVisibleLux() {
writeRegister(TSL2561_Address, TSL2561_Control, 0x03); // POWER UP
delay(14);
getLux();
writeRegister(TSL2561_Address, TSL2561_Control, 0x00); // POWER Down
if (ch1 == 0) {
return 0;
}
if (ch0 / ch1 < 2 && ch0 > 4900) {
return -1; //ch0 out of range, but ch1 not. the lux is not valid in this situation.
}
return calculateLux(0, 0, 0); //T package, no gain, 13ms
}
unsigned long TSL2561_CalculateLux::calculateLux(unsigned int iGain, unsigned int tInt, int iType) {
switch (tInt) {
case 0: // 13.7 msec
chScale = CHSCALE_TINT0;
break;
case 1: // 101 msec
chScale = CHSCALE_TINT1;
break;
default: // assume no scaling
chScale = (1 << CH_SCALE);
break;
}
if (!iGain) {
chScale = chScale << 4; // scale 1X to 16X
}
// scale the channel values
channel0 = (ch0 * chScale) >> CH_SCALE;
channel1 = (ch1 * chScale) >> CH_SCALE;
ratio1 = 0;
if (channel0 != 0) {
ratio1 = (channel1 << (RATIO_SCALE + 1)) / channel0;
}
// round the ratio value
unsigned long ratio = (ratio1 + 1) >> 1;
switch (iType) {
case 0: // T package
if ((ratio >= 0) && (ratio <= K1T)) {
b = B1T;
m = M1T;
} else if (ratio <= K2T) {
b = B2T;
m = M2T;
} else if (ratio <= K3T) {
b = B3T;
m = M3T;
} else if (ratio <= K4T) {
b = B4T;
m = M4T;
} else if (ratio <= K5T) {
b = B5T;
m = M5T;
} else if (ratio <= K6T) {
b = B6T;
m = M6T;
} else if (ratio <= K7T) {
b = B7T;
m = M7T;
} else if (ratio > K8T) {
b = B8T;
m = M8T;
}
break;
case 1:// CS package
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) {
b = B4C;
m = M4C;
} else if (ratio <= K5C) {
b = B5C;
m = M5C;
} else if (ratio <= K6C) {
b = B6C;
m = M6C;
} else if (ratio <= K7C) {
b = B7C;
m = M7C;
}
}
temp = ((channel0 * b) - (channel1 * m));
if (temp < 0) {
temp = 0;
}
temp += (1 << (LUX_SCALE - 1));
// strip off fractional portion
lux = temp >> LUX_SCALE;
return (lux);
}
bool TSL2561_CalculateLux::checkHadTimeout(){
bool timeout = hadTimeout;
hadTimeout = false;
return timeout;
}
TSL2561_CalculateLux TSL2561;