|
1 /** |
|
2 * @file APDS-9930.h |
|
3 * @brief Library for the SparkFun APDS-9930 breakout board |
|
4 * @author Shawn Hymel (SparkFun Electronics) |
|
5 * |
|
6 * @copyright This code is public domain but you buy me a beer if you use |
|
7 * this and we meet someday (Beerware license). |
|
8 * |
|
9 * This library interfaces the Avago APDS-9930 to ESP-IDF over I2C. |
|
10 * Ported from Arduino library by Davide Depau. |
|
11 * |
|
12 * MIT Licensed as described in the file LICENSE |
|
13 */ |
|
14 |
|
15 #ifndef __APDS9930_H__ |
|
16 #define __APDS9930_H__ |
|
17 |
|
18 #include <stdint.h> |
|
19 #include <stdbool.h> |
|
20 #include <esp_err.h> |
|
21 #include <i2cdev.h> |
|
22 |
|
23 #ifdef __cplusplus |
|
24 extern "C" { |
|
25 #endif |
|
26 |
|
27 #define APDS9930_ID_1 0x12 |
|
28 #define APDS9930_ID_2 0x39 |
|
29 #define APDS9930_ID_3 0x30 // My chip gives this |
|
30 |
|
31 |
|
32 /* LED Drive values */ |
|
33 #define APDS9930_LED_DRIVE_100MA 0 |
|
34 #define APDS9930_LED_DRIVE_50MA 1 |
|
35 #define APDS9930_LED_DRIVE_25MA 2 |
|
36 #define APDS9930_LED_DRIVE_12_5MA 3 |
|
37 |
|
38 /* Proximity Gain (PGAIN) values */ |
|
39 #define APDS9930_PGAIN_1X 0 |
|
40 #define APDS9930_PGAIN_2X 1 |
|
41 #define APDS9930_PGAIN_4X 2 |
|
42 #define APDS9930_PGAIN_8X 3 |
|
43 |
|
44 /* ALS Gain (AGAIN) values */ |
|
45 #define APDS9930_AGAIN_1X 0 |
|
46 #define APDS9930_AGAIN_8X 1 |
|
47 #define APDS9930_AGAIN_16X 2 |
|
48 #define APDS9930_AGAIN_120X 3 |
|
49 |
|
50 |
|
51 |
|
52 /** |
|
53 * @brief Initialize device descriptor |
|
54 * |
|
55 * Default SCL frequency is 100kHz |
|
56 * |
|
57 * @param dev Pointer to I2C device descriptor |
|
58 * @param port I2C port number |
|
59 * @param addr I2C address (0x39 for APDS9930) |
|
60 * @param sda_gpio SDA GPIO |
|
61 * @param scl_gpio SCL GPIO |
|
62 * @return `ESP_OK` on success |
|
63 */ |
|
64 esp_err_t apds9930_init_desc(i2c_dev_t *dev, uint8_t addr, i2c_port_t port, gpio_num_t sda_gpio, gpio_num_t scl_gpio); |
|
65 |
|
66 /** |
|
67 * @brief Free device descriptor |
|
68 * @param dev Pointer to I2C device descriptor |
|
69 * @return `ESP_OK` on success |
|
70 */ |
|
71 esp_err_t apds9930_free_desc(i2c_dev_t *dev); |
|
72 |
|
73 |
|
74 |
|
75 /* APDS9930 Class */ |
|
76 //class APDS9930 { |
|
77 //public: |
|
78 |
|
79 |
|
80 /* Initialization methods */ |
|
81 // APDS9930(); |
|
82 // ~APDS9930(); |
|
83 |
|
84 /** |
|
85 * @brief Setup APDS9930 and initializes registers to defaults |
|
86 * @param dev Pointer to I2C device descriptor |
|
87 * @return `ESP_OK` on success |
|
88 */ |
|
89 esp_err_t apds9930_init(i2c_dev_t *dev); |
|
90 |
|
91 /** |
|
92 * @brief Reads and returns the contents of the ENABLE register |
|
93 * @param dev Pointer to I2C device descriptor |
|
94 * @return Contents of the ENABLE register. 0xFF if error. |
|
95 */ |
|
96 uint8_t apds9930_getMode(i2c_dev_t *dev); |
|
97 |
|
98 /** |
|
99 * @brief Enables or disables a feature in the APDS-9930 |
|
100 * @param dev Pointer to I2C device descriptor |
|
101 * @param mode which feature to enable |
|
102 * @param enable ON (1) or OFF (0) |
|
103 * @return `ESP_OK` on success |
|
104 */ |
|
105 esp_err_t apds9930_setMode(i2c_dev_t *dev, uint8_t mode, uint8_t enable); |
|
106 |
|
107 /** |
|
108 * @brief Turn the APDS-9930 on |
|
109 * @param dev Pointer to I2C device descriptor |
|
110 * @return ESP_OK if operation successful. |
|
111 */ |
|
112 esp_err_t apds9930_enablePower(i2c_dev_t *dev); |
|
113 |
|
114 /** |
|
115 * @brief Turn the APDS-9930 off |
|
116 * @param dev Pointer to I2C device descriptor |
|
117 * @return ESP_OK if operation successful. False otherwise. |
|
118 */ |
|
119 esp_err_t apds9930_disablePower(i2c_dev_t *dev); |
|
120 |
|
121 /** |
|
122 * @brief Starts the light (Ambient/IR) sensor on the APDS-9930 |
|
123 * @param dev Pointer to I2C device descriptor |
|
124 * @param interrupts true to enable hardware interrupt on high or low light |
|
125 * @return ESP_OK if sensor enabled correctly. |
|
126 */ |
|
127 esp_err_t apds9930_enableLightSensor(i2c_dev_t *dev, bool interrupts); |
|
128 |
|
129 /** |
|
130 * @brief Ends the light sensor on the APDS-9930 |
|
131 * @param dev Pointer to I2C device descriptor |
|
132 * @return ESP_OK if sensor disabled correctly. |
|
133 */ |
|
134 esp_err_t apds9930_disableLightSensor(i2c_dev_t *dev); |
|
135 |
|
136 /** |
|
137 * @brief Starts the proximity sensor on the APDS-9930 |
|
138 * @param dev Pointer to I2C device descriptor |
|
139 * @param interrupts true to enable hardware external interrupt on proximity |
|
140 * @return ESP_OK if sensor enabled correctly. |
|
141 */ |
|
142 esp_err_t apds9930_enableProximitySensor(i2c_dev_t *dev, bool interrupts); |
|
143 |
|
144 /** |
|
145 * @brief Ends the proximity sensor on the APDS-9930 |
|
146 * @param dev Pointer to I2C device descriptor |
|
147 * @return ESP_OK if sensor disabled correctly. |
|
148 */ |
|
149 esp_err_t apds9930_disableProximitySensor(i2c_dev_t *dev); |
|
150 |
|
151 |
|
152 /** |
|
153 * @brief Returns LED drive strength for proximity and ALS |
|
154 * @param dev Pointer to I2C device descriptor |
|
155 * |
|
156 * Value LED Current |
|
157 * 0 100 mA |
|
158 * 1 50 mA |
|
159 * 2 25 mA |
|
160 * 3 12.5 mA |
|
161 * |
|
162 * @return the value of the LED drive strength. 0xFF on failure. |
|
163 */ |
|
164 uint8_t APDS9930_getLEDDrive(i2c_dev_t *dev); |
|
165 |
|
166 /** |
|
167 * @brief Sets the LED drive strength for proximity and ALS |
|
168 * @param dev Pointer to I2C device descriptor |
|
169 * |
|
170 * Value LED Current |
|
171 * 0 100 mA |
|
172 * 1 50 mA |
|
173 * 2 25 mA |
|
174 * 3 12.5 mA |
|
175 * |
|
176 * @param drive the value (0-3) for the LED drive strength |
|
177 * @return ESP_OK if operation successful. |
|
178 */ |
|
179 esp_err_t apds9930_setLEDDrive(i2c_dev_t *dev, uint8_t drive); |
|
180 |
|
181 /** |
|
182 * @brief Gets the receiver ALS gain level for the ambient light sensor. |
|
183 * When set the gain is extra scaled down with 0.16. |
|
184 * @param dev Pointer to I2C device descriptor |
|
185 * @return 1 if extra ALS gain level is enabled, 0 if not. 0xFF on error. |
|
186 */ |
|
187 uint8_t apds9930_getAmbientGainLevel(i2c_dev_t *dev); |
|
188 |
|
189 /** |
|
190 * @brief Sets the receiver ALS gain level for the ambient light sensor |
|
191 * When asserted, the 1x and 8x gain modes are scaled by 0.16. |
|
192 * Otherwise AGAIN is scaled by 1. Do not use with AGAIN greater then 8x. |
|
193 * @param dev Pointer to I2C device descriptor |
|
194 * @param enable the value (0-1) for the gain level. |
|
195 * @return ESP_OK if operation successful. |
|
196 */ |
|
197 esp_err_t apds9930_setAmbientGainLevel(i2c_dev_t *dev, uint8_t enable); |
|
198 |
|
199 /** |
|
200 * @brief Returns receiver gain for the ambient light sensor (ALS) |
|
201 * @param dev Pointer to I2C device descriptor |
|
202 * |
|
203 * Value Gain |
|
204 * 0 1x |
|
205 * 1 4x |
|
206 * 2 16x |
|
207 * 3 120x |
|
208 * |
|
209 * @return the value of the ALS gain. 0xFF on failure. |
|
210 */ |
|
211 uint8_t apds9930_getAmbientLightGain(i2c_dev_t *dev); |
|
212 |
|
213 /** |
|
214 * @brief Sets the receiver gain for the ambient light sensor (ALS) |
|
215 * @param dev Pointer to I2C device descriptor |
|
216 * |
|
217 * Value Gain |
|
218 * 0 1x |
|
219 * 1 4x |
|
220 * 2 16x |
|
221 * 3 120x |
|
222 * |
|
223 * @param drive the value (0-3) for the gain |
|
224 * @return ESP_OK if operation successful. False otherwise. |
|
225 */ |
|
226 esp_err_t apds9930_setAmbientLightGain(i2c_dev_t *dev, uint8_t gain); |
|
227 |
|
228 /** |
|
229 * @brief Returns receiver gain for proximity detection |
|
230 * @param dev Pointer to I2C device descriptor |
|
231 * |
|
232 * Value Gain |
|
233 * 0 1x |
|
234 * 1 2x |
|
235 * 2 4x |
|
236 * 3 8x |
|
237 * |
|
238 * @return the value of the proximity gain. 0xFF on failure. |
|
239 */ |
|
240 uint8_t apds9930_getProximityGain(i2c_dev_t *dev); |
|
241 |
|
242 /** |
|
243 * @brief Sets the receiver gain for proximity detection |
|
244 * @param dev Pointer to I2C device descriptor |
|
245 * |
|
246 * Value Gain |
|
247 * 0 1x |
|
248 * 1 2x |
|
249 * 2 4x |
|
250 * 3 8x |
|
251 * |
|
252 * @param drive the value (0-3) for the gain |
|
253 * @return True if operation successful. False otherwise. |
|
254 */ |
|
255 esp_err_t apds9930_setProximityGain(i2c_dev_t *dev, uint8_t gain); |
|
256 |
|
257 /** |
|
258 * @brief Selects the proximity diode |
|
259 * @param dev Pointer to I2C device descriptor |
|
260 * |
|
261 * Value Diode selection |
|
262 * 0 Reserved |
|
263 * 1 Reserved |
|
264 * 2 Use Ch1 diode |
|
265 * 3 Reserved |
|
266 * |
|
267 * @param[in] drive the value (0-3) for the diode |
|
268 * @return True if operation successful. False otherwise. |
|
269 */ |
|
270 esp_err_t apds9930_setProximityDiode(i2c_dev_t *dev, uint8_t drive); |
|
271 |
|
272 /** |
|
273 * @brief Returns the proximity diode |
|
274 * @param dev Pointer to I2C device descriptor |
|
275 * |
|
276 * Value Diode selection |
|
277 * 0 Reserved |
|
278 * 1 Reserved |
|
279 * 2 Use Ch1 diode |
|
280 * 3 Reserved |
|
281 * |
|
282 * @return the selected diode. 0xFF on failure. |
|
283 */ |
|
284 uint8_t apds9930_getProximityDiode(i2c_dev_t *dev); |
|
285 |
|
286 /** |
|
287 * @brief Gets the low threshold for ambient light interrupts |
|
288 * @param dev Pointer to I2C device descriptor |
|
289 * @param threshold current low threshold stored on the APDS-9930 |
|
290 * @return ESP_OK if operation successful. |
|
291 */ |
|
292 esp_err_t apds9930_getLightIntLowThreshold(i2c_dev_t *dev, uint16_t *threshold); |
|
293 |
|
294 /** |
|
295 * @brief Sets the low threshold for ambient light interrupts |
|
296 * @param dev Pointer to I2C device descriptor |
|
297 * @param threshold low threshold value for interrupt to trigger |
|
298 * @return ESP_OK if operation successful. |
|
299 */ |
|
300 esp_err_t apds9930_setLightIntLowThreshold(i2c_dev_t *dev, uint16_t threshold); |
|
301 |
|
302 /** |
|
303 * @brief Gets the high threshold for ambient light interrupts |
|
304 * @param dev Pointer to I2C device descriptor |
|
305 * @param threshold current low threshold stored on the APDS-9930 |
|
306 * @return ESP_OK if operation successful. |
|
307 */ |
|
308 esp_err_t apds9930_getLightIntHighThreshold(i2c_dev_t *dev, uint16_t *threshold); |
|
309 |
|
310 /** |
|
311 * @brief Sets the high threshold for ambient light interrupts |
|
312 * @param dev Pointer to I2C device descriptor |
|
313 * @param threshold low threshold value for interrupt to trigger |
|
314 * @return ESP_OK if operation successful. |
|
315 */ |
|
316 esp_err_t apds9930_setLightIntHighThreshold(i2c_dev_t *dev, uint16_t threshold); |
|
317 |
|
318 /** |
|
319 * @brief Gets if ambient light interrupts are enabled or not |
|
320 * @param dev Pointer to I2C device descriptor |
|
321 * @return 1 if interrupts are enabled, 0 if not. 0xFF on error. |
|
322 */ |
|
323 uint8_t apds9930_getAmbientLightIntEnable(i2c_dev_t *dev); |
|
324 |
|
325 /** |
|
326 * @brief Turns ambient light interrupts on or off |
|
327 * @param dev Pointer to I2C device descriptor |
|
328 * @param[in] enable 1 to enable interrupts, 0 to turn them off |
|
329 * @return ESP_OK if operation successful. |
|
330 */ |
|
331 esp_err_t apds9930_setAmbientLightIntEnable(i2c_dev_t *dev, uint8_t enable); |
|
332 |
|
333 /** |
|
334 * @brief Gets if proximity interrupts are enabled or not |
|
335 * @param dev Pointer to I2C device descriptor |
|
336 * @return 1 if interrupts are enabled, 0 if not. 0xFF on error. |
|
337 */ |
|
338 uint8_t apds9930_getProximityIntEnable(i2c_dev_t *dev); |
|
339 |
|
340 /** |
|
341 * @brief Turns proximity interrupts on or off |
|
342 * @param dev Pointer to I2C device descriptor |
|
343 * @param[in] enable 1 to enable interrupts, 0 to turn them off |
|
344 * @return ESP_OK if operation successful. |
|
345 */ |
|
346 esp_err_t apds9930_setProximityIntEnable(i2c_dev_t *dev, uint8_t enable); |
|
347 |
|
348 /** |
|
349 * @brief Clears the ambient light interrupt |
|
350 * @param dev Pointer to I2C device descriptor |
|
351 * @return ESP_OK if operation completed successfully. |
|
352 */ |
|
353 esp_err_t apds9930_clearAmbientLightInt(i2c_dev_t *dev); |
|
354 |
|
355 /** |
|
356 * @brief Clears the proximity interrupt |
|
357 * @param dev Pointer to I2C device descriptor |
|
358 * @return ESP_OK if operation completed successfully. |
|
359 */ |
|
360 esp_err_t apds9930_clearProximityInt(i2c_dev_t *dev); |
|
361 |
|
362 /** |
|
363 * @brief Clears all interrupts |
|
364 * @param dev Pointer to I2C device descriptor |
|
365 * @return ESP_OK if operation completed successfully. |
|
366 */ |
|
367 esp_err_t apds9930_clearAllInts(i2c_dev_t *dev); |
|
368 |
|
369 /** |
|
370 * @brief Reads the proximity level as an 16-bit value |
|
371 * @param dev Pointer to I2C device descriptor |
|
372 * @param val value of the proximity sensor. |
|
373 * @return ESP_OK if operation successful. |
|
374 */ |
|
375 esp_err_t apds9930_readProximity(i2c_dev_t *dev, uint16_t *val); |
|
376 |
|
377 /** |
|
378 * @brief Reads the ambient (clear) light level as a float value |
|
379 * @param dev Pointer to I2C device descriptor |
|
380 * @param val value of the light sensor. |
|
381 * @return ESP_OK if operation successful. |
|
382 */ |
|
383 esp_err_t apds9930_readAmbientLightLux(i2c_dev_t *dev, float *val); |
|
384 |
|
385 /** |
|
386 * @brief Reads the ambient (clear) light level as a unsigned long value |
|
387 * @param dev Pointer to I2C device descriptor |
|
388 * @param val value of the light sensor. |
|
389 * @return ESP_OK if operation successful. |
|
390 */ |
|
391 esp_err_t apds9930_readulAmbientLightLux(i2c_dev_t *dev, unsigned long *val); |
|
392 |
|
393 float apds9930_floatAmbientToLux(i2c_dev_t *dev, uint16_t Ch0, uint16_t Ch1); |
|
394 |
|
395 unsigned long apds9930_ulongAmbientToLux(i2c_dev_t *dev, uint16_t Ch0, uint16_t Ch1); |
|
396 |
|
397 /** |
|
398 * @brief Read the channel 0 light value from the APDS-9930 |
|
399 * @param dev Pointer to I2C device descriptor |
|
400 * @param val The value read. |
|
401 * @return ESP_OK if read is correctly. |
|
402 */ |
|
403 esp_err_t apds9930_readCh0Light(i2c_dev_t *dev, uint16_t *val); |
|
404 |
|
405 /** |
|
406 * @brief Read the channel 1 light value from the APDS-9930 |
|
407 * @param dev Pointer to I2C device descriptor |
|
408 * @param val The value read. |
|
409 * @return ESP_OK if read is correctly. |
|
410 */ |
|
411 esp_err_t apds9930_readCh1Light(i2c_dev_t *dev, uint16_t *val); |
|
412 |
|
413 /** |
|
414 * @brief Returns the lower threshold for proximity detection |
|
415 * @param dev Pointer to I2C device descriptor |
|
416 * @return lower threshold |
|
417 */ |
|
418 uint16_t apds9930_getProximityIntLowThreshold(i2c_dev_t *dev); |
|
419 |
|
420 /** |
|
421 * @brief Sets the lower threshold for proximity detection |
|
422 * @param dev Pointer to I2C device descriptor |
|
423 * @param threshold the lower proximity threshold |
|
424 * @return ESP_OK if operation successful. |
|
425 */ |
|
426 esp_err_t apds9930_setProximityIntLowThreshold(i2c_dev_t *dev, uint16_t threshold); |
|
427 |
|
428 /** |
|
429 * @brief Returns the high threshold for proximity detection |
|
430 * @param dev Pointer to I2C device descriptor |
|
431 * @return high threshold |
|
432 */ |
|
433 uint16_t apds9930_getProximityIntHighThreshold(i2c_dev_t *dev); |
|
434 |
|
435 /** |
|
436 * @brief Sets the high threshold for proximity detection |
|
437 * @param dev Pointer to I2C device descriptor |
|
438 * @param threshold the high proximity threshold |
|
439 * @return ESP_OK if operation successful. |
|
440 */ |
|
441 esp_err_t apds9930_setProximityIntHighThreshold(i2c_dev_t *dev, uint16_t threshold); |
|
442 |
|
443 |
|
444 #ifdef __cplusplus |
|
445 } |
|
446 #endif |
|
447 |
|
448 #endif |