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 
9 #pragma once
10 
204 #ifdef __cplusplus
205 extern "C"
206 {
207 #endif
208 
209 #include <stdbool.h>
210 #include <stdint.h>
211 
212 #include "periph/gpio.h"
213 #include "periph/i2c.h"
214 
222 #if MODULE_APDS9900
223 #define APDS99XX_ID (0x29)
224 #define APDS99XX_T_PRX_PULSE (16)
225 #define APDS99XX_T_PRX_CNV (2720)
226 #define APDS99XX_T_WAIT_STEP (2720)
227 #define APDS99XX_T_ALS_STEP (2720)
228 #define APDS99XX_CNTS_P_STEP (1023)
230 #elif MODULE_APDS9901
231 #define APDS99XX_ID (0x20)
232 #define APDS99XX_T_PRX_PULSE (16)
233 #define APDS99XX_T_PRX_CNV (2720)
234 #define APDS99XX_T_WAIT_STEP (2720)
235 #define APDS99XX_T_ALS_STEP (2720)
236 #define APDS99XX_CNTS_P_STEP (1023)
238 #elif MODULE_APDS9930
239 #define APDS99XX_ID (0x39)
240 #define APDS99XX_T_PRX_PULSE (16)
241 #define APDS99XX_T_PRX_CNV (2730)
242 #define APDS99XX_T_WAIT_STEP (2730)
243 #define APDS99XX_T_ALS_STEP (2730)
244 #define APDS99XX_CNTS_P_STEP (1023)
246 #elif MODULE_APDS9950
247 #define APDS99XX_ID (0x69)
248 #define APDS99XX_T_PRX_PULSE (14)
249 #define APDS99XX_T_PRX_CNV (2400)
250 #define APDS99XX_T_WAIT_STEP (2400)
251 #define APDS99XX_T_ALS_STEP (2400)
252 #define APDS99XX_CNTS_P_STEP (1024)
254 #elif MODULE_APDS9960 || DOXYGEN
255 #define APDS99XX_ID (0xab)
256 #define APDS99XX_T_PRX_PULSE (36)
258 #define APDS99XX_T_PRX_CNV (841)
260 #define APDS99XX_T_WAIT_STEP (2780)
261 #define APDS99XX_T_ALS_STEP (2780)
262 #define APDS99XX_CNTS_P_STEP (1025)
264 #else
265 #error "Please provide a valid aps99xx variant (apds9900, apds9901, adps9930, apds9950, apds9960)"
266 #endif
272 #define APDS99XX_I2C_ADDRESS (0x39)
273 
277 typedef enum {
284 
288 typedef enum {
292  #if MODULE_APDS9950 || MODULE_APDS9960 || DOXYGEN
294  #endif /* MODULE_APDS9950 || MODULE_APDS9960 || DOXYGEN */
295  #if MODULE_APDS9900 || MODULE_APDS9901 || APDS9930 || DOXYGEN
297  #endif /* MODULE_APDS9900 || MODULE_APDS9901 || APDS9930 || DOXYGEN */
299 
303 typedef enum {
305  #if MODULE_APDS9930 || MODULE_APDS9960 || DOXYGEN
309  #endif /* MODULE_APDS9930 || MODULE_APDS9960 || DOXYGEN */
311 
315 typedef enum {
321 
325 typedef struct {
326 
327  unsigned dev;
329  uint16_t als_steps;
334  uint8_t prx_pulses;
342  uint16_t wait_steps;
344  bool wait_long;
347  gpio_t int_pin;
350 
351 #if MODULE_APDS99XX_FULL || DOXYGEN
352 
373 typedef struct {
374  bool als_int_en;
375  uint8_t als_pers;
381  uint16_t als_thresh_low;
382  uint16_t als_thresh_high;
384  bool prx_int_en;
385  uint8_t prx_pers;
394  uint16_t prx_thresh_low;
396  uint16_t prx_thresh_high;
400 
406 typedef struct {
407  bool als_int;
408  bool prx_int;
410 
414 typedef void (*apds99xx_isr_t)(void *arg);
415 
416 #endif /* MODULE_APDS99XX_FULL */
417 
421 typedef struct {
422 
425 #if MODULE_APDS99XX_FULL || DOXYGEN
427  void* isr_arg;
429  bool gpio_init;
430 #endif /* MODULE_APDS99XX_FULL */
431 
432 } apds99xx_t;
433 
434 #if MODULE_APDS9950 || MODULE_APDS9960 || DOXYGEN
438 typedef union {
439 
440  struct {
441  uint16_t red;
442  uint16_t green;
443  uint16_t blue;
444  };
445  uint16_t val[3];
448 #endif
449 
463 int apds99xx_init(apds99xx_t *dev, const apds99xx_params_t *params);
464 
479 
501 int apds99xx_read_als_raw(const apds99xx_t *dev, uint16_t *raw);
502 
503 #if MODULE_APDS9900 || MODULE_APDS9901 || MODULE_APDS9930 || DOXYGEN
522 int apds99xx_read_illuminance(const apds99xx_t *dev, uint16_t *lux);
523 
524 #endif /* MODULE_APDS9900 || MODULE_APDS9901 || MODULE_APDS9930 || DOXYGEN */
525 
526 #if MODULE_APDS9950 || MODULE_APDS9960 || DOXYGEN
555 
556 #endif /* MODULE_APDS9950 || MODULE_APDS9960 || DOXYGEN */
557 
572 
597 int apds99xx_read_prx_raw(const apds99xx_t *dev, uint16_t *prx);
598 
613 
626 
627 #if MODULE_APDS99XX_FULL || DOXYGEN
628 
657  apds99xx_isr_t isr, void *isr_arg);
658 
678 
679 #endif /* MODULE_APDS99XX_FULL */
680 
681 #ifdef __cplusplus
682 }
683 #endif
684 
Low-level GPIO peripheral driver interface definitions.
void(* apds99xx_isr_t)(void *arg)
Interrupt service routine function prototype.
Definition: apds99xx.h:414
int apds99xx_power_down(const apds99xx_t *dev)
Power down the sensor.
int apds99xx_data_ready_prx(const apds99xx_t *dev)
Proximity sensing (PRX) data-ready status function.
apds99xx_prx_gain_t
Proximity sensing (PRX) gain.
Definition: apds99xx.h:303
int apds99xx_int_source(apds99xx_t *dev, apds99xx_int_source_t *src)
Get the source of an interrupt.
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_prx_drive_t
Proximity sensing (PRX) LED drive strength.
Definition: apds99xx.h:315
int apds99xx_data_ready_als(const apds99xx_t *dev)
Ambient light sensing (ALS) data-ready status function.
int apds99xx_read_illuminance(const apds99xx_t *dev, uint16_t *lux)
Read one data sample of illuminance in lux.
int apds99xx_read_prx_raw(const apds99xx_t *dev, uint16_t *prx)
Read one data sample of proximity sensing (PRX)
int apds99xx_read_als_raw(const apds99xx_t *dev, uint16_t *raw)
Read one raw data sample of ambient light sensing (ALS)
int apds99xx_power_up(const apds99xx_t *dev)
Power up the sensor.
apds99xx_error_codes_t
Definition of error codes.
Definition: apds99xx.h:277
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_als_gain_t
Ambient light sensing (ALS) gain.
Definition: apds99xx.h:288
int apds99xx_init(apds99xx_t *dev, const apds99xx_params_t *params)
Initialize the APDS99XX sensor device.
@ APDS99XX_PRX_GAIN_4
4 x gain (APDS9930, APDS9960 only)
Definition: apds99xx.h:307
@ APDS99XX_PRX_GAIN_8
8 x gain (APDS9930, APDS9960 only)
Definition: apds99xx.h:308
@ APDS99XX_PRX_GAIN_1
1 x gain (default)
Definition: apds99xx.h:304
@ APDS99XX_PRX_GAIN_2
2 x gain (APDS9930, APDS9960 only)
Definition: apds99xx.h:306
@ APDS99XX_PRX_DRIVE_25
25.0 mA
Definition: apds99xx.h:318
@ APDS99XX_PRX_DRIVE_12_5
12.5 mA
Definition: apds99xx.h:319
@ APDS99XX_PRX_DRIVE_100
100.0 mA (default)
Definition: apds99xx.h:316
@ APDS99XX_PRX_DRIVE_50
50.0 mA
Definition: apds99xx.h:317
@ APDS99XX_ERROR_NO_DATA
no data are available
Definition: apds99xx.h:281
@ APDS99XX_ERROR_I2C
I2C communication error.
Definition: apds99xx.h:279
@ APDS99XX_ERROR_RAW_DATA
reading raw data failed
Definition: apds99xx.h:282
@ APDS99XX_ERROR_WRONG_ID
wrong id read
Definition: apds99xx.h:280
@ APDS99XX_OK
success
Definition: apds99xx.h:278
@ APDS99XX_ALS_GAIN_120
120 x gain (APDS9900, APDS9901, APDS9930 only)
Definition: apds99xx.h:296
@ APDS99XX_ALS_GAIN_16
16 x gain
Definition: apds99xx.h:291
@ APDS99XX_ALS_GAIN_64
64 x gain (APDS9950, APDS9960 only)
Definition: apds99xx.h:293
@ APDS99XX_ALS_GAIN_8
8 x gain
Definition: apds99xx.h:290
@ APDS99XX_ALS_GAIN_1
1 x gain (default)
Definition: apds99xx.h:289
Low-level I2C peripheral driver interface definition.
Interrupt configuration.
Definition: apds99xx.h:373
uint16_t als_thresh_high
High threshold value for ALS interrupts.
Definition: apds99xx.h:382
uint8_t prx_pers
Number of consecutive proximity values that have to be outside the thresholds to generate an interrup...
Definition: apds99xx.h:385
uint16_t als_thresh_low
Low threshold value for ALS interrupts.
Definition: apds99xx.h:381
uint16_t prx_thresh_low
Low threshold for proximity values (only the low byte is used for APDS9960)
Definition: apds99xx.h:394
bool als_int_en
ALS interrupt enabled.
Definition: apds99xx.h:374
bool prx_int_en
Proximity interrupt enabled.
Definition: apds99xx.h:384
uint8_t als_pers
Number of consecutive ALS values that have to be outside the thresholds to generate an interrupt:
Definition: apds99xx.h:375
uint16_t prx_thresh_high
High threshold for proximity values (only the low byte is used for APDS9960)
Definition: apds99xx.h:396
Interrupt source.
Definition: apds99xx.h:406
bool prx_int
Proximity interrupt happened.
Definition: apds99xx.h:408
bool als_int
ALS interrupt happened.
Definition: apds99xx.h:407
APDS99XX device initialization parameters.
Definition: apds99xx.h:325
unsigned dev
I2C device (default I2C_DEV(0))
Definition: apds99xx.h:327
gpio_t int_pin
interrupt pin: GPIO_UNDEF if not used
Definition: apds99xx.h:347
apds99xx_als_gain_t als_gain
Gain used for ALS.
Definition: apds99xx.h:331
uint16_t wait_steps
Waiting time in steps.
Definition: apds99xx.h:342
apds99xx_prx_gain_t prx_gain
Gain used for proximity sensing.
Definition: apds99xx.h:339
uint8_t prx_pulses
IR LED pulses for proximity sensing.
Definition: apds99xx.h:334
apds99xx_prx_drive_t prx_drive
IR LED current for proximity sensing (default APDS99XX_PRX_DRIVE_100)
Definition: apds99xx.h:337
uint16_t als_steps
ALS integration time in steps.
Definition: apds99xx.h:329
bool wait_long
Long waiting time.
Definition: apds99xx.h:344
APDS99XX sensor device data structure type.
Definition: apds99xx.h:421
apds99xx_params_t params
device initialization parameters
Definition: apds99xx.h:423
void * isr_arg
user ISR argument
Definition: apds99xx.h:427
bool gpio_init
GPIO is already initialized.
Definition: apds99xx.h:429
apds99xx_isr_t isr
user ISR
Definition: apds99xx.h:426
RGB count value data structure (APDS9950 and APDS9960 only)
Definition: apds99xx.h:438
uint16_t green
G photodiode count value (green)
Definition: apds99xx.h:442
uint16_t blue
B photodiode count value (blue)
Definition: apds99xx.h:443
uint16_t red
R photodiode count value (red)
Definition: apds99xx.h:441