A library for the combined proximity, ambient light, RGB color and gesture sensor Avago APDS9960. Tested with corresponding breakout board from SparkFun.

Dependents:   mbed_blinky

Small test program that briefly shows the usage of the library (only gesture detection). It uses my SFE_MicroOLED Library for result output on a 64 x 48 pixel small 0.66" OLED screen. But it should be easy to change the output to the Serial interface. The platform used was a LPCXpresso11U68:

gesture detection Test-Program

#include "mbed.h"
#include "SparkFun_APDS9960.h"
#include "SFE_MicroOLED.h"

// Constants

// Global Variables and forward declarations
InterruptIn apds_int(P1_24);
SPI my_spi(P0_9, P0_8, P1_29);
MicroOLED uView(my_spi, P2_3, P1_28, P1_25);
I2C my_i2c(P0_5, P0_4);
SparkFun_APDS9960 apds = SparkFun_APDS9960(my_i2c);
volatile int isr_flag = 0;

void interruptRoutine();
void handleGesture();
void drawArrowUp();
void drawArrowDown();
void drawArrowLeft();
void drawArrowRight();
void drawCircle();
void drawX();

int main() {
  
  // Initialize MicroView
  uView.init(0, 8000000);
  uView.clear(PAGE);
  
  // Initialize interrupt service routine
  apds_int.mode(PullUp);
  apds_int.fall(&interruptRoutine);
  apds_int.disable_irq();
  
  uView.setFontType(0);
  wait_ms(500);
  
  // Initialize APDS-9960 (configure I2C and initial values)
  if ( apds.init(400000) ) {
    uView.puts("INIT OK!\n");
  } else {
    uView.puts("I-ERROR!\n");
  }
  
  // Start running the gesture sensor engine (with interrupts)
  if ( apds.enableGestureSensor(true) ) {
    uView.puts("Gest OK!");
  } else {
    uView.puts("G-ERROR!");
  }
  
  uView.display();
  
  // Wait for initialization and calibration to finish
  wait_ms(2000);
  
  uView.clear(PAGE);
  
  // Draw title
  uView.setCursor(16, 0);
  uView.puts("Swipe!");
  uView.display();
  
  apds_int.enable_irq();

  while(1) {
  
    // If interrupt, display gesture on MicroOLED
    if( isr_flag == 1 ) {
      apds_int.disable_irq();
      handleGesture();
      isr_flag = 0;
      wait_ms(500);
      uView.display();
      apds_int.enable_irq();
    }
  
    // Wait before next reading
    wait_ms(100);
  }
}

void interruptRoutine() {
  isr_flag = 1;
}

void handleGesture() {
  bool do_clear = true;
  
  // Draw symbol based on gesture
  if ( apds.isGestureAvailable() ) {
    switch ( apds.readGesture() ) {
      case DIR_UP:
        drawArrowUp();
        break;
      case DIR_DOWN:
        drawArrowDown();
        break;
      case DIR_LEFT:
        drawArrowLeft();
        break;
      case DIR_RIGHT:
        drawArrowRight();
        break;
      case DIR_NEAR:
        drawCircle();
        break;
      case DIR_FAR:
        drawX();
        break;
      default:
        do_clear = false;
    }
  }
  
  // Let symbol sit on screen for a while, then clear
  if ( do_clear ) {
    uView.clear(PAGE);
    uView.setCursor(16, 0);
    uView.puts("Swipe!");
  }
}

void drawArrowUp() {
  uView.clear(PAGE);
  uView.line(27, 43, 37, 43);  // Bottom: over 10
  uView.line(27, 43, 27, 18);  // Legs: Up 25
  uView.line(37, 43, 37, 18);  // Legs: Up 25
  uView.line(27, 18, 17, 18);  // Head: Over 10
  uView.line(37, 18, 47, 18);  // Head: Over 10
  uView.line(17, 18, 32, 3);   // Tip: Over 14, up 15
  uView.line(47, 18, 32, 3);   // Tip: Over 14, up 15
  uView.display();
}

