forked from tedyapo/arduino-VEML7700
-
Notifications
You must be signed in to change notification settings - Fork 1
/
VEML7700.cpp
470 lines (428 loc) · 10.4 KB
/
VEML7700.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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
// VEML7700.cpp: VEML7700 Ambient light sensor arduino driver
//
// Copyright 2016 Theodore C. Yapo
//
// released under MIT License (see file)
#include <VEML7700.h>
VEML7700::
VEML7700()
{
}
void
VEML7700::
begin()
{
Wire.begin();
// write initial state to VEML7700
register_cache[0] = ( (uint16_t(ALS_GAIN_x2) << ALS_SM_SHIFT) |
(uint16_t(ALS_INTEGRATION_100ms) << ALS_IT_SHIFT) |
(uint16_t(ALS_PERSISTENCE_1) << ALS_PERS_SHIFT) |
(uint16_t(0) << ALS_INT_EN_SHIFT) |
(uint16_t(0) << ALS_SD_SHIFT) );
register_cache[1] = 0x0000;
register_cache[2] = 0xffff;
register_cache[3] = ( (uint16_t(ALS_POWER_MODE_3) << PSM_SHIFT) |
(uint16_t(0) << PSM_EN_SHIFT) );
for (uint8_t i=0; i<4; i++){
sendData(i, register_cache[i]);
}
// wait at least 2.5ms as per datasheet
delay(3);
}
uint8_t
VEML7700::
sendData(uint8_t command, uint16_t data)
{
Wire.beginTransmission(I2C_ADDRESS);
if (Wire.write(command) != 1){
return STATUS_ERROR;
}
if (Wire.write(uint8_t(data & 0xff)) != 1){
return STATUS_ERROR;
}
if (Wire.write(uint8_t(data >> 8)) != 1){
return STATUS_ERROR;
}
if (Wire.endTransmission()){
return STATUS_ERROR;
}
return STATUS_OK;
}
uint8_t
VEML7700::
receiveData(uint8_t command, uint16_t& data)
{
Wire.beginTransmission(I2C_ADDRESS);
if (Wire.write(command) != 1){
return STATUS_ERROR;
}
if (Wire.endTransmission(false)){ // NB: don't send stop here
return STATUS_ERROR;
}
if (Wire.requestFrom(uint8_t(I2C_ADDRESS), uint8_t(2)) != 2){
return STATUS_ERROR;
}
data = Wire.read();
data |= uint16_t(Wire.read()) << 8;
return STATUS_OK;
}
uint8_t
VEML7700::
setGain(als_gain_t gain)
{
uint16_t reg = ( (register_cache[COMMAND_ALS_SM] & ~ALS_SM_MASK) |
((uint16_t(gain) << ALS_SM_SHIFT) & ALS_SM_MASK) );
register_cache[COMMAND_ALS_SM] = reg;
return sendData(COMMAND_ALS_SM, reg);
}
uint8_t
VEML7700::
getGain(als_gain_t& gain)
{
gain = als_gain_t(
(register_cache[COMMAND_ALS_SM] & ALS_SM_MASK) >> ALS_SM_SHIFT );
return STATUS_OK;
}
uint8_t
VEML7700::
setIntegrationTime(als_itime_t itime)
{
uint16_t reg = ( (register_cache[COMMAND_ALS_IT] & ~ALS_IT_MASK) |
((uint16_t(itime) << ALS_IT_SHIFT) & ALS_IT_MASK) );
register_cache[COMMAND_ALS_IT] = reg;
return sendData(COMMAND_ALS_IT, reg);
}
uint8_t
VEML7700::
getIntegrationTime(als_itime_t& itime)
{
itime = als_itime_t(
(register_cache[COMMAND_ALS_IT] & ALS_IT_MASK) >> ALS_IT_SHIFT );
return STATUS_OK;
}
uint8_t
VEML7700::
setPersistence(als_persist_t persist)
{
uint16_t reg = ( (register_cache[COMMAND_ALS_PERS] & ~ALS_PERS_MASK) |
((uint16_t(persist) << ALS_PERS_SHIFT) & ALS_PERS_MASK) );
register_cache[COMMAND_ALS_PERS] = reg;
return sendData(COMMAND_ALS_PERS, reg);
}
uint8_t
VEML7700::
setPowerSavingMode(als_powmode_t powmode)
{
uint16_t reg = ( (register_cache[COMMAND_PSM] & ~PSM_MASK) |
((uint16_t(powmode) << PSM_SHIFT) & PSM_MASK) );
register_cache[COMMAND_PSM] = reg;
return sendData(COMMAND_PSM, reg);
}
uint8_t
VEML7700::
setPowerSaving(uint8_t enabled)
{
uint16_t reg = ( (register_cache[COMMAND_PSM_EN] & ~PSM_EN_MASK) |
((uint16_t(enabled) << PSM_EN_SHIFT) & PSM_EN_MASK) );
register_cache[COMMAND_PSM_EN] = reg;
return sendData(COMMAND_PSM_EN, reg);
}
uint8_t
VEML7700::
setInterrupts(uint8_t enabled)
{
uint16_t reg = ( (register_cache[COMMAND_ALS_INT_EN] & ~ALS_INT_EN_MASK) |
((uint16_t(enabled) << ALS_INT_EN_SHIFT) &
ALS_INT_EN_MASK) );
register_cache[COMMAND_ALS_INT_EN] = reg;
return sendData(COMMAND_ALS_INT_EN, reg);
}
uint8_t
VEML7700::
setPower(uint8_t on)
{
uint16_t reg = ( (register_cache[COMMAND_ALS_SD] & ~ALS_SD_MASK) |
((uint16_t(~on) << ALS_SD_SHIFT) & ALS_SD_MASK) );
register_cache[COMMAND_ALS_SD] = reg;
uint8_t status = sendData(COMMAND_ALS_SD, reg);
if (on) {
delay(3); // minimu 2.5us delay per datasheet
}
return status;
}
uint8_t
VEML7700::
setALSHighThreshold(uint16_t thresh)
{
return sendData(COMMAND_ALS_WH, thresh);
}
uint8_t
VEML7700::
setALSLowThreshold(uint16_t thresh)
{
return sendData(COMMAND_ALS_WL, thresh);
}
uint8_t
VEML7700::
getALS(uint16_t& als)
{
return receiveData(COMMAND_ALS, als);
}
uint8_t
VEML7700::
getWhite(uint16_t& white)
{
return receiveData(COMMAND_WHITE, white);
}
uint8_t
VEML7700::
getHighThresholdEvent(uint8_t& event)
{
uint16_t reg;
uint8_t status = receiveData(COMMAND_ALS_IF_H, reg);
event = (reg & ALS_IF_H_MASK) >> ALS_IF_H_SHIFT;
return status;
}
uint8_t
VEML7700::
getLowThresholdEvent(uint8_t& event)
{
uint16_t reg;
uint8_t status = receiveData(COMMAND_ALS_IF_L, reg);
event = (reg & ALS_IF_L_MASK) >> ALS_IF_L_SHIFT;
return status;
}
void
VEML7700::
scaleLux(uint16_t raw_counts, float& lux)
{
als_gain_t gain;
als_itime_t itime;
getGain(gain);
getIntegrationTime(itime);
float factor1, factor2;
switch(gain & 0x3){
case ALS_GAIN_x1:
factor1 = 1.f;
break;
case ALS_GAIN_x2:
factor1 = 0.5f;
break;
case ALS_GAIN_d8:
factor1 = 8.f;
break;
case ALS_GAIN_d4:
factor1 = 4.f;
break;
default:
factor1 = 1.f;
break;
}
switch(itime){
case ALS_INTEGRATION_25ms:
factor2 = 0.2304f;
break;
case ALS_INTEGRATION_50ms:
factor2 = 0.1152f;
break;
case ALS_INTEGRATION_100ms:
factor2 = 0.0576f;
break;
case ALS_INTEGRATION_200ms:
factor2 = 0.0288f;
break;
case ALS_INTEGRATION_400ms:
factor2 = 0.0144f;
break;
case ALS_INTEGRATION_800ms:
factor2 = 0.0072f;
break;
default:
factor2 = 0.2304f;
break;
}
lux = raw_counts * factor1 * factor2;
// apply correction from App. Note for all readings
// using Horner's method
lux = lux * (1.0023f + lux * (8.1488e-5f + lux * (-9.3924e-9f +
lux * 6.0135e-13f)));
}
uint8_t
VEML7700::
getALSLux(float& lux)
{
uint16_t raw_counts;
uint8_t status = getALS(raw_counts);
scaleLux(raw_counts, lux);
return status;
}
uint8_t
VEML7700::
getWhiteLux(float& lux)
{
uint16_t raw_counts;
uint8_t status = getWhite(raw_counts);
scaleLux(raw_counts, lux);
return status;
}
uint8_t
VEML7700::
getAutoXLux(float& lux,
VEML7700::getCountsFunction counts_func,
VEML7700::als_gain_t& auto_gain,
VEML7700::als_itime_t& auto_itime,
uint16_t& raw_counts)
{
als_gain_t gains[4] = { ALS_GAIN_d8,
ALS_GAIN_d4,
ALS_GAIN_x1,
ALS_GAIN_x2 };
als_itime_t itimes[6] = {ALS_INTEGRATION_25ms,
ALS_INTEGRATION_50ms,
ALS_INTEGRATION_100ms,
ALS_INTEGRATION_200ms,
ALS_INTEGRATION_400ms,
ALS_INTEGRATION_800ms };
uint16_t counts_threshold = 200;
int8_t itime_idx;
uint8_t gain_idx;
if (setPower(0)){
return STATUS_ERROR;
}
for (itime_idx = 2; itime_idx < 6; itime_idx++){
if (setIntegrationTime(itimes[itime_idx])){
return STATUS_ERROR;
}
for (gain_idx = 0; gain_idx < 4; gain_idx++){
if (setGain(gains[gain_idx])){
return STATUS_ERROR;
}
if (setPower(1)){
return STATUS_ERROR;
}
sampleDelay();
if ((this->*counts_func)(raw_counts)){
return STATUS_ERROR;
}
if (raw_counts > counts_threshold){
do {
if (raw_counts < 10000){
scaleLux(raw_counts, lux);
auto_gain = gains[gain_idx];
auto_itime = itimes[itime_idx];
return STATUS_OK;
}
if(setPower(0)){
return STATUS_ERROR;
}
itime_idx--;
if (setIntegrationTime(itimes[itime_idx])){
return STATUS_ERROR;
}
if (setPower(1)){
return STATUS_ERROR;
}
sampleDelay();
if ((this->*counts_func)(raw_counts)){
return STATUS_ERROR;
}
} while (itime_idx > 0);
scaleLux(raw_counts, lux);
auto_gain = gains[gain_idx];
auto_itime = itimes[itime_idx];
return STATUS_OK;
}
if(setPower(0)){
return STATUS_ERROR;
}
}
}
scaleLux(raw_counts, lux);
auto_gain = gains[gain_idx];
auto_itime = itimes[itime_idx];
return STATUS_OK;
}
uint8_t
VEML7700::
getAutoALSLux(float& lux)
{
VEML7700::als_gain_t auto_gain;
VEML7700::als_itime_t auto_itime;
uint16_t raw_counts;
return getAutoXLux(lux,
&VEML7700::getALS,
auto_gain,
auto_itime,
raw_counts);
}
uint8_t
VEML7700::
getAutoWhiteLux(float& lux)
{
VEML7700::als_gain_t auto_gain;
VEML7700::als_itime_t auto_itime;
uint16_t raw_counts;
return getAutoXLux(lux,
&VEML7700::getWhite,
auto_gain,
auto_itime,
raw_counts);
}
uint8_t
VEML7700::
getAutoALSLux(float& lux,
VEML7700::als_gain_t& auto_gain,
VEML7700::als_itime_t& auto_itime,
uint16_t& raw_counts)
{
return getAutoXLux(lux,
&VEML7700::getALS,
auto_gain,
auto_itime,
raw_counts);
}
uint8_t
VEML7700::
getAutoWhiteLux(float& lux,
VEML7700::als_gain_t& auto_gain,
VEML7700::als_itime_t& auto_itime,
uint16_t& raw_counts)
{
return getAutoXLux(lux,
&VEML7700::getWhite,
auto_gain,
auto_itime,
raw_counts);
}
uint8_t
VEML7700::
sampleDelay()
{
als_itime_t itime;
getIntegrationTime(itime);
// extend nominal delay to ensure new sample is generated
#define extended_delay(ms) delay(2*(ms))
switch(itime){
case ALS_INTEGRATION_25ms:
extended_delay(25);
break;
case ALS_INTEGRATION_50ms:
extended_delay(50);
break;
case ALS_INTEGRATION_100ms:
extended_delay(100);
break;
case ALS_INTEGRATION_200ms:
extended_delay(200);
break;
case ALS_INTEGRATION_400ms:
extended_delay(400);
break;
case ALS_INTEGRATION_800ms:
extended_delay(800);
break;
default:
extended_delay(100);
break;
}
}