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