void drawArrowDown() {
  uView.clear(PAGE);
  uView.line(27, 5, 37, 5);  // Bottom: over 10
  uView.line(27, 5, 27, 30);  // Legs: Up 25
  uView.line(37, 5, 37, 30);  // Legs: Up 25
  uView.line(27, 30, 17, 30);  // Head: Over 10
  uView.line(37, 30, 47, 30);  // Head: Over 10
  uView.line(17, 30, 32, 45);   // Tip: Over 14, up 15
  uView.line(47, 30, 32, 45);   // Tip: Over 14, up 15
  uView.display();
}

void drawArrowLeft() {
  uView.clear(PAGE);
  uView.line(52, 19, 52, 29);  // Bottom: up 10
  uView.line(52, 19, 27, 19);  // Legs: over 25
  uView.line(52, 29, 27, 29);  // Legs: over 25
  uView.line(27, 19, 27, 9);   // Head: up 10
  uView.line(27, 29, 27, 39);  // Head: down 10
  uView.line(27, 9, 12, 24);   // Tip: over 15, down 14
  uView.line(27, 39, 12, 24);  // Tip: over 15, down 14
  uView.display();
}

void drawArrowRight() {
  uView.clear(PAGE);
  uView.line(12, 19, 12, 29);  // Bottom: up 10
  uView.line(12, 19, 37, 19);  // Legs: over 25
  uView.line(12, 29, 37, 29);  // Legs: over 25
  uView.line(37, 19, 37, 9);   // Head: up 10
  uView.line(37, 29, 37, 39);  // Head: down 10
  uView.line(37, 9, 53, 25);   // Tip: over 15, down 14
  uView.line(37, 39, 52, 24);  // Tip: over 15, down 14
  uView.display();
}

void drawCircle() {
  uView.clear(PAGE);
  uView.circle(32, 24, 20);
  uView.display();
}

void drawX() {
  uView.clear(PAGE);
  uView.line(12, 4, 52, 44);
  uView.line(12, 44, 52, 4);
  uView.display();
}

Here is a video from Sparkfun showing all the implemented gestures:

