apds99xx.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018 Gunar Schorcht
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
202 #ifndef APDS99XX_H
203 #define APDS99XX_H
204 
205 #ifdef __cplusplus
206 extern "C"
207 {
208 #endif
209 
210 #include <stdbool.h>
211 #include <stdint.h>
212 
213 #include "periph/gpio.h"
214 #include "periph/i2c.h"
215 
223 #if MODULE_APDS9900
224 #define APDS99XX_ID (0x29)
225 #define APDS99XX_T_PRX_PULSE (16)
226 #define APDS99XX_T_PRX_CNV (2720)
227 #define APDS99XX_T_WAIT_STEP (2720)
228 #define APDS99XX_T_ALS_STEP (2720)
229 #define APDS99XX_CNTS_P_STEP (1023)
231 #elif MODULE_APDS9901
232 #define APDS99XX_ID (0x20)
233 #define APDS99XX_T_PRX_PULSE (16)
234 #define APDS99XX_T_PRX_CNV (2720)
235 #define APDS99XX_T_WAIT_STEP (2720)
236 #define APDS99XX_T_ALS_STEP (2720)
237 #define APDS99XX_CNTS_P_STEP (1023)
239 #elif MODULE_APDS9930
240 #define APDS99XX_ID (0x39)
241 #define APDS99XX_T_PRX_PULSE (16)
242 #define APDS99XX_T_PRX_CNV (2730)
243 #define APDS99XX_T_WAIT_STEP (2730)
244 #define APDS99XX_T_ALS_STEP (2730)
245 #define APDS99XX_CNTS_P_STEP (1023)
247 #elif MODULE_APDS9950
248 #define APDS99XX_ID (0x69)
249 #define APDS99XX_T_PRX_PULSE (14)
250 #define APDS99XX_T_PRX_CNV (2400)
251 #define APDS99XX_T_WAIT_STEP (2400)
252 #define APDS99XX_T_ALS_STEP (2400)
253 #define APDS99XX_CNTS_P_STEP (1024)
255 #elif MODULE_APDS9960 || DOXYGEN
256 #define APDS99XX_ID (0xab)
257 #define APDS99XX_T_PRX_PULSE (36)
259 #define APDS99XX_T_PRX_CNV (841)
261 #define APDS99XX_T_WAIT_STEP (2780)
262 #define APDS99XX_T_ALS_STEP (2780)
263 #define APDS99XX_CNTS_P_STEP (1025)
265 #else
266 #error "Please provide a valid aps99xx variant (apds9900, apds9901, adps9930, apds9950, apds9960)"
267 #endif
268 
273 #define APDS99XX_I2C_ADDRESS (0x39)
274 
278 typedef enum {
285 
289 typedef enum {
293  #if MODULE_APDS9950 || MODULE_APDS9960 || DOXYGEN
295  #endif /* MODULE_APDS9950 || MODULE_APDS9960 || DOXYGEN */
296  #if MODULE_APDS9900 || MODULE_APDS9901 || APDS9930 || DOXYGEN
298  #endif /* MODULE_APDS9900 || MODULE_APDS9901 || APDS9930 || DOXYGEN */
300 
304 typedef enum {
306  #if MODULE_APDS9930 || MODULE_APDS9960 || DOXYGEN
310  #endif /* MODULE_APDS9930 || MODULE_APDS9960 || DOXYGEN */
312 
316 typedef enum {
322 
326 typedef struct {
327 
328  unsigned dev;
330  uint16_t als_steps;
335  uint8_t prx_pulses;
343  uint16_t wait_steps;
345  bool wait_long;
348  gpio_t int_pin;
351 
352 #if MODULE_APDS99XX_FULL || DOXYGEN
353 
374 typedef struct {
375  bool als_int_en;
376  uint8_t als_pers;
382  uint16_t als_thresh_low;
383  uint16_t als_thresh_high;
385  bool prx_int_en;
386  uint8_t prx_pers;
395  uint16_t prx_thresh_low;
397  uint16_t prx_thresh_high;
401 
407 typedef struct {
408  bool als_int;
409  bool prx_int;
411 
415 typedef void (*apds99xx_isr_t)(void *arg);
416 
417 #endif /* MODULE_APDS99XX_FULL */
418 
422 typedef struct {
423 
426 #if MODULE_APDS99XX_FULL || DOXYGEN
428  void* isr_arg;
430  bool gpio_init;
431 #endif /* MODULE_APDS99XX_FULL */
432 
433 } apds99xx_t;
434 
435 #if MODULE_APDS9950 || MODULE_APDS9960 || DOXYGEN
436 
439 typedef union {
440 
441  struct {
442  uint16_t red;
443  uint16_t green;
444  uint16_t blue;
445  };
446  uint16_t val[3];
449 #endif
450 
464 int apds99xx_init(apds99xx_t *dev, const apds99xx_params_t *params);
465 
479 int apds99xx_data_ready_als(const apds99xx_t *dev);
480 
502 int apds99xx_read_als_raw(const apds99xx_t *dev, uint16_t *raw);
503 
504 #if MODULE_APDS9900 || MODULE_APDS9901 || MODULE_APDS9930 || DOXYGEN
505 
523 int apds99xx_read_illuminance(const apds99xx_t *dev, uint16_t *lux);
524 
525 #endif /* MODULE_APDS9900 || MODULE_APDS9901 || MODULE_APDS9930 || DOXYGEN */
526 
527 #if MODULE_APDS9950 || MODULE_APDS9960 || DOXYGEN
528 
555 int apds99xx_read_rgb_raw(const apds99xx_t *dev, apds99xx_rgb_t *rgb);
556 
557 #endif /* MODULE_APDS9950 || MODULE_APDS9960 || DOXYGEN */
558 
572 int apds99xx_data_ready_prx(const apds99xx_t *dev);
573 
598 int apds99xx_read_prx_raw(const apds99xx_t *dev, uint16_t *prx);
599 
613 int apds99xx_power_down(const apds99xx_t *dev);
614 
626 int apds99xx_power_up(const apds99xx_t *dev);
627 
628 #if MODULE_APDS99XX_FULL || DOXYGEN
629 
658  apds99xx_isr_t isr, void *isr_arg);
659 
679 
680 #endif /* MODULE_APDS99XX_FULL */
681 
682 #ifdef __cplusplus
683 }
684 #endif
685 
686 #endif /* APDS99XX_H */
687 
APDS99XX_PRX_DRIVE_50
@ APDS99XX_PRX_DRIVE_50
50.0 mA
Definition: apds99xx.h:318
apds99xx_als_gain_t
apds99xx_als_gain_t
Ambient light sensing (ALS) gain.
Definition: apds99xx.h:289
apds99xx_t
APDS99XX sensor device data structure type.
Definition: apds99xx.h:422
apds99xx_int_config_t::prx_thresh_low
uint16_t prx_thresh_low
Low threshold for proximity values (only the low byte is used for APDS9960)
Definition: apds99xx.h:395
apds99xx_t::params
apds99xx_params_t params
device initialization parameters
Definition: apds99xx.h:424
apds99xx_read_rgb_raw
int apds99xx_read_rgb_raw(const apds99xx_t *dev, apds99xx_rgb_t *rgb)
Read one raw RGB color data sample (APDS9950 and APDS9960 only)
apds99xx_int_source_t
Interrupt source.
Definition: apds99xx.h:407
APDS99XX_PRX_GAIN_8
@ APDS99XX_PRX_GAIN_8
8 x gain (APDS9930, APDS9960 only)
Definition: apds99xx.h:309
apds99xx_prx_drive_t
apds99xx_prx_drive_t
Proximity sensing (PRX) LED drive strength.
Definition: apds99xx.h:316
apds99xx_int_config_t::als_pers
uint8_t als_pers
Number of consecutive ALS values that have to be outside the thresholds to generate an interrupt:
Definition: apds99xx.h:376
apds99xx_params_t
APDS99XX device initialization parameters.
Definition: apds99xx.h:326
APDS99XX_ALS_GAIN_16
@ APDS99XX_ALS_GAIN_16
16 x gain
Definition: apds99xx.h:292
APDS99XX_ERROR_RAW_DATA
@ APDS99XX_ERROR_RAW_DATA
reading raw data failed
Definition: apds99xx.h:283
apds99xx_data_ready_als
int apds99xx_data_ready_als(const apds99xx_t *dev)
Ambient light sensing (ALS) data-ready status function.
APDS99XX_ERROR_NO_DATA
@ APDS99XX_ERROR_NO_DATA
no data are available
Definition: apds99xx.h:282
apds99xx_int_source_t::prx_int
bool prx_int
Proximity interrupt happened.
Definition: apds99xx.h:409
apds99xx_read_als_raw
int apds99xx_read_als_raw(const apds99xx_t *dev, uint16_t *raw)
Read one raw data sample of ambient light sensing (ALS)
apds99xx_power_down
int apds99xx_power_down(const apds99xx_t *dev)
Power down the sensor.
apds99xx_rgb_t::blue
uint16_t blue
B photodiode count value (blue)
Definition: apds99xx.h:444
apds99xx_data_ready_prx
int apds99xx_data_ready_prx(const apds99xx_t *dev)
Proximity sensing (PRX) data-ready status function.
APDS99XX_ALS_GAIN_8
@ APDS99XX_ALS_GAIN_8
8 x gain
Definition: apds99xx.h:291
APDS99XX_OK
@ APDS99XX_OK
success
Definition: apds99xx.h:279
APDS99XX_PRX_DRIVE_100
@ APDS99XX_PRX_DRIVE_100
100.0 mA (default)
Definition: apds99xx.h:317
APDS99XX_ERROR_I2C
@ APDS99XX_ERROR_I2C
I2C communication error.
Definition: apds99xx.h:280
apds99xx_rgb_t
RGB count value data structure (APDS9950 and APDS9960 only)
Definition: apds99xx.h:439
apds99xx_params_t::int_pin
gpio_t int_pin
interrupt pin: GPIO_UNDEF if not used
Definition: apds99xx.h:348
apds99xx_params_t::wait_steps
uint16_t wait_steps
Waiting time in steps.
Definition: apds99xx.h:343
apds99xx_params_t::als_steps
uint16_t als_steps
ALS integration time in steps.
Definition: apds99xx.h:330
apds99xx_int_source_t::als_int
bool als_int
ALS interrupt happened.
Definition: apds99xx.h:408
apds99xx_int_source
int apds99xx_int_source(apds99xx_t *dev, apds99xx_int_source_t *src)
Get the source of an interrupt.
apds99xx_isr_t
void(* apds99xx_isr_t)(void *arg)
Interrupt service routine function prototype.
Definition: apds99xx.h:415
apds99xx_error_codes_t
apds99xx_error_codes_t
Definition of error codes.
Definition: apds99xx.h:278
apds99xx_read_prx_raw
int apds99xx_read_prx_raw(const apds99xx_t *dev, uint16_t *prx)
Read one data sample of proximity sensing (PRX)
APDS99XX_ALS_GAIN_1
@ APDS99XX_ALS_GAIN_1
1 x gain (default)
Definition: apds99xx.h:290
apds99xx_prx_gain_t
apds99xx_prx_gain_t
Proximity sensing (PRX) gain.
Definition: apds99xx.h:304
APDS99XX_ALS_GAIN_120
@ APDS99XX_ALS_GAIN_120
120 x gain (APDS9900, APDS9901, APDS9930 only)
Definition: apds99xx.h:297
apds99xx_int_config_t::prx_thresh_high
uint16_t prx_thresh_high
High threshold for proximity values (only the low byte is used for APDS9960)
Definition: apds99xx.h:397
APDS99XX_PRX_GAIN_2
@ APDS99XX_PRX_GAIN_2
2 x gain (APDS9930, APDS9960 only)
Definition: apds99xx.h:307
apds99xx_rgb_t::red
uint16_t red
R photodiode count value (red)
Definition: apds99xx.h:442
apds99xx_params_t::als_gain
apds99xx_als_gain_t als_gain
Gain used for ALS.
Definition: apds99xx.h:332
APDS99XX_PRX_GAIN_4
@ APDS99XX_PRX_GAIN_4
4 x gain (APDS9930, APDS9960 only)
Definition: apds99xx.h:308
APDS99XX_ERROR_WRONG_ID
@ APDS99XX_ERROR_WRONG_ID
wrong id read
Definition: apds99xx.h:281
APDS99XX_PRX_GAIN_1
@ APDS99XX_PRX_GAIN_1
1 x gain (default)
Definition: apds99xx.h:305
apds99xx_int_config
int apds99xx_int_config(apds99xx_t *dev, apds99xx_int_config_t *cfg, apds99xx_isr_t isr, void *isr_arg)
Configure the interrupts of the sensor.
apds99xx_params_t::prx_pulses
uint8_t prx_pulses
IR LED pulses for proximity sensing.
Definition: apds99xx.h:335
apds99xx_params_t::prx_gain
apds99xx_prx_gain_t prx_gain
Gain used for proximity sensing.
Definition: apds99xx.h:340
apds99xx_t::gpio_init
bool gpio_init
GPIO is already initialized.
Definition: apds99xx.h:430
apds99xx_params_t::wait_long
bool wait_long
Long waiting time.
Definition: apds99xx.h:345
apds99xx_rgb_t::green
uint16_t green
G photodiode count value (green)
Definition: apds99xx.h:443
APDS99XX_PRX_DRIVE_25
@ APDS99XX_PRX_DRIVE_25
25.0 mA
Definition: apds99xx.h:319
apds99xx_int_config_t::prx_pers
uint8_t prx_pers
Number of consecutive proximity values that have to be outside the thresholds to generate an interrup...
Definition: apds99xx.h:386
apds99xx_read_illuminance
int apds99xx_read_illuminance(const apds99xx_t *dev, uint16_t *lux)
Read one data sample of illuminance in lux.
APDS99XX_PRX_DRIVE_12_5
@ APDS99XX_PRX_DRIVE_12_5
12.5 mA
Definition: apds99xx.h:320
apds99xx_power_up
int apds99xx_power_up(const apds99xx_t *dev)
Power up the sensor.
gpio.h
Low-level GPIO peripheral driver interface definitions.
apds99xx_t::isr_arg
void * isr_arg
user ISR argument
Definition: apds99xx.h:428
APDS99XX_ALS_GAIN_64
@ APDS99XX_ALS_GAIN_64
64 x gain (APDS9950, APDS9960 only)
Definition: apds99xx.h:294
apds99xx_params_t::dev
unsigned dev
I2C device (default I2C_DEV(0))
Definition: apds99xx.h:328
apds99xx_int_config_t::prx_int_en
bool prx_int_en
Proximity interrupt enabled.
Definition: apds99xx.h:385
apds99xx_int_config_t::als_thresh_high
uint16_t als_thresh_high
High threshold value for ALS interrupts.
Definition: apds99xx.h:383
apds99xx_params_t::prx_drive
apds99xx_prx_drive_t prx_drive
IR LED current for proximity sensing (default APDS99XX_PRX_DRIVE_100)
Definition: apds99xx.h:338
apds99xx_init
int apds99xx_init(apds99xx_t *dev, const apds99xx_params_t *params)
Initialize the APDS99XX sensor device.
apds99xx_int_config_t::als_thresh_low
uint16_t als_thresh_low
Low threshold value for ALS interrupts.
Definition: apds99xx.h:382
apds99xx_int_config_t::als_int_en
bool als_int_en
ALS interrupt enabled.
Definition: apds99xx.h:375
apds99xx_t::isr
apds99xx_isr_t isr
user ISR
Definition: apds99xx.h:427
i2c.h
Low-level I2C peripheral driver interface definition.
apds99xx_int_config_t
Interrupt configuration.
Definition: apds99xx.h:374