Uses the APDS_9960 Digital Proximity, Ambient Light, RGB and Gesture Sensor library to play detected gesture sounds on a speaker from the SDcard

Dependencies:   mbed SDFileSystem wave_player

Committer:
kbhagat6
Date:
Fri Mar 13 16:37:51 2015 +0000
Revision:
15:339ffde549dc
Parent:
13:5930645dc5f0
final..

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kbhagat6 0:437ae08befe3 1 #include "glibr.h"
kbhagat6 0:437ae08befe3 2 #include "mbed.h"
kbhagat6 0:437ae08befe3 3
kbhagat6 0:437ae08befe3 4
kbhagat6 0:437ae08befe3 5
kbhagat6 0:437ae08befe3 6
kbhagat6 0:437ae08befe3 7 glibr::glibr(PinName sda, PinName scl):i2c(sda, scl){
kbhagat6 6:687dff16904e 8 gesture_ud_delta_ = 0;
kbhagat6 6:687dff16904e 9 gesture_lr_delta_ = 0;
kbhagat6 6:687dff16904e 10
kbhagat6 6:687dff16904e 11 gesture_ud_count_ = 0;
kbhagat6 6:687dff16904e 12 gesture_lr_count_ = 0;
kbhagat6 6:687dff16904e 13
kbhagat6 6:687dff16904e 14 gesture_near_count_ = 0;
kbhagat6 6:687dff16904e 15 gesture_far_count_ = 0;
kbhagat6 6:687dff16904e 16
kbhagat6 6:687dff16904e 17 gesture_state_ = 0;
kbhagat6 6:687dff16904e 18 gesture_motion_ = DIR_NONE;
kbhagat6 0:437ae08befe3 19 }
kbhagat6 0:437ae08befe3 20
kbhagat6 0:437ae08befe3 21 glibr::~glibr(){
kbhagat6 0:437ae08befe3 22
kbhagat6 0:437ae08befe3 23 }
kbhagat6 0:437ae08befe3 24
kbhagat6 9:286d00d9db80 25 bool glibr::ginit(){
kbhagat6 0:437ae08befe3 26 uint8_t id;
kbhagat6 0:437ae08befe3 27
kbhagat6 0:437ae08befe3 28 id=I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ID);
kbhagat6 0:437ae08befe3 29
kbhagat6 0:437ae08befe3 30 if( (!(id == APDS9960_ID_1 || id == APDS9960_ID_2))||id==ERROR) {
kbhagat6 0:437ae08befe3 31 return false;
kbhagat6 0:437ae08befe3 32 }
kbhagat6 0:437ae08befe3 33
kbhagat6 0:437ae08befe3 34 if(!setMode(ALL, OFF)) {
kbhagat6 0:437ae08befe3 35 return false;
kbhagat6 0:437ae08befe3 36 }
kbhagat6 0:437ae08befe3 37 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_ATIME, DEFAULT_ATIME)){
kbhagat6 0:437ae08befe3 38 return false;
kbhagat6 0:437ae08befe3 39 }
kbhagat6 0:437ae08befe3 40 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_WTIME, DEFAULT_WTIME)){
kbhagat6 0:437ae08befe3 41 return false;
kbhagat6 0:437ae08befe3 42 }
kbhagat6 0:437ae08befe3 43 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PPULSE, DEFAULT_PROX_PPULSE)){
kbhagat6 0:437ae08befe3 44 return false;
kbhagat6 0:437ae08befe3 45 }
kbhagat6 0:437ae08befe3 46 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_POFFSET_UR, DEFAULT_POFFSET_UR)){
kbhagat6 0:437ae08befe3 47 return false;
kbhagat6 0:437ae08befe3 48 }
kbhagat6 0:437ae08befe3 49 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_POFFSET_DL, DEFAULT_POFFSET_DL)){
kbhagat6 0:437ae08befe3 50 return false;
kbhagat6 0:437ae08befe3 51 }
kbhagat6 0:437ae08befe3 52 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONFIG1, DEFAULT_CONFIG1)){
kbhagat6 0:437ae08befe3 53 return false;
kbhagat6 0:437ae08befe3 54 }
kbhagat6 0:437ae08befe3 55
kbhagat6 0:437ae08befe3 56 if( !setLEDDrive(DEFAULT_LDRIVE) ) {
kbhagat6 0:437ae08befe3 57 return false;
kbhagat6 0:437ae08befe3 58 }
kbhagat6 0:437ae08befe3 59
kbhagat6 0:437ae08befe3 60 if( !setProximityGain(DEFAULT_PGAIN) ) {
kbhagat6 0:437ae08befe3 61 return false;
kbhagat6 0:437ae08befe3 62 }
kbhagat6 0:437ae08befe3 63 if( !setAmbientLightGain(DEFAULT_AGAIN) ) {
kbhagat6 0:437ae08befe3 64 return false;
kbhagat6 0:437ae08befe3 65 }
kbhagat6 0:437ae08befe3 66 if( !setProxIntLowThresh(DEFAULT_PILT) ) {
kbhagat6 0:437ae08befe3 67 return false;
kbhagat6 0:437ae08befe3 68 }
kbhagat6 1:c7215f5f9a72 69 if( !setProxIntHighThresh(DEFAULT_PIHT) ) {
kbhagat6 1:c7215f5f9a72 70 return false;
kbhagat6 1:c7215f5f9a72 71 }
kbhagat6 1:c7215f5f9a72 72 if( !setLightIntLowThreshold(DEFAULT_AILT) ) {
kbhagat6 1:c7215f5f9a72 73 return false;
kbhagat6 1:c7215f5f9a72 74 }
kbhagat6 1:c7215f5f9a72 75 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONFIG2, DEFAULT_CONFIG2) ) {
kbhagat6 1:c7215f5f9a72 76 return false;
kbhagat6 1:c7215f5f9a72 77 }
kbhagat6 1:c7215f5f9a72 78 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONFIG3, DEFAULT_CONFIG3) ) {
kbhagat6 1:c7215f5f9a72 79 return false;
kbhagat6 1:c7215f5f9a72 80 }
kbhagat6 1:c7215f5f9a72 81
kbhagat6 1:c7215f5f9a72 82 if( !setGestureEnterThresh(DEFAULT_GPENTH) ) {
kbhagat6 1:c7215f5f9a72 83 return false;
kbhagat6 1:c7215f5f9a72 84 }
kbhagat6 1:c7215f5f9a72 85 if( !setGestureExitThresh(DEFAULT_GEXTH) ) {
kbhagat6 1:c7215f5f9a72 86 return false;
kbhagat6 1:c7215f5f9a72 87 }
kbhagat6 1:c7215f5f9a72 88 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GCONF1, DEFAULT_GCONF1) ) {
kbhagat6 1:c7215f5f9a72 89 return false;
kbhagat6 1:c7215f5f9a72 90 }
kbhagat6 1:c7215f5f9a72 91 if( !setGestureGain(DEFAULT_GGAIN) ) {
kbhagat6 1:c7215f5f9a72 92 return false;
kbhagat6 1:c7215f5f9a72 93 }
kbhagat6 1:c7215f5f9a72 94 if( !setGestureLEDDrive(DEFAULT_GLDRIVE) ) {
kbhagat6 1:c7215f5f9a72 95 return false;
kbhagat6 1:c7215f5f9a72 96 }
kbhagat6 1:c7215f5f9a72 97 if( !setGestureWaitTime(DEFAULT_GWTIME) ) {
kbhagat6 1:c7215f5f9a72 98 return false;
kbhagat6 1:c7215f5f9a72 99 }
kbhagat6 1:c7215f5f9a72 100 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GOFFSET_U, DEFAULT_GOFFSET) ) {
kbhagat6 1:c7215f5f9a72 101 return false;
kbhagat6 1:c7215f5f9a72 102 }
kbhagat6 1:c7215f5f9a72 103 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GOFFSET_D, DEFAULT_GOFFSET) ) {
kbhagat6 1:c7215f5f9a72 104 return false;
kbhagat6 1:c7215f5f9a72 105 }
kbhagat6 1:c7215f5f9a72 106 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GOFFSET_L, DEFAULT_GOFFSET) ) {
kbhagat6 1:c7215f5f9a72 107 return false;
kbhagat6 1:c7215f5f9a72 108 }
kbhagat6 1:c7215f5f9a72 109 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GOFFSET_R, DEFAULT_GOFFSET) ) {
kbhagat6 1:c7215f5f9a72 110 return false;
kbhagat6 1:c7215f5f9a72 111 }
kbhagat6 1:c7215f5f9a72 112 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GPULSE, DEFAULT_GPULSE) ) {
kbhagat6 1:c7215f5f9a72 113 return false;
kbhagat6 1:c7215f5f9a72 114 }
kbhagat6 1:c7215f5f9a72 115 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GCONF3, DEFAULT_GCONF3) ) {
kbhagat6 1:c7215f5f9a72 116 return false;
kbhagat6 1:c7215f5f9a72 117 }
kbhagat6 1:c7215f5f9a72 118 if( !setGestureIntEnable(DEFAULT_GIEN) ) {
kbhagat6 1:c7215f5f9a72 119 return false;
kbhagat6 1:c7215f5f9a72 120 }
kbhagat6 0:437ae08befe3 121
kbhagat6 9:286d00d9db80 122 return true;
kbhagat6 0:437ae08befe3 123
kbhagat6 0:437ae08befe3 124 }
kbhagat6 8:6fa15d4e31fb 125
kbhagat6 9:286d00d9db80 126 //#if 0
kbhagat6 9:286d00d9db80 127 // /* Gesture config register dump */
kbhagat6 9:286d00d9db80 128 // uint8_t reg;
kbhagat6 9:286d00d9db80 129 // uint8_t val;
kbhagat6 9:286d00d9db80 130 //
kbhagat6 9:286d00d9db80 131 // for(reg = 0x80; reg <= 0xAF; reg++) {
kbhagat6 9:286d00d9db80 132 // if( (reg != 0x82) && \
kbhagat6 9:286d00d9db80 133 // (reg != 0x8A) && \
kbhagat6 9:286d00d9db80 134 // (reg != 0x91) && \
kbhagat6 9:286d00d9db80 135 // (reg != 0xA8) && \
kbhagat6 9:286d00d9db80 136 // (reg != 0xAC) && \
kbhagat6 9:286d00d9db80 137 // (reg != 0xAD) )
kbhagat6 9:286d00d9db80 138 // {
kbhagat6 9:286d00d9db80 139 // val= I2CreadByte(APDS9960_I2C_ADDR, reg);
kbhagat6 9:286d00d9db80 140 // if(val==ERROR){
kbhagat6 9:286d00d9db80 141 // printf("ERROR");
kbhagat6 9:286d00d9db80 142 // }
kbhagat6 9:286d00d9db80 143 // /*
kbhagat6 9:286d00d9db80 144 // print(reg, HEX);
kbhagat6 9:286d00d9db80 145 // print(": 0x");
kbhagat6 9:286d00d9db80 146 // println(val, HEX);*/
kbhagat6 9:286d00d9db80 147 // }
kbhagat6 9:286d00d9db80 148 // }
kbhagat6 9:286d00d9db80 149 //
kbhagat6 9:286d00d9db80 150 // for(reg = 0xE4; reg <= 0xE7; reg++) {
kbhagat6 9:286d00d9db80 151 // val= I2CreadByte(APDS9960_I2C_ADDR, reg);
kbhagat6 9:286d00d9db80 152 // /* Serial.print(reg, HEX);
kbhagat6 9:286d00d9db80 153 // Serial.print(": 0x");
kbhagat6 9:286d00d9db80 154 // Serial.println(val, HEX);*/
kbhagat6 9:286d00d9db80 155 // }
kbhagat6 9:286d00d9db80 156 //#endif
kbhagat6 7:0564c6faf8aa 157
kbhagat6 8:6fa15d4e31fb 158 // return true;
kbhagat6 9:286d00d9db80 159
kbhagat6 0:437ae08befe3 160
kbhagat6 0:437ae08befe3 161
kbhagat6 0:437ae08befe3 162
kbhagat6 0:437ae08befe3 163 /**
kbhagat6 0:437ae08befe3 164 * @brief Enables or disables a feature in the APDS-9960
kbhagat6 0:437ae08befe3 165 *
kbhagat6 0:437ae08befe3 166 * @param[in] mode which feature to enable
kbhagat6 0:437ae08befe3 167 * @param[in] enable ON (1) or OFF (0)
kbhagat6 0:437ae08befe3 168 * @return True if operation success. False otherwise.
kbhagat6 0:437ae08befe3 169 */
kbhagat6 0:437ae08befe3 170 bool glibr::setMode(uint8_t mode, uint8_t enable)
kbhagat6 0:437ae08befe3 171 {
kbhagat6 0:437ae08befe3 172 uint8_t reg_val;
kbhagat6 0:437ae08befe3 173
kbhagat6 0:437ae08befe3 174 /* Read current ENABLE register */
kbhagat6 0:437ae08befe3 175 reg_val = getMode();
kbhagat6 0:437ae08befe3 176 if( reg_val == ERROR ) {
kbhagat6 0:437ae08befe3 177 return false;
kbhagat6 0:437ae08befe3 178 }
kbhagat6 0:437ae08befe3 179
kbhagat6 0:437ae08befe3 180 /* Change bit(s) in ENABLE register */
kbhagat6 0:437ae08befe3 181 enable = enable & 0x01;
kbhagat6 0:437ae08befe3 182 if( mode >= 0 && mode <= 6 ) {
kbhagat6 0:437ae08befe3 183 if (enable) {
kbhagat6 0:437ae08befe3 184 reg_val |= (1 << mode);
kbhagat6 0:437ae08befe3 185 } else {
kbhagat6 0:437ae08befe3 186 reg_val &= ~(1 << mode);
kbhagat6 0:437ae08befe3 187 }
kbhagat6 0:437ae08befe3 188 } else if( mode == ALL ) {
kbhagat6 0:437ae08befe3 189 if (enable) {
kbhagat6 0:437ae08befe3 190 reg_val = 0x7F;
kbhagat6 0:437ae08befe3 191 } else {
kbhagat6 0:437ae08befe3 192 reg_val = 0x00;
kbhagat6 0:437ae08befe3 193 }
kbhagat6 0:437ae08befe3 194 }
kbhagat6 0:437ae08befe3 195
kbhagat6 0:437ae08befe3 196 /* Write value back to ENABLE register */
kbhagat6 0:437ae08befe3 197 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_ENABLE, reg_val)){
kbhagat6 0:437ae08befe3 198 return false;
kbhagat6 0:437ae08befe3 199 }
kbhagat6 0:437ae08befe3 200
kbhagat6 0:437ae08befe3 201
kbhagat6 0:437ae08befe3 202
kbhagat6 0:437ae08befe3 203 return true;
kbhagat6 0:437ae08befe3 204 }
kbhagat6 0:437ae08befe3 205
kbhagat6 0:437ae08befe3 206 uint8_t glibr::getMode()
kbhagat6 0:437ae08befe3 207 {
kbhagat6 6:687dff16904e 208 uint8_t val;
kbhagat6 6:687dff16904e 209 val= I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ENABLE);
kbhagat6 6:687dff16904e 210 if(val==ERROR){
kbhagat6 6:687dff16904e 211 return ERROR;
kbhagat6 6:687dff16904e 212 }
kbhagat6 6:687dff16904e 213 return val;
kbhagat6 0:437ae08befe3 214 }
kbhagat6 0:437ae08befe3 215
kbhagat6 0:437ae08befe3 216
kbhagat6 0:437ae08befe3 217
kbhagat6 3:26146a08bb22 218 bool glibr::enableLightSensor(bool interrupts)
kbhagat6 3:26146a08bb22 219 {
kbhagat6 3:26146a08bb22 220
kbhagat6 3:26146a08bb22 221 /* Set default gain, interrupts, enable power, and enable sensor */
kbhagat6 3:26146a08bb22 222 if( !setAmbientLightGain(DEFAULT_AGAIN) ) {
kbhagat6 3:26146a08bb22 223 return false;
kbhagat6 3:26146a08bb22 224 }
kbhagat6 3:26146a08bb22 225 if( interrupts ) {
kbhagat6 3:26146a08bb22 226 if( !setAmbientLightIntEnable(1) ) {
kbhagat6 3:26146a08bb22 227 return false;
kbhagat6 3:26146a08bb22 228 }
kbhagat6 3:26146a08bb22 229 } else {
kbhagat6 3:26146a08bb22 230 if( !setAmbientLightIntEnable(0) ) {
kbhagat6 3:26146a08bb22 231 return false;
kbhagat6 3:26146a08bb22 232 }
kbhagat6 3:26146a08bb22 233 }
kbhagat6 3:26146a08bb22 234 if( !enablePower() ){
kbhagat6 3:26146a08bb22 235 return false;
kbhagat6 3:26146a08bb22 236 }
kbhagat6 3:26146a08bb22 237 if( !setMode(AMBIENT_LIGHT, 1) ) {
kbhagat6 3:26146a08bb22 238 return false;
kbhagat6 3:26146a08bb22 239 }
kbhagat6 3:26146a08bb22 240
kbhagat6 3:26146a08bb22 241 return true;
kbhagat6 3:26146a08bb22 242
kbhagat6 3:26146a08bb22 243 }
kbhagat6 3:26146a08bb22 244
kbhagat6 3:26146a08bb22 245 /**
kbhagat6 3:26146a08bb22 246 * @brief Ends the light sensor on the APDS-9960
kbhagat6 3:26146a08bb22 247 *
kbhagat6 3:26146a08bb22 248 * @return True if sensor disabled correctly. False on error.
kbhagat6 3:26146a08bb22 249 */
kbhagat6 3:26146a08bb22 250 bool glibr::disableLightSensor()
kbhagat6 3:26146a08bb22 251 {
kbhagat6 3:26146a08bb22 252 if( !setAmbientLightIntEnable(0) ) {
kbhagat6 3:26146a08bb22 253 return false;
kbhagat6 3:26146a08bb22 254 }
kbhagat6 3:26146a08bb22 255 if( !setMode(AMBIENT_LIGHT, 0) ) {
kbhagat6 3:26146a08bb22 256 return false;
kbhagat6 3:26146a08bb22 257 }
kbhagat6 3:26146a08bb22 258
kbhagat6 3:26146a08bb22 259 return true;
kbhagat6 3:26146a08bb22 260 }
kbhagat6 3:26146a08bb22 261
kbhagat6 3:26146a08bb22 262 /**
kbhagat6 3:26146a08bb22 263 * @brief Starts the proximity sensor on the APDS-9960
kbhagat6 3:26146a08bb22 264 *
kbhagat6 3:26146a08bb22 265 * @param[in] interrupts true to enable hardware external interrupt on proximity
kbhagat6 3:26146a08bb22 266 * @return True if sensor enabled correctly. False on error.
kbhagat6 3:26146a08bb22 267 */
kbhagat6 3:26146a08bb22 268 bool glibr::enableProximitySensor(bool interrupts)
kbhagat6 3:26146a08bb22 269 {
kbhagat6 3:26146a08bb22 270 /* Set default gain, LED, interrupts, enable power, and enable sensor */
kbhagat6 3:26146a08bb22 271 if( !setProximityGain(DEFAULT_PGAIN) ) {
kbhagat6 3:26146a08bb22 272 return false;
kbhagat6 3:26146a08bb22 273 }
kbhagat6 3:26146a08bb22 274 if( !setLEDDrive(DEFAULT_LDRIVE) ) {
kbhagat6 3:26146a08bb22 275 return false;
kbhagat6 3:26146a08bb22 276 }
kbhagat6 3:26146a08bb22 277 if( interrupts ) {
kbhagat6 3:26146a08bb22 278 if( !setProximityIntEnable(1) ) {
kbhagat6 3:26146a08bb22 279 return false;
kbhagat6 3:26146a08bb22 280 }
kbhagat6 3:26146a08bb22 281 } else {
kbhagat6 3:26146a08bb22 282 if( !setProximityIntEnable(0) ) {
kbhagat6 3:26146a08bb22 283 return false;
kbhagat6 3:26146a08bb22 284 }
kbhagat6 3:26146a08bb22 285 }
kbhagat6 3:26146a08bb22 286 if( !enablePower() ){
kbhagat6 3:26146a08bb22 287 return false;
kbhagat6 3:26146a08bb22 288 }
kbhagat6 3:26146a08bb22 289 if( !setMode(PROXIMITY, 1) ) {
kbhagat6 3:26146a08bb22 290 return false;
kbhagat6 3:26146a08bb22 291 }
kbhagat6 3:26146a08bb22 292
kbhagat6 3:26146a08bb22 293 return true;
kbhagat6 3:26146a08bb22 294 }
kbhagat6 3:26146a08bb22 295
kbhagat6 3:26146a08bb22 296 /**
kbhagat6 3:26146a08bb22 297 * @brief Ends the proximity sensor on the APDS-9960
kbhagat6 3:26146a08bb22 298 *
kbhagat6 3:26146a08bb22 299 * @return True if sensor disabled correctly. False on error.
kbhagat6 3:26146a08bb22 300 */
kbhagat6 3:26146a08bb22 301 bool glibr::disableProximitySensor()
kbhagat6 3:26146a08bb22 302 {
kbhagat6 3:26146a08bb22 303 if( !setProximityIntEnable(0) ) {
kbhagat6 3:26146a08bb22 304 return false;
kbhagat6 3:26146a08bb22 305 }
kbhagat6 3:26146a08bb22 306 if( !setMode(PROXIMITY, 0) ) {
kbhagat6 3:26146a08bb22 307 return false;
kbhagat6 3:26146a08bb22 308 }
kbhagat6 3:26146a08bb22 309
kbhagat6 3:26146a08bb22 310 return true;
kbhagat6 3:26146a08bb22 311 }
kbhagat6 3:26146a08bb22 312
kbhagat6 3:26146a08bb22 313
kbhagat6 3:26146a08bb22 314 /**
kbhagat6 3:26146a08bb22 315 * @brief Starts the gesture recognition engine on the APDS-9960
kbhagat6 3:26146a08bb22 316 *
kbhagat6 3:26146a08bb22 317 * @param[in] interrupts true to enable hardware external interrupt on gesture
kbhagat6 3:26146a08bb22 318 * @return True if engine enabled correctly. False on error.
kbhagat6 3:26146a08bb22 319 */
kbhagat6 3:26146a08bb22 320 bool glibr::enableGestureSensor(bool interrupts)
kbhagat6 3:26146a08bb22 321 {
kbhagat6 3:26146a08bb22 322
kbhagat6 3:26146a08bb22 323 /* Enable gesture mode
kbhagat6 3:26146a08bb22 324 Set ENABLE to 0 (power off)
kbhagat6 3:26146a08bb22 325 Set WTIME to 0xFF
kbhagat6 3:26146a08bb22 326 Set AUX to LED_BOOST_300
kbhagat6 3:26146a08bb22 327 Enable PON, WEN, PEN, GEN in ENABLE
kbhagat6 3:26146a08bb22 328 */
kbhagat6 3:26146a08bb22 329
kbhagat6 3:26146a08bb22 330 resetGestureParameters();
kbhagat6 3:26146a08bb22 331 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_WTIME, 0xFF) ) {
kbhagat6 3:26146a08bb22 332 return false;
kbhagat6 3:26146a08bb22 333 }
kbhagat6 3:26146a08bb22 334 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PPULSE, DEFAULT_GESTURE_PPULSE) ) {
kbhagat6 3:26146a08bb22 335 return false;
kbhagat6 3:26146a08bb22 336 }
kbhagat6 3:26146a08bb22 337 if( !setLEDBoost(LED_BOOST_300) ) {
kbhagat6 3:26146a08bb22 338 return false;
kbhagat6 3:26146a08bb22 339 }
kbhagat6 3:26146a08bb22 340 if( interrupts ) {
kbhagat6 3:26146a08bb22 341 if( !setGestureIntEnable(1) ) {
kbhagat6 3:26146a08bb22 342 return false;
kbhagat6 3:26146a08bb22 343 }
kbhagat6 3:26146a08bb22 344 } else {
kbhagat6 3:26146a08bb22 345 if( !setGestureIntEnable(0) ) {
kbhagat6 3:26146a08bb22 346 return false;
kbhagat6 3:26146a08bb22 347 }
kbhagat6 3:26146a08bb22 348 }
kbhagat6 3:26146a08bb22 349 if( !setGestureMode(1) ) {
kbhagat6 3:26146a08bb22 350 return false;
kbhagat6 3:26146a08bb22 351 }
kbhagat6 3:26146a08bb22 352 if( !enablePower() ){
kbhagat6 3:26146a08bb22 353 return false;
kbhagat6 3:26146a08bb22 354 }
kbhagat6 3:26146a08bb22 355 if( !setMode(WAIT, 1) ) {
kbhagat6 3:26146a08bb22 356 return false;
kbhagat6 3:26146a08bb22 357 }
kbhagat6 3:26146a08bb22 358 if( !setMode(PROXIMITY, 1) ) {
kbhagat6 3:26146a08bb22 359 return false;
kbhagat6 3:26146a08bb22 360 }
kbhagat6 3:26146a08bb22 361 if( !setMode(GESTURE, 1) ) {
kbhagat6 3:26146a08bb22 362 return false;
kbhagat6 3:26146a08bb22 363 }
kbhagat6 3:26146a08bb22 364
kbhagat6 3:26146a08bb22 365 return true;
kbhagat6 3:26146a08bb22 366 }
kbhagat6 3:26146a08bb22 367
kbhagat6 3:26146a08bb22 368 /**
kbhagat6 3:26146a08bb22 369 * @brief Ends the gesture recognition engine on the APDS-9960
kbhagat6 3:26146a08bb22 370 *
kbhagat6 3:26146a08bb22 371 * @return True if engine disabled correctly. False on error.
kbhagat6 3:26146a08bb22 372 */
kbhagat6 3:26146a08bb22 373 bool glibr::disableGestureSensor()
kbhagat6 3:26146a08bb22 374 {
kbhagat6 3:26146a08bb22 375 resetGestureParameters();
kbhagat6 3:26146a08bb22 376 if( !setGestureIntEnable(0) ) {
kbhagat6 3:26146a08bb22 377 return false;
kbhagat6 3:26146a08bb22 378 }
kbhagat6 3:26146a08bb22 379 if( !setGestureMode(0) ) {
kbhagat6 3:26146a08bb22 380 return false;
kbhagat6 3:26146a08bb22 381 }
kbhagat6 3:26146a08bb22 382 if( !setMode(GESTURE, 0) ) {
kbhagat6 3:26146a08bb22 383 return false;
kbhagat6 3:26146a08bb22 384 }
kbhagat6 3:26146a08bb22 385
kbhagat6 3:26146a08bb22 386 return true;
kbhagat6 3:26146a08bb22 387 }
kbhagat6 3:26146a08bb22 388
kbhagat6 3:26146a08bb22 389
kbhagat6 3:26146a08bb22 390 /**
kbhagat6 3:26146a08bb22 391 * @brief Determines if there is a gesture available for reading
kbhagat6 3:26146a08bb22 392 *
kbhagat6 3:26146a08bb22 393 * @return True if gesture available. False otherwise.
kbhagat6 3:26146a08bb22 394 */
kbhagat6 3:26146a08bb22 395 bool glibr::isGestureAvailable()
kbhagat6 3:26146a08bb22 396 {
kbhagat6 3:26146a08bb22 397 uint8_t val;
kbhagat6 3:26146a08bb22 398
kbhagat6 3:26146a08bb22 399 /* Read value from GSTATUS register */
kbhagat6 4:84545b0e63a9 400 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_GSTATUS);
kbhagat6 5:3eb4f3091bd8 401 if( val==ERROR) {
kbhagat6 3:26146a08bb22 402 return ERROR;
kbhagat6 3:26146a08bb22 403 }
kbhagat6 3:26146a08bb22 404
kbhagat6 3:26146a08bb22 405 /* Shift and mask out GVALID bit */
kbhagat6 3:26146a08bb22 406 val &= APDS9960_GVALID;
kbhagat6 3:26146a08bb22 407
kbhagat6 3:26146a08bb22 408 /* Return true/false based on GVALID bit */
kbhagat6 3:26146a08bb22 409 if( val == 1) {
kbhagat6 3:26146a08bb22 410 return true;
kbhagat6 3:26146a08bb22 411 } else {
kbhagat6 3:26146a08bb22 412 return false;
kbhagat6 3:26146a08bb22 413 }
kbhagat6 3:26146a08bb22 414 }
kbhagat6 3:26146a08bb22 415
kbhagat6 4:84545b0e63a9 416 int glibr::readGesture()
kbhagat6 4:84545b0e63a9 417 {
kbhagat6 4:84545b0e63a9 418 uint8_t fifo_level = 0;
kbhagat6 8:6fa15d4e31fb 419 // uint8_t bytes_expected= 0;
kbhagat6 8:6fa15d4e31fb 420 int check;
kbhagat6 9:286d00d9db80 421 //char fifo_data[128];
kbhagat6 9:286d00d9db80 422 char fifo_data[128];
kbhagat6 9:286d00d9db80 423 char *fptr;
kbhagat6 9:286d00d9db80 424 fptr= fifo_data;
kbhagat6 9:286d00d9db80 425
kbhagat6 4:84545b0e63a9 426 uint8_t gstatus;
kbhagat6 4:84545b0e63a9 427 int motion;
kbhagat6 4:84545b0e63a9 428 int i;
kbhagat6 4:84545b0e63a9 429
kbhagat6 4:84545b0e63a9 430 /* Make sure that power and gesture is on and data is valid */
kbhagat6 4:84545b0e63a9 431 if( !isGestureAvailable() || !(getMode() & 0x41) ) {
kbhagat6 4:84545b0e63a9 432 return DIR_NONE;
kbhagat6 4:84545b0e63a9 433 }
kbhagat6 10:e8adab2fb829 434
kbhagat6 4:84545b0e63a9 435
kbhagat6 4:84545b0e63a9 436 /* Keep looping as long as gesture data is valid */
kbhagat6 4:84545b0e63a9 437 while(1) {
kbhagat6 10:e8adab2fb829 438
kbhagat6 4:84545b0e63a9 439 /* Wait some time to collect next batch of FIFO data */
kbhagat6 5:3eb4f3091bd8 440 wait(FIFO_PAUSE_TIME);
kbhagat6 4:84545b0e63a9 441
kbhagat6 4:84545b0e63a9 442 /* Get the contents of the STATUS register. Is data still valid? */
kbhagat6 10:e8adab2fb829 443
kbhagat6 4:84545b0e63a9 444 gstatus=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_GSTATUS);
kbhagat6 4:84545b0e63a9 445 if( gstatus==ERROR ) {
kbhagat6 4:84545b0e63a9 446 return ERROR;
kbhagat6 4:84545b0e63a9 447 }
kbhagat6 4:84545b0e63a9 448 /* If we have valid data, read in FIFO */
kbhagat6 4:84545b0e63a9 449 if( (gstatus & APDS9960_GVALID) == APDS9960_GVALID ) {
kbhagat6 4:84545b0e63a9 450
kbhagat6 4:84545b0e63a9 451 /* Read the current FIFO level */
kbhagat6 8:6fa15d4e31fb 452 fifo_level=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_GFLVL);
kbhagat6 8:6fa15d4e31fb 453 if( fifo_level==ERROR ) {
kbhagat6 4:84545b0e63a9 454 return ERROR;
kbhagat6 4:84545b0e63a9 455 }
kbhagat6 8:6fa15d4e31fb 456
kbhagat6 4:84545b0e63a9 457 //#if DEBUG
kbhagat6 4:84545b0e63a9 458 // Serial.print("FIFO Level: ");
kbhagat6 4:84545b0e63a9 459 // Serial.println(fifo_level);
kbhagat6 4:84545b0e63a9 460 //#endif
kbhagat6 4:84545b0e63a9 461
kbhagat6 4:84545b0e63a9 462 /* If there's stuff in the FIFO, read it into our data block */ //NEED TO FIGURE OUT WHAT THIS IS DOING.
kbhagat6 5:3eb4f3091bd8 463
kbhagat6 4:84545b0e63a9 464 if( fifo_level > 0) {
kbhagat6 8:6fa15d4e31fb 465 check = I2CReadDataBlock(APDS9960_I2C_ADDR,APDS9960_GFIFO_U,
kbhagat6 9:286d00d9db80 466 fptr,
kbhagat6 4:84545b0e63a9 467 (fifo_level * 4) );
kbhagat6 8:6fa15d4e31fb 468
kbhagat6 8:6fa15d4e31fb 469 if( check == -1 ) {
kbhagat6 4:84545b0e63a9 470 return ERROR;
kbhagat6 4:84545b0e63a9 471 }
kbhagat6 10:e8adab2fb829 472
kbhagat6 4:84545b0e63a9 473 #if DEBUG
kbhagat6 8:6fa15d4e31fb 474 //Serial.print("FIFO Dump: ");
kbhagat6 8:6fa15d4e31fb 475 for ( i = 0; i < (fifo_level * 4); i++ ) {
kbhagat6 8:6fa15d4e31fb 476 // Serial.print(fifo_data[i]);
kbhagat6 8:6fa15d4e31fb 477 // Serial.print(" ");
kbhagat6 4:84545b0e63a9 478 }
kbhagat6 8:6fa15d4e31fb 479 //Serial.println();
kbhagat6 4:84545b0e63a9 480 #endif
kbhagat6 4:84545b0e63a9 481
kbhagat6 4:84545b0e63a9 482 /* If at least 1 set of data, sort the data into U/D/L/R */
kbhagat6 8:6fa15d4e31fb 483 if((fifo_level * 4) >= 4 ) {
kbhagat6 8:6fa15d4e31fb 484 for( i = 0; i < (fifo_level * 4); i += 4 ) {
kbhagat6 8:6fa15d4e31fb 485 gesture_data_.u_data[gesture_data_.sindex] = \
kbhagat6 4:84545b0e63a9 486 fifo_data[i + 0];
kbhagat6 8:6fa15d4e31fb 487 gesture_data_.d_data[gesture_data_.sindex] = \
kbhagat6 4:84545b0e63a9 488 fifo_data[i + 1];
kbhagat6 8:6fa15d4e31fb 489 gesture_data_.l_data[gesture_data_.sindex] = \
kbhagat6 4:84545b0e63a9 490 fifo_data[i + 2];
kbhagat6 8:6fa15d4e31fb 491 gesture_data_.r_data[gesture_data_.sindex] = \
kbhagat6 4:84545b0e63a9 492 fifo_data[i + 3];
kbhagat6 8:6fa15d4e31fb 493 gesture_data_.sindex++;
kbhagat6 4:84545b0e63a9 494 gesture_data_.total_gestures++;
kbhagat6 4:84545b0e63a9 495 }
kbhagat6 4:84545b0e63a9 496
kbhagat6 4:84545b0e63a9 497 #if DEBUG
kbhagat6 5:3eb4f3091bd8 498 // Serial.print("Up Data: ");
kbhagat6 4:84545b0e63a9 499 for ( i = 0; i < gesture_data_.total_gestures; i++ ) {
kbhagat6 5:3eb4f3091bd8 500 // Serial.print(gesture_data_.u_data[i]);
kbhagat6 5:3eb4f3091bd8 501 // Serial.print(" ");
kbhagat6 4:84545b0e63a9 502 }
kbhagat6 5:3eb4f3091bd8 503 // Serial.println();
kbhagat6 4:84545b0e63a9 504 #endif
kbhagat6 4:84545b0e63a9 505
kbhagat6 4:84545b0e63a9 506 /* Filter and process gesture data. Decode near/far state */
kbhagat6 4:84545b0e63a9 507 if( processGestureData() ) {
kbhagat6 4:84545b0e63a9 508 if( decodeGesture() ) {
kbhagat6 4:84545b0e63a9 509 //***TODO: U-Turn Gestures
kbhagat6 4:84545b0e63a9 510 #if DEBUG
kbhagat6 4:84545b0e63a9 511 //Serial.println(gesture_motion_);
kbhagat6 4:84545b0e63a9 512 #endif
kbhagat6 4:84545b0e63a9 513 }
kbhagat6 4:84545b0e63a9 514 }
kbhagat6 4:84545b0e63a9 515
kbhagat6 4:84545b0e63a9 516 /* Reset data */
kbhagat6 8:6fa15d4e31fb 517 gesture_data_.sindex = 0;
kbhagat6 4:84545b0e63a9 518 gesture_data_.total_gestures = 0;
kbhagat6 4:84545b0e63a9 519 }
kbhagat6 4:84545b0e63a9 520 }
kbhagat6 4:84545b0e63a9 521 } else {
kbhagat6 4:84545b0e63a9 522
kbhagat6 4:84545b0e63a9 523 /* Determine best guessed gesture and clean up */
kbhagat6 5:3eb4f3091bd8 524 wait(FIFO_PAUSE_TIME);
kbhagat6 4:84545b0e63a9 525 decodeGesture();
kbhagat6 4:84545b0e63a9 526 motion = gesture_motion_;
kbhagat6 4:84545b0e63a9 527 #if DEBUG
kbhagat6 5:3eb4f3091bd8 528 // Serial.print("END: ");
kbhagat6 5:3eb4f3091bd8 529 // Serial.println(gesture_motion_);
kbhagat6 4:84545b0e63a9 530 #endif
kbhagat6 4:84545b0e63a9 531 resetGestureParameters();
kbhagat6 4:84545b0e63a9 532 return motion;
kbhagat6 4:84545b0e63a9 533 }
kbhagat6 4:84545b0e63a9 534 }
kbhagat6 9:286d00d9db80 535 // delete fptr;
kbhagat6 4:84545b0e63a9 536 }
kbhagat6 6:687dff16904e 537 /**
kbhagat6 6:687dff16904e 538 * Turn the APDS-9960 on
kbhagat6 6:687dff16904e 539 *
kbhagat6 6:687dff16904e 540 * @return True if operation successful. False otherwise.
kbhagat6 6:687dff16904e 541 */
kbhagat6 6:687dff16904e 542 bool glibr::enablePower()
kbhagat6 6:687dff16904e 543 {
kbhagat6 6:687dff16904e 544 if( !setMode(POWER, 1) ) {
kbhagat6 6:687dff16904e 545 return false;
kbhagat6 6:687dff16904e 546 }
kbhagat6 6:687dff16904e 547
kbhagat6 6:687dff16904e 548 return true;
kbhagat6 6:687dff16904e 549 }
kbhagat6 6:687dff16904e 550
kbhagat6 6:687dff16904e 551 /**
kbhagat6 6:687dff16904e 552 * Turn the APDS-9960 off
kbhagat6 6:687dff16904e 553 *
kbhagat6 6:687dff16904e 554 * @return True if operation successful. False otherwise.
kbhagat6 6:687dff16904e 555 */
kbhagat6 6:687dff16904e 556 bool glibr::disablePower()
kbhagat6 6:687dff16904e 557 {
kbhagat6 6:687dff16904e 558 if( !setMode(POWER, 0) ) {
kbhagat6 6:687dff16904e 559 return false;
kbhagat6 6:687dff16904e 560 }
kbhagat6 6:687dff16904e 561
kbhagat6 6:687dff16904e 562 return true;
kbhagat6 6:687dff16904e 563 }
kbhagat6 6:687dff16904e 564
kbhagat6 6:687dff16904e 565 /*******************************************************************************
kbhagat6 6:687dff16904e 566 * Ambient light and color sensor controls
kbhagat6 6:687dff16904e 567 ******************************************************************************/
kbhagat6 6:687dff16904e 568
kbhagat6 6:687dff16904e 569 /**
kbhagat6 6:687dff16904e 570 * @brief Reads the ambient (clear) light level as a 16-bit value
kbhagat6 6:687dff16904e 571 *
kbhagat6 6:687dff16904e 572 * @param[out] val value of the light sensor.
kbhagat6 6:687dff16904e 573 * @return True if operation successful. False otherwise.
kbhagat6 6:687dff16904e 574 */
kbhagat6 6:687dff16904e 575 bool glibr::readAmbientLight(uint16_t &val)
kbhagat6 6:687dff16904e 576 {
kbhagat6 6:687dff16904e 577 uint8_t val_byte;
kbhagat6 6:687dff16904e 578 val = 0;
kbhagat6 6:687dff16904e 579
kbhagat6 6:687dff16904e 580 /* Read value from clear channel, low byte register */
kbhagat6 6:687dff16904e 581 val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CDATAL);
kbhagat6 8:6fa15d4e31fb 582 if( val_byte==ERROR) {
kbhagat6 6:687dff16904e 583 return false;
kbhagat6 6:687dff16904e 584 }
kbhagat6 6:687dff16904e 585 val = val_byte;
kbhagat6 6:687dff16904e 586
kbhagat6 6:687dff16904e 587 /* Read value from clear channel, high byte register */
kbhagat6 6:687dff16904e 588
kbhagat6 6:687dff16904e 589 val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CDATAH);
kbhagat6 8:6fa15d4e31fb 590 if( val_byte==ERROR) {
kbhagat6 6:687dff16904e 591 return false;
kbhagat6 6:687dff16904e 592 }
kbhagat6 6:687dff16904e 593 val = val + ((uint16_t)val_byte << 8);
kbhagat6 6:687dff16904e 594
kbhagat6 6:687dff16904e 595 return true;
kbhagat6 6:687dff16904e 596 }
kbhagat6 6:687dff16904e 597
kbhagat6 6:687dff16904e 598 /**
kbhagat6 6:687dff16904e 599 * @brief Reads the red light level as a 16-bit value
kbhagat6 6:687dff16904e 600 *
kbhagat6 6:687dff16904e 601 * @param[out] val value of the light sensor.
kbhagat6 6:687dff16904e 602 * @return True if operation successful. False otherwise.
kbhagat6 6:687dff16904e 603 */
kbhagat6 6:687dff16904e 604 bool glibr::readRedLight(uint16_t &val)
kbhagat6 6:687dff16904e 605 {
kbhagat6 6:687dff16904e 606 uint8_t val_byte;
kbhagat6 6:687dff16904e 607 val = 0;
kbhagat6 6:687dff16904e 608
kbhagat6 6:687dff16904e 609 /* Read value from clear channel, low byte register */
kbhagat6 6:687dff16904e 610 val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_RDATAL);
kbhagat6 8:6fa15d4e31fb 611 if( val_byte==ERROR) {
kbhagat6 6:687dff16904e 612 return false;
kbhagat6 6:687dff16904e 613 }
kbhagat6 6:687dff16904e 614
kbhagat6 6:687dff16904e 615 val = val_byte;
kbhagat6 6:687dff16904e 616
kbhagat6 6:687dff16904e 617 /* Read value from clear channel, high byte register */
kbhagat6 6:687dff16904e 618 val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_RDATAH);
kbhagat6 8:6fa15d4e31fb 619 if( val_byte==ERROR) {
kbhagat6 6:687dff16904e 620 return false;
kbhagat6 6:687dff16904e 621 }
kbhagat6 6:687dff16904e 622 val = val + ((uint16_t)val_byte << 8);
kbhagat6 6:687dff16904e 623
kbhagat6 6:687dff16904e 624 return true;
kbhagat6 6:687dff16904e 625 }
kbhagat6 6:687dff16904e 626
kbhagat6 6:687dff16904e 627 /**
kbhagat6 6:687dff16904e 628 * @brief Reads the green light level as a 16-bit value
kbhagat6 6:687dff16904e 629 *
kbhagat6 6:687dff16904e 630 * @param[out] val value of the light sensor.
kbhagat6 6:687dff16904e 631 * @return True if operation successful. False otherwise.
kbhagat6 6:687dff16904e 632 */
kbhagat6 6:687dff16904e 633
kbhagat6 6:687dff16904e 634 bool glibr::readGreenLight(uint16_t &val)
kbhagat6 6:687dff16904e 635 {
kbhagat6 6:687dff16904e 636 uint8_t val_byte;
kbhagat6 6:687dff16904e 637 val = 0;
kbhagat6 6:687dff16904e 638
kbhagat6 6:687dff16904e 639 /* Read value from clear channel, low byte register */
kbhagat6 6:687dff16904e 640 val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_GDATAL);
kbhagat6 8:6fa15d4e31fb 641 if( val_byte==ERROR) {
kbhagat6 6:687dff16904e 642 return false;
kbhagat6 6:687dff16904e 643 }
kbhagat6 6:687dff16904e 644
kbhagat6 6:687dff16904e 645 val = val_byte;
kbhagat6 6:687dff16904e 646
kbhagat6 6:687dff16904e 647 /* Read value from clear channel, high byte register */
kbhagat6 6:687dff16904e 648 val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_GDATAH);
kbhagat6 8:6fa15d4e31fb 649 if( val_byte==ERROR) {
kbhagat6 6:687dff16904e 650 return false;
kbhagat6 6:687dff16904e 651 }
kbhagat6 6:687dff16904e 652 val = val + ((uint16_t)val_byte << 8);
kbhagat6 6:687dff16904e 653
kbhagat6 6:687dff16904e 654 return true;
kbhagat6 6:687dff16904e 655 }
kbhagat6 6:687dff16904e 656
kbhagat6 6:687dff16904e 657 /**
kbhagat6 6:687dff16904e 658 * @brief Reads the red light level as a 16-bit value
kbhagat6 6:687dff16904e 659 *
kbhagat6 6:687dff16904e 660 * @param[out] val value of the light sensor.
kbhagat6 6:687dff16904e 661 * @return True if operation successful. False otherwise.
kbhagat6 6:687dff16904e 662 */
kbhagat6 6:687dff16904e 663
kbhagat6 6:687dff16904e 664 bool glibr::readBlueLight(uint16_t &val)
kbhagat6 6:687dff16904e 665 {
kbhagat6 6:687dff16904e 666 uint8_t val_byte;
kbhagat6 6:687dff16904e 667 val = 0;
kbhagat6 6:687dff16904e 668
kbhagat6 6:687dff16904e 669 /* Read value from clear channel, low byte register */
kbhagat6 6:687dff16904e 670 val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_BDATAL);
kbhagat6 8:6fa15d4e31fb 671 if( val_byte==ERROR) {
kbhagat6 6:687dff16904e 672 return false;
kbhagat6 6:687dff16904e 673 }
kbhagat6 6:687dff16904e 674
kbhagat6 6:687dff16904e 675 val = val_byte;
kbhagat6 6:687dff16904e 676
kbhagat6 6:687dff16904e 677 /* Read value from clear channel, high byte register */
kbhagat6 6:687dff16904e 678 val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_BDATAH);
kbhagat6 8:6fa15d4e31fb 679 if( val_byte==ERROR) {
kbhagat6 6:687dff16904e 680 return false;
kbhagat6 6:687dff16904e 681 }
kbhagat6 6:687dff16904e 682 val = val + ((uint16_t)val_byte << 8);
kbhagat6 6:687dff16904e 683
kbhagat6 6:687dff16904e 684 return true;
kbhagat6 6:687dff16904e 685 }
kbhagat6 6:687dff16904e 686
kbhagat6 6:687dff16904e 687 /*******************************************************************************
kbhagat6 6:687dff16904e 688 * Proximity sensor controls
kbhagat6 6:687dff16904e 689 ******************************************************************************/
kbhagat6 6:687dff16904e 690
kbhagat6 6:687dff16904e 691 /**
kbhagat6 6:687dff16904e 692 * @brief Reads the proximity level as an 8-bit value
kbhagat6 6:687dff16904e 693 *
kbhagat6 6:687dff16904e 694 * @param[out] val value of the proximity sensor.
kbhagat6 6:687dff16904e 695 * @return True if operation successful. False otherwise.
kbhagat6 6:687dff16904e 696 */
kbhagat6 6:687dff16904e 697 bool glibr::readProximity(uint8_t &val)
kbhagat6 6:687dff16904e 698 {
kbhagat6 6:687dff16904e 699 val = 0;
kbhagat6 6:687dff16904e 700
kbhagat6 6:687dff16904e 701 /* Read value from proximity data register */
kbhagat6 6:687dff16904e 702 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_PDATA);
kbhagat6 6:687dff16904e 703
kbhagat6 6:687dff16904e 704 if(val==ERROR){
kbhagat6 6:687dff16904e 705 return false;
kbhagat6 6:687dff16904e 706 }
kbhagat6 6:687dff16904e 707
kbhagat6 6:687dff16904e 708 return true;
kbhagat6 6:687dff16904e 709 }
kbhagat6 6:687dff16904e 710
kbhagat6 6:687dff16904e 711 /*******************************************************************************
kbhagat6 6:687dff16904e 712 * High-level gesture controls
kbhagat6 6:687dff16904e 713 ******************************************************************************/
kbhagat6 6:687dff16904e 714
kbhagat6 6:687dff16904e 715 /**
kbhagat6 6:687dff16904e 716 * @brief Resets all the parameters in the gesture data member
kbhagat6 6:687dff16904e 717 */
kbhagat6 6:687dff16904e 718 void glibr::resetGestureParameters()
kbhagat6 6:687dff16904e 719 {
kbhagat6 8:6fa15d4e31fb 720 gesture_data_.sindex = 0;
kbhagat6 6:687dff16904e 721 gesture_data_.total_gestures = 0;
kbhagat6 6:687dff16904e 722
kbhagat6 6:687dff16904e 723 gesture_ud_delta_ = 0;
kbhagat6 6:687dff16904e 724 gesture_lr_delta_ = 0;
kbhagat6 6:687dff16904e 725
kbhagat6 6:687dff16904e 726 gesture_ud_count_ = 0;
kbhagat6 6:687dff16904e 727 gesture_lr_count_ = 0;
kbhagat6 6:687dff16904e 728
kbhagat6 6:687dff16904e 729 gesture_near_count_ = 0;
kbhagat6 6:687dff16904e 730 gesture_far_count_ = 0;
kbhagat6 6:687dff16904e 731
kbhagat6 6:687dff16904e 732 gesture_state_ = 0;
kbhagat6 6:687dff16904e 733 gesture_motion_ = DIR_NONE;
kbhagat6 6:687dff16904e 734 }
kbhagat6 6:687dff16904e 735
kbhagat6 7:0564c6faf8aa 736 bool glibr::processGestureData()
kbhagat6 7:0564c6faf8aa 737 {
kbhagat6 7:0564c6faf8aa 738 uint8_t u_first = 0;
kbhagat6 7:0564c6faf8aa 739 uint8_t d_first = 0;
kbhagat6 7:0564c6faf8aa 740 uint8_t l_first = 0;
kbhagat6 7:0564c6faf8aa 741 uint8_t r_first = 0;
kbhagat6 7:0564c6faf8aa 742 uint8_t u_last = 0;
kbhagat6 7:0564c6faf8aa 743 uint8_t d_last = 0;
kbhagat6 7:0564c6faf8aa 744 uint8_t l_last = 0;
kbhagat6 7:0564c6faf8aa 745 uint8_t r_last = 0;
kbhagat6 7:0564c6faf8aa 746 int ud_ratio_first;
kbhagat6 7:0564c6faf8aa 747 int lr_ratio_first;
kbhagat6 7:0564c6faf8aa 748 int ud_ratio_last;
kbhagat6 7:0564c6faf8aa 749 int lr_ratio_last;
kbhagat6 7:0564c6faf8aa 750 int ud_delta;
kbhagat6 7:0564c6faf8aa 751 int lr_delta;
kbhagat6 7:0564c6faf8aa 752 int i;
kbhagat6 7:0564c6faf8aa 753
kbhagat6 7:0564c6faf8aa 754 /* If we have less than 4 total gestures, that's not enough */
kbhagat6 7:0564c6faf8aa 755 if( gesture_data_.total_gestures <= 4 ) {
kbhagat6 7:0564c6faf8aa 756 return false;
kbhagat6 7:0564c6faf8aa 757 }
kbhagat6 7:0564c6faf8aa 758
kbhagat6 7:0564c6faf8aa 759 /* Check to make sure our data isn't out of bounds */
kbhagat6 7:0564c6faf8aa 760 if( (gesture_data_.total_gestures <= 32) && \
kbhagat6 7:0564c6faf8aa 761 (gesture_data_.total_gestures > 0) ) {
kbhagat6 7:0564c6faf8aa 762
kbhagat6 7:0564c6faf8aa 763 /* Find the first value in U/D/L/R above the threshold */
kbhagat6 7:0564c6faf8aa 764 for( i = 0; i < gesture_data_.total_gestures; i++ ) {
kbhagat6 7:0564c6faf8aa 765 if( (gesture_data_.u_data[i] > GESTURE_THRESHOLD_OUT) &&
kbhagat6 7:0564c6faf8aa 766 (gesture_data_.d_data[i] > GESTURE_THRESHOLD_OUT) &&
kbhagat6 7:0564c6faf8aa 767 (gesture_data_.l_data[i] > GESTURE_THRESHOLD_OUT) &&
kbhagat6 7:0564c6faf8aa 768 (gesture_data_.r_data[i] > GESTURE_THRESHOLD_OUT) ) {
kbhagat6 7:0564c6faf8aa 769
kbhagat6 7:0564c6faf8aa 770 u_first = gesture_data_.u_data[i];
kbhagat6 7:0564c6faf8aa 771 d_first = gesture_data_.d_data[i];
kbhagat6 7:0564c6faf8aa 772 l_first = gesture_data_.l_data[i];
kbhagat6 7:0564c6faf8aa 773 r_first = gesture_data_.r_data[i];
kbhagat6 7:0564c6faf8aa 774 break;
kbhagat6 7:0564c6faf8aa 775 }
kbhagat6 7:0564c6faf8aa 776 }
kbhagat6 7:0564c6faf8aa 777
kbhagat6 7:0564c6faf8aa 778 /* If one of the _first values is 0, then there is no good data */
kbhagat6 7:0564c6faf8aa 779 if( (u_first == 0) || (d_first == 0) || \
kbhagat6 7:0564c6faf8aa 780 (l_first == 0) || (r_first == 0) ) {
kbhagat6 7:0564c6faf8aa 781
kbhagat6 7:0564c6faf8aa 782 return false;
kbhagat6 7:0564c6faf8aa 783 }
kbhagat6 7:0564c6faf8aa 784 /* Find the last value in U/D/L/R above the threshold */
kbhagat6 7:0564c6faf8aa 785 for( i = gesture_data_.total_gestures - 1; i >= 0; i-- ) {
kbhagat6 7:0564c6faf8aa 786 /* #if DEBUG
kbhagat6 7:0564c6faf8aa 787 Serial.print(F("Finding last: "));
kbhagat6 7:0564c6faf8aa 788 Serial.print(F("U:"));
kbhagat6 7:0564c6faf8aa 789 Serial.print(gesture_data_.u_data[i]);
kbhagat6 7:0564c6faf8aa 790 Serial.print(F(" D:"));
kbhagat6 7:0564c6faf8aa 791 Serial.print(gesture_data_.d_data[i]);
kbhagat6 7:0564c6faf8aa 792 Serial.print(F(" L:"));
kbhagat6 7:0564c6faf8aa 793 Serial.print(gesture_data_.l_data[i]);
kbhagat6 7:0564c6faf8aa 794 Serial.print(F(" R:"));
kbhagat6 7:0564c6faf8aa 795 Serial.println(gesture_data_.r_data[i]);
kbhagat6 7:0564c6faf8aa 796 #endif */
kbhagat6 7:0564c6faf8aa 797 if( (gesture_data_.u_data[i] > GESTURE_THRESHOLD_OUT) &&
kbhagat6 7:0564c6faf8aa 798 (gesture_data_.d_data[i] > GESTURE_THRESHOLD_OUT) &&
kbhagat6 7:0564c6faf8aa 799 (gesture_data_.l_data[i] > GESTURE_THRESHOLD_OUT) &&
kbhagat6 7:0564c6faf8aa 800 (gesture_data_.r_data[i] > GESTURE_THRESHOLD_OUT) ) {
kbhagat6 7:0564c6faf8aa 801
kbhagat6 7:0564c6faf8aa 802 u_last = gesture_data_.u_data[i];
kbhagat6 7:0564c6faf8aa 803 d_last = gesture_data_.d_data[i];
kbhagat6 7:0564c6faf8aa 804 l_last = gesture_data_.l_data[i];
kbhagat6 7:0564c6faf8aa 805 r_last = gesture_data_.r_data[i];
kbhagat6 7:0564c6faf8aa 806 break;
kbhagat6 7:0564c6faf8aa 807 }
kbhagat6 7:0564c6faf8aa 808 }
kbhagat6 7:0564c6faf8aa 809 }
kbhagat6 7:0564c6faf8aa 810
kbhagat6 7:0564c6faf8aa 811 /* Calculate the first vs. last ratio of up/down and left/right */
kbhagat6 7:0564c6faf8aa 812 ud_ratio_first = ((u_first - d_first) * 100) / (u_first + d_first);
kbhagat6 7:0564c6faf8aa 813 lr_ratio_first = ((l_first - r_first) * 100) / (l_first + r_first);
kbhagat6 7:0564c6faf8aa 814 ud_ratio_last = ((u_last - d_last) * 100) / (u_last + d_last);
kbhagat6 7:0564c6faf8aa 815 lr_ratio_last = ((l_last - r_last) * 100) / (l_last + r_last);
kbhagat6 7:0564c6faf8aa 816
kbhagat6 7:0564c6faf8aa 817 /* #if DEBUG
kbhagat6 7:0564c6faf8aa 818 Serial.print(F("Last Values: "));
kbhagat6 7:0564c6faf8aa 819 Serial.print(F("U:"));
kbhagat6 7:0564c6faf8aa 820 Serial.print(u_last);
kbhagat6 7:0564c6faf8aa 821 Serial.print(F(" D:"));
kbhagat6 7:0564c6faf8aa 822 Serial.print(d_last);
kbhagat6 7:0564c6faf8aa 823 Serial.print(F(" L:"));
kbhagat6 7:0564c6faf8aa 824 Serial.print(l_last);
kbhagat6 7:0564c6faf8aa 825 Serial.print(F(" R:"));
kbhagat6 7:0564c6faf8aa 826 Serial.println(r_last);
kbhagat6 7:0564c6faf8aa 827
kbhagat6 7:0564c6faf8aa 828 Serial.print(F("Ratios: "));
kbhagat6 7:0564c6faf8aa 829 Serial.print(F("UD Fi: "));
kbhagat6 7:0564c6faf8aa 830 Serial.print(ud_ratio_first);
kbhagat6 7:0564c6faf8aa 831 Serial.print(F(" UD La: "));
kbhagat6 7:0564c6faf8aa 832 Serial.print(ud_ratio_last);
kbhagat6 7:0564c6faf8aa 833 Serial.print(F(" LR Fi: "));
kbhagat6 7:0564c6faf8aa 834 Serial.print(lr_ratio_first);
kbhagat6 7:0564c6faf8aa 835 Serial.print(F(" LR La: "));
kbhagat6 7:0564c6faf8aa 836 Serial.println(lr_ratio_last);
kbhagat6 7:0564c6faf8aa 837 #endif */
kbhagat6 7:0564c6faf8aa 838
kbhagat6 7:0564c6faf8aa 839 /* Determine the difference between the first and last ratios */
kbhagat6 7:0564c6faf8aa 840 ud_delta = ud_ratio_last - ud_ratio_first;
kbhagat6 7:0564c6faf8aa 841 lr_delta = lr_ratio_last - lr_ratio_first;
kbhagat6 7:0564c6faf8aa 842
kbhagat6 7:0564c6faf8aa 843 /* #if DEBUG
kbhagat6 7:0564c6faf8aa 844 Serial.print("Deltas: ");
kbhagat6 7:0564c6faf8aa 845 Serial.print("UD: ");
kbhagat6 7:0564c6faf8aa 846 Serial.print(ud_delta);
kbhagat6 7:0564c6faf8aa 847 Serial.print(" LR: ");
kbhagat6 7:0564c6faf8aa 848 Serial.println(lr_delta);
kbhagat6 7:0564c6faf8aa 849 #endif */
kbhagat6 7:0564c6faf8aa 850
kbhagat6 7:0564c6faf8aa 851 /* Accumulate the UD and LR delta values */
kbhagat6 7:0564c6faf8aa 852 gesture_ud_delta_ += ud_delta;
kbhagat6 7:0564c6faf8aa 853 gesture_lr_delta_ += lr_delta;
kbhagat6 7:0564c6faf8aa 854
kbhagat6 7:0564c6faf8aa 855 /* #if DEBUG
kbhagat6 7:0564c6faf8aa 856 Serial.print("Accumulations: ");
kbhagat6 7:0564c6faf8aa 857 Serial.print("UD: ");
kbhagat6 7:0564c6faf8aa 858 Serial.print(gesture_ud_delta_);
kbhagat6 7:0564c6faf8aa 859 Serial.print(" LR: ");
kbhagat6 7:0564c6faf8aa 860 Serial.println(gesture_lr_delta_);
kbhagat6 7:0564c6faf8aa 861 #endif */
kbhagat6 7:0564c6faf8aa 862
kbhagat6 7:0564c6faf8aa 863 /* Determine U/D gesture */
kbhagat6 7:0564c6faf8aa 864 if( gesture_ud_delta_ >= GESTURE_SENSITIVITY_1 ) {
kbhagat6 7:0564c6faf8aa 865 gesture_ud_count_ = 1;
kbhagat6 7:0564c6faf8aa 866 } else if( gesture_ud_delta_ <= -GESTURE_SENSITIVITY_1 ) {
kbhagat6 7:0564c6faf8aa 867 gesture_ud_count_ = -1;
kbhagat6 7:0564c6faf8aa 868 } else {
kbhagat6 7:0564c6faf8aa 869 gesture_ud_count_ = 0;
kbhagat6 7:0564c6faf8aa 870 }
kbhagat6 7:0564c6faf8aa 871
kbhagat6 7:0564c6faf8aa 872 /* Determine L/R gesture */
kbhagat6 7:0564c6faf8aa 873 if( gesture_lr_delta_ >= GESTURE_SENSITIVITY_1 ) {
kbhagat6 7:0564c6faf8aa 874 gesture_lr_count_ = 1;
kbhagat6 7:0564c6faf8aa 875 } else if( gesture_lr_delta_ <= -GESTURE_SENSITIVITY_1 ) {
kbhagat6 7:0564c6faf8aa 876 gesture_lr_count_ = -1;
kbhagat6 7:0564c6faf8aa 877 } else {
kbhagat6 7:0564c6faf8aa 878 gesture_lr_count_ = 0;
kbhagat6 7:0564c6faf8aa 879 }
kbhagat6 7:0564c6faf8aa 880
kbhagat6 7:0564c6faf8aa 881 /* Determine Near/Far gesture */
kbhagat6 7:0564c6faf8aa 882 if( (gesture_ud_count_ == 0) && (gesture_lr_count_ == 0) ) {
kbhagat6 7:0564c6faf8aa 883 if( (abs(ud_delta) < GESTURE_SENSITIVITY_2) && \
kbhagat6 7:0564c6faf8aa 884 (abs(lr_delta) < GESTURE_SENSITIVITY_2) ) {
kbhagat6 7:0564c6faf8aa 885
kbhagat6 7:0564c6faf8aa 886 if( (ud_delta == 0) && (lr_delta == 0) ) {
kbhagat6 7:0564c6faf8aa 887 gesture_near_count_++;
kbhagat6 7:0564c6faf8aa 888 } else if( (ud_delta != 0) || (lr_delta != 0) ) {
kbhagat6 7:0564c6faf8aa 889 gesture_far_count_++;
kbhagat6 7:0564c6faf8aa 890 }
kbhagat6 7:0564c6faf8aa 891
kbhagat6 7:0564c6faf8aa 892 if( (gesture_near_count_ >= 10) && (gesture_far_count_ >= 2) ) {
kbhagat6 7:0564c6faf8aa 893 if( (ud_delta == 0) && (lr_delta == 0) ) {
kbhagat6 7:0564c6faf8aa 894 gesture_state_ = NEAR_STATE;
kbhagat6 7:0564c6faf8aa 895 } else if( (ud_delta != 0) && (lr_delta != 0) ) {
kbhagat6 7:0564c6faf8aa 896 gesture_state_ = FAR_STATE;
kbhagat6 7:0564c6faf8aa 897 }
kbhagat6 7:0564c6faf8aa 898 return true;
kbhagat6 7:0564c6faf8aa 899 }
kbhagat6 7:0564c6faf8aa 900 }
kbhagat6 7:0564c6faf8aa 901 } else {
kbhagat6 7:0564c6faf8aa 902 if( (abs(ud_delta) < GESTURE_SENSITIVITY_2) && \
kbhagat6 7:0564c6faf8aa 903 (abs(lr_delta) < GESTURE_SENSITIVITY_2) ) {
kbhagat6 7:0564c6faf8aa 904
kbhagat6 7:0564c6faf8aa 905 if( (ud_delta == 0) && (lr_delta == 0) ) {
kbhagat6 7:0564c6faf8aa 906 gesture_near_count_++;
kbhagat6 7:0564c6faf8aa 907 }
kbhagat6 7:0564c6faf8aa 908
kbhagat6 13:5930645dc5f0 909 if( gesture_near_count_ >= 5 ) {
kbhagat6 7:0564c6faf8aa 910 gesture_ud_count_ = 0;
kbhagat6 7:0564c6faf8aa 911 gesture_lr_count_ = 0;
kbhagat6 7:0564c6faf8aa 912 gesture_ud_delta_ = 0;
kbhagat6 7:0564c6faf8aa 913 gesture_lr_delta_ = 0;
kbhagat6 7:0564c6faf8aa 914 }
kbhagat6 7:0564c6faf8aa 915 }
kbhagat6 7:0564c6faf8aa 916 }
kbhagat6 7:0564c6faf8aa 917
kbhagat6 11:ea43b0b83222 918 // #if DEBUG
kbhagat6 13:5930645dc5f0 919 /* printf("UD_CT: %d\n",gesture_ud_count_);
kbhagat6 11:ea43b0b83222 920 printf("LR_CT: %d\n",gesture_lr_count_);
kbhagat6 11:ea43b0b83222 921 printf("NEAR_CT: %d\n",gesture_near_count_);
kbhagat6 11:ea43b0b83222 922 printf(" FAR_CT: %d\n",gesture_far_count_);
kbhagat6 13:5930645dc5f0 923 printf("----------"); */
kbhagat6 11:ea43b0b83222 924 //#endif */
kbhagat6 7:0564c6faf8aa 925
kbhagat6 7:0564c6faf8aa 926 return false;
kbhagat6 7:0564c6faf8aa 927 }
kbhagat6 7:0564c6faf8aa 928
kbhagat6 7:0564c6faf8aa 929 /**
kbhagat6 7:0564c6faf8aa 930 * @brief Determines swipe direction or near/far state
kbhagat6 7:0564c6faf8aa 931 *
kbhagat6 7:0564c6faf8aa 932 * @return True if near/far event. False otherwise.
kbhagat6 7:0564c6faf8aa 933 */
kbhagat6 8:6fa15d4e31fb 934 bool glibr::decodeGesture()
kbhagat6 7:0564c6faf8aa 935 {
kbhagat6 7:0564c6faf8aa 936 /* Return if near or far event is detected */
kbhagat6 7:0564c6faf8aa 937 if( gesture_state_ == NEAR_STATE ) {
kbhagat6 7:0564c6faf8aa 938 gesture_motion_ = DIR_NEAR;
kbhagat6 7:0564c6faf8aa 939 return true;
kbhagat6 7:0564c6faf8aa 940 } else if ( gesture_state_ == FAR_STATE ) {
kbhagat6 7:0564c6faf8aa 941 gesture_motion_ = DIR_FAR;
kbhagat6 7:0564c6faf8aa 942 return true;
kbhagat6 7:0564c6faf8aa 943 }
kbhagat6 7:0564c6faf8aa 944
kbhagat6 7:0564c6faf8aa 945 /* Determine swipe direction */
kbhagat6 7:0564c6faf8aa 946 if( (gesture_ud_count_ == -1) && (gesture_lr_count_ == 0) ) {
kbhagat6 7:0564c6faf8aa 947 gesture_motion_ = DIR_UP;
kbhagat6 7:0564c6faf8aa 948 } else if( (gesture_ud_count_ == 1) && (gesture_lr_count_ == 0) ) {
kbhagat6 7:0564c6faf8aa 949 gesture_motion_ = DIR_DOWN;
kbhagat6 7:0564c6faf8aa 950 } else if( (gesture_ud_count_ == 0) && (gesture_lr_count_ == 1) ) {
kbhagat6 7:0564c6faf8aa 951 gesture_motion_ = DIR_RIGHT;
kbhagat6 7:0564c6faf8aa 952 } else if( (gesture_ud_count_ == 0) && (gesture_lr_count_ == -1) ) {
kbhagat6 7:0564c6faf8aa 953 gesture_motion_ = DIR_LEFT;
kbhagat6 7:0564c6faf8aa 954 } else if( (gesture_ud_count_ == -1) && (gesture_lr_count_ == 1) ) {
kbhagat6 7:0564c6faf8aa 955 if( abs(gesture_ud_delta_) > abs(gesture_lr_delta_) ) {
kbhagat6 7:0564c6faf8aa 956 gesture_motion_ = DIR_UP;
kbhagat6 7:0564c6faf8aa 957 } else {
kbhagat6 7:0564c6faf8aa 958 gesture_motion_ = DIR_RIGHT;
kbhagat6 7:0564c6faf8aa 959 }
kbhagat6 7:0564c6faf8aa 960 } else if( (gesture_ud_count_ == 1) && (gesture_lr_count_ == -1) ) {
kbhagat6 7:0564c6faf8aa 961 if( abs(gesture_ud_delta_) > abs(gesture_lr_delta_) ) {
kbhagat6 7:0564c6faf8aa 962 gesture_motion_ = DIR_DOWN;
kbhagat6 7:0564c6faf8aa 963 } else {
kbhagat6 7:0564c6faf8aa 964 gesture_motion_ = DIR_LEFT;
kbhagat6 7:0564c6faf8aa 965 }
kbhagat6 7:0564c6faf8aa 966 } else if( (gesture_ud_count_ == -1) && (gesture_lr_count_ == -1) ) {
kbhagat6 7:0564c6faf8aa 967 if( abs(gesture_ud_delta_) > abs(gesture_lr_delta_) ) {
kbhagat6 7:0564c6faf8aa 968 gesture_motion_ = DIR_UP;
kbhagat6 7:0564c6faf8aa 969 } else {
kbhagat6 7:0564c6faf8aa 970 gesture_motion_ = DIR_LEFT;
kbhagat6 7:0564c6faf8aa 971 }
kbhagat6 7:0564c6faf8aa 972 } else if( (gesture_ud_count_ == 1) && (gesture_lr_count_ == 1) ) {
kbhagat6 7:0564c6faf8aa 973 if( abs(gesture_ud_delta_) > abs(gesture_lr_delta_) ) {
kbhagat6 7:0564c6faf8aa 974 gesture_motion_ = DIR_DOWN;
kbhagat6 7:0564c6faf8aa 975 } else {
kbhagat6 7:0564c6faf8aa 976 gesture_motion_ = DIR_RIGHT;
kbhagat6 7:0564c6faf8aa 977 }
kbhagat6 7:0564c6faf8aa 978 } else {
kbhagat6 7:0564c6faf8aa 979 return false;
kbhagat6 7:0564c6faf8aa 980 }
kbhagat6 7:0564c6faf8aa 981
kbhagat6 7:0564c6faf8aa 982 return true;
kbhagat6 7:0564c6faf8aa 983 }
kbhagat6 7:0564c6faf8aa 984
kbhagat6 7:0564c6faf8aa 985 /*******************************************************************************
kbhagat6 7:0564c6faf8aa 986 * Getters and setters for register values
kbhagat6 7:0564c6faf8aa 987 ******************************************************************************/
kbhagat6 7:0564c6faf8aa 988
kbhagat6 7:0564c6faf8aa 989 /**
kbhagat6 7:0564c6faf8aa 990 * @brief Returns the lower threshold for proximity detection
kbhagat6 7:0564c6faf8aa 991 *
kbhagat6 7:0564c6faf8aa 992 * @return lower threshold
kbhagat6 7:0564c6faf8aa 993 */
kbhagat6 7:0564c6faf8aa 994
kbhagat6 7:0564c6faf8aa 995 uint8_t glibr::getProxIntLowThresh()
kbhagat6 7:0564c6faf8aa 996 {
kbhagat6 7:0564c6faf8aa 997 uint8_t val;
kbhagat6 7:0564c6faf8aa 998
kbhagat6 7:0564c6faf8aa 999 /* Read value from PILT register */
kbhagat6 7:0564c6faf8aa 1000 /* if( !wireReadDataByte(APDS9960_PILT, val) ) {
kbhagat6 7:0564c6faf8aa 1001 val = 0;
kbhagat6 7:0564c6faf8aa 1002 }*/
kbhagat6 7:0564c6faf8aa 1003 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_PILT);
kbhagat6 7:0564c6faf8aa 1004 if(val==ERROR){
kbhagat6 7:0564c6faf8aa 1005 val=0;
kbhagat6 7:0564c6faf8aa 1006 }
kbhagat6 7:0564c6faf8aa 1007
kbhagat6 7:0564c6faf8aa 1008 return val;
kbhagat6 7:0564c6faf8aa 1009 }
kbhagat6 7:0564c6faf8aa 1010
kbhagat6 9:286d00d9db80 1011 /**
kbhagat6 9:286d00d9db80 1012 * @brief Sets the lower threshold for proximity detection
kbhagat6 9:286d00d9db80 1013 *
kbhagat6 9:286d00d9db80 1014 * @param[in] threshold the lower proximity threshold
kbhagat6 9:286d00d9db80 1015 * @return True if operation successful. False otherwise.
kbhagat6 9:286d00d9db80 1016 */
kbhagat6 7:0564c6faf8aa 1017 bool glibr::setProxIntLowThresh(uint8_t threshold)
kbhagat6 7:0564c6faf8aa 1018 {
kbhagat6 7:0564c6faf8aa 1019 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PILT, threshold) ) {
kbhagat6 7:0564c6faf8aa 1020 return false;
kbhagat6 7:0564c6faf8aa 1021 }
kbhagat6 7:0564c6faf8aa 1022
kbhagat6 7:0564c6faf8aa 1023 return true;
kbhagat6 7:0564c6faf8aa 1024 }
kbhagat6 7:0564c6faf8aa 1025
kbhagat6 9:286d00d9db80 1026 /**
kbhagat6 9:286d00d9db80 1027 * @brief Returns the high threshold for proximity detection
kbhagat6 9:286d00d9db80 1028 *
kbhagat6 9:286d00d9db80 1029 * @return high threshold
kbhagat6 9:286d00d9db80 1030 */
kbhagat6 9:286d00d9db80 1031 uint8_t glibr::getProxIntHighThresh()
kbhagat6 9:286d00d9db80 1032 {
kbhagat6 9:286d00d9db80 1033 uint8_t val;
kbhagat6 9:286d00d9db80 1034
kbhagat6 9:286d00d9db80 1035 /* Read value from PIHT register */
kbhagat6 9:286d00d9db80 1036 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_PILT);
kbhagat6 9:286d00d9db80 1037 if( val==ERROR ) {
kbhagat6 9:286d00d9db80 1038 val = 0;
kbhagat6 9:286d00d9db80 1039 }
kbhagat6 9:286d00d9db80 1040
kbhagat6 9:286d00d9db80 1041 return val;
kbhagat6 9:286d00d9db80 1042 }
kbhagat6 9:286d00d9db80 1043
kbhagat6 9:286d00d9db80 1044 /**
kbhagat6 9:286d00d9db80 1045 * @brief Sets the high threshold for proximity detection
kbhagat6 9:286d00d9db80 1046 *
kbhagat6 9:286d00d9db80 1047 * @param[in] threshold the high proximity threshold
kbhagat6 9:286d00d9db80 1048 * @return True if operation successful. False otherwise.
kbhagat6 9:286d00d9db80 1049 */
kbhagat6 7:0564c6faf8aa 1050 bool glibr::setProxIntHighThresh(uint8_t threshold)
kbhagat6 7:0564c6faf8aa 1051 {
kbhagat6 7:0564c6faf8aa 1052
kbhagat6 7:0564c6faf8aa 1053 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PIHT, threshold) ) {
kbhagat6 7:0564c6faf8aa 1054 return false;
kbhagat6 7:0564c6faf8aa 1055 }
kbhagat6 7:0564c6faf8aa 1056
kbhagat6 7:0564c6faf8aa 1057 return true;
kbhagat6 7:0564c6faf8aa 1058 }
kbhagat6 7:0564c6faf8aa 1059
kbhagat6 9:286d00d9db80 1060 /**
kbhagat6 9:286d00d9db80 1061 * @brief Returns LED drive strength for proximity and ALS
kbhagat6 9:286d00d9db80 1062 *
kbhagat6 9:286d00d9db80 1063 * Value LED Current
kbhagat6 9:286d00d9db80 1064 * 0 100 mA
kbhagat6 9:286d00d9db80 1065 * 1 50 mA
kbhagat6 9:286d00d9db80 1066 * 2 25 mA
kbhagat6 9:286d00d9db80 1067 * 3 12.5 mA
kbhagat6 9:286d00d9db80 1068 *
kbhagat6 9:286d00d9db80 1069 * @return the value of the LED drive strength. 0xFF on failure.
kbhagat6 9:286d00d9db80 1070 */
kbhagat6 9:286d00d9db80 1071 uint8_t glibr::getLEDDrive()
kbhagat6 9:286d00d9db80 1072 {
kbhagat6 9:286d00d9db80 1073 uint8_t val;
kbhagat6 9:286d00d9db80 1074
kbhagat6 9:286d00d9db80 1075 /* Read value from CONTROL register */
kbhagat6 9:286d00d9db80 1076 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL);
kbhagat6 9:286d00d9db80 1077 if( val == ERROR ){//!wireReadDataByte(APDS9960_CONTROL, val) ) {
kbhagat6 9:286d00d9db80 1078 return ERROR;
kbhagat6 9:286d00d9db80 1079 }
kbhagat6 9:286d00d9db80 1080
kbhagat6 9:286d00d9db80 1081 /* Shift and mask out LED drive bits */
kbhagat6 9:286d00d9db80 1082 val = (val >> 6) & 0x03;//0b00000011;
kbhagat6 9:286d00d9db80 1083
kbhagat6 9:286d00d9db80 1084 return val;
kbhagat6 9:286d00d9db80 1085 }
kbhagat6 7:0564c6faf8aa 1086
kbhagat6 9:286d00d9db80 1087 /**
kbhagat6 9:286d00d9db80 1088 * @brief Sets the LED drive strength for proximity and ALS
kbhagat6 9:286d00d9db80 1089 *
kbhagat6 9:286d00d9db80 1090 * Value LED Current
kbhagat6 9:286d00d9db80 1091 * 0 100 mA
kbhagat6 9:286d00d9db80 1092 * 1 50 mA
kbhagat6 9:286d00d9db80 1093 * 2 25 mA
kbhagat6 9:286d00d9db80 1094 * 3 12.5 mA
kbhagat6 9:286d00d9db80 1095 *
kbhagat6 9:286d00d9db80 1096 * @param[in] drive the value (0-3) for the LED drive strength
kbhagat6 9:286d00d9db80 1097 * @return True if operation successful. False otherwise.
kbhagat6 9:286d00d9db80 1098 */
kbhagat6 7:0564c6faf8aa 1099
kbhagat6 7:0564c6faf8aa 1100 bool glibr::setLEDDrive(uint8_t drive)
kbhagat6 7:0564c6faf8aa 1101 {
kbhagat6 7:0564c6faf8aa 1102 uint8_t val;
kbhagat6 7:0564c6faf8aa 1103
kbhagat6 7:0564c6faf8aa 1104 /* Read value from CONTROL register */
kbhagat6 7:0564c6faf8aa 1105 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL);
kbhagat6 7:0564c6faf8aa 1106 if(val==ERROR){
kbhagat6 7:0564c6faf8aa 1107 return false;
kbhagat6 7:0564c6faf8aa 1108 }
kbhagat6 7:0564c6faf8aa 1109 /* Set bits in register to given value */
kbhagat6 9:286d00d9db80 1110 //drive &= 0b00000011
kbhagat6 7:0564c6faf8aa 1111 drive &= 0x03;
kbhagat6 7:0564c6faf8aa 1112 drive = drive << 6;
kbhagat6 7:0564c6faf8aa 1113 //val &= 0b00111111;
kbhagat6 7:0564c6faf8aa 1114 val &= 0x3F;
kbhagat6 7:0564c6faf8aa 1115 val |= drive;
kbhagat6 7:0564c6faf8aa 1116
kbhagat6 7:0564c6faf8aa 1117 /* Write register value back into CONTROL register */
kbhagat6 7:0564c6faf8aa 1118 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONTROL, val) ) {
kbhagat6 7:0564c6faf8aa 1119 return false;
kbhagat6 7:0564c6faf8aa 1120 }
kbhagat6 7:0564c6faf8aa 1121
kbhagat6 7:0564c6faf8aa 1122 return true;
kbhagat6 7:0564c6faf8aa 1123 }
kbhagat6 7:0564c6faf8aa 1124
kbhagat6 9:286d00d9db80 1125 /**
kbhagat6 9:286d00d9db80 1126 * @brief Returns receiver gain for proximity detection
kbhagat6 9:286d00d9db80 1127 *
kbhagat6 9:286d00d9db80 1128 * Value Gain
kbhagat6 9:286d00d9db80 1129 * 0 1x
kbhagat6 9:286d00d9db80 1130 * 1 2x
kbhagat6 9:286d00d9db80 1131 * 2 4x
kbhagat6 9:286d00d9db80 1132 * 3 8x
kbhagat6 9:286d00d9db80 1133 *
kbhagat6 9:286d00d9db80 1134 * @return the value of the proximity gain. 0xFF on failure.
kbhagat6 9:286d00d9db80 1135 */
kbhagat6 9:286d00d9db80 1136 uint8_t glibr::getProximityGain()
kbhagat6 9:286d00d9db80 1137 {
kbhagat6 9:286d00d9db80 1138 uint8_t val;
kbhagat6 9:286d00d9db80 1139
kbhagat6 9:286d00d9db80 1140 /* Read value from CONTROL register */
kbhagat6 9:286d00d9db80 1141 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL);
kbhagat6 9:286d00d9db80 1142 if( val == ERROR){//!wireReadDataByte(APDS9960_CONTROL, val) ) {
kbhagat6 9:286d00d9db80 1143 return ERROR;
kbhagat6 9:286d00d9db80 1144 }
kbhagat6 9:286d00d9db80 1145
kbhagat6 9:286d00d9db80 1146 /* Shift and mask out PDRIVE bits */
kbhagat6 9:286d00d9db80 1147 val = (val >> 2) & 0x03;//0b00000011;
kbhagat6 9:286d00d9db80 1148
kbhagat6 9:286d00d9db80 1149 return val;
kbhagat6 9:286d00d9db80 1150 }
kbhagat6 6:687dff16904e 1151
kbhagat6 9:286d00d9db80 1152 /**
kbhagat6 9:286d00d9db80 1153 * @brief Sets the receiver gain for proximity detection
kbhagat6 9:286d00d9db80 1154 *
kbhagat6 9:286d00d9db80 1155 * Value Gain
kbhagat6 9:286d00d9db80 1156 * 0 1x
kbhagat6 9:286d00d9db80 1157 * 1 2x
kbhagat6 9:286d00d9db80 1158 * 2 4x
kbhagat6 9:286d00d9db80 1159 * 3 8x
kbhagat6 9:286d00d9db80 1160 *
kbhagat6 9:286d00d9db80 1161 * @param[in] drive the value (0-3) for the gain
kbhagat6 9:286d00d9db80 1162 * @return True if operation successful. False otherwise.
kbhagat6 9:286d00d9db80 1163 */
kbhagat6 0:437ae08befe3 1164 bool glibr::setProximityGain(uint8_t drive)
kbhagat6 0:437ae08befe3 1165 {
kbhagat6 0:437ae08befe3 1166 uint8_t val;
kbhagat6 0:437ae08befe3 1167
kbhagat6 0:437ae08befe3 1168 /* Read value from CONTROL register */
kbhagat6 1:c7215f5f9a72 1169
kbhagat6 5:3eb4f3091bd8 1170 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL);
kbhagat6 0:437ae08befe3 1171
kbhagat6 1:c7215f5f9a72 1172 if(val==ERROR){
kbhagat6 1:c7215f5f9a72 1173 return false;
kbhagat6 1:c7215f5f9a72 1174 }
kbhagat6 0:437ae08befe3 1175 /* Set bits in register to given value */
kbhagat6 1:c7215f5f9a72 1176 //drive &= 0b00000011;
kbhagat6 1:c7215f5f9a72 1177 drive &=0x03;
kbhagat6 0:437ae08befe3 1178 drive = drive << 2;
kbhagat6 9:286d00d9db80 1179 //val &= 0b11110011
kbhagat6 1:c7215f5f9a72 1180 val &= 0xF3;
kbhagat6 0:437ae08befe3 1181 val |= drive;
kbhagat6 0:437ae08befe3 1182
kbhagat6 0:437ae08befe3 1183 /* Write register value back into CONTROL register */
kbhagat6 1:c7215f5f9a72 1184 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONTROL, val) ) {
kbhagat6 1:c7215f5f9a72 1185 return false;
kbhagat6 1:c7215f5f9a72 1186 }
kbhagat6 1:c7215f5f9a72 1187 return true;
kbhagat6 1:c7215f5f9a72 1188 }
kbhagat6 1:c7215f5f9a72 1189
kbhagat6 9:286d00d9db80 1190 /**
kbhagat6 9:286d00d9db80 1191 * @brief Returns receiver gain for the ambient light sensor (ALS)
kbhagat6 9:286d00d9db80 1192 *
kbhagat6 9:286d00d9db80 1193 * Value Gain
kbhagat6 9:286d00d9db80 1194 * 0 1x
kbhagat6 9:286d00d9db80 1195 * 1 4x
kbhagat6 9:286d00d9db80 1196 * 2 16x
kbhagat6 9:286d00d9db80 1197 * 3 64x
kbhagat6 9:286d00d9db80 1198 *
kbhagat6 9:286d00d9db80 1199 * @return the value of the ALS gain. 0xFF on failure.
kbhagat6 9:286d00d9db80 1200 */
kbhagat6 9:286d00d9db80 1201 uint8_t glibr::getAmbientLightGain()
kbhagat6 9:286d00d9db80 1202 {
kbhagat6 9:286d00d9db80 1203 uint8_t val;
kbhagat6 9:286d00d9db80 1204
kbhagat6 9:286d00d9db80 1205 /* Read value from CONTROL register */
kbhagat6 9:286d00d9db80 1206 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL);
kbhagat6 9:286d00d9db80 1207 if( val == ERROR){//!wireReadDataByte(APDS9960_CONTROL, val) ) {
kbhagat6 9:286d00d9db80 1208 return ERROR;
kbhagat6 9:286d00d9db80 1209 }
kbhagat6 9:286d00d9db80 1210
kbhagat6 9:286d00d9db80 1211 /* Shift and mask out ADRIVE bits */
kbhagat6 9:286d00d9db80 1212 val &= 0x03;//0b00000011;
kbhagat6 9:286d00d9db80 1213
kbhagat6 9:286d00d9db80 1214 return val;
kbhagat6 9:286d00d9db80 1215 }
kbhagat6 1:c7215f5f9a72 1216
kbhagat6 9:286d00d9db80 1217 /**
kbhagat6 9:286d00d9db80 1218 * @brief Sets the receiver gain for the ambient light sensor (ALS)
kbhagat6 9:286d00d9db80 1219 *
kbhagat6 9:286d00d9db80 1220 * Value Gain
kbhagat6 9:286d00d9db80 1221 * 0 1x
kbhagat6 9:286d00d9db80 1222 * 1 4x
kbhagat6 9:286d00d9db80 1223 * 2 16x
kbhagat6 9:286d00d9db80 1224 * 3 64x
kbhagat6 9:286d00d9db80 1225 *
kbhagat6 9:286d00d9db80 1226 * @param[in] drive the value (0-3) for the gain
kbhagat6 9:286d00d9db80 1227 * @return True if operation successful. False otherwise.
kbhagat6 9:286d00d9db80 1228 */
kbhagat6 1:c7215f5f9a72 1229 bool glibr::setAmbientLightGain(uint8_t drive){
kbhagat6 9:286d00d9db80 1230
kbhagat6 1:c7215f5f9a72 1231 uint8_t val;
kbhagat6 1:c7215f5f9a72 1232
kbhagat6 1:c7215f5f9a72 1233 /* Read value from CONTROL register */
kbhagat6 1:c7215f5f9a72 1234
kbhagat6 5:3eb4f3091bd8 1235 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL);
kbhagat6 1:c7215f5f9a72 1236
kbhagat6 1:c7215f5f9a72 1237 if(val==ERROR){
kbhagat6 1:c7215f5f9a72 1238 return false;
kbhagat6 1:c7215f5f9a72 1239 }
kbhagat6 1:c7215f5f9a72 1240 /* Set bits in register to given value */
kbhagat6 1:c7215f5f9a72 1241 //drive &= 0b00000011;
kbhagat6 1:c7215f5f9a72 1242 drive &=0x03;
kbhagat6 1:c7215f5f9a72 1243 drive = drive << 2;
kbhagat6 9:286d00d9db80 1244 //val &=0b11111100
kbhagat6 1:c7215f5f9a72 1245 val &= 0xF3;
kbhagat6 1:c7215f5f9a72 1246 val |= drive;
kbhagat6 1:c7215f5f9a72 1247
kbhagat6 1:c7215f5f9a72 1248 /* Write register value back into CONTROL register */
kbhagat6 1:c7215f5f9a72 1249 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONTROL, val) ) {
kbhagat6 1:c7215f5f9a72 1250 return false;
kbhagat6 1:c7215f5f9a72 1251 }
kbhagat6 1:c7215f5f9a72 1252 return true;
kbhagat6 1:c7215f5f9a72 1253 }
kbhagat6 1:c7215f5f9a72 1254
kbhagat6 9:286d00d9db80 1255 /**
kbhagat6 9:286d00d9db80 1256 * @brief Get the current LED boost value
kbhagat6 9:286d00d9db80 1257 *
kbhagat6 9:286d00d9db80 1258 * Value Boost Current
kbhagat6 9:286d00d9db80 1259 * 0 100%
kbhagat6 9:286d00d9db80 1260 * 1 150%
kbhagat6 9:286d00d9db80 1261 * 2 200%
kbhagat6 9:286d00d9db80 1262 * 3 300%
kbhagat6 9:286d00d9db80 1263 *
kbhagat6 9:286d00d9db80 1264 * @return The LED boost value. 0xFF on failure.
kbhagat6 9:286d00d9db80 1265 */
kbhagat6 9:286d00d9db80 1266 uint8_t glibr::getLEDBoost() {
kbhagat6 9:286d00d9db80 1267 uint8_t val;
kbhagat6 9:286d00d9db80 1268
kbhagat6 9:286d00d9db80 1269 /* Read value from CONFIG2 register */
kbhagat6 9:286d00d9db80 1270 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_CONFIG2);
kbhagat6 9:286d00d9db80 1271 if( val == ERROR){//!wireReadDataByte(APDS9960_CONFIG2, val) ) {
kbhagat6 9:286d00d9db80 1272 return ERROR;
kbhagat6 9:286d00d9db80 1273 }
kbhagat6 9:286d00d9db80 1274
kbhagat6 9:286d00d9db80 1275 /* Shift and mask out LED_BOOST bits */
kbhagat6 9:286d00d9db80 1276 val = (val >> 4) & 0x03;//0b00000011;
kbhagat6 9:286d00d9db80 1277
kbhagat6 9:286d00d9db80 1278 return val;
kbhagat6 9:286d00d9db80 1279 }
kbhagat6 1:c7215f5f9a72 1280
kbhagat6 9:286d00d9db80 1281 /**
kbhagat6 9:286d00d9db80 1282 * @brief Sets the LED current boost value
kbhagat6 9:286d00d9db80 1283 *
kbhagat6 9:286d00d9db80 1284 * Value Boost Current
kbhagat6 9:286d00d9db80 1285 * 0 100%
kbhagat6 9:286d00d9db80 1286 * 1 150%
kbhagat6 9:286d00d9db80 1287 * 2 200%
kbhagat6 9:286d00d9db80 1288 * 3 300%
kbhagat6 9:286d00d9db80 1289 *
kbhagat6 9:286d00d9db80 1290 * @param[in] drive the value (0-3) for current boost (100-300%)
kbhagat6 9:286d00d9db80 1291 * @return True if operation successful. False otherwise.
kbhagat6 9:286d00d9db80 1292 */
kbhagat6 9:286d00d9db80 1293 bool glibr::setLEDBoost(uint8_t boost)
kbhagat6 9:286d00d9db80 1294 {
kbhagat6 9:286d00d9db80 1295 uint8_t val;
kbhagat6 9:286d00d9db80 1296
kbhagat6 9:286d00d9db80 1297 /* Read value from CONFIG2 register */
kbhagat6 9:286d00d9db80 1298 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_CONFIG2);
kbhagat6 9:286d00d9db80 1299 if( val == ERROR){//!wireReadDataByte(APDS9960_CONFIG2, val) ) {
kbhagat6 9:286d00d9db80 1300 return false;
kbhagat6 9:286d00d9db80 1301 }
kbhagat6 9:286d00d9db80 1302
kbhagat6 9:286d00d9db80 1303 /* Set bits in register to given value */
kbhagat6 9:286d00d9db80 1304 boost &= 0x03;//0b00000011;
kbhagat6 9:286d00d9db80 1305 boost = boost << 4;
kbhagat6 9:286d00d9db80 1306 val &= 0xCF;//0b11001111;
kbhagat6 9:286d00d9db80 1307 val |= boost;
kbhagat6 9:286d00d9db80 1308
kbhagat6 9:286d00d9db80 1309 /* Write register value back into CONFIG2 register */
kbhagat6 9:286d00d9db80 1310
kbhagat6 9:286d00d9db80 1311 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONFIG2, val)){//!wireWriteDataByte(APDS9960_CONFIG2, val) ) {
kbhagat6 9:286d00d9db80 1312 return false;
kbhagat6 9:286d00d9db80 1313 }
kbhagat6 9:286d00d9db80 1314
kbhagat6 9:286d00d9db80 1315 return true;
kbhagat6 9:286d00d9db80 1316 }
kbhagat6 9:286d00d9db80 1317
kbhagat6 9:286d00d9db80 1318 /**
kbhagat6 9:286d00d9db80 1319 * @brief Gets proximity gain compensation enable
kbhagat6 9:286d00d9db80 1320 *
kbhagat6 9:286d00d9db80 1321 * @return 1 if compensation is enabled. 0 if not. 0xFF on error.
kbhagat6 9:286d00d9db80 1322 */
kbhagat6 9:286d00d9db80 1323 uint8_t glibr::getProxGainCompEnable()
kbhagat6 9:286d00d9db80 1324 {
kbhagat6 9:286d00d9db80 1325 uint8_t val;
kbhagat6 9:286d00d9db80 1326
kbhagat6 9:286d00d9db80 1327 /* Read value from CONFIG3 register */
kbhagat6 9:286d00d9db80 1328 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_CONFIG3);
kbhagat6 9:286d00d9db80 1329 if( val == ERROR){//!wireReadDataByte(APDS9960_CONFIG3, val) ) {
kbhagat6 9:286d00d9db80 1330 return ERROR;
kbhagat6 9:286d00d9db80 1331 }
kbhagat6 9:286d00d9db80 1332
kbhagat6 9:286d00d9db80 1333 /* Shift and mask out PCMP bits */
kbhagat6 9:286d00d9db80 1334 val = (val >> 5) & 0x01;//0b00000001;
kbhagat6 9:286d00d9db80 1335
kbhagat6 9:286d00d9db80 1336 return val;
kbhagat6 9:286d00d9db80 1337 }
kbhagat6 9:286d00d9db80 1338
kbhagat6 9:286d00d9db80 1339 /**
kbhagat6 9:286d00d9db80 1340 * @brief Sets the proximity gain compensation enable
kbhagat6 9:286d00d9db80 1341 *
kbhagat6 9:286d00d9db80 1342 * @param[in] enable 1 to enable compensation. 0 to disable compensation.
kbhagat6 9:286d00d9db80 1343 * @return True if operation successful. False otherwise.
kbhagat6 9:286d00d9db80 1344 */
kbhagat6 9:286d00d9db80 1345 bool glibr::setProxGainCompEnable(uint8_t enable)
kbhagat6 9:286d00d9db80 1346 {
kbhagat6 9:286d00d9db80 1347 uint8_t val;
kbhagat6 9:286d00d9db80 1348
kbhagat6 9:286d00d9db80 1349 /* Read value from CONFIG3 register */
kbhagat6 9:286d00d9db80 1350 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_CONFIG3);
kbhagat6 9:286d00d9db80 1351 if( val == ERROR){//!wireReadDataByte(APDS9960_CONFIG3, val) ) {
kbhagat6 9:286d00d9db80 1352 return false;
kbhagat6 9:286d00d9db80 1353 }
kbhagat6 9:286d00d9db80 1354
kbhagat6 9:286d00d9db80 1355 /* Set bits in register to given value */
kbhagat6 9:286d00d9db80 1356 enable &= 0x01;//0b00000001;
kbhagat6 9:286d00d9db80 1357 enable = enable << 5;
kbhagat6 9:286d00d9db80 1358 val &= 0xCF;//0b11011111;
kbhagat6 9:286d00d9db80 1359 val |= enable;
kbhagat6 9:286d00d9db80 1360
kbhagat6 9:286d00d9db80 1361 /* Write register value back into CONFIG3 register */
kbhagat6 9:286d00d9db80 1362
kbhagat6 9:286d00d9db80 1363 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONFIG3, val)){//!wireWriteDataByte(APDS9960_CONFIG3, val) ) {
kbhagat6 9:286d00d9db80 1364 return false;
kbhagat6 9:286d00d9db80 1365 }
kbhagat6 9:286d00d9db80 1366
kbhagat6 9:286d00d9db80 1367 return true;
kbhagat6 9:286d00d9db80 1368 }
kbhagat6 9:286d00d9db80 1369
kbhagat6 9:286d00d9db80 1370 /**
kbhagat6 9:286d00d9db80 1371 * @brief Gets the current mask for enabled/disabled proximity photodiodes
kbhagat6 9:286d00d9db80 1372 *
kbhagat6 9:286d00d9db80 1373 * 1 = disabled, 0 = enabled
kbhagat6 9:286d00d9db80 1374 * Bit Photodiode
kbhagat6 9:286d00d9db80 1375 * 3 UP
kbhagat6 9:286d00d9db80 1376 * 2 DOWN
kbhagat6 9:286d00d9db80 1377 * 1 LEFT
kbhagat6 9:286d00d9db80 1378 * 0 RIGHT
kbhagat6 9:286d00d9db80 1379 *
kbhagat6 9:286d00d9db80 1380 * @return Current proximity mask for photodiodes. 0xFF on error.
kbhagat6 9:286d00d9db80 1381 */
kbhagat6 9:286d00d9db80 1382 uint8_t glibr::getProxPhotoMask()
kbhagat6 9:286d00d9db80 1383 {
kbhagat6 9:286d00d9db80 1384 uint8_t val;
kbhagat6 9:286d00d9db80 1385
kbhagat6 9:286d00d9db80 1386 /* Read value from CONFIG3 register */
kbhagat6 9:286d00d9db80 1387 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_CONFIG3);
kbhagat6 9:286d00d9db80 1388 if( val == ERROR){//!wireReadDataByte(APDS9960_CONFIG3, val) ) {
kbhagat6 9:286d00d9db80 1389 return ERROR;
kbhagat6 9:286d00d9db80 1390 }
kbhagat6 9:286d00d9db80 1391
kbhagat6 9:286d00d9db80 1392 /* Mask out photodiode enable mask bits */
kbhagat6 9:286d00d9db80 1393 val &= 0x0F;//0b00001111;
kbhagat6 9:286d00d9db80 1394
kbhagat6 9:286d00d9db80 1395 return val;
kbhagat6 9:286d00d9db80 1396 }
kbhagat6 9:286d00d9db80 1397
kbhagat6 9:286d00d9db80 1398 /**
kbhagat6 9:286d00d9db80 1399 * @brief Sets the mask for enabling/disabling proximity photodiodes
kbhagat6 9:286d00d9db80 1400 *
kbhagat6 9:286d00d9db80 1401 * 1 = disabled, 0 = enabled
kbhagat6 9:286d00d9db80 1402 * Bit Photodiode
kbhagat6 9:286d00d9db80 1403 * 3 UP
kbhagat6 9:286d00d9db80 1404 * 2 DOWN
kbhagat6 9:286d00d9db80 1405 * 1 LEFT
kbhagat6 9:286d00d9db80 1406 * 0 RIGHT
kbhagat6 9:286d00d9db80 1407 *
kbhagat6 9:286d00d9db80 1408 * @param[in] mask 4-bit mask value
kbhagat6 9:286d00d9db80 1409 * @return True if operation successful. False otherwise.
kbhagat6 9:286d00d9db80 1410 */
kbhagat6 9:286d00d9db80 1411 bool glibr::setProxPhotoMask(uint8_t mask)
kbhagat6 9:286d00d9db80 1412 {
kbhagat6 9:286d00d9db80 1413 uint8_t val;
kbhagat6 9:286d00d9db80 1414
kbhagat6 9:286d00d9db80 1415 /* Read value from CONFIG3 register */
kbhagat6 9:286d00d9db80 1416 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_CONFIG3);
kbhagat6 9:286d00d9db80 1417 if( val == ERROR){//!wireReadDataByte(APDS9960_CONFIG3, val) ) {
kbhagat6 9:286d00d9db80 1418 return false;
kbhagat6 9:286d00d9db80 1419 }
kbhagat6 9:286d00d9db80 1420
kbhagat6 9:286d00d9db80 1421 /* Set bits in register to given value */
kbhagat6 9:286d00d9db80 1422 mask &= 0x0F;//0b00001111;
kbhagat6 9:286d00d9db80 1423 val &= 0xF0;//0b11110000;
kbhagat6 9:286d00d9db80 1424 val |= mask;
kbhagat6 9:286d00d9db80 1425
kbhagat6 9:286d00d9db80 1426 /* Write register value back into CONFIG3 register */
kbhagat6 9:286d00d9db80 1427 I2CwriteByte(APDS9960_I2C_ADDR, APDS9960_CONFIG3, val);
kbhagat6 9:286d00d9db80 1428 if( val == ERROR){//!wireWriteDataByte(APDS9960_CONFIG3, val) ) {
kbhagat6 9:286d00d9db80 1429 return false;
kbhagat6 9:286d00d9db80 1430 }
kbhagat6 9:286d00d9db80 1431
kbhagat6 9:286d00d9db80 1432 return true;
kbhagat6 9:286d00d9db80 1433 }
kbhagat6 9:286d00d9db80 1434
kbhagat6 9:286d00d9db80 1435 /**
kbhagat6 9:286d00d9db80 1436 * @brief Gets the entry proximity threshold for gesture sensing
kbhagat6 9:286d00d9db80 1437 *
kbhagat6 9:286d00d9db80 1438 * @return Current entry proximity threshold.
kbhagat6 9:286d00d9db80 1439 */
kbhagat6 9:286d00d9db80 1440 uint8_t glibr::getGestureEnterThresh()
kbhagat6 9:286d00d9db80 1441 {
kbhagat6 9:286d00d9db80 1442 uint8_t val;
kbhagat6 9:286d00d9db80 1443
kbhagat6 9:286d00d9db80 1444 /* Read value from GPENTH register */
kbhagat6 9:286d00d9db80 1445 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GPENTH);
kbhagat6 9:286d00d9db80 1446 if( val == ERROR){//!wireReadDataByte(APDS9960_GPENTH, val) ) {
kbhagat6 9:286d00d9db80 1447 val = 0;
kbhagat6 9:286d00d9db80 1448 }
kbhagat6 9:286d00d9db80 1449
kbhagat6 9:286d00d9db80 1450 return val;
kbhagat6 9:286d00d9db80 1451 }
kbhagat6 9:286d00d9db80 1452
kbhagat6 9:286d00d9db80 1453 /**
kbhagat6 9:286d00d9db80 1454 * @brief Sets the entry proximity threshold for gesture sensing
kbhagat6 9:286d00d9db80 1455 *
kbhagat6 9:286d00d9db80 1456 * @param[in] threshold proximity value needed to start gesture mode
kbhagat6 9:286d00d9db80 1457 * @return True if operation successful. False otherwise.
kbhagat6 9:286d00d9db80 1458 */
kbhagat6 9:286d00d9db80 1459 bool glibr::setGestureEnterThresh(uint8_t threshold)
kbhagat6 9:286d00d9db80 1460 {
kbhagat6 9:286d00d9db80 1461
kbhagat6 9:286d00d9db80 1462 if( I2CwriteByte(APDS9960_I2C_ADDR, APDS9960_GPENTH, threshold)){;//!wireWriteDataByte(APDS9960_GPENTH, threshold) ) {
kbhagat6 9:286d00d9db80 1463 return false;
kbhagat6 9:286d00d9db80 1464 }
kbhagat6 9:286d00d9db80 1465
kbhagat6 9:286d00d9db80 1466 return true;
kbhagat6 9:286d00d9db80 1467 }
kbhagat6 9:286d00d9db80 1468
kbhagat6 9:286d00d9db80 1469 /**
kbhagat6 9:286d00d9db80 1470 * @brief Gets the exit proximity threshold for gesture sensing
kbhagat6 9:286d00d9db80 1471 *
kbhagat6 9:286d00d9db80 1472 * @return Current exit proximity threshold.
kbhagat6 9:286d00d9db80 1473 */
kbhagat6 9:286d00d9db80 1474 uint8_t glibr::getGestureExitThresh()
kbhagat6 9:286d00d9db80 1475 {
kbhagat6 9:286d00d9db80 1476 uint8_t val;
kbhagat6 9:286d00d9db80 1477
kbhagat6 9:286d00d9db80 1478 /* Read value from GEXTH register */
kbhagat6 9:286d00d9db80 1479 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GEXTH);
kbhagat6 9:286d00d9db80 1480 if( val == ERROR){//!wireReadDataByte(APDS9960_GEXTH, val) ) {
kbhagat6 9:286d00d9db80 1481 val = 0;
kbhagat6 9:286d00d9db80 1482 }
kbhagat6 9:286d00d9db80 1483
kbhagat6 9:286d00d9db80 1484 return val;
kbhagat6 9:286d00d9db80 1485 }
kbhagat6 9:286d00d9db80 1486
kbhagat6 9:286d00d9db80 1487 /**
kbhagat6 9:286d00d9db80 1488 * @brief Sets the exit proximity threshold for gesture sensing
kbhagat6 9:286d00d9db80 1489 *
kbhagat6 9:286d00d9db80 1490 * @param[in] threshold proximity value needed to end gesture mode
kbhagat6 9:286d00d9db80 1491 * @return True if operation successful. False otherwise.
kbhagat6 9:286d00d9db80 1492 */
kbhagat6 9:286d00d9db80 1493 bool glibr::setGestureExitThresh(uint8_t threshold)
kbhagat6 9:286d00d9db80 1494 {
kbhagat6 9:286d00d9db80 1495 if( I2CwriteByte(APDS9960_I2C_ADDR, APDS9960_GEXTH, threshold)){//!wireWriteDataByte(APDS9960_GEXTH, threshold) ) {
kbhagat6 9:286d00d9db80 1496 return false;
kbhagat6 9:286d00d9db80 1497 }
kbhagat6 9:286d00d9db80 1498
kbhagat6 9:286d00d9db80 1499 return true;
kbhagat6 9:286d00d9db80 1500 }
kbhagat6 9:286d00d9db80 1501
kbhagat6 9:286d00d9db80 1502 /**
kbhagat6 9:286d00d9db80 1503 * @brief Gets the gain of the photodiode during gesture mode
kbhagat6 9:286d00d9db80 1504 *
kbhagat6 9:286d00d9db80 1505 * Value Gain
kbhagat6 9:286d00d9db80 1506 * 0 1x
kbhagat6 9:286d00d9db80 1507 * 1 2x
kbhagat6 9:286d00d9db80 1508 * 2 4x
kbhagat6 9:286d00d9db80 1509 * 3 8x
kbhagat6 9:286d00d9db80 1510 *
kbhagat6 9:286d00d9db80 1511 * @return the current photodiode gain. 0xFF on error.
kbhagat6 9:286d00d9db80 1512 */
kbhagat6 9:286d00d9db80 1513 uint8_t glibr::getGestureGain()
kbhagat6 9:286d00d9db80 1514 {
kbhagat6 9:286d00d9db80 1515 uint8_t val;
kbhagat6 9:286d00d9db80 1516
kbhagat6 9:286d00d9db80 1517 /* Read value from GCONF2 register */
kbhagat6 9:286d00d9db80 1518 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF2);
kbhagat6 9:286d00d9db80 1519 if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF2, val) ) {
kbhagat6 9:286d00d9db80 1520 return ERROR;
kbhagat6 9:286d00d9db80 1521 }
kbhagat6 9:286d00d9db80 1522
kbhagat6 9:286d00d9db80 1523 /* Shift and mask out GGAIN bits */
kbhagat6 9:286d00d9db80 1524 val = (val >> 5) & 0x03;//0b00000011;
kbhagat6 9:286d00d9db80 1525
kbhagat6 9:286d00d9db80 1526 return val;
kbhagat6 9:286d00d9db80 1527 }
kbhagat6 9:286d00d9db80 1528
kbhagat6 9:286d00d9db80 1529 /**
kbhagat6 9:286d00d9db80 1530 * @brief Sets the gain of the photodiode during gesture mode
kbhagat6 9:286d00d9db80 1531 *
kbhagat6 9:286d00d9db80 1532 * Value Gain
kbhagat6 9:286d00d9db80 1533 * 0 1x
kbhagat6 9:286d00d9db80 1534 * 1 2x
kbhagat6 9:286d00d9db80 1535 * 2 4x
kbhagat6 9:286d00d9db80 1536 * 3 8x
kbhagat6 9:286d00d9db80 1537 *
kbhagat6 9:286d00d9db80 1538 * @param[in] gain the value for the photodiode gain
kbhagat6 9:286d00d9db80 1539 * @return True if operation successful. False otherwise.
kbhagat6 9:286d00d9db80 1540 */
kbhagat6 9:286d00d9db80 1541 bool glibr::setGestureGain(uint8_t gain)
kbhagat6 9:286d00d9db80 1542 {
kbhagat6 9:286d00d9db80 1543 uint8_t val;
kbhagat6 9:286d00d9db80 1544
kbhagat6 9:286d00d9db80 1545 /* Read value from GCONF2 register */
kbhagat6 9:286d00d9db80 1546 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF2);
kbhagat6 9:286d00d9db80 1547 if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF2, val) ) {
kbhagat6 9:286d00d9db80 1548 return false;
kbhagat6 9:286d00d9db80 1549 }
kbhagat6 9:286d00d9db80 1550
kbhagat6 9:286d00d9db80 1551 /* Set bits in register to given value */
kbhagat6 9:286d00d9db80 1552 gain &= 0x03;//0b00000011;
kbhagat6 9:286d00d9db80 1553 gain = gain << 5;
kbhagat6 9:286d00d9db80 1554 val &= 0x9F;//0b10011111;
kbhagat6 9:286d00d9db80 1555 val |= gain;
kbhagat6 9:286d00d9db80 1556
kbhagat6 9:286d00d9db80 1557 /* Write register value back into GCONF2 register */
kbhagat6 9:286d00d9db80 1558 if( I2CwriteByte(APDS9960_I2C_ADDR, APDS9960_GCONF2, val)){//!wireWriteDataByte(APDS9960_GCONF2, val) ) {
kbhagat6 9:286d00d9db80 1559 return false;
kbhagat6 9:286d00d9db80 1560 }
kbhagat6 9:286d00d9db80 1561
kbhagat6 9:286d00d9db80 1562 return true;
kbhagat6 9:286d00d9db80 1563 }
kbhagat6 9:286d00d9db80 1564
kbhagat6 9:286d00d9db80 1565 /**
kbhagat6 9:286d00d9db80 1566 * @brief Gets the drive current of the LED during gesture mode
kbhagat6 9:286d00d9db80 1567 *
kbhagat6 9:286d00d9db80 1568 * Value LED Current
kbhagat6 9:286d00d9db80 1569 * 0 100 mA
kbhagat6 9:286d00d9db80 1570 * 1 50 mA
kbhagat6 9:286d00d9db80 1571 * 2 25 mA
kbhagat6 9:286d00d9db80 1572 * 3 12.5 mA
kbhagat6 9:286d00d9db80 1573 *
kbhagat6 9:286d00d9db80 1574 * @return the LED drive current value. 0xFF on error.
kbhagat6 9:286d00d9db80 1575 */
kbhagat6 9:286d00d9db80 1576 uint8_t glibr::getGestureLEDDrive()
kbhagat6 9:286d00d9db80 1577 {
kbhagat6 9:286d00d9db80 1578 uint8_t val;
kbhagat6 9:286d00d9db80 1579
kbhagat6 9:286d00d9db80 1580 /* Read value from GCONF2 register */
kbhagat6 9:286d00d9db80 1581 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF2);
kbhagat6 9:286d00d9db80 1582 if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF2, val) ) {
kbhagat6 9:286d00d9db80 1583 return ERROR;
kbhagat6 9:286d00d9db80 1584 }
kbhagat6 9:286d00d9db80 1585
kbhagat6 9:286d00d9db80 1586 /* Shift and mask out GLDRIVE bits */
kbhagat6 9:286d00d9db80 1587 val = (val >> 3) & 0x03;//0b00000011;
kbhagat6 9:286d00d9db80 1588
kbhagat6 9:286d00d9db80 1589 return val;
kbhagat6 9:286d00d9db80 1590 }
kbhagat6 9:286d00d9db80 1591
kbhagat6 9:286d00d9db80 1592 /**
kbhagat6 9:286d00d9db80 1593 * @brief Sets the LED drive current during gesture mode
kbhagat6 9:286d00d9db80 1594 *
kbhagat6 9:286d00d9db80 1595 * Value LED Current
kbhagat6 9:286d00d9db80 1596 * 0 100 mA
kbhagat6 9:286d00d9db80 1597 * 1 50 mA
kbhagat6 9:286d00d9db80 1598 * 2 25 mA
kbhagat6 9:286d00d9db80 1599 * 3 12.5 mA
kbhagat6 9:286d00d9db80 1600 *
kbhagat6 9:286d00d9db80 1601 * @param[in] drive the value for the LED drive current
kbhagat6 9:286d00d9db80 1602 * @return True if operation successful. False otherwise.
kbhagat6 9:286d00d9db80 1603 */
kbhagat6 9:286d00d9db80 1604 bool glibr::setGestureLEDDrive(uint8_t drive)
kbhagat6 9:286d00d9db80 1605 {
kbhagat6 9:286d00d9db80 1606 uint8_t val;
kbhagat6 9:286d00d9db80 1607
kbhagat6 9:286d00d9db80 1608 /* Read value from GCONF2 register */
kbhagat6 9:286d00d9db80 1609 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF2);
kbhagat6 9:286d00d9db80 1610 if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF2, val) ) {
kbhagat6 9:286d00d9db80 1611 return false;
kbhagat6 9:286d00d9db80 1612 }
kbhagat6 9:286d00d9db80 1613
kbhagat6 9:286d00d9db80 1614 /* Set bits in register to given value */
kbhagat6 9:286d00d9db80 1615 drive &= 0x03;//0b00000011;
kbhagat6 9:286d00d9db80 1616 drive = drive << 3;
kbhagat6 9:286d00d9db80 1617 val &= 0xE7;//0b11100111;
kbhagat6 9:286d00d9db80 1618 val |= drive;
kbhagat6 9:286d00d9db80 1619
kbhagat6 9:286d00d9db80 1620 /* Write register value back into GCONF2 register */
kbhagat6 9:286d00d9db80 1621 if( I2CwriteByte(APDS9960_I2C_ADDR, APDS9960_GCONF2, val)){//!wireWriteDataByte(APDS9960_GCONF2, val) ) {
kbhagat6 9:286d00d9db80 1622 return false;
kbhagat6 9:286d00d9db80 1623 }
kbhagat6 9:286d00d9db80 1624
kbhagat6 9:286d00d9db80 1625 return true;
kbhagat6 9:286d00d9db80 1626 }
kbhagat6 9:286d00d9db80 1627
kbhagat6 9:286d00d9db80 1628 /**
kbhagat6 9:286d00d9db80 1629 * @brief Gets the time in low power mode between gesture detections
kbhagat6 9:286d00d9db80 1630 *
kbhagat6 9:286d00d9db80 1631 * Value Wait time
kbhagat6 9:286d00d9db80 1632 * 0 0 ms
kbhagat6 9:286d00d9db80 1633 * 1 2.8 ms
kbhagat6 9:286d00d9db80 1634 * 2 5.6 ms
kbhagat6 9:286d00d9db80 1635 * 3 8.4 ms
kbhagat6 9:286d00d9db80 1636 * 4 14.0 ms
kbhagat6 9:286d00d9db80 1637 * 5 22.4 ms
kbhagat6 9:286d00d9db80 1638 * 6 30.8 ms
kbhagat6 9:286d00d9db80 1639 * 7 39.2 ms
kbhagat6 9:286d00d9db80 1640 *
kbhagat6 9:286d00d9db80 1641 * @return the current wait time between gestures. 0xFF on error.
kbhagat6 9:286d00d9db80 1642 */
kbhagat6 9:286d00d9db80 1643 uint8_t glibr::getGestureWaitTime()
kbhagat6 9:286d00d9db80 1644 {
kbhagat6 9:286d00d9db80 1645 uint8_t val;
kbhagat6 9:286d00d9db80 1646
kbhagat6 9:286d00d9db80 1647 /* Read value from GCONF2 register */
kbhagat6 9:286d00d9db80 1648 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF2);
kbhagat6 9:286d00d9db80 1649 if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF2, val) ) {
kbhagat6 9:286d00d9db80 1650 return ERROR;
kbhagat6 9:286d00d9db80 1651 }
kbhagat6 9:286d00d9db80 1652
kbhagat6 9:286d00d9db80 1653 /* Mask out GWTIME bits */
kbhagat6 9:286d00d9db80 1654 val &= 0x07;//0b00000111;
kbhagat6 9:286d00d9db80 1655
kbhagat6 9:286d00d9db80 1656 return val;
kbhagat6 9:286d00d9db80 1657 }
kbhagat6 9:286d00d9db80 1658
kbhagat6 9:286d00d9db80 1659 /*
kbhagat6 9:286d00d9db80 1660 *
kbhagat6 9:286d00d9db80 1661 *
kbhagat6 9:286d00d9db80 1662 *
kbhagat6 9:286d00d9db80 1663 *LEFT OFF HERE AT 3:47PM ON 3/6/15
kbhagat6 9:286d00d9db80 1664 *
kbhagat6 9:286d00d9db80 1665 *
kbhagat6 9:286d00d9db80 1666 *
kbhagat6 9:286d00d9db80 1667 *
kbhagat6 9:286d00d9db80 1668 */
kbhagat6 9:286d00d9db80 1669
kbhagat6 9:286d00d9db80 1670
kbhagat6 9:286d00d9db80 1671 /**
kbhagat6 9:286d00d9db80 1672 * @brief Sets the time in low power mode between gesture detections
kbhagat6 9:286d00d9db80 1673 *
kbhagat6 9:286d00d9db80 1674 * Value Wait time
kbhagat6 9:286d00d9db80 1675 * 0 0 ms
kbhagat6 9:286d00d9db80 1676 * 1 2.8 ms
kbhagat6 9:286d00d9db80 1677 * 2 5.6 ms
kbhagat6 9:286d00d9db80 1678 * 3 8.4 ms
kbhagat6 9:286d00d9db80 1679 * 4 14.0 ms
kbhagat6 9:286d00d9db80 1680 * 5 22.4 ms
kbhagat6 9:286d00d9db80 1681 * 6 30.8 ms
kbhagat6 9:286d00d9db80 1682 * 7 39.2 ms
kbhagat6 9:286d00d9db80 1683 *
kbhagat6 9:286d00d9db80 1684 * @param[in] the value for the wait time
kbhagat6 9:286d00d9db80 1685 * @return True if operation successful. False otherwise.
kbhagat6 9:286d00d9db80 1686 */
kbhagat6 9:286d00d9db80 1687 bool glibr::setGestureWaitTime(uint8_t time)
kbhagat6 9:286d00d9db80 1688 {
kbhagat6 9:286d00d9db80 1689 uint8_t val;
kbhagat6 9:286d00d9db80 1690
kbhagat6 9:286d00d9db80 1691 /* Read value from GCONF2 register */
kbhagat6 9:286d00d9db80 1692 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF2);
kbhagat6 9:286d00d9db80 1693 if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF2, val) ) {
kbhagat6 9:286d00d9db80 1694 return false;
kbhagat6 9:286d00d9db80 1695 }
kbhagat6 9:286d00d9db80 1696 /* if( !wireReadDataByte(APDS9960_GCONF2, val) ) {
kbhagat6 9:286d00d9db80 1697 return false;
kbhagat6 9:286d00d9db80 1698 } */
kbhagat6 9:286d00d9db80 1699
kbhagat6 9:286d00d9db80 1700 /* Set bits in register to given value */
kbhagat6 9:286d00d9db80 1701 time &= 0x07;//0b00000111;
kbhagat6 9:286d00d9db80 1702 val &= 0xF8;//0b11111000;
kbhagat6 9:286d00d9db80 1703 val |= time;
kbhagat6 9:286d00d9db80 1704
kbhagat6 9:286d00d9db80 1705 /* Write register value back into GCONF2 register */
kbhagat6 9:286d00d9db80 1706 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GCONF2,val)){//!wireWriteDataByte(APDS9960_GCONF2, val) ) {
kbhagat6 9:286d00d9db80 1707 return false;
kbhagat6 9:286d00d9db80 1708 }
kbhagat6 9:286d00d9db80 1709 /*if( !wireWriteDataByte(APDS9960_GCONF2, val) ) {
kbhagat6 9:286d00d9db80 1710 return false;
kbhagat6 9:286d00d9db80 1711 }*/
kbhagat6 9:286d00d9db80 1712 return true;
kbhagat6 9:286d00d9db80 1713 }
kbhagat6 9:286d00d9db80 1714
kbhagat6 9:286d00d9db80 1715 /**
kbhagat6 9:286d00d9db80 1716 * @brief Gets the low threshold for ambient light interrupts
kbhagat6 9:286d00d9db80 1717 *
kbhagat6 9:286d00d9db80 1718 * @param[out] threshold current low threshold stored on the APDS-9960
kbhagat6 9:286d00d9db80 1719 * @return True if operation successful. False otherwise.
kbhagat6 9:286d00d9db80 1720 */
kbhagat6 9:286d00d9db80 1721 bool glibr::getLightIntLowThreshold(uint16_t &threshold)
kbhagat6 9:286d00d9db80 1722 {
kbhagat6 9:286d00d9db80 1723 uint8_t val_byte;
kbhagat6 9:286d00d9db80 1724 threshold = 0;
kbhagat6 9:286d00d9db80 1725
kbhagat6 9:286d00d9db80 1726 /* Read value from ambient light low threshold, low byte register */
kbhagat6 9:286d00d9db80 1727 val_byte = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_AILTL);
kbhagat6 9:286d00d9db80 1728 if( val_byte == ERROR){//!wireReadDataByte(APDS9960_AILTL, val_byte) ) {
kbhagat6 9:286d00d9db80 1729 return false;
kbhagat6 9:286d00d9db80 1730 }
kbhagat6 9:286d00d9db80 1731 threshold = val_byte;
kbhagat6 9:286d00d9db80 1732
kbhagat6 9:286d00d9db80 1733 /* Read value from ambient light low threshold, high byte register */
kbhagat6 9:286d00d9db80 1734 val_byte = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_AILTH);
kbhagat6 9:286d00d9db80 1735 if( val_byte == ERROR){//!wireReadDataByte(APDS9960_AILTH, val_byte) ) {
kbhagat6 9:286d00d9db80 1736 return false;
kbhagat6 9:286d00d9db80 1737 }
kbhagat6 9:286d00d9db80 1738 threshold = threshold + ((uint16_t)val_byte << 8);
kbhagat6 9:286d00d9db80 1739
kbhagat6 9:286d00d9db80 1740 return true;
kbhagat6 9:286d00d9db80 1741 }
kbhagat6 9:286d00d9db80 1742
kbhagat6 9:286d00d9db80 1743 /**
kbhagat6 9:286d00d9db80 1744 * @brief Sets the low threshold for ambient light interrupts
kbhagat6 9:286d00d9db80 1745 *
kbhagat6 9:286d00d9db80 1746 * @param[in] threshold low threshold value for interrupt to trigger
kbhagat6 9:286d00d9db80 1747 * @return True if operation successful. False otherwise.
kbhagat6 9:286d00d9db80 1748 */
kbhagat6 1:c7215f5f9a72 1749 bool glibr::setLightIntLowThreshold(uint16_t threshold)
kbhagat6 1:c7215f5f9a72 1750 {
kbhagat6 1:c7215f5f9a72 1751 uint8_t val_low;
kbhagat6 1:c7215f5f9a72 1752 uint8_t val_high;
kbhagat6 1:c7215f5f9a72 1753
kbhagat6 1:c7215f5f9a72 1754 /* Break 16-bit threshold into 2 8-bit values */
kbhagat6 1:c7215f5f9a72 1755 val_low = threshold & 0x00FF;
kbhagat6 1:c7215f5f9a72 1756 val_high = (threshold & 0xFF00) >> 8;
kbhagat6 1:c7215f5f9a72 1757
kbhagat6 1:c7215f5f9a72 1758 /* Write low byte */
kbhagat6 9:286d00d9db80 1759 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_AILTL,val_low)){//!wireWriteDataByte(APDS9960_AILTL, val_low) ) {
kbhagat6 9:286d00d9db80 1760 return false;
kbhagat6 9:286d00d9db80 1761 }
kbhagat6 9:286d00d9db80 1762
kbhagat6 9:286d00d9db80 1763 /* Write high byte */
kbhagat6 9:286d00d9db80 1764 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_AILTH,val_high)){//!wireWriteDataByte(APDS9960_AILTH, val_high) ) {
kbhagat6 9:286d00d9db80 1765 return false;
kbhagat6 9:286d00d9db80 1766 }
kbhagat6 9:286d00d9db80 1767
kbhagat6 9:286d00d9db80 1768 return true;
kbhagat6 9:286d00d9db80 1769 }
kbhagat6 9:286d00d9db80 1770
kbhagat6 9:286d00d9db80 1771 /**
kbhagat6 9:286d00d9db80 1772 * @brief Gets the high threshold for ambient light interrupts
kbhagat6 9:286d00d9db80 1773 *
kbhagat6 9:286d00d9db80 1774 * @param[out] threshold current low threshold stored on the APDS-9960
kbhagat6 9:286d00d9db80 1775 * @return True if operation successful. False otherwise.
kbhagat6 9:286d00d9db80 1776 */
kbhagat6 9:286d00d9db80 1777 bool glibr::getLightIntHighThreshold(uint16_t &threshold)
kbhagat6 9:286d00d9db80 1778 {
kbhagat6 9:286d00d9db80 1779 uint8_t val_byte;
kbhagat6 9:286d00d9db80 1780 threshold = 0;
kbhagat6 9:286d00d9db80 1781
kbhagat6 9:286d00d9db80 1782 /* Read value from ambient light high threshold, low byte register */
kbhagat6 9:286d00d9db80 1783 val_byte = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_AIHTL);
kbhagat6 9:286d00d9db80 1784 if( val_byte == ERROR){//!wireReadDataByte(APDS9960_AIHTL, val_byte) ) {
kbhagat6 9:286d00d9db80 1785 return false;
kbhagat6 9:286d00d9db80 1786 }
kbhagat6 9:286d00d9db80 1787 threshold = val_byte;
kbhagat6 9:286d00d9db80 1788
kbhagat6 9:286d00d9db80 1789 /* Read value from ambient light high threshold, high byte register */
kbhagat6 9:286d00d9db80 1790 val_byte = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_AIHTH);
kbhagat6 9:286d00d9db80 1791 if( val_byte == ERROR){//!wireReadDataByte(APDS9960_AIHTH, val_byte) ) {
kbhagat6 9:286d00d9db80 1792 return false;
kbhagat6 9:286d00d9db80 1793 }
kbhagat6 9:286d00d9db80 1794 threshold = threshold + ((uint16_t)val_byte << 8);
kbhagat6 9:286d00d9db80 1795
kbhagat6 9:286d00d9db80 1796 return true;
kbhagat6 9:286d00d9db80 1797 }
kbhagat6 9:286d00d9db80 1798
kbhagat6 9:286d00d9db80 1799 /**
kbhagat6 9:286d00d9db80 1800 * @brief Sets the high threshold for ambient light interrupts
kbhagat6 9:286d00d9db80 1801 *
kbhagat6 9:286d00d9db80 1802 * @param[in] threshold high threshold value for interrupt to trigger
kbhagat6 9:286d00d9db80 1803 * @return True if operation successful. False otherwise.
kbhagat6 9:286d00d9db80 1804 */
kbhagat6 9:286d00d9db80 1805 bool glibr::setLightIntHighThreshold(uint16_t threshold)
kbhagat6 9:286d00d9db80 1806 {
kbhagat6 9:286d00d9db80 1807 uint8_t val_low;
kbhagat6 9:286d00d9db80 1808 uint8_t val_high;
kbhagat6 9:286d00d9db80 1809
kbhagat6 9:286d00d9db80 1810 /* Break 16-bit threshold into 2 8-bit values */
kbhagat6 9:286d00d9db80 1811 val_low = threshold & 0x00FF;
kbhagat6 9:286d00d9db80 1812 val_high = (threshold & 0xFF00) >> 8;
kbhagat6 9:286d00d9db80 1813
kbhagat6 9:286d00d9db80 1814 /* Write low byte */
kbhagat6 9:286d00d9db80 1815 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_AIHTL,val_low)){//!wireWriteDataByte(APDS9960_AIHTL, val_low) ) {
kbhagat6 1:c7215f5f9a72 1816 return false;
kbhagat6 1:c7215f5f9a72 1817 }
kbhagat6 1:c7215f5f9a72 1818
kbhagat6 1:c7215f5f9a72 1819 /* Write high byte */
kbhagat6 9:286d00d9db80 1820 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_AIHTH,val_high)){//!wireWriteDataByte(APDS9960_AIHTH, val_high) ) {
kbhagat6 9:286d00d9db80 1821 return false;
kbhagat6 9:286d00d9db80 1822 }
kbhagat6 9:286d00d9db80 1823
kbhagat6 9:286d00d9db80 1824 return true;
kbhagat6 9:286d00d9db80 1825 }
kbhagat6 9:286d00d9db80 1826
kbhagat6 9:286d00d9db80 1827 /**
kbhagat6 9:286d00d9db80 1828 * @brief Gets the low threshold for proximity interrupts
kbhagat6 9:286d00d9db80 1829 *
kbhagat6 9:286d00d9db80 1830 * @param[out] threshold current low threshold stored on the APDS-9960
kbhagat6 9:286d00d9db80 1831 * @return True if operation successful. False otherwise.
kbhagat6 9:286d00d9db80 1832 */
kbhagat6 9:286d00d9db80 1833 bool glibr::getProximityIntLowThreshold(uint8_t &threshold)
kbhagat6 9:286d00d9db80 1834 {
kbhagat6 9:286d00d9db80 1835 threshold = 0;
kbhagat6 9:286d00d9db80 1836
kbhagat6 9:286d00d9db80 1837 /* Read value from proximity low threshold register */
kbhagat6 9:286d00d9db80 1838 threshold = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_PILT);
kbhagat6 9:286d00d9db80 1839 if( threshold == ERROR){//!wireReadDataByte(APDS9960_PILT, threshold) ) {
kbhagat6 9:286d00d9db80 1840 return false;
kbhagat6 9:286d00d9db80 1841 }
kbhagat6 9:286d00d9db80 1842
kbhagat6 9:286d00d9db80 1843 return true;
kbhagat6 9:286d00d9db80 1844 }
kbhagat6 9:286d00d9db80 1845
kbhagat6 9:286d00d9db80 1846 /**
kbhagat6 9:286d00d9db80 1847 * @brief Sets the low threshold for proximity interrupts
kbhagat6 9:286d00d9db80 1848 *
kbhagat6 9:286d00d9db80 1849 * @param[in] threshold low threshold value for interrupt to trigger
kbhagat6 9:286d00d9db80 1850 * @return True if operation successful. False otherwise.
kbhagat6 9:286d00d9db80 1851 */
kbhagat6 9:286d00d9db80 1852 bool glibr::setProximityIntLowThreshold(uint8_t threshold)
kbhagat6 9:286d00d9db80 1853 {
kbhagat6 9:286d00d9db80 1854
kbhagat6 9:286d00d9db80 1855 /* Write threshold value to register */
kbhagat6 9:286d00d9db80 1856 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PILT,threshold)){//!wireWriteDataByte(APDS9960_PILT, threshold) ) {
kbhagat6 9:286d00d9db80 1857 return false;
kbhagat6 9:286d00d9db80 1858 }
kbhagat6 9:286d00d9db80 1859
kbhagat6 9:286d00d9db80 1860 return true;
kbhagat6 9:286d00d9db80 1861 }
kbhagat6 9:286d00d9db80 1862
kbhagat6 9:286d00d9db80 1863 /**
kbhagat6 9:286d00d9db80 1864 * @brief Gets the high threshold for proximity interrupts
kbhagat6 9:286d00d9db80 1865 *
kbhagat6 9:286d00d9db80 1866 * @param[out] threshold current low threshold stored on the APDS-9960
kbhagat6 9:286d00d9db80 1867 * @return True if operation successful. False otherwise.
kbhagat6 9:286d00d9db80 1868 */
kbhagat6 9:286d00d9db80 1869 bool glibr::getProximityIntHighThreshold(uint8_t &threshold)
kbhagat6 9:286d00d9db80 1870 {
kbhagat6 9:286d00d9db80 1871 threshold = 0;
kbhagat6 9:286d00d9db80 1872
kbhagat6 9:286d00d9db80 1873 /* Read value from proximity low threshold register */
kbhagat6 9:286d00d9db80 1874 threshold = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_PIHT);
kbhagat6 9:286d00d9db80 1875 if( threshold == ERROR){//!wireReadDataByte(APDS9960_PIHT, threshold) ) {
kbhagat6 9:286d00d9db80 1876 return false;
kbhagat6 9:286d00d9db80 1877 }
kbhagat6 9:286d00d9db80 1878
kbhagat6 9:286d00d9db80 1879 return true;
kbhagat6 9:286d00d9db80 1880 }
kbhagat6 9:286d00d9db80 1881
kbhagat6 9:286d00d9db80 1882 /**
kbhagat6 9:286d00d9db80 1883 * @brief Sets the high threshold for proximity interrupts
kbhagat6 9:286d00d9db80 1884 *
kbhagat6 9:286d00d9db80 1885 * @param[in] threshold high threshold value for interrupt to trigger
kbhagat6 9:286d00d9db80 1886 * @return True if operation successful. False otherwise.
kbhagat6 9:286d00d9db80 1887 */
kbhagat6 9:286d00d9db80 1888 bool glibr::setProximityIntHighThreshold(uint8_t threshold)
kbhagat6 9:286d00d9db80 1889 {
kbhagat6 9:286d00d9db80 1890
kbhagat6 9:286d00d9db80 1891 /* Write threshold value to register */
kbhagat6 9:286d00d9db80 1892 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PIHT,threshold)){//!wireWriteDataByte(APDS9960_PIHT, threshold) ) {
kbhagat6 9:286d00d9db80 1893 return false;
kbhagat6 9:286d00d9db80 1894 }
kbhagat6 9:286d00d9db80 1895
kbhagat6 9:286d00d9db80 1896 return true;
kbhagat6 9:286d00d9db80 1897 }
kbhagat6 9:286d00d9db80 1898
kbhagat6 9:286d00d9db80 1899 /**
kbhagat6 9:286d00d9db80 1900 * @brief Gets if ambient light interrupts are enabled or not
kbhagat6 9:286d00d9db80 1901 *
kbhagat6 9:286d00d9db80 1902 * @return 1 if interrupts are enabled, 0 if not. 0xFF on error.
kbhagat6 9:286d00d9db80 1903 */
kbhagat6 9:286d00d9db80 1904 uint8_t glibr::getAmbientLightIntEnable()
kbhagat6 9:286d00d9db80 1905 {
kbhagat6 9:286d00d9db80 1906 uint8_t val;
kbhagat6 9:286d00d9db80 1907
kbhagat6 9:286d00d9db80 1908 /* Read value from ENABLE register */
kbhagat6 9:286d00d9db80 1909 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ENABLE);
kbhagat6 9:286d00d9db80 1910 if( val == ERROR){//!wireReadDataByte(APDS9960_ENABLE, val) ) {
kbhagat6 9:286d00d9db80 1911 return ERROR;
kbhagat6 9:286d00d9db80 1912 }
kbhagat6 9:286d00d9db80 1913
kbhagat6 9:286d00d9db80 1914 /* Shift and mask out AIEN bit */
kbhagat6 9:286d00d9db80 1915 val = (val >> 4) & 0x01;//0b00000001;
kbhagat6 9:286d00d9db80 1916
kbhagat6 9:286d00d9db80 1917 return val;
kbhagat6 9:286d00d9db80 1918 }
kbhagat6 9:286d00d9db80 1919
kbhagat6 9:286d00d9db80 1920 /**
kbhagat6 9:286d00d9db80 1921 * @brief Turns ambient light interrupts on or off
kbhagat6 9:286d00d9db80 1922 *
kbhagat6 9:286d00d9db80 1923 * @param[in] enable 1 to enable interrupts, 0 to turn them off
kbhagat6 9:286d00d9db80 1924 * @return True if operation successful. False otherwise.
kbhagat6 9:286d00d9db80 1925 */
kbhagat6 9:286d00d9db80 1926 bool glibr::setAmbientLightIntEnable(uint8_t enable)
kbhagat6 9:286d00d9db80 1927 {
kbhagat6 9:286d00d9db80 1928 uint8_t val;
kbhagat6 9:286d00d9db80 1929
kbhagat6 9:286d00d9db80 1930 /* Read value from ENABLE register */
kbhagat6 9:286d00d9db80 1931 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ENABLE);
kbhagat6 9:286d00d9db80 1932 if( val == ERROR){//!wireReadDataByte(APDS9960_ENABLE, val) ) {
kbhagat6 9:286d00d9db80 1933 return false;
kbhagat6 9:286d00d9db80 1934 }
kbhagat6 9:286d00d9db80 1935
kbhagat6 9:286d00d9db80 1936 /* Set bits in register to given value */
kbhagat6 9:286d00d9db80 1937 enable &= 0x01;//0b00000001;
kbhagat6 9:286d00d9db80 1938 enable = enable << 4;
kbhagat6 9:286d00d9db80 1939 val &= 0xEF;//0b11101111;
kbhagat6 9:286d00d9db80 1940 val |= enable;
kbhagat6 9:286d00d9db80 1941
kbhagat6 9:286d00d9db80 1942 /* Write register value back into ENABLE register */
kbhagat6 9:286d00d9db80 1943 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_ENABLE,val)){//!wireWriteDataByte(APDS9960_ENABLE, val) ) {
kbhagat6 9:286d00d9db80 1944 return false;
kbhagat6 9:286d00d9db80 1945 }
kbhagat6 9:286d00d9db80 1946
kbhagat6 9:286d00d9db80 1947 return true;
kbhagat6 9:286d00d9db80 1948 }
kbhagat6 9:286d00d9db80 1949
kbhagat6 9:286d00d9db80 1950 /**
kbhagat6 9:286d00d9db80 1951 * @brief Gets if proximity interrupts are enabled or not
kbhagat6 9:286d00d9db80 1952 *
kbhagat6 9:286d00d9db80 1953 * @return 1 if interrupts are enabled, 0 if not. 0xFF on error.
kbhagat6 9:286d00d9db80 1954 */
kbhagat6 9:286d00d9db80 1955 uint8_t glibr::getProximityIntEnable()
kbhagat6 9:286d00d9db80 1956 {
kbhagat6 9:286d00d9db80 1957 uint8_t val;
kbhagat6 9:286d00d9db80 1958
kbhagat6 9:286d00d9db80 1959 /* Read value from ENABLE register */
kbhagat6 9:286d00d9db80 1960 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ENABLE);
kbhagat6 9:286d00d9db80 1961 if( val == ERROR){//!wireReadDataByte(APDS9960_ENABLE, val) ) {
kbhagat6 9:286d00d9db80 1962 return ERROR;
kbhagat6 9:286d00d9db80 1963 }
kbhagat6 9:286d00d9db80 1964
kbhagat6 9:286d00d9db80 1965 /* Shift and mask out PIEN bit */
kbhagat6 9:286d00d9db80 1966 val = (val >> 5) & 0x01;//0b00000001;
kbhagat6 9:286d00d9db80 1967
kbhagat6 9:286d00d9db80 1968 return val;
kbhagat6 9:286d00d9db80 1969 }
kbhagat6 9:286d00d9db80 1970
kbhagat6 9:286d00d9db80 1971 /**
kbhagat6 9:286d00d9db80 1972 * @brief Turns proximity interrupts on or off
kbhagat6 9:286d00d9db80 1973 *
kbhagat6 9:286d00d9db80 1974 * @param[in] enable 1 to enable interrupts, 0 to turn them off
kbhagat6 9:286d00d9db80 1975 * @return True if operation successful. False otherwise.
kbhagat6 9:286d00d9db80 1976 */
kbhagat6 9:286d00d9db80 1977 bool glibr::setProximityIntEnable(uint8_t enable)
kbhagat6 9:286d00d9db80 1978 {
kbhagat6 9:286d00d9db80 1979 uint8_t val;
kbhagat6 9:286d00d9db80 1980
kbhagat6 9:286d00d9db80 1981 /* Read value from ENABLE register */
kbhagat6 9:286d00d9db80 1982 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ENABLE);
kbhagat6 9:286d00d9db80 1983 if( val == ERROR){//!wireReadDataByte(APDS9960_ENABLE, val) ) {
kbhagat6 9:286d00d9db80 1984 return false;
kbhagat6 9:286d00d9db80 1985 }
kbhagat6 9:286d00d9db80 1986
kbhagat6 9:286d00d9db80 1987 /* Set bits in register to given value */
kbhagat6 9:286d00d9db80 1988 enable &= 0x01;//0b00000001;
kbhagat6 9:286d00d9db80 1989 enable = enable << 5;
kbhagat6 9:286d00d9db80 1990 val &= 0xDF;//0b11011111;
kbhagat6 9:286d00d9db80 1991 val |= enable;
kbhagat6 9:286d00d9db80 1992
kbhagat6 9:286d00d9db80 1993 /* Write register value back into ENABLE register */
kbhagat6 9:286d00d9db80 1994 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_ENABLE,val)){//!wireWriteDataByte(APDS9960_ENABLE, val) ) {
kbhagat6 9:286d00d9db80 1995 return false;
kbhagat6 9:286d00d9db80 1996 }
kbhagat6 9:286d00d9db80 1997
kbhagat6 9:286d00d9db80 1998 return true;
kbhagat6 9:286d00d9db80 1999 }
kbhagat6 9:286d00d9db80 2000
kbhagat6 9:286d00d9db80 2001 /**
kbhagat6 9:286d00d9db80 2002 * @brief Gets if gesture interrupts are enabled or not
kbhagat6 9:286d00d9db80 2003 *
kbhagat6 9:286d00d9db80 2004 * @return 1 if interrupts are enabled, 0 if not. 0xFF on error.
kbhagat6 9:286d00d9db80 2005 */
kbhagat6 9:286d00d9db80 2006 uint8_t glibr::getGestureIntEnable()
kbhagat6 9:286d00d9db80 2007 {
kbhagat6 9:286d00d9db80 2008 uint8_t val;
kbhagat6 9:286d00d9db80 2009
kbhagat6 9:286d00d9db80 2010 /* Read value from GCONF4 register */
kbhagat6 9:286d00d9db80 2011 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF4);
kbhagat6 9:286d00d9db80 2012 if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF4, val) ) {
kbhagat6 9:286d00d9db80 2013 return ERROR;
kbhagat6 9:286d00d9db80 2014 }
kbhagat6 9:286d00d9db80 2015
kbhagat6 9:286d00d9db80 2016 /* Shift and mask out GIEN bit */
kbhagat6 9:286d00d9db80 2017 val = (val >> 1) & 0x01;//0b00000001;
kbhagat6 9:286d00d9db80 2018
kbhagat6 9:286d00d9db80 2019 return val;
kbhagat6 9:286d00d9db80 2020 }
kbhagat6 9:286d00d9db80 2021
kbhagat6 9:286d00d9db80 2022 /**
kbhagat6 9:286d00d9db80 2023 * @brief Turns gesture-related interrupts on or off
kbhagat6 9:286d00d9db80 2024 *
kbhagat6 9:286d00d9db80 2025 * @param[in] enable 1 to enable interrupts, 0 to turn them off
kbhagat6 9:286d00d9db80 2026 * @return True if operation successful. False otherwise.
kbhagat6 9:286d00d9db80 2027 */
kbhagat6 9:286d00d9db80 2028 bool glibr::setGestureIntEnable(uint8_t enable)
kbhagat6 9:286d00d9db80 2029 {
kbhagat6 9:286d00d9db80 2030 uint8_t val;
kbhagat6 9:286d00d9db80 2031
kbhagat6 9:286d00d9db80 2032 /* Read value from GCONF4 register */
kbhagat6 9:286d00d9db80 2033 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF4);
kbhagat6 9:286d00d9db80 2034 if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF4, val) ) {
kbhagat6 9:286d00d9db80 2035 return false;
kbhagat6 9:286d00d9db80 2036 }
kbhagat6 9:286d00d9db80 2037
kbhagat6 9:286d00d9db80 2038 /* Set bits in register to given value */
kbhagat6 9:286d00d9db80 2039 enable &= 0x01;//0b00000001;
kbhagat6 9:286d00d9db80 2040 enable = enable << 1;
kbhagat6 9:286d00d9db80 2041 val &= 0xFD;//0b11111101;
kbhagat6 9:286d00d9db80 2042 val |= enable;
kbhagat6 9:286d00d9db80 2043
kbhagat6 9:286d00d9db80 2044 /* Write register value back into GCONF4 register */
kbhagat6 9:286d00d9db80 2045 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GCONF4,val)){//!wireWriteDataByte(APDS9960_GCONF4, val) ) {
kbhagat6 9:286d00d9db80 2046 return false;
kbhagat6 9:286d00d9db80 2047 }
kbhagat6 9:286d00d9db80 2048
kbhagat6 9:286d00d9db80 2049 return true;
kbhagat6 9:286d00d9db80 2050 }
kbhagat6 9:286d00d9db80 2051
kbhagat6 9:286d00d9db80 2052 /**
kbhagat6 9:286d00d9db80 2053 * @brief Clears the ambient light interrupt
kbhagat6 9:286d00d9db80 2054 *
kbhagat6 9:286d00d9db80 2055 * @return True if operation completed successfully. False otherwise.
kbhagat6 9:286d00d9db80 2056 */
kbhagat6 9:286d00d9db80 2057 bool glibr::clearAmbientLightInt()
kbhagat6 9:286d00d9db80 2058 {
kbhagat6 9:286d00d9db80 2059 uint8_t throwaway;
kbhagat6 9:286d00d9db80 2060 throwaway = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_AICLEAR);
kbhagat6 9:286d00d9db80 2061 if( throwaway == ERROR){//!wireReadDataByte(APDS9960_AICLEAR, throwaway) ) {
kbhagat6 9:286d00d9db80 2062 return false;
kbhagat6 9:286d00d9db80 2063 }
kbhagat6 9:286d00d9db80 2064
kbhagat6 9:286d00d9db80 2065 return true;
kbhagat6 9:286d00d9db80 2066 }
kbhagat6 9:286d00d9db80 2067
kbhagat6 9:286d00d9db80 2068 /**
kbhagat6 9:286d00d9db80 2069 * @brief Clears the proximity interrupt
kbhagat6 9:286d00d9db80 2070 *
kbhagat6 9:286d00d9db80 2071 * @return True if operation completed successfully. False otherwise.
kbhagat6 9:286d00d9db80 2072 */
kbhagat6 9:286d00d9db80 2073 bool glibr::clearProximityInt()
kbhagat6 9:286d00d9db80 2074 {
kbhagat6 9:286d00d9db80 2075 uint8_t throwaway;
kbhagat6 9:286d00d9db80 2076 throwaway = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_PICLEAR);
kbhagat6 9:286d00d9db80 2077 if( throwaway == ERROR){//!wireReadDataByte(APDS9960_PICLEAR, throwaway) ) {
kbhagat6 9:286d00d9db80 2078 return false;
kbhagat6 9:286d00d9db80 2079 }
kbhagat6 9:286d00d9db80 2080
kbhagat6 9:286d00d9db80 2081 return true;
kbhagat6 9:286d00d9db80 2082 }
kbhagat6 9:286d00d9db80 2083
kbhagat6 9:286d00d9db80 2084 /**
kbhagat6 9:286d00d9db80 2085 * @brief Tells if the gesture state machine is currently running
kbhagat6 9:286d00d9db80 2086 *
kbhagat6 9:286d00d9db80 2087 * @return 1 if gesture state machine is running, 0 if not. 0xFF on error.
kbhagat6 9:286d00d9db80 2088 */
kbhagat6 9:286d00d9db80 2089 uint8_t glibr::getGestureMode()
kbhagat6 9:286d00d9db80 2090 {
kbhagat6 9:286d00d9db80 2091 uint8_t val;
kbhagat6 9:286d00d9db80 2092
kbhagat6 9:286d00d9db80 2093 /* Read value from GCONF4 register */
kbhagat6 9:286d00d9db80 2094 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF4);
kbhagat6 9:286d00d9db80 2095 if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF4, val) ) {
kbhagat6 9:286d00d9db80 2096 return ERROR;
kbhagat6 9:286d00d9db80 2097 }
kbhagat6 9:286d00d9db80 2098
kbhagat6 9:286d00d9db80 2099 /* Mask out GMODE bit */
kbhagat6 9:286d00d9db80 2100 val &= 0x01;//0b00000001;
kbhagat6 9:286d00d9db80 2101
kbhagat6 9:286d00d9db80 2102 return val;
kbhagat6 9:286d00d9db80 2103 }
kbhagat6 9:286d00d9db80 2104
kbhagat6 9:286d00d9db80 2105 /**
kbhagat6 9:286d00d9db80 2106 * @brief Tells the state machine to either enter or exit gesture state machine
kbhagat6 9:286d00d9db80 2107 *
kbhagat6 9:286d00d9db80 2108 * @param[in] mode 1 to enter gesture state machine, 0 to exit.
kbhagat6 9:286d00d9db80 2109 * @return True if operation successful. False otherwise.
kbhagat6 9:286d00d9db80 2110 */
kbhagat6 9:286d00d9db80 2111 bool glibr::setGestureMode(uint8_t mode)
kbhagat6 9:286d00d9db80 2112 {
kbhagat6 9:286d00d9db80 2113 uint8_t val;
kbhagat6 9:286d00d9db80 2114
kbhagat6 9:286d00d9db80 2115 /* Read value from GCONF4 register */
kbhagat6 9:286d00d9db80 2116 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF4);
kbhagat6 9:286d00d9db80 2117 if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF4, val) ) {
kbhagat6 9:286d00d9db80 2118 return false;
kbhagat6 9:286d00d9db80 2119 }
kbhagat6 9:286d00d9db80 2120
kbhagat6 9:286d00d9db80 2121 /* Set bits in register to given value */
kbhagat6 9:286d00d9db80 2122 mode &= 0x01;//0b00000001;
kbhagat6 9:286d00d9db80 2123 val &= 0xFE;//0b11111110;
kbhagat6 9:286d00d9db80 2124 val |= mode;
kbhagat6 9:286d00d9db80 2125
kbhagat6 9:286d00d9db80 2126 /* Write register value back into GCONF4 register */
kbhagat6 9:286d00d9db80 2127 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GCONF4,val)){//!wireWriteDataByte(APDS9960_GCONF4, val) ) {
kbhagat6 1:c7215f5f9a72 2128 return false;
kbhagat6 1:c7215f5f9a72 2129 }
kbhagat6 1:c7215f5f9a72 2130
kbhagat6 1:c7215f5f9a72 2131 return true;
kbhagat6 1:c7215f5f9a72 2132 }
kbhagat6 1:c7215f5f9a72 2133
kbhagat6 5:3eb4f3091bd8 2134
kbhagat6 5:3eb4f3091bd8 2135
kbhagat6 5:3eb4f3091bd8 2136
kbhagat6 5:3eb4f3091bd8 2137
kbhagat6 5:3eb4f3091bd8 2138 int glibr::I2CwriteByte(char address, char subAddress, char data)
kbhagat6 5:3eb4f3091bd8 2139 {
kbhagat6 5:3eb4f3091bd8 2140 int ret;
kbhagat6 5:3eb4f3091bd8 2141 char cmd[2] = {subAddress, data};
kbhagat6 5:3eb4f3091bd8 2142 ret=i2c.write(address<<1, cmd, 2); //if ret is 1, then not acked.
kbhagat6 5:3eb4f3091bd8 2143 return ret;
kbhagat6 5:3eb4f3091bd8 2144 }
kbhagat6 5:3eb4f3091bd8 2145
kbhagat6 5:3eb4f3091bd8 2146
kbhagat6 5:3eb4f3091bd8 2147
kbhagat6 0:437ae08befe3 2148 uint8_t glibr::I2CreadByte(char address, char subAddress)
kbhagat6 0:437ae08befe3 2149 {
kbhagat6 0:437ae08befe3 2150 char data; // store the register data
kbhagat6 0:437ae08befe3 2151
kbhagat6 0:437ae08befe3 2152 if(i2c.write(address<<1, &subAddress, 1, true)){
kbhagat6 0:437ae08befe3 2153 return ERROR; //7 bit //not acked
kbhagat6 0:437ae08befe3 2154 }
kbhagat6 15:339ffde549dc 2155
kbhagat6 15:339ffde549dc 2156 if(i2c.read(address<<1, &data, 1)){
kbhagat6 15:339ffde549dc 2157 return ERROR;
kbhagat6 15:339ffde549dc 2158 }
kbhagat6 15:339ffde549dc 2159 //i2c.read(address<<1, &data, 1);
kbhagat6 0:437ae08befe3 2160 return data;
kbhagat6 0:437ae08befe3 2161
kbhagat6 0:437ae08befe3 2162 }
kbhagat6 5:3eb4f3091bd8 2163
kbhagat6 9:286d00d9db80 2164
kbhagat6 9:286d00d9db80 2165 // * @brief Reads a block (array) of bytes from the I2C device and register
kbhagat6 9:286d00d9db80 2166 // *
kbhagat6 9:286d00d9db80 2167 // * @param[in] reg the register to read from
kbhagat6 9:286d00d9db80 2168 // * @param[out] val pointer to the beginning of the data
kbhagat6 9:286d00d9db80 2169 // * @param[in] len number of bytes to read
kbhagat6 9:286d00d9db80 2170 // * @return Number of bytes read. -1 on read error.
kbhagat6 9:286d00d9db80 2171 // */
kbhagat6 9:286d00d9db80 2172 int glibr::I2CReadDataBlock(char address, char subAddress, char *data, unsigned int len)
kbhagat6 5:3eb4f3091bd8 2173 {
kbhagat6 5:3eb4f3091bd8 2174 // unsigned char i = 0;
kbhagat6 8:6fa15d4e31fb 2175
kbhagat6 5:3eb4f3091bd8 2176 /* Indicate which register we want to read from */
kbhagat6 5:3eb4f3091bd8 2177
kbhagat6 9:286d00d9db80 2178 if(i2c.write(address<<1, &subAddress, 1, true)){
kbhagat6 5:3eb4f3091bd8 2179 return -1; //7 bit //not acked
kbhagat6 8:6fa15d4e31fb 2180 }
kbhagat6 5:3eb4f3091bd8 2181
kbhagat6 5:3eb4f3091bd8 2182 /* Read block data */
kbhagat6 8:6fa15d4e31fb 2183
kbhagat6 9:286d00d9db80 2184 if(i2c.read(address<<1, data, len)){
kbhagat6 8:6fa15d4e31fb 2185 return -1;
kbhagat6 8:6fa15d4e31fb 2186 }
kbhagat6 5:3eb4f3091bd8 2187
kbhagat6 8:6fa15d4e31fb 2188 return 1;
kbhagat6 5:3eb4f3091bd8 2189 //Wire.requestFrom(APDS9960_I2C_ADDR, len);
kbhagat6 5:3eb4f3091bd8 2190 /*while (Wire.available()) {
kbhagat6 5:3eb4f3091bd8 2191 if (i >= len) {
kbhagat6 5:3eb4f3091bd8 2192 return -1;
kbhagat6 5:3eb4f3091bd8 2193 }
kbhagat6 5:3eb4f3091bd8 2194 val[i] = Wire.read();
kbhagat6 5:3eb4f3091bd8 2195 i++;
kbhagat6 5:3eb4f3091bd8 2196 }*/
kbhagat6 5:3eb4f3091bd8 2197 }