Revision:
0:c04b49e0a678
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SparkFun_APDS9960.h	Fri Apr 10 13:21:48 2015 +0000
@@ -0,0 +1,345 @@
+/**
+ * @file    SparkFun_APDS-9960.h
+ * @brief   Library for the SparkFun APDS-9960 breakout board
+ * @author  Shawn Hymel (SparkFun Electronics)
+ *
+ * @copyright	This code is public domain but you buy me a beer if you use
+ * this and we meet someday (Beerware license).
+ *
+ * Adapted for mbed by Nenad Milosevic (synvox.ch) in April 2015.
+ *
+ * This library interfaces the Avago APDS-9960 to mbed over I2C. The library
+ * relies on the mbed I2C-library. To use the library, instantiate an
+ * APDS9960 object, call init(), and call the appropriate functions.
+ */
+ 
+#ifndef SparkFun_APDS9960_H
+#define SparkFun_APDS9960_H
+
+/* APDS-9960 I2C address */
+#define APDS9960_I2C_ADDR       0x39
+#define APDS9960_I2C_ADDR8		(APDS9960_I2C_ADDR << 1)
+
+/* Gesture parameters */
+#define GESTURE_THRESHOLD_OUT   10
+#define GESTURE_SENSITIVITY_1   50
+#define GESTURE_SENSITIVITY_2   20
+
+/* Error code for returned values */
+#define ERROR                   0xFF
+
+/* Acceptable device IDs */
+#define APDS9960_ID_1           0xAB
+#define APDS9960_ID_2           0x9C 
+
+/* Misc parameters */
+#define FIFO_PAUSE_TIME         30      // Wait period (ms) between FIFO reads
+
+/* APDS-9960 register addresses */
+#define APDS9960_ENABLE         0x80
+#define APDS9960_ATIME          0x81
+#define APDS9960_WTIME          0x83
+#define APDS9960_AILTL          0x84
+#define APDS9960_AILTH          0x85
+#define APDS9960_AIHTL          0x86
+#define APDS9960_AIHTH          0x87
+#define APDS9960_PILT           0x89
+#define APDS9960_PIHT           0x8B
+#define APDS9960_PERS           0x8C
+#define APDS9960_CONFIG1        0x8D
+#define APDS9960_PPULSE         0x8E
+#define APDS9960_CONTROL        0x8F
+#define APDS9960_CONFIG2        0x90
+#define APDS9960_ID             0x92
+#define APDS9960_STATUS         0x93
+#define APDS9960_CDATAL         0x94
+#define APDS9960_CDATAH         0x95
+#define APDS9960_RDATAL         0x96
+#define APDS9960_RDATAH         0x97
+#define APDS9960_GDATAL         0x98
+#define APDS9960_GDATAH         0x99
+#define APDS9960_BDATAL         0x9A
+#define APDS9960_BDATAH         0x9B
+#define APDS9960_PDATA          0x9C
+#define APDS9960_POFFSET_UR     0x9D
+#define APDS9960_POFFSET_DL     0x9E
+#define APDS9960_CONFIG3        0x9F
+#define APDS9960_GPENTH         0xA0
+#define APDS9960_GEXTH          0xA1
+#define APDS9960_GCONF1         0xA2
+#define APDS9960_GCONF2         0xA3
+#define APDS9960_GOFFSET_U      0xA4
+#define APDS9960_GOFFSET_D      0xA5
+#define APDS9960_GOFFSET_L      0xA7
+#define APDS9960_GOFFSET_R      0xA9
+#define APDS9960_GPULSE         0xA6
+#define APDS9960_GCONF3         0xAA
+#define APDS9960_GCONF4         0xAB
+#define APDS9960_GFLVL          0xAE
+#define APDS9960_GSTATUS        0xAF
+#define APDS9960_IFORCE         0xE4
+#define APDS9960_PICLEAR        0xE5
+#define APDS9960_CICLEAR        0xE6
+#define APDS9960_AICLEAR        0xE7
+#define APDS9960_GFIFO_U        0xFC
+#define APDS9960_GFIFO_D        0xFD
+#define APDS9960_GFIFO_L        0xFE
+#define APDS9960_GFIFO_R        0xFF
+
+/* Bit fields */
+#define APDS9960_PON            0x01 //0b00000001
+#define APDS9960_AEN            0x02 //0b00000010
+#define APDS9960_PEN            0x04 //0b00000100
+#define APDS9960_WEN            0x08 //0b00001000
+#define APSD9960_AIEN           0x10 //0b00010000
+#define APDS9960_PIEN           0x20 //0b00100000
+#define APDS9960_GEN            0x40 //0b01000000
+#define APDS9960_GVALID         0x01 //0b00000001
+
+/* On/Off definitions */
+#define OFF                     0
+#define ON                      1
+
+/* Acceptable parameters for setMode */
+#define POWER                   0
+#define AMBIENT_LIGHT           1
+#define PROXIMITY               2
+#define WAIT                    3
+#define AMBIENT_LIGHT_INT       4
+#define PROXIMITY_INT           5
+#define GESTURE                 6
+#define ALL_MODES               7
+
+/* LED Drive values */
+#define LED_DRIVE_100MA         0
+#define LED_DRIVE_50MA          1
+#define LED_DRIVE_25MA          2
+#define LED_DRIVE_12_5MA        3
+
+/* Proximity Gain (PGAIN) values */
+#define PGAIN_1X                0
+#define PGAIN_2X                1
+#define PGAIN_4X                2
+#define PGAIN_8X                3
+
+/* ALS Gain (AGAIN) values */
+#define AGAIN_1X                0
+#define AGAIN_4X                1
+#define AGAIN_16X               2
+#define AGAIN_64X               3
+
+/* Gesture Gain (GGAIN) values */
+#define GGAIN_1X                0
+#define GGAIN_2X                1
+#define GGAIN_4X                2
+#define GGAIN_8X                3
+
+/* LED Boost values */
+#define LED_BOOST_100           0
+#define LED_BOOST_150           1
+#define LED_BOOST_200           2
+#define LED_BOOST_300           3    
+
+/* Gesture wait time values */
+#define GWTIME_0MS              0
+#define GWTIME_2_8MS            1
+#define GWTIME_5_6MS            2
+#define GWTIME_8_4MS            3
+#define GWTIME_14_0MS           4
+#define GWTIME_22_4MS           5
+#define GWTIME_30_8MS           6
+#define GWTIME_39_2MS           7
+
+/* Default values */
+#define DEFAULT_ATIME           219     // 103ms
+#define DEFAULT_WTIME           246     // 27ms
+#define DEFAULT_PROX_PPULSE     0x87    // 16us, 8 pulses
+#define DEFAULT_GESTURE_PPULSE  0x89    // 16us, 10 pulses
+#define DEFAULT_POFFSET_UR      0       // 0 offset
+#define DEFAULT_POFFSET_DL      0       // 0 offset      
+#define DEFAULT_CONFIG1         0x60    // No 12x wait (WTIME) factor
+#define DEFAULT_LDRIVE          LED_DRIVE_100MA
+#define DEFAULT_PGAIN           PGAIN_4X
+#define DEFAULT_AGAIN           AGAIN_4X
+#define DEFAULT_PILT            0       // Low proximity threshold
+#define DEFAULT_PIHT            50      // High proximity threshold
+#define DEFAULT_AILT            0xFFFF  // Force interrupt for calibration
+#define DEFAULT_AIHT            0
+#define DEFAULT_PERS            0x11    // 2 consecutive prox or ALS for int.
+#define DEFAULT_CONFIG2         0x01    // No saturation interrupts or LED boost  
+#define DEFAULT_CONFIG3         0       // Enable all photodiodes, no SAI
+#define DEFAULT_GPENTH          40      // Threshold for entering gesture mode
+#define DEFAULT_GEXTH           30      // Threshold for exiting gesture mode    
+#define DEFAULT_GCONF1          0x40    // 4 gesture events for int., 1 for exit
+#define DEFAULT_GGAIN           GGAIN_4X
+#define DEFAULT_GLDRIVE         LED_DRIVE_100MA
+#define DEFAULT_GWTIME          GWTIME_2_8MS
+#define DEFAULT_GOFFSET         0       // No offset scaling for gesture mode
+#define DEFAULT_GPULSE          0xC9    // 32us, 10 pulses
+#define DEFAULT_GCONF3          0       // All photodiodes active during gesture
+#define DEFAULT_GIEN            0       // Disable gesture interrupts
+
+/* Direction definitions */
+enum {
+  DIR_NONE,
+  DIR_LEFT,
+  DIR_RIGHT,
+  DIR_UP,
+  DIR_DOWN,
+  DIR_NEAR,
+  DIR_FAR,
+  DIR_ALL
+};
+
+/* State definitions */
+enum {
+  NA_STATE,
+  NEAR_STATE,
+  FAR_STATE,
+  ALL_STATE
+};
+
+/* Container for gesture data */
+typedef struct gesture_data_type {
+    uint8_t u_data[32];
+    uint8_t d_data[32];
+    uint8_t l_data[32];
+    uint8_t r_data[32];
+    uint8_t index;
+    uint8_t total_gestures;
+    uint8_t in_threshold;
+    uint8_t out_threshold;
+} gesture_data_type;
+
+/* APDS9960 Class */
+class SparkFun_APDS9960 {
+public:
+
+    /* Initialization methods */
+    SparkFun_APDS9960(I2C &i2c);
+    ~SparkFun_APDS9960();
+    bool init(int apds_i2c_freq);
+    uint8_t getMode();
+    bool setMode(uint8_t mode, uint8_t enable);
+    
+    /* Turn the APDS-9960 on and off */
+    bool enablePower();
+    bool disablePower();
+    
+    /* Enable or disable specific sensors */
+    bool enableLightSensor(bool interrupts = false);
+    bool disableLightSensor();
+    bool enableProximitySensor(bool interrupts = false);
+    bool disableProximitySensor();
+    bool enableGestureSensor(bool interrupts = true);
+    bool disableGestureSensor();
+    
+    /* LED drive strength control */
+    uint8_t getLEDDrive();
+    bool setLEDDrive(uint8_t drive);
+    uint8_t getGestureLEDDrive();
+    bool setGestureLEDDrive(uint8_t drive);
+    
+    /* Gain control */
+    uint8_t getAmbientLightGain();
+    bool setAmbientLightGain(uint8_t gain);
+    uint8_t getProximityGain();
+    bool setProximityGain(uint8_t gain);
+    uint8_t getGestureGain();
+    bool setGestureGain(uint8_t gain);
+    
+    /* Get and set light interrupt thresholds */
+    bool getLightIntLowThreshold(uint16_t &threshold);
+    bool setLightIntLowThreshold(uint16_t threshold);
+    bool getLightIntHighThreshold(uint16_t &threshold);
+    bool setLightIntHighThreshold(uint16_t threshold);
+    
+    /* Get and set proximity interrupt thresholds */
+    bool getProximityIntLowThreshold(uint8_t &threshold);
+    bool setProximityIntLowThreshold(uint8_t threshold);
+    bool getProximityIntHighThreshold(uint8_t &threshold);
+    bool setProximityIntHighThreshold(uint8_t threshold);
+    
+    /* Get and set interrupt enables */
+    uint8_t getAmbientLightIntEnable();
+    bool setAmbientLightIntEnable(uint8_t enable);
+    uint8_t getProximityIntEnable();
+    bool setProximityIntEnable(uint8_t enable);
+    uint8_t getGestureIntEnable();
+    bool setGestureIntEnable(uint8_t enable);
+    
+    /* Clear interrupts */
+    bool clearAmbientLightInt();
+    bool clearProximityInt();
+    
+    /* Ambient light methods */
+    bool readAmbientLight(uint16_t &val);
+    bool readRedLight(uint16_t &val);
+    bool readGreenLight(uint16_t &val);
+    bool readBlueLight(uint16_t &val);
+    
+    /* Proximity methods */
+    bool readProximity(uint8_t &val);
+    
+    /* Gesture methods */
+    bool isGestureAvailable();
+    int readGesture();
+    
+private:
+
+    /* Gesture processing */
+    void resetGestureParameters();
+    bool processGestureData();
+    bool decodeGesture();
+
+    /* Proximity Interrupt Threshold */
+    uint8_t getProxIntLowThresh();
+    bool setProxIntLowThresh(uint8_t threshold);
+    uint8_t getProxIntHighThresh();
+    bool setProxIntHighThresh(uint8_t threshold);
+    
+    /* LED Boost Control */
+    uint8_t getLEDBoost();
+    bool setLEDBoost(uint8_t boost);
+    
+    /* Proximity photodiode select */
+    uint8_t getProxGainCompEnable();
+    bool setProxGainCompEnable(uint8_t enable);
+    uint8_t getProxPhotoMask();
+    bool setProxPhotoMask(uint8_t mask);
+    
+    /* Gesture threshold control */
+    uint8_t getGestureEnterThresh();
+    bool setGestureEnterThresh(uint8_t threshold);
+    uint8_t getGestureExitThresh();
+    bool setGestureExitThresh(uint8_t threshold);
+    
+    /* Gesture LED, gain, and time control */
+    uint8_t getGestureWaitTime();
+    bool setGestureWaitTime(uint8_t time);
+    
+    /* Gesture mode */
+    uint8_t getGestureMode();
+    bool setGestureMode(uint8_t mode);
+
+    /* Raw I2C Commands */
+    bool wireWriteByte(uint8_t val);
+    bool wireWriteDataByte(uint8_t reg, uint8_t val);
+    //bool wireWriteDataBlock(uint8_t reg, uint8_t *val, unsigned int len);
+    bool wireReadDataByte(uint8_t reg, uint8_t &val);
+    int wireReadDataBlock(uint8_t reg, uint8_t *val, unsigned int len);
+
+    /* Members */
+	I2C &apds_i2c;
+    gesture_data_type gesture_data_;
+    int gesture_ud_delta_;
+    int gesture_lr_delta_;
+    int gesture_ud_count_;
+    int gesture_lr_count_;
+    int gesture_near_count_;
+    int gesture_far_count_;
+    int gesture_state_;
+    int gesture_motion_;
+};
+
+#endif
\ No newline at end of file