-
Notifications
You must be signed in to change notification settings - Fork 4
/
DFRobot_VEML7700.h
437 lines (395 loc) · 13.2 KB
/
DFRobot_VEML7700.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
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
/*!
* @file DFRobot_VEML7700.h
* @brief DFRobot's Light Sensor
* @n High Accuracy Ambient Light Sensor
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
* @license The MIT License (MIT)
* @author [yangyang]([email protected])
* @version V1.0
* @date 2016-12-08
* @url https://github.com/DFRobot/DFRobot_VEML7700
*/
#ifndef __DFROBOT_VEML7700_H__
#define __DFROBOT_VEML7700_H__
#include <Arduino.h>
#include <Wire.h>
// #define ENABLE_DBG //!< Open this macro and you can see the details of the program
#ifdef ENABLE_DBG
#define DBG(...) {Serial.print("[");Serial.print(__FUNCTION__); Serial.print("(): "); Serial.print(__LINE__); Serial.print(" ] "); Serial.println(__VA_ARGS__);}
#else
#define DBG(...)
#endif
class DFRobot_VEML7700
{
public:
/**
* @enum eAlsGain_t
* @brief Gain selection
*/
typedef enum
{
ALS_GAIN_x1 = 0x0, /**< x 1 */
ALS_GAIN_x2 = 0x1, /**< x 2 */
ALS_GAIN_d8 = 0x2, /**< x 1/8 */
ALS_GAIN_d4 = 0x3, /**< x 1/4 */
}eAlsGain_t;
/**
* @enum eAlsItime_t
* @brief ALS integration time setting
*/
typedef enum
{
ALS_INTEGRATION_25ms = 0xc,
ALS_INTEGRATION_50ms = 0x8,
ALS_INTEGRATION_100ms = 0x0,
ALS_INTEGRATION_200ms = 0x1,
ALS_INTEGRATION_400ms = 0x2,
ALS_INTEGRATION_800ms = 0x3,
}eAlsItime_t;
/**
* @enum eAlsPersist_t
* @brief ALS persistence protect number setting
*/
typedef enum
{
ALS_PERSISTENCE_1 = 0x0,
ALS_PERSISTENCE_2 = 0x1,
ALS_PERSISTENCE_4 = 0x2,
ALS_PERSISTENCE_8 = 0x3,
}eAlsPersist_t;
/**
* @enum eAlsPowerMode_t
* @brief Power saving mode
*/
typedef enum
{
ALS_POWER_MODE_1 = 0x0,
ALS_POWER_MODE_2 = 0x1,
ALS_POWER_MODE_3 = 0x2,
ALS_POWER_MODE_4 = 0x3,
}eAlsPowerMode_t;
/**
* @enum eAlsSTATUS_t
* @brief ALS status
*/
typedef enum
{
STATUS_OK = 0,
STATUS_ERROR = 0xff,
}eAlsSTATUS_t;
/**
* @fn DFRobot_VEML7700
* @brief Constructor
* @return None
*/
DFRobot_VEML7700();
/**
* @fn setGain
* @brief Set ALS gain coefficient
* @param gain Gain selection:
* @n ALS_GAIN_x1, ALS_GAIN_x2, ALS_GAIN_d8, ALS_GAIN_d4
* @return Return operation result
* @retval STATUS_OK Operation succeed, no anomaly
* @retval STATUS_ERROR Operation failed
*/
uint8_t setGain(eAlsGain_t gain);
/**
* @fn getGain
* @brief Get the current ALS gain coefficient
* @param gain The current ALS gain coefficient:
* @n ALS_GAIN_x1, ALS_GAIN_x2, ALS_GAIN_d8, ALS_GAIN_d4
* @return Return operation result
* @retval STATUS_OK Operation succeed, no anomaly
* @retval STATUS_ERROR Operation failed
*/
uint8_t getGain(eAlsGain_t& gain);
/**
* @fn setIntegrationTime
* @brief Set ALS integration time
* @param itime ALS integration time:
* @n ALS_INTEGRATION_25ms, ALS_INTEGRATION_50ms, ALS_INTEGRATION_100ms,
* @n ALS_INTEGRATION_200ms, ALS_INTEGRATION_400ms, ALS_INTEGRATION_800ms
* @return Return operation result
* @retval STATUS_OK Operation succeed, no anomaly
* @retval STATUS_ERROR Operation failed
*/
uint8_t setIntegrationTime(eAlsItime_t itime);
/**
* @fn getIntegrationTime
* @brief Get ALS integration time
* @param itime The current ALS integration time:
* @n ALS_INTEGRATION_25ms, ALS_INTEGRATION_50ms, ALS_INTEGRATION_100ms,
* @n ALS_INTEGRATION_200ms, ALS_INTEGRATION_400ms, ALS_INTEGRATION_800ms
* @return Return operation result
* @retval STATUS_OK Operation succeed, no anomaly
* @retval STATUS_ERROR Operation failed
*/
uint8_t getIntegrationTime(eAlsItime_t& itime);
/**
* @fn setPersistence
* @brief persistence protect number setting
* @param persist persistence protect number:
* @n ALS_PERSISTENCE_1, ALS_PERSISTENCE_2, ALS_PERSISTENCE_4, ALS_PERSISTENCE_8
* @return Return operation result
* @retval STATUS_OK Operation succeed, no anomaly
* @retval STATUS_ERROR Operation failed
*/
uint8_t setPersistence(eAlsPersist_t persist);
/**
* @fn setPowerSavingMode
* @brief Power saving mode setting
* @param powerMode Power saving mode:
* @n ALS_POWER_MODE_1, ALS_POWER_MODE_2, ALS_POWER_MODE_3, ALS_POWER_MODE_4
* @return Return operation result
* @retval STATUS_OK Operation succeed, no anomaly
* @retval STATUS_ERROR Operation failed
*/
uint8_t setPowerSavingMode(eAlsPowerMode_t powerMode);
/**
* @fn setPowerSaving
* @brief Power saving mode enable
* @param enabled :
* @n 0 Disable
* @n 1 Enable
* @return Return operation result
* @retval STATUS_OK Operation succeed, no anomaly
* @retval STATUS_ERROR Operation failed
*/
uint8_t setPowerSaving(uint8_t enabled);
/**
* @fn setInterrupts
* @brief ALS interrupt enable setting
* @param enabled :
* @n 0 Disable
* @n 1 Enable
* @return Return operation result
* @retval STATUS_OK Operation succeed, no anomaly
* @retval STATUS_ERROR Operation failed
*/
uint8_t setInterrupts(uint8_t enabled);
/**
* @fn setPower
* @brief ALS shut down setting
* @param on :
* @n 0 power on
* @n 1 shut down
* @return Return operation result
* @retval STATUS_OK Operation succeed, no anomaly
* @retval STATUS_ERROR Operation failed
*/
uint8_t setPower(uint8_t on);
/**
* @fn setALSHighThreshold
* @brief Set high threshold of interrupt
* @param thresh 16-bit high threshold set value
* @return Return operation result
* @retval STATUS_OK Operation succeed, no anomaly
* @retval STATUS_ERROR Operation failed
*/
uint8_t setALSHighThreshold(uint32_t thresh);
/**
* @fn setALSLowThreshold
* @brief Set low threshold of interrupt
* @param thresh 16-bit low threshold set value
* @return Return operation result
* @retval STATUS_OK Operation succeed, no anomaly
* @retval STATUS_ERROR Operation failed
*/
uint8_t setALSLowThreshold(uint32_t thresh);
/**
* @fn getALS
* @brief get high resolution ALS output
* @param als Buffer the sensor data, 16-bit data
* @return Return operation result
* @retval STATUS_OK Operation succeed, no anomaly
* @retval STATUS_ERROR Operation failed
*/
uint8_t getALS(uint32_t& als);
/**
* @fn getWhite
* @brief get WHITE output
* @param white Buffer the sensor data, 16-bit data
* @return Return operation result
* @retval STATUS_OK Operation succeed, no anomaly
* @retval STATUS_ERROR Operation failed
*/
uint8_t getWhite(uint32_t& white);
/**
* @fn getHighThresholdEvent
* @brief Get high threshold trigger interrupt event
* @param event :
* @n 0 Not detect high threshold interrupt event
* @n 1 Detected high threshold interrupt event
* @return Return operation result
* @retval STATUS_OK Operation succeed, no anomaly
* @retval STATUS_ERROR Operation failed
*/
uint8_t getHighThresholdEvent(uint8_t& event);
/**
* @fn getLowThresholdEvent
* @brief Get low threshold trigger interrupt event
* @param event :
* @n 0 Not detect low threshold interrupt event
* @n 1 Detected low threshold interrupt event
* @return Return operation result
* @retval STATUS_OK Operation succeed, no anomaly
* @retval STATUS_ERROR Operation failed
*/
uint8_t getLowThresholdEvent(uint8_t& event);
/**
* @fn begin
* @brief Init function
* @return None
*/
void begin();
/**
* @fn begin
* @brief Init function
* @param als_gain Set ALS gain coefficient
* @return None
*/
void begin(uint8_t als_gain);
/**
* @fn getALSLux
* @brief Get measured ALS illumination intensity value
* @param lux Buffer the sensor data, 16-bit data
* @return Return operation result
* @retval STATUS_OK Operation succeed, no anomaly
* @retval STATUS_ERROR Operation failed
*/
uint8_t getALSLux(float& lux);
/**
* @fn getWhiteLux
* @brief Get measured White illumination intensity value
* @param lux Buffer the sensor data, 16-bit data
* @return Return operation result
* @retval STATUS_OK Operation succeed, no anomaly
* @retval STATUS_ERROR Operation failed
*/
uint8_t getWhiteLux(float& lux);
/**
* @fn getAutoALSLux(float& lux)
* @brief Get measured AutoALS illumination intensity value
* @param lux Buffer the sensor data, 16-bit data
* @return Return operation result
* @retval STATUS_OK Operation succeed, no anomaly
* @retval STATUS_ERROR Operation failed
*/
uint8_t getAutoALSLux(float& lux);
/**
* @fn getAutoWhiteLux(float& lux)
* @brief Get measured AutoWhite illumination intensity value
* @param lux Buffer the sensor data, 16-bit data
* @return Return operation result
* @retval STATUS_OK Operation succeed, no anomaly
* @retval STATUS_ERROR Operation failed
*/
uint8_t getAutoWhiteLux(float& lux);
/**
* @fn getAutoALSLux(float& lux,
* @n DFRobot_VEML7700::eAlsGain_t& auto_gain,
* @n DFRobot_VEML7700::eAlsItime_t& auto_itime,
* @n uint32_t& raw_counts)
* @brief Get measured AutoALS illumination intensity value
* @param lux Buffer the sensor data, 16-bit data
* @param auto_gain Gain selection
* @param auto_itime ALS integration time setting
* @param raw_counts Raw counts
* @return Return operation result
* @retval STATUS_OK Operation succeed, no anomaly
* @retval STATUS_ERROR Operation failed
*/
uint8_t getAutoALSLux(float& lux,
DFRobot_VEML7700::eAlsGain_t& auto_gain,
DFRobot_VEML7700::eAlsItime_t& auto_itime,
uint32_t& raw_counts);
/**
* @fn getAutoWhiteLux(float& lux,
* @n DFRobot_VEML7700::eAlsGain_t& auto_gain,
* @n DFRobot_VEML7700::eAlsItime_t& auto_itime,
* @n uint32_t& raw_counts)
* @brief Get measured AutoWhite illumination intensity value
* @param lux Buffer the sensor data, 16-bit data
* @param auto_gain Gain selection
* @param auto_itime ALS integration time setting
* @param raw_counts Raw counts
* @return Return operation result
* @retval STATUS_OK Operation succeed, no anomaly
* @retval STATUS_ERROR Operation failed
*/
uint8_t getAutoWhiteLux(float& lux,
DFRobot_VEML7700::eAlsGain_t& auto_gain,
DFRobot_VEML7700::eAlsItime_t& auto_itime,
uint32_t& raw_counts);
/**
* @fn sampleDelay
* @brief Data sampling and process delay
*/
void sampleDelay(void);
private:
typedef uint8_t (DFRobot_VEML7700::*getCountsFunction)(uint32_t& counts);
enum { I2C_ADDRESS = 0x10 };
enum { COMMAND_ALS_SM = 0x00, ALS_SM_MASK = 0x1800, ALS_SM_SHIFT = 11 };
enum { COMMAND_ALS_IT = 0x00, ALS_IT_MASK = 0x03c0, ALS_IT_SHIFT = 6 };
enum { COMMAND_ALS_PERS = 0x00, ALS_PERS_MASK = 0x0030, ALS_PERS_SHIFT = 4 };
enum { COMMAND_ALS_INT_EN = 0x00, ALS_INT_EN_MASK = 0x0002,
ALS_INT_EN_SHIFT = 1 };
enum { COMMAND_ALS_SD = 0x00, ALS_SD_MASK = 0x0001, ALS_SD_SHIFT = 0 };
enum { COMMAND_ALS_WH = 0x01 };
enum { COMMAND_ALS_WL = 0x02 };
enum { COMMAND_PSM = 0x03, PSM_MASK = 0x0006, PSM_SHIFT = 1 };
enum { COMMAND_PSM_EN = 0x03, PSM_EN_MASK = 0x0001, PSM_EN_SHIFT = 0 };
enum { COMMAND_ALS = 0x04 };
enum { COMMAND_WHITE = 0x05 };
enum { COMMAND_ALS_IF_L = 0x06, ALS_IF_L_MASK = 0x8000, ALS_IF_L_SHIFT = 15 };
enum { COMMAND_ALS_IF_H = 0x06, ALS_IF_H_MASK = 0x4000, ALS_IF_H_SHIFT = 14 };
uint32_t register_cache[4]; // Register cache
/**
* @fn sendData
* @brief Send data to the sensor
* @param command Command to be sent
* @param data Data to be sent
* @return Return operation result
* @retval STATUS_OK Operation succeed, no anomaly
* @retval STATUS_ERROR Operation failed
*/
uint8_t sendData(uint8_t command, uint32_t data = 0);
/**
* @fn receiveData
* @brief Receive sensor data
* @param command Command to be sent
* @param data Buffer the sensor data, 16-bit data
* @return Return operation result
* @retval STATUS_OK Operation succeed, no anomaly
* @retval STATUS_ERROR Operation failed
*/
uint8_t receiveData(uint8_t command, uint32_t& data);
/**
* @fn scaleLux
* @brief Process data based on the current set parameters
* @param raw_counts Raw counts
* @param lux Buffer the sensor data, 16-bit data
* @return Return operation result
* @retval STATUS_OK Operation succeed, no anomaly
* @retval STATUS_ERROR Operation failed
*/
void scaleLux(uint32_t raw_counts, float& lux);
/**
* @fn getAutoXLux
* @brief Process original data according to the parameters
* @param lux Buffer the sensor data, 16-bit data
* @param counts_func Callback function
* @param auto_gain Gain selection
* @param auto_itime ALS integration time setting
* @param raw_counts Raw counts
* @return Return operation result
* @retval STATUS_OK Operation succeed, no anomaly
* @retval STATUS_ERROR Operation failed
*/
uint8_t getAutoXLux(float& lux,
DFRobot_VEML7700::getCountsFunction counts_func,
DFRobot_VEML7700::eAlsGain_t& auto_gain,
DFRobot_VEML7700::eAlsItime_t& auto_itime,
uint32_t& raw_counts);
};
#endif