涂 桂旺 / APDS_9960

Fork of APDS_9960 by Krishan Bhagat

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers glibr.h Source File

glibr.h

00001 #include "mbed.h"
00002 
00003 
00004 #define DEBUG                   0
00005 
00006 /* APDS-9960 I2C address */
00007 #define APDS9960_I2C_ADDR       0x39
00008 
00009 /* Gesture parameters */
00010 #define GESTURE_THRESHOLD_OUT   10
00011 #define GESTURE_SENSITIVITY_1   50
00012 #define GESTURE_SENSITIVITY_2   20
00013 
00014 /* Error code for returned values */
00015 #define ERROR                   0xFF
00016 
00017 /* Acceptable device IDs */
00018 #define APDS9960_ID_1           0xAB
00019 #define APDS9960_ID_2           0x9C 
00020 #define APDS9960_ID_3           0xA8
00021 
00022 /* Misc parameters */
00023 #define FIFO_PAUSE_TIME         0.30      // Wait period (ms) between FIFO reads
00024 
00025 /* APDS-9960 register addresses */
00026 #define APDS9960_ENABLE         0x80
00027 #define APDS9960_ATIME          0x81
00028 #define APDS9960_WTIME          0x83
00029 #define APDS9960_AILTL          0x84
00030 #define APDS9960_AILTH          0x85
00031 #define APDS9960_AIHTL          0x86
00032 #define APDS9960_AIHTH          0x87
00033 #define APDS9960_PILT           0x89
00034 #define APDS9960_PIHT           0x8B
00035 #define APDS9960_PERS           0x8C
00036 #define APDS9960_CONFIG1        0x8D
00037 #define APDS9960_PPULSE         0x8E
00038 #define APDS9960_CONTROL        0x8F
00039 #define APDS9960_CONFIG2        0x90
00040 #define APDS9960_ID             0x92
00041 #define APDS9960_STATUS         0x93
00042 #define APDS9960_CDATAL         0x94
00043 #define APDS9960_CDATAH         0x95
00044 #define APDS9960_RDATAL         0x96
00045 #define APDS9960_RDATAH         0x97
00046 #define APDS9960_GDATAL         0x98
00047 #define APDS9960_GDATAH         0x99
00048 #define APDS9960_BDATAL         0x9A
00049 #define APDS9960_BDATAH         0x9B
00050 #define APDS9960_PDATA          0x9C
00051 #define APDS9960_POFFSET_UR     0x9D
00052 #define APDS9960_POFFSET_DL     0x9E
00053 #define APDS9960_CONFIG3        0x9F
00054 #define APDS9960_GPENTH         0xA0
00055 #define APDS9960_GEXTH          0xA1
00056 #define APDS9960_GCONF1         0xA2
00057 #define APDS9960_GCONF2         0xA3
00058 #define APDS9960_GOFFSET_U      0xA4
00059 #define APDS9960_GOFFSET_D      0xA5
00060 #define APDS9960_GOFFSET_L      0xA7
00061 #define APDS9960_GOFFSET_R      0xA9
00062 #define APDS9960_GPULSE         0xA6
00063 #define APDS9960_GCONF3         0xAA
00064 #define APDS9960_GCONF4         0xAB
00065 #define APDS9960_GFLVL          0xAE
00066 #define APDS9960_GSTATUS        0xAF
00067 #define APDS9960_IFORCE         0xE4
00068 #define APDS9960_PICLEAR        0xE5
00069 #define APDS9960_CICLEAR        0xE6
00070 #define APDS9960_AICLEAR        0xE7
00071 #define APDS9960_GFIFO_U        0xFC
00072 #define APDS9960_GFIFO_D        0xFD
00073 #define APDS9960_GFIFO_L        0xFE
00074 #define APDS9960_GFIFO_R        0xFF
00075 
00076 /* Bit fields */
00077 #define APDS9960_PON            0x01
00078 #define APDS9960_AEN            0x02
00079 #define APDS9960_PEN            0x04
00080 #define APDS9960_WEN            0x08
00081 #define APSD9960_AIEN           0x10
00082 #define APDS9960_PIEN           0x20
00083 #define APDS9960_GEN            0x40
00084 #define APDS9960_GVALID         0x01
00085 
00086 /* On/Off definitions */
00087 #define Off                     0
00088 #define On                      1
00089 
00090 /* Acceptable parameters for setMode */
00091 #define Power                   0
00092 #define AMBIENT_LIGHT           1
00093 #define PROXIMITY               2
00094 #define WAIT                    3
00095 #define AMBIENT_LIGHT_INT       4
00096 #define PROXIMITY_INT           5
00097 #define GESTURE                 6
00098 #define ALL                     7
00099 
00100 /* LED Drive values */
00101 #define LED_DRIVE_100MA         0
00102 #define LED_DRIVE_50MA          1
00103 #define LED_DRIVE_25MA          2
00104 #define LED_DRIVE_12_5MA        3
00105 
00106 /* Proximity Gain (PGAIN) values */
00107 #define PGAIN_1X                0
00108 #define PGAIN_2X                1
00109 #define PGAIN_4X                2
00110 #define PGAIN_8X                3
00111 
00112 /* ALS Gain (AGAIN) values */
00113 #define AGAIN_1X                0
00114 #define AGAIN_4X                1
00115 #define AGAIN_16X               2
00116 #define AGAIN_64X               3
00117 
00118 /* Gesture Gain (GGAIN) values */
00119 #define GGAIN_1X                0
00120 #define GGAIN_2X                1
00121 #define GGAIN_4X                2
00122 #define GGAIN_8X                3
00123 
00124 /* LED Boost values */
00125 #define LED_BOOST_100           0
00126 #define LED_BOOST_150           1
00127 #define LED_BOOST_200           2
00128 #define LED_BOOST_300           3    
00129 
00130 /* Gesture wait time values */
00131 #define GWTIME_0MS              0
00132 #define GWTIME_2_8MS            1
00133 #define GWTIME_5_6MS            2
00134 #define GWTIME_8_4MS            3
00135 #define GWTIME_14_0MS           4
00136 #define GWTIME_22_4MS           5
00137 #define GWTIME_30_8MS           6
00138 #define GWTIME_39_2MS           7
00139 
00140 /* Default values */
00141 #define DEFAULT_ATIME           219     // 103ms
00142 #define DEFAULT_WTIME           246     // 27ms
00143 #define DEFAULT_PROX_PPULSE     0x87    // 16us, 8 pulses
00144 #define DEFAULT_GESTURE_PPULSE  0x89    // 16us, 10 pulses
00145 #define DEFAULT_POFFSET_UR      0       // 0 offset
00146 #define DEFAULT_POFFSET_DL      0       // 0 offset      
00147 #define DEFAULT_CONFIG1         0x60    // No 12x wait (WTIME) factor
00148 #define DEFAULT_LDRIVE          LED_DRIVE_100MA
00149 #define DEFAULT_PGAIN           PGAIN_4X
00150 #define DEFAULT_AGAIN           AGAIN_4X
00151 #define DEFAULT_PILT            0       // Low proximity threshold
00152 #define DEFAULT_PIHT            50      // High proximity threshold
00153 #define DEFAULT_AILT            0xFFFF  // Force interrupt for calibration
00154 #define DEFAULT_AIHT            0
00155 #define DEFAULT_PERS            0x11    // 2 consecutive prox or ALS for int.
00156 #define DEFAULT_CONFIG2         0x01    // No saturation interrupts or LED boost  
00157 #define DEFAULT_CONFIG3         0       // Enable all photodiodes, no SAI
00158 #define DEFAULT_GPENTH          40      // Threshold for entering gesture mode
00159 #define DEFAULT_GEXTH           30      // Threshold for exiting gesture mode    
00160 #define DEFAULT_GCONF1          0x40    // 4 gesture events for int., 1 for exit
00161 #define DEFAULT_GGAIN           GGAIN_4X
00162 #define DEFAULT_GLDRIVE         LED_DRIVE_100MA
00163 #define DEFAULT_GWTIME          GWTIME_2_8MS
00164 #define DEFAULT_GOFFSET         0       // No offset scaling for gesture mode
00165 #define DEFAULT_GPULSE          0xC9    // 32us, 10 pulses
00166 #define DEFAULT_GCONF3          0       // All photodiodes active during gesture
00167 #define DEFAULT_GIEN            0       // Disable gesture interrupts
00168 
00169 /* Direction definitions */
00170 enum {
00171   DIR_NONE,
00172   DIR_LEFT,
00173   DIR_RIGHT,
00174   DIR_UP,
00175   DIR_DOWN,
00176   DIR_NEAR,
00177   DIR_FAR,
00178   DIR_ALL
00179 };
00180 
00181 /* State definitions */
00182 enum {
00183   NA_STATE,
00184   NEAR_STATE,
00185   FAR_STATE,
00186   ALL_STATE
00187 };
00188 
00189 /* Container for gesture data */
00190 typedef struct gesture_data_type {
00191     uint8_t u_data[32];
00192     uint8_t d_data[32];
00193     uint8_t l_data[32];
00194     uint8_t r_data[32];
00195     uint8_t sindex;
00196     uint8_t total_gestures;
00197     uint8_t in_threshold;
00198     uint8_t out_threshold;
00199 } gesture_data_type;
00200 
00201 
00202 class glibr{
00203     
00204     public:
00205   
00206   
00207      glibr(PinName sda, PinName scl);  //constructor
00208     ~glibr();
00209 
00210     
00211     bool ginit();
00212     /* Initialization methods */
00213     
00214     uint8_t getMode();
00215     bool setMode(uint8_t mode, uint8_t enable);
00216     
00217     /* Turn the APDS-9960 on and off */
00218     bool enablePower();
00219     bool disablePower();
00220     
00221     /* Enable or disable specific sensors */
00222     bool enableLightSensor(bool interrupts = false);
00223     bool disableLightSensor();
00224     bool enableProximitySensor(bool interrupts = false);
00225     bool disableProximitySensor();
00226     bool enableGestureSensor(bool interrupts = true);
00227     bool disableGestureSensor();
00228     
00229     /* LED drive strength control */
00230     uint8_t getLEDDrive();
00231     bool setLEDDrive(uint8_t drive);
00232     uint8_t getGestureLEDDrive();
00233     bool setGestureLEDDrive(uint8_t drive);
00234     
00235     /* Gain control */
00236     uint8_t getAmbientLightGain();
00237     bool setAmbientLightGain(uint8_t gain);
00238     uint8_t getProximityGain();
00239     bool setProximityGain(uint8_t gain);
00240     uint8_t getGestureGain();
00241     bool setGestureGain(uint8_t gain);
00242     
00243     /* Get and set light interrupt thresholds */
00244     bool getLightIntLowThreshold(uint16_t &threshold);
00245     bool setLightIntLowThreshold(uint16_t threshold);
00246     bool getLightIntHighThreshold(uint16_t &threshold);
00247     bool setLightIntHighThreshold(uint16_t threshold);
00248     
00249     /* Get and set proximity interrupt thresholds */
00250     bool getProximityIntLowThreshold(uint8_t &threshold);
00251     bool setProximityIntLowThreshold(uint8_t threshold);
00252     bool getProximityIntHighThreshold(uint8_t &threshold);
00253     bool setProximityIntHighThreshold(uint8_t threshold);
00254     
00255     /* Get and set interrupt enables */
00256     uint8_t getAmbientLightIntEnable();
00257     bool setAmbientLightIntEnable(uint8_t enable);
00258     uint8_t getProximityIntEnable();
00259     bool setProximityIntEnable(uint8_t enable);
00260     uint8_t getGestureIntEnable();
00261     bool setGestureIntEnable(uint8_t enable);
00262     
00263     /* Clear interrupts */
00264     bool clearAmbientLightInt();
00265     bool clearProximityInt();
00266     
00267     /* Ambient light methods */
00268     bool readAmbientLight(uint16_t &val);
00269     bool readRedLight(uint16_t &val);
00270     bool readGreenLight(uint16_t &val);
00271     bool readBlueLight(uint16_t &val);
00272     
00273     /* Proximity methods */
00274     bool readProximity(uint8_t &val);
00275     
00276     /* Gesture methods */
00277     bool isGestureAvailable();
00278     int readGesture();
00279      
00280     private:  
00281     /* Gesture processing */
00282     void resetGestureParameters();
00283     bool processGestureData();
00284     bool decodeGesture();
00285 
00286     /* Proximity Interrupt Threshold */
00287     uint8_t getProxIntLowThresh();
00288     bool setProxIntLowThresh(uint8_t threshold);
00289     uint8_t getProxIntHighThresh();
00290     bool setProxIntHighThresh(uint8_t threshold);
00291     
00292     /* LED Boost Control */
00293     uint8_t getLEDBoost();
00294     bool setLEDBoost(uint8_t boost);
00295     
00296     /* Proximity photodiode select */
00297     uint8_t getProxGainCompEnable();
00298     bool setProxGainCompEnable(uint8_t enable);
00299     uint8_t getProxPhotoMask();
00300     bool setProxPhotoMask(uint8_t mask);
00301     
00302     /* Gesture threshold control */
00303     uint8_t getGestureEnterThresh();
00304     bool setGestureEnterThresh(uint8_t threshold);
00305     uint8_t getGestureExitThresh();
00306     bool setGestureExitThresh(uint8_t threshold);
00307     
00308     /* Gesture LED, gain, and time control */
00309     uint8_t getGestureWaitTime();
00310     bool setGestureWaitTime(uint8_t time);
00311     
00312     /* Gesture mode */
00313     uint8_t getGestureMode();
00314     bool setGestureMode(uint8_t mode);
00315     
00316     /* Members */
00317     gesture_data_type gesture_data_;  //instanciation from struct 
00318     int gesture_ud_delta_;
00319     int gesture_lr_delta_;
00320     int gesture_ud_count_;
00321     int gesture_lr_count_;
00322     int gesture_near_count_;
00323     int gesture_far_count_;
00324     int gesture_state_;
00325     int gesture_motion_;
00326     
00327     
00328     
00329     uint8_t I2CreadByte(char address, char subAddress);
00330     int I2CwriteByte(char address, char subAddress, char data);   //if return value is 1, then ack not sent
00331     int I2CReadDataBlock(char address, char subAddress, char *data, unsigned int len);  //reads array of bytes
00332     I2C i2c;
00333     
00334 };