Abraham Marsen / Mbed 2 deprecated Jazz_Hands_Nordic

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers APDS9960.h Source File

APDS9960.h

00001 /**
00002  * @author Abraham Marsen & Allison Ashlock
00003  * Georgia Institute of Technology 
00004  * ECE 4180 Embedded Systems Design
00005  * Professor Hamblen
00006  * Spring 2015
00007  * 
00008  * @section LICENSE
00009  * ----------------------------------------------------------------------------
00010  * "THE BEER-WARE LICENSE" (Revision 42):
00011  * <amarsen3@gmail.com> wrote this file. As long as you retain this notice you
00012  * can do whatever you want with this stuff. If we meet some day, and you think
00013  * this stuff is worth it, you can buy me a beer in return.
00014  * ----------------------------------------------------------------------------
00015  *
00016  *
00017  * @section DESCRIPTION
00018  *
00019  * Avago Digital Proximity, Ambient Light, RGB and Gesture Sensor APDS-9960
00020  *
00021  * Datasheet, specs, and information:
00022  *
00023  * https://www.sparkfun.com/products/12787
00024  */
00025 
00026 
00027 
00028 #ifndef APDS9960_H
00029 #define APDS9960_H
00030 
00031 #include "mbed.h"
00032 
00033 //Location of I2C slave
00034 #define APDS9960_I2C_ADDR   0x72
00035 
00036 /* Error code for returned values */
00037 #define ERROR 0xFF
00038 
00039 /* Acceptable device IDs */
00040 #define APDS9960_ID_1 0xAB
00041 #define APDS9960_ID_2 0x9C
00042 
00043 //reused wait time
00044 #define FIFO_TIME           0.30
00045 
00046 //Gest param
00047 #define GEST_THRESHOLD_OUT 5
00048 #define GEST_SENSITIVITY_1 50
00049 #define GEST_SENSITIVITY_2 20
00050 
00051 //LOOK AT ALL MY ADDRESSES
00052 #define APDS9960_ENABLE     0x80
00053 #define APDS9960_ATIME      0x81
00054 #define APDS9960_WTIME      0x83
00055 #define APDS9960_AILTL      0x84
00056 #define APDS9960_AILTH      0x85
00057 #define APDS9960_AIHTL      0x86
00058 #define APDS9960_AIHTH      0x87
00059 #define APDS9960_PILT       0x89
00060 #define APDS9960_PIHT       0x8B
00061 #define APDS9960_PERS       0x8C
00062 #define APDS9960_CONFIG1    0x8D
00063 #define APDS9960_PPULSE     0x8E
00064 #define APDS9960_CONTROL    0x8F
00065 #define APDS9960_CONFIG2    0x90
00066 #define APDS9960_ID         0x92
00067 #define APDS9960_STATUS     0x93
00068 #define APDS9960_CDATAL     0x94
00069 #define APDS9960_CDATAH     0x95
00070 #define APDS9960_RDATAL     0x96
00071 #define APDS9960_RDATAH     0x97
00072 #define APDS9960_GDATAL     0x98
00073 #define APDS9960_GDATAH     0x99
00074 #define APDS9960_BDATAL     0x9A
00075 #define APDS9960_BDATAH     0x9B
00076 #define APDS9960_PDATA      0x9C
00077 #define APDS9960_POFFSET_UR 0x9D
00078 #define APDS9960_POFFSET_DL 0x9E
00079 #define APDS9960_CONFIG3    0x9F
00080 #define APDS9960_GPENTH     0xA0
00081 #define APDS9960_GEXTH      0xA1
00082 #define APDS9960_GCONF1     0xA2
00083 #define APDS9960_GCONF2     0xA3
00084 #define APDS9960_GOFFSET_U  0xA4
00085 #define APDS9960_GOFFSET_D  0xA5
00086 #define APDS9960_GOFFSET_L  0xA7
00087 #define APDS9960_GOFFSET_R  0xA9
00088 #define APDS9960_GPULSE     0xA6
00089 #define APDS9960_GCONF3     0xAA
00090 #define APDS9960_GCONF4     0xAB
00091 #define APDS9960_GFLVL      0xAE
00092 #define APDS9960_GSTATUS    0xAF
00093 #define APDS9960_IFORCE     0xE4
00094 #define APDS9960_PICLEAR    0xE5
00095 #define APDS9960_CICLEAR    0xE6
00096 #define APDS9960_AICLEAR    0xE7
00097 #define APDS9960_GFIFO_U    0xFC
00098 #define APDS9960_GFIFO_D    0xFD
00099 #define APDS9960_GFIFO_L    0xFE
00100 #define APDS9960_GFIFO_R    0xFF
00101 
00102 //bitmasks
00103 #define APDS9960_PON        0x01
00104 #define APDS9960_AEN        0x02
00105 #define APDS9960_PEN        0x04
00106 #define APDS9960_WEN        0x08
00107 #define APSD9960_AIEN       0x10
00108 #define APDS9960_PIEN       0x20
00109 #define APDS9960_GEN        0x40
00110 #define APDS9960_GVALID     0x01
00111 
00112 /* On/Off definitions */
00113 #define OFF                 0
00114 #define ON                  1
00115 
00116 // Acceptable parameters for setMode
00117 enum {
00118     POWER,
00119     AMBIENT_LIGHT,
00120     PROXIMITY,
00121     WAIT,
00122     AMBIENT_LIGHT_INT,
00123     PROXIMITY_INT,
00124     GESTURE,
00125     ALL
00126 };
00127 
00128 /* LED Drive values */
00129 enum {
00130     LED_DRIVE_100MA,
00131     LED_DRIVE_50MA,
00132     LED_DRIVE_25MA,
00133     LED_DRIVE_12_5MA
00134 };
00135 
00136 /* Proximity Gain (PGAIN) values */
00137 enum {
00138     PGAIN_1X,
00139     PGAIN_2X,
00140     PGAIN_4X,
00141     PGAIN_8X
00142 };
00143 
00144 /* ALS Gain (AGAIN) values */
00145 enum {
00146     AGAIN_1X,
00147     AGAIN_4X,
00148     AGAIN_16X,
00149     AGAIN_64X
00150 };
00151 
00152 /* Gesture Gain (GGAIN) values */
00153 enum {
00154     GGAIN_1X,
00155     GGAIN_2X,
00156     GGAIN_4X,
00157     GGAIN_8X
00158 };
00159 /* LED Boost values */
00160 enum {
00161     LED_BOOST_100,
00162     LED_BOOST_150,
00163     LED_BOOST_200,
00164     LED_BOOST_300
00165 };
00166 
00167 /* Gesture wait time values */
00168 enum {
00169     GWTIME_0MS,
00170     GWTIME_2_8MS,
00171     GWTIME_5_6MS,
00172     GWTIME_8_4MS,
00173     GWTIME_14_0MS,
00174     GWTIME_22_4MS,
00175     GWTIME_30_8MS,
00176     GWTIME_39_2MS
00177 };
00178 
00179 /* Default values */
00180 #define DEFAULT_ATIME           219 // 103ms
00181 #define DEFAULT_WTIME           246 // 27ms
00182 #define DEFAULT_PROX_PPULSE     0x87 // 16us, 8 pulses
00183 #define DEFAULT_GEST_PPULSE     0x89 // 16us, 10 pulses
00184 #define DEFAULT_POFFSET_UR      0 // 0 offset
00185 #define DEFAULT_POFFSET_DL      0 // 0 offset
00186 #define DEFAULT_CONFIG1         0x60 // No 12x wait (WTIME) factor
00187 #define DEFAULT_LDRIVE          LED_DRIVE_100MA
00188 #define DEFAULT_PGAIN           PGAIN_4X
00189 #define DEFAULT_AGAIN           AGAIN_4X
00190 #define DEFAULT_PILT            0 // Low proximity threshold
00191 #define DEFAULT_PIHT            50 // High proximity threshold
00192 #define DEFAULT_AILT            0xFFFF // Force interrupt for calibration
00193 #define DEFAULT_AIHT            0
00194 #define DEFAULT_PERS            0x11 // 2 consecutive prox or ALS for int.
00195 #define DEFAULT_CONFIG2         0x01 // No saturation interrupts or LED boost
00196 #define DEFAULT_CONFIG3         0 // Enable all photodiodes, no SAI
00197 #define DEFAULT_GPENTH          40 // Threshold for entering gesture mode
00198 #define DEFAULT_GEXTH           30 // Threshold for exiting gesture mode
00199 #define DEFAULT_GCONF1          0x40 // 4 gesture events for int., 1 for exit
00200 #define DEFAULT_GGAIN           GGAIN_4X
00201 #define DEFAULT_GLDRIVE         LED_DRIVE_100MA
00202 #define DEFAULT_GWTIME          GWTIME_2_8MS
00203 #define DEFAULT_GOFFSET         0 // No offset scaling for gesture mode
00204 #define DEFAULT_GPULSE          0xC9 // 32us, 10 pulses
00205 #define DEFAULT_GCONF3          0 // All photodiodes active during gesture
00206 #define DEFAULT_GIEN            0 // Disable gesture interrupts
00207 
00208 //Directions X, Y, Z
00209 enum {
00210     DIR_NA,
00211     DIR_W,
00212     DIR_E,
00213     DIR_N,
00214     DIR_S,
00215     DIR_I,
00216     DIR_O,
00217     DIR_NW,
00218     DIR_SW,
00219     DIR_IW,
00220     DIR_OW,
00221     DIR_NE,
00222     DIR_SE,
00223     DIR_IE,
00224     DIR_OE,
00225     DIR_NI,
00226     DIR_NO,
00227     DIR_SI,
00228     DIR_SO,
00229     DIR_AN
00230 };
00231 
00232 /* State definitions */
00233 enum {
00234   NA_STATE,
00235   NEAR_STATE,
00236   FAR_STATE,
00237   ALL_STATE
00238 };
00239 
00240 /* Container for gesture data */
00241 typedef struct gesture_data_type {
00242     uint8_t u_data[32];
00243     uint8_t d_data[32];
00244     uint8_t l_data[32];
00245     uint8_t r_data[32];
00246     uint8_t index_gest;
00247     uint8_t total_gests;
00248     uint8_t in_threshold;
00249     uint8_t out_threshold;
00250 } gest_data_type;
00251 
00252 /* APDS9960 Class */
00253 class APDS9960 {
00254 public:
00255 /* Initialization methods */
00256     APDS9960(PinName sda, PinName scl);
00257     bool init();
00258     uint8_t getMode();
00259     bool setMode(uint8_t mode, uint8_t enable);
00260 /* Turn the APDS-9960 on and off */
00261     bool enPower();
00262     bool disPower();
00263 /* Enable or disable specific sensors */
00264     bool enLightSens(bool interrupts = false);
00265     bool disLightSens();
00266     bool enProxSens(bool interrupts = false);
00267     bool disProxSens();
00268     bool enGestSens(bool interrupts = true);
00269     bool disGestSens();
00270 /* LED drive strength control */
00271     uint8_t getLEDDrive();
00272     bool setLEDDrive(uint8_t drive);
00273     uint8_t getGestLEDDrive();
00274     bool setGestLEDDrive(uint8_t drive);
00275 /* Gain control */
00276     uint8_t getAmbientLightGain();
00277     bool setAmbientLightGain(uint8_t gain);
00278     uint8_t getProxGain();
00279     bool setProxGain(uint8_t gain);
00280     uint8_t getGestGain();
00281     bool setGestGain(uint8_t gain);
00282 /* Get and set light interrupt thresholds */
00283     bool getLightIntLowThreshold(uint16_t &threshold);
00284     bool setLightIntLowThreshold(uint16_t threshold);
00285     bool getLightIntHighThreshold(uint16_t &threshold);
00286     bool setLightIntHighThreshold(uint16_t threshold);
00287 /* Get and set proximity interrupt thresholds */
00288     bool getProxIntLowThreshold(uint8_t &threshold);
00289     bool setProxIntLowThreshold(uint8_t threshold);
00290     bool getProxIntHighThreshold(uint8_t &threshold);
00291     bool setProxIntHighThreshold(uint8_t threshold);
00292 /* Get and set interrupt enables */
00293     uint8_t getAmbientLightIntEnable();
00294     bool setAmbientLightIntEnable(uint8_t enable);
00295     uint8_t getProxIntEnable();
00296     bool setProxIntEnable(uint8_t enable);
00297     uint8_t getGestIntEnable();
00298     bool setGestIntEnable(uint8_t enable);
00299 /* Clear interrupts */
00300     bool clrAmbientLightInt();
00301     bool clrProxInt();
00302 /* Ambient light methods */
00303     bool readAmbientLight(uint16_t &val);
00304     bool readRedLight(uint16_t &val);
00305     bool readGreenLight(uint16_t &val);
00306     bool readBlueLight(uint16_t &val);
00307 /* Prox methods */
00308     bool readProx(uint8_t &val);
00309 /* Gesture methods */
00310     bool isGestAvailable();
00311     int readGest();
00312 /*Destructor*/
00313     ~APDS9960();
00314 
00315 
00316 private:
00317 /* Gesture processing */
00318     I2C i2c_;
00319     void rstGestParameters();
00320     bool procGestData();
00321     bool decodeGest();
00322 /* Proximity Interrupt Threshold */
00323     uint8_t getProxIntLowThresh();
00324     bool setProxIntLowThresh(uint8_t threshold);
00325     uint8_t getProxIntHighThresh();
00326     bool setProxIntHighThresh(uint8_t threshold);
00327 /* LED Boost Control */
00328     uint8_t getLEDBoost();
00329     bool setLEDBoost(uint8_t boost);
00330 /* Proximity photodiode select */
00331     uint8_t getProxGainCompEnable();
00332     bool setProxGainCompEnable(uint8_t enable);
00333     uint8_t getProxPhotoMask();
00334     bool setProxPhotoMask(uint8_t mask);
00335 /* Gesture threshold control */
00336     uint8_t getGestEnterThresh();
00337     bool setGestEnterThresh(uint8_t threshold);
00338     uint8_t getGestExitThresh();
00339     bool setGestExitThresh(uint8_t threshold);
00340 /* Gesture LED, gain, and time control */
00341     uint8_t getGestWaitTime();
00342     bool setGestWaitTime(uint8_t time);
00343 /* Gesture mode */
00344     uint8_t getGestMode();
00345     bool setGestMode(uint8_t mode);
00346 /* Raw I2C Commands */
00347     bool WriteByte(uint8_t val);
00348     int WriteDataByte(char reg, char val);
00349     bool WriteDataBlock(uint8_t reg, uint8_t *val, unsigned int len);
00350     uint8_t ReadDataByte(char reg);
00351     int ReadDataBlock(uint8_t reg, uint8_t *val, unsigned int len);
00352 /* Members */
00353     gest_data_type gest_data_;
00354     int gest_ud_delta_;
00355     int gest_lr_delta_;
00356     int gest_ud_count_;
00357     int gest_lr_count_;
00358     int gest_near_count_;
00359     int gest_far_count_;
00360     int gest_state_;
00361     int gest_motion_;
00362 };
00363 
00364 #endif