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