-
Notifications
You must be signed in to change notification settings - Fork 8
/
LTR303.h
340 lines (301 loc) · 15.4 KB
/
LTR303.h
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
LTR303 illumination sensor library for Arduino
Lovelesh, thingTronics
The MIT License (MIT)
Copyright (c) 2015 thingTronics Limited
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.
version 0.1
*/
#ifndef LTR303_h
#define LTR303_h
#include "Arduino.h"
#define LTR303_ADDR 0x29 // default address
// LTR303 register addresses
#define LTR303_CONTR 0x80
#define LTR303_MEAS_RATE 0x85
#define LTR303_PART_ID 0x86
#define LTR303_MANUFAC_ID 0x87
#define LTR303_DATA_CH1_0 0x88
#define LTR303_DATA_CH1_1 0x89
#define LTR303_DATA_CH0_0 0x8A
#define LTR303_DATA_CH0_1 0x8B
#define LTR303_STATUS 0x8C
#define LTR303_INTERRUPT 0x8F
#define LTR303_THRES_UP_0 0x97
#define LTR303_THRES_UP_1 0x98
#define LTR303_THRES_LOW_0 0x99
#define LTR303_THRES_LOW_1 0x9A
#define LTR303_INTR_PERS 0x9E
class LTR303 {
public:
LTR303(void);
// LTR303 object
boolean begin(void);
// Initialize LTR303 library with default address (0x29)
// Always returns true
boolean setPowerUp(void);
// Turn on LTR303, begin integration
// Returns true (1) if successful, false (0) if there was an I2C error
// (Also see getError() below)
boolean setPowerDown(void);
// Turn off LTR303
// Returns true (1) if successful, false (0) if there was an I2C error
// (Also see getError() below)
boolean setControl(byte gain, boolean reset, boolean mode);
// Sets the gain, SW reset and mode of LTR303
// Default value is 0x00
// If gain = 0, device is set to 1X gain (default)
// If gain = 1, device is set to 2X gain
// If gain = 2, device is set to 4X gain
// If gain = 3, device is set to 8X gain
// If gain = 4, invalid
// If gain = 5, invalid
// If gain = 6, device is set to 48X gain
// If gain = 7, device is set to 96X gain
//----------------------------------------
// If reset = false(0), initial start-up procedure not started (default)
// If reset = true(1), initial start-up procedure started
//----------------------------------------
// If mode = false(0), stand-by mode (default)
// If mode = true(1), active mode
boolean getControl(byte &gain, boolean reset, boolean mode);
// Gets the control register values
// Default value is 0x00
// If gain = 0, device is set to 1X gain (default)
// If gain = 1, device is set to 2X gain
// If gain = 2, device is set to 4X gain
// If gain = 3, device is set to 8X gain
// If gain = 4, invalid
// If gain = 5, invalid
// If gain = 6, device is set to 48X gain
// If gain = 7, device is set to 96X gain
//----------------------------------------
// If reset = false(0), initial start-up procedure not started (default)
// If reset = true(1), initial start-up procedure started
//----------------------------------------
// If mode = false(0), stand-by mode (default)
// If mode = true(1), active mode
// Returns true (1) if successful, false (0) if there was an I2C error
// (Also see getError() below)
boolean setMeasurementRate(byte integrationTime, byte measurementRate);
// Sets the integration time and measurement rate of the sensor
// integrationTime is the measurement time for each ALs cycle
// measurementRate is the interval between DATA_REGISTERS update
// measurementRate must be set to be equal or greater than integrationTime
// Default value is 0x03
// If integrationTime = 0, integrationTime will be 100ms (default)
// If integrationTime = 1, integrationTime will be 50ms
// If integrationTime = 2, integrationTime will be 200ms
// If integrationTime = 3, integrationTime will be 400ms
// If integrationTime = 4, integrationTime will be 150ms
// If integrationTime = 5, integrationTime will be 250ms
// If integrationTime = 6, integrationTime will be 300ms
// If integrationTime = 7, integrationTime will be 350ms
//------------------------------------------------------
// If measurementRate = 0, measurementRate will be 50ms
// If measurementRate = 1, measurementRate will be 100ms
// If measurementRate = 2, measurementRate will be 200ms
// If measurementRate = 3, measurementRate will be 500ms (default)
// If measurementRate = 4, measurementRate will be 1000ms
// If measurementRate = 5, measurementRate will be 2000ms
// If measurementRate = 6, measurementRate will be 2000ms
// If measurementRate = 7, measurementRate will be 2000ms
// Returns true (1) if successful, false (0) if there was an I2C error
// (Also see getError() below)
boolean getMeasurementRate(byte &integrationTime, byte &measurementRate);
// Gets the value of Measurement Rate register
// Default value is 0x03
// If integrationTime = 0, integrationTime will be 100ms (default)
// If integrationTime = 1, integrationTime will be 50ms
// If integrationTime = 2, integrationTime will be 200ms
// If integrationTime = 3, integrationTime will be 400ms
// If integrationTime = 4, integrationTime will be 150ms
// If integrationTime = 5, integrationTime will be 250ms
// If integrationTime = 6, integrationTime will be 300ms
// If integrationTime = 7, integrationTime will be 350ms
//------------------------------------------------------
// If measurementRate = 0, measurementRate will be 50ms
// If measurementRate = 1, measurementRate will be 100ms
// If measurementRate = 2, measurementRate will be 200ms
// If measurementRate = 3, measurementRate will be 500ms (default)
// If measurementRate = 4, measurementRate will be 1000ms
// If measurementRate = 5, measurementRate will be 2000ms
// If measurementRate = 6, measurementRate will be 2000ms
// If measurementRate = 7, measurementRate will be 2000ms
// Returns true (1) if successful, false (0) if there was an I2C error
// (Also see getError() below)
boolean getPartID(byte &partID);
// Gets the part number ID and revision ID of the chip
// Default value is 0x0A
// part number ID = 0x0A (default)
// Revision ID = 0x00
// Returns true (1) if successful, false (0) if there was an I2C error
// (Also see getError() below)
boolean getManufacID(byte &manufacID);
// Gets the Manufacturers ID
// Default value is 0x05
// Returns true (1) if successful, false (0) if there was an I2C error
// (Also see getError() below)
boolean getData(unsigned int &CH0, unsigned int &CH1);
// Gets the 16-bit channel 0 and channel 1 data
// Default value of both channels is 0x00
// Returns true (1) if successful, false (0) if there was an I2C error
// (Also see getError() below)
boolean getStatus(boolean valid, byte &gain, boolean intrStatus, boolean dataStatus);
// Gets the status information of LTR303
// Default value is 0x00
// If valid = false(0), Sensor data is valid (default)
// If valid = true(1), Sensor data is invalid
//--------------------------------------------
// If gain = 0, device is set to 1X gain (default)
// If gain = 1, device is set to 2X gain
// If gain = 2, device is set to 4X gain
// If gain = 3, device is set to 8X gain
// If gain = 4, invalid
// If gain = 5, invalid
// If gain = 6, device is set to 48X gain
// If gain = 7, device is set to 96X gain
//---------------------------------------------
// If intrStatus = false(0), INTR in inactive (default)
// If intrStatus = true(1), INTR in active
//---------------------------------------------
// If dataStatus = false(0), OLD data (already read) (default)
// If dataStatus = true(1), NEW data
// Returns true (1) if successful, false (0) if there was an I2C error
// (Also see getError() below)
boolean setInterruptControl(boolean intrMode, boolean polarity);
// Sets up interrupt operations
// Default value is 0x08
// If intrMode = false(0), INT pin is inactive (default)
// If intrMode = true(1), INT pin is active
//------------------------------------------------------
// If polarity = false(0), INT pin is active at logic 0 (default)
// If polarity = true(1), INT pin is active at logic 1
//------------------------------------------------------
// Returns true (1) if successful, false (0) if there was an I2C error
// (Also see getError() below)
boolean getInterruptControl(boolean polarity, boolean intrMode);
// Sets up interrupt operations
// Default value is 0x08
// If polarity = false(0), INT pin is active at logic 0 (default)
// If polarity = true(1), INT pin is active at logic 1
//------------------------------------------------------
// If intrMode = false(0), INT pin is inactive (default)
// If intrMode = true(1), INT pin is active
//------------------------------------------------------
// Returns true (1) if successful, false (0) if there was an I2C error
// (Also see getError() below)
boolean setThreshold(unsigned int upperLimit, unsigned int lowerLimit);
// Sets the upper limit and lower limit of the threshold
// Default value of upper threshold is 0xFF and lower threshold is 0x00
// Both the threshold are 16-bit integer values
// Returns true (1) if successful, false (0) if there was an I2C error
// (Also see getError() below)
boolean getThreshold(unsigned int &upperLimit, unsigned int &lowerLimit);
// Gets the upper limit and lower limit of the threshold
// Default value of upper threshold is 0xFF and lower threshold is 0x00
// Both the threshold are 16-bit integer values
// Returns true (1) if successful, false (0) if there was an I2C error
// (Also see getError() below)
boolean setIntrPersist(byte persist);
// Sets the interrupt persistance i.e. controls the N number of times the
// measurement data is outside the range defined by upper and lower threshold
// Default value is 0x00
// If persist = 0, every sensor value out of threshold range (default)
// If persist = 1, every 2 consecutive value out of threshold range
// If persist = 2, every 3 consecutive value out of threshold range
// If persist = 3, every 4 consecutive value out of threshold range
// If persist = 4, every 5 consecutive value out of threshold range
// If persist = 5, every 6 consecutive value out of threshold range
// If persist = 6, every 7 consecutive value out of threshold range
// If persist = 7, every 8 consecutive value out of threshold range
// If persist = 8, every 9 consecutive value out of threshold range
// If persist = 9, every 10 consecutive value out of threshold range
// If persist = 10, every 11 consecutive value out of threshold range
// If persist = 11, every 12 consecutive value out of threshold range
// If persist = 12, every 13 consecutive value out of threshold range
// If persist = 13, every 14 consecutive value out of threshold range
// If persist = 14, every 15 consecutive value out of threshold range
// If persist = 15, every 16 consecutive value out of threshold range
// Returns true (1) if successful, false (0) if there was an I2C error
// (Also see getError() below)
boolean getIntrPersist(byte &persist);
// Gets the interrupt persistance i.e. controls the N number of times the measurement data is outside the range defined by upper and lower threshold
// Default value is 0x00
// If persist = 0, every sensor value out of threshold range (default)
// If persist = 1, every 2 consecutive value out of threshold range
// If persist = 2, every 3 consecutive value out of threshold range
// If persist = 3, every 4 consecutive value out of threshold range
// If persist = 4, every 5 consecutive value out of threshold range
// If persist = 5, every 6 consecutive value out of threshold range
// If persist = 6, every 7 consecutive value out of threshold range
// If persist = 7, every 8 consecutive value out of threshold range
// If persist = 8, every 9 consecutive value out of threshold range
// If persist = 9, every 10 consecutive value out of threshold range
// If persist = 10, every 11 consecutive value out of threshold range
// If persist = 11, every 12 consecutive value out of threshold range
// If persist = 12, every 13 consecutive value out of threshold range
// If persist = 13, every 14 consecutive value out of threshold range
// If persist = 14, every 15 consecutive value out of threshold range
// If persist = 15, every 16 consecutive value out of threshold range
// Returns true (1) if successful, false (0) if there was an I2C error
// (Also see getError() below)
boolean getLux(byte gain, byte integrationTime, unsigned int CH0, unsigned int CH1, double &lux);
// Convert raw data to lux
// gain: 0 (1X) or 7 (96X), see getControl()
// integrationTime: integration time in ms, from getMeasurementRate()
// CH0, CH1: results from getData()
// lux will be set to resulting lux calculation
// returns true (1) if calculation was successful
// returns false (0) AND lux = 0.0 IF EITHER SENSOR WAS SATURATED (0XFFFF)
byte getError(void);
// If any library command fails, you can retrieve an extended
// error code using this command. Errors are from the wire library:
// 0 = Success
// 1 = Data too long to fit in transmit buffer
// 2 = Received NACK on transmit of address
// 3 = Received NACK on transmit of data
// 4 = Other error
private:
boolean readByte(byte address, byte &value);
// Reads a byte from a LTR303 address
// Address: LTR303 address (0 to 15)
// Value will be set to stored byte
// Returns true (1) if successful, false (0) if there was an I2C error
// (Also see getError() above)
boolean writeByte(byte address, byte value);
// Write a byte to a LTR303 address
// Address: LTR303 address (0 to 15)
// Value: byte to write to address
// Returns true (1) if successful, false (0) if there was an I2C error
// (Also see getError() above)
boolean readUInt(byte address, unsigned int &value);
// Reads an unsigned integer (16 bits) from a LTR303 address (low byte first)
// Address: LTR303 address (0 to 15), low byte first
// Value will be set to stored unsigned integer
// Returns true (1) if successful, false (0) if there was an I2C error
// (Also see getError() above)
boolean writeUInt(byte address, unsigned int value);
// Write an unsigned integer (16 bits) to a LTR303 address (low byte first)
// Address: LTR303 address (0 to 15), low byte first
// Value: unsigned int to write to address
// Returns true (1) if successful, false (0) if there was an I2C error
// (Also see getError() above)
byte _i2c_address;
byte _error;
};
#endif