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