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