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 06 21:50:03 2015 +0000
Revision:
8:6fa15d4e31fb
Parent:
7:0564c6faf8aa
Child:
9:286d00d9db80
added/organized more.. getting Qualified name erro

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 0:437ae08befe3 25 uint8_t glibr::ginit(){
kbhagat6 0:437ae08befe3 26 uint8_t id;
kbhagat6 0:437ae08befe3 27
kbhagat6 0:437ae08befe3 28 // if( !wireReadDataByte(APDS9960_ID, id) ) {
kbhagat6 0:437ae08befe3 29
kbhagat6 0:437ae08befe3 30 id=I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ID);
kbhagat6 0:437ae08befe3 31
kbhagat6 0:437ae08befe3 32 if( (!(id == APDS9960_ID_1 || id == APDS9960_ID_2))||id==ERROR) {
kbhagat6 0:437ae08befe3 33 return false;
kbhagat6 0:437ae08befe3 34 }
kbhagat6 0:437ae08befe3 35
kbhagat6 0:437ae08befe3 36 if(!setMode(ALL, OFF)) {
kbhagat6 0:437ae08befe3 37 return false;
kbhagat6 0:437ae08befe3 38 }
kbhagat6 0:437ae08befe3 39 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_ATIME, DEFAULT_ATIME)){
kbhagat6 0:437ae08befe3 40 return false;
kbhagat6 0:437ae08befe3 41 }
kbhagat6 0:437ae08befe3 42 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_WTIME, DEFAULT_WTIME)){
kbhagat6 0:437ae08befe3 43 return false;
kbhagat6 0:437ae08befe3 44 }
kbhagat6 0:437ae08befe3 45 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PPULSE, DEFAULT_PROX_PPULSE)){
kbhagat6 0:437ae08befe3 46 return false;
kbhagat6 0:437ae08befe3 47 }
kbhagat6 0:437ae08befe3 48 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_POFFSET_UR, DEFAULT_POFFSET_UR)){
kbhagat6 0:437ae08befe3 49 return false;
kbhagat6 0:437ae08befe3 50 }
kbhagat6 0:437ae08befe3 51 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_POFFSET_DL, DEFAULT_POFFSET_DL)){
kbhagat6 0:437ae08befe3 52 return false;
kbhagat6 0:437ae08befe3 53 }
kbhagat6 0:437ae08befe3 54 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONFIG1, DEFAULT_CONFIG1)){
kbhagat6 0:437ae08befe3 55 return false;
kbhagat6 0:437ae08befe3 56 }
kbhagat6 0:437ae08befe3 57
kbhagat6 0:437ae08befe3 58 if( !setLEDDrive(DEFAULT_LDRIVE) ) {
kbhagat6 0:437ae08befe3 59 return false;
kbhagat6 0:437ae08befe3 60 }
kbhagat6 0:437ae08befe3 61
kbhagat6 0:437ae08befe3 62 if( !setProximityGain(DEFAULT_PGAIN) ) {
kbhagat6 0:437ae08befe3 63 return false;
kbhagat6 0:437ae08befe3 64 }
kbhagat6 0:437ae08befe3 65 if( !setAmbientLightGain(DEFAULT_AGAIN) ) {
kbhagat6 0:437ae08befe3 66 return false;
kbhagat6 0:437ae08befe3 67 }
kbhagat6 0:437ae08befe3 68 if( !setProxIntLowThresh(DEFAULT_PILT) ) {
kbhagat6 0:437ae08befe3 69 return false;
kbhagat6 0:437ae08befe3 70 }
kbhagat6 1:c7215f5f9a72 71 if( !setProxIntHighThresh(DEFAULT_PIHT) ) {
kbhagat6 1:c7215f5f9a72 72 return false;
kbhagat6 1:c7215f5f9a72 73 }
kbhagat6 1:c7215f5f9a72 74 if( !setLightIntLowThreshold(DEFAULT_AILT) ) {
kbhagat6 1:c7215f5f9a72 75 return false;
kbhagat6 1:c7215f5f9a72 76 }
kbhagat6 1:c7215f5f9a72 77 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONFIG2, DEFAULT_CONFIG2) ) {
kbhagat6 1:c7215f5f9a72 78 return false;
kbhagat6 1:c7215f5f9a72 79 }
kbhagat6 1:c7215f5f9a72 80 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONFIG3, DEFAULT_CONFIG3) ) {
kbhagat6 1:c7215f5f9a72 81 return false;
kbhagat6 1:c7215f5f9a72 82 }
kbhagat6 1:c7215f5f9a72 83
kbhagat6 1:c7215f5f9a72 84 if( !setGestureEnterThresh(DEFAULT_GPENTH) ) {
kbhagat6 1:c7215f5f9a72 85 return false;
kbhagat6 1:c7215f5f9a72 86 }
kbhagat6 1:c7215f5f9a72 87 if( !setGestureExitThresh(DEFAULT_GEXTH) ) {
kbhagat6 1:c7215f5f9a72 88 return false;
kbhagat6 1:c7215f5f9a72 89 }
kbhagat6 1:c7215f5f9a72 90 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GCONF1, DEFAULT_GCONF1) ) {
kbhagat6 1:c7215f5f9a72 91 return false;
kbhagat6 1:c7215f5f9a72 92 }
kbhagat6 1:c7215f5f9a72 93 if( !setGestureGain(DEFAULT_GGAIN) ) {
kbhagat6 1:c7215f5f9a72 94 return false;
kbhagat6 1:c7215f5f9a72 95 }
kbhagat6 1:c7215f5f9a72 96 if( !setGestureLEDDrive(DEFAULT_GLDRIVE) ) {
kbhagat6 1:c7215f5f9a72 97 return false;
kbhagat6 1:c7215f5f9a72 98 }
kbhagat6 1:c7215f5f9a72 99 if( !setGestureWaitTime(DEFAULT_GWTIME) ) {
kbhagat6 1:c7215f5f9a72 100 return false;
kbhagat6 1:c7215f5f9a72 101 }
kbhagat6 1:c7215f5f9a72 102 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GOFFSET_U, DEFAULT_GOFFSET) ) {
kbhagat6 1:c7215f5f9a72 103 return false;
kbhagat6 1:c7215f5f9a72 104 }
kbhagat6 1:c7215f5f9a72 105 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GOFFSET_D, DEFAULT_GOFFSET) ) {
kbhagat6 1:c7215f5f9a72 106 return false;
kbhagat6 1:c7215f5f9a72 107 }
kbhagat6 1:c7215f5f9a72 108 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GOFFSET_L, DEFAULT_GOFFSET) ) {
kbhagat6 1:c7215f5f9a72 109 return false;
kbhagat6 1:c7215f5f9a72 110 }
kbhagat6 1:c7215f5f9a72 111 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GOFFSET_R, DEFAULT_GOFFSET) ) {
kbhagat6 1:c7215f5f9a72 112 return false;
kbhagat6 1:c7215f5f9a72 113 }
kbhagat6 1:c7215f5f9a72 114 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GPULSE, DEFAULT_GPULSE) ) {
kbhagat6 1:c7215f5f9a72 115 return false;
kbhagat6 1:c7215f5f9a72 116 }
kbhagat6 1:c7215f5f9a72 117 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GCONF3, DEFAULT_GCONF3) ) {
kbhagat6 1:c7215f5f9a72 118 return false;
kbhagat6 1:c7215f5f9a72 119 }
kbhagat6 1:c7215f5f9a72 120 if( !setGestureIntEnable(DEFAULT_GIEN) ) {
kbhagat6 1:c7215f5f9a72 121 return false;
kbhagat6 1:c7215f5f9a72 122 }
kbhagat6 0:437ae08befe3 123
kbhagat6 0:437ae08befe3 124
kbhagat6 0:437ae08befe3 125
kbhagat6 0:437ae08befe3 126 return id;
kbhagat6 0:437ae08befe3 127 }
kbhagat6 8:6fa15d4e31fb 128
kbhagat6 7:0564c6faf8aa 129 #if 0
kbhagat6 7:0564c6faf8aa 130 /* Gesture config register dump */
kbhagat6 7:0564c6faf8aa 131 uint8_t reg;
kbhagat6 7:0564c6faf8aa 132 uint8_t val;
kbhagat6 7:0564c6faf8aa 133
kbhagat6 7:0564c6faf8aa 134 for(reg = 0x80; reg <= 0xAF; reg++) {
kbhagat6 7:0564c6faf8aa 135 if( (reg != 0x82) && \
kbhagat6 7:0564c6faf8aa 136 (reg != 0x8A) && \
kbhagat6 7:0564c6faf8aa 137 (reg != 0x91) && \
kbhagat6 7:0564c6faf8aa 138 (reg != 0xA8) && \
kbhagat6 7:0564c6faf8aa 139 (reg != 0xAC) && \
kbhagat6 7:0564c6faf8aa 140 (reg != 0xAD) )
kbhagat6 7:0564c6faf8aa 141 {
kbhagat6 7:0564c6faf8aa 142 val= I2CreadByte(APDS9960_I2C_ADDR, reg);
kbhagat6 7:0564c6faf8aa 143 if(val==ERROR){
kbhagat6 7:0564c6faf8aa 144 printf("ERROR");
kbhagat6 7:0564c6faf8aa 145 }
kbhagat6 7:0564c6faf8aa 146 /*
kbhagat6 7:0564c6faf8aa 147 print(reg, HEX);
kbhagat6 7:0564c6faf8aa 148 print(": 0x");
kbhagat6 7:0564c6faf8aa 149 println(val, HEX);*/
kbhagat6 7:0564c6faf8aa 150 }
kbhagat6 7:0564c6faf8aa 151 }
kbhagat6 0:437ae08befe3 152
kbhagat6 7:0564c6faf8aa 153 for(reg = 0xE4; reg <= 0xE7; reg++) {
kbhagat6 7:0564c6faf8aa 154 val= I2CreadByte(APDS9960_I2C_ADDR, reg);
kbhagat6 8:6fa15d4e31fb 155 /* Serial.print(reg, HEX);
kbhagat6 7:0564c6faf8aa 156 Serial.print(": 0x");
kbhagat6 8:6fa15d4e31fb 157 Serial.println(val, HEX);*/
kbhagat6 7:0564c6faf8aa 158 }
kbhagat6 8:6fa15d4e31fb 159 #endif
kbhagat6 7:0564c6faf8aa 160
kbhagat6 8:6fa15d4e31fb 161 // return true;
kbhagat6 8:6fa15d4e31fb 162 //}
kbhagat6 0:437ae08befe3 163
kbhagat6 0:437ae08befe3 164
kbhagat6 0:437ae08befe3 165
kbhagat6 0:437ae08befe3 166 /**
kbhagat6 0:437ae08befe3 167 * @brief Enables or disables a feature in the APDS-9960
kbhagat6 0:437ae08befe3 168 *
kbhagat6 0:437ae08befe3 169 * @param[in] mode which feature to enable
kbhagat6 0:437ae08befe3 170 * @param[in] enable ON (1) or OFF (0)
kbhagat6 0:437ae08befe3 171 * @return True if operation success. False otherwise.
kbhagat6 0:437ae08befe3 172 */
kbhagat6 0:437ae08befe3 173 bool glibr::setMode(uint8_t mode, uint8_t enable)
kbhagat6 0:437ae08befe3 174 {
kbhagat6 0:437ae08befe3 175 uint8_t reg_val;
kbhagat6 0:437ae08befe3 176
kbhagat6 0:437ae08befe3 177 /* Read current ENABLE register */
kbhagat6 0:437ae08befe3 178 reg_val = getMode();
kbhagat6 0:437ae08befe3 179 if( reg_val == ERROR ) {
kbhagat6 0:437ae08befe3 180 return false;
kbhagat6 0:437ae08befe3 181 }
kbhagat6 0:437ae08befe3 182
kbhagat6 0:437ae08befe3 183 /* Change bit(s) in ENABLE register */
kbhagat6 0:437ae08befe3 184 enable = enable & 0x01;
kbhagat6 0:437ae08befe3 185 if( mode >= 0 && mode <= 6 ) {
kbhagat6 0:437ae08befe3 186 if (enable) {
kbhagat6 0:437ae08befe3 187 reg_val |= (1 << mode);
kbhagat6 0:437ae08befe3 188 } else {
kbhagat6 0:437ae08befe3 189 reg_val &= ~(1 << mode);
kbhagat6 0:437ae08befe3 190 }
kbhagat6 0:437ae08befe3 191 } else if( mode == ALL ) {
kbhagat6 0:437ae08befe3 192 if (enable) {
kbhagat6 0:437ae08befe3 193 reg_val = 0x7F;
kbhagat6 0:437ae08befe3 194 } else {
kbhagat6 0:437ae08befe3 195 reg_val = 0x00;
kbhagat6 0:437ae08befe3 196 }
kbhagat6 0:437ae08befe3 197 }
kbhagat6 0:437ae08befe3 198
kbhagat6 0:437ae08befe3 199 /* Write value back to ENABLE register */
kbhagat6 0:437ae08befe3 200 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_ENABLE, reg_val)){
kbhagat6 0:437ae08befe3 201 return false;
kbhagat6 0:437ae08befe3 202 }
kbhagat6 0:437ae08befe3 203
kbhagat6 0:437ae08befe3 204
kbhagat6 0:437ae08befe3 205
kbhagat6 0:437ae08befe3 206 return true;
kbhagat6 0:437ae08befe3 207 }
kbhagat6 0:437ae08befe3 208
kbhagat6 0:437ae08befe3 209 uint8_t glibr::getMode()
kbhagat6 0:437ae08befe3 210 {
kbhagat6 6:687dff16904e 211 uint8_t val;
kbhagat6 6:687dff16904e 212 val= I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ENABLE);
kbhagat6 6:687dff16904e 213 if(val==ERROR){
kbhagat6 6:687dff16904e 214 return ERROR;
kbhagat6 6:687dff16904e 215 }
kbhagat6 6:687dff16904e 216 return val;
kbhagat6 0:437ae08befe3 217 }
kbhagat6 0:437ae08befe3 218
kbhagat6 0:437ae08befe3 219
kbhagat6 0:437ae08befe3 220
kbhagat6 3:26146a08bb22 221 bool glibr::enableLightSensor(bool interrupts)
kbhagat6 3:26146a08bb22 222 {
kbhagat6 3:26146a08bb22 223
kbhagat6 3:26146a08bb22 224 /* Set default gain, interrupts, enable power, and enable sensor */
kbhagat6 3:26146a08bb22 225 if( !setAmbientLightGain(DEFAULT_AGAIN) ) {
kbhagat6 3:26146a08bb22 226 return false;
kbhagat6 3:26146a08bb22 227 }
kbhagat6 3:26146a08bb22 228 if( interrupts ) {
kbhagat6 3:26146a08bb22 229 if( !setAmbientLightIntEnable(1) ) {
kbhagat6 3:26146a08bb22 230 return false;
kbhagat6 3:26146a08bb22 231 }
kbhagat6 3:26146a08bb22 232 } else {
kbhagat6 3:26146a08bb22 233 if( !setAmbientLightIntEnable(0) ) {
kbhagat6 3:26146a08bb22 234 return false;
kbhagat6 3:26146a08bb22 235 }
kbhagat6 3:26146a08bb22 236 }
kbhagat6 3:26146a08bb22 237 if( !enablePower() ){
kbhagat6 3:26146a08bb22 238 return false;
kbhagat6 3:26146a08bb22 239 }
kbhagat6 3:26146a08bb22 240 if( !setMode(AMBIENT_LIGHT, 1) ) {
kbhagat6 3:26146a08bb22 241 return false;
kbhagat6 3:26146a08bb22 242 }
kbhagat6 3:26146a08bb22 243
kbhagat6 3:26146a08bb22 244 return true;
kbhagat6 3:26146a08bb22 245
kbhagat6 3:26146a08bb22 246 }
kbhagat6 3:26146a08bb22 247
kbhagat6 3:26146a08bb22 248 /**
kbhagat6 3:26146a08bb22 249 * @brief Ends the light sensor on the APDS-9960
kbhagat6 3:26146a08bb22 250 *
kbhagat6 3:26146a08bb22 251 * @return True if sensor disabled correctly. False on error.
kbhagat6 3:26146a08bb22 252 */
kbhagat6 3:26146a08bb22 253 bool glibr::disableLightSensor()
kbhagat6 3:26146a08bb22 254 {
kbhagat6 3:26146a08bb22 255 if( !setAmbientLightIntEnable(0) ) {
kbhagat6 3:26146a08bb22 256 return false;
kbhagat6 3:26146a08bb22 257 }
kbhagat6 3:26146a08bb22 258 if( !setMode(AMBIENT_LIGHT, 0) ) {
kbhagat6 3:26146a08bb22 259 return false;
kbhagat6 3:26146a08bb22 260 }
kbhagat6 3:26146a08bb22 261
kbhagat6 3:26146a08bb22 262 return true;
kbhagat6 3:26146a08bb22 263 }
kbhagat6 3:26146a08bb22 264
kbhagat6 3:26146a08bb22 265 /**
kbhagat6 3:26146a08bb22 266 * @brief Starts the proximity sensor on the APDS-9960
kbhagat6 3:26146a08bb22 267 *
kbhagat6 3:26146a08bb22 268 * @param[in] interrupts true to enable hardware external interrupt on proximity
kbhagat6 3:26146a08bb22 269 * @return True if sensor enabled correctly. False on error.
kbhagat6 3:26146a08bb22 270 */
kbhagat6 3:26146a08bb22 271 bool glibr::enableProximitySensor(bool interrupts)
kbhagat6 3:26146a08bb22 272 {
kbhagat6 3:26146a08bb22 273 /* Set default gain, LED, interrupts, enable power, and enable sensor */
kbhagat6 3:26146a08bb22 274 if( !setProximityGain(DEFAULT_PGAIN) ) {
kbhagat6 3:26146a08bb22 275 return false;
kbhagat6 3:26146a08bb22 276 }
kbhagat6 3:26146a08bb22 277 if( !setLEDDrive(DEFAULT_LDRIVE) ) {
kbhagat6 3:26146a08bb22 278 return false;
kbhagat6 3:26146a08bb22 279 }
kbhagat6 3:26146a08bb22 280 if( interrupts ) {
kbhagat6 3:26146a08bb22 281 if( !setProximityIntEnable(1) ) {
kbhagat6 3:26146a08bb22 282 return false;
kbhagat6 3:26146a08bb22 283 }
kbhagat6 3:26146a08bb22 284 } else {
kbhagat6 3:26146a08bb22 285 if( !setProximityIntEnable(0) ) {
kbhagat6 3:26146a08bb22 286 return false;
kbhagat6 3:26146a08bb22 287 }
kbhagat6 3:26146a08bb22 288 }
kbhagat6 3:26146a08bb22 289 if( !enablePower() ){
kbhagat6 3:26146a08bb22 290 return false;
kbhagat6 3:26146a08bb22 291 }
kbhagat6 3:26146a08bb22 292 if( !setMode(PROXIMITY, 1) ) {
kbhagat6 3:26146a08bb22 293 return false;
kbhagat6 3:26146a08bb22 294 }
kbhagat6 3:26146a08bb22 295
kbhagat6 3:26146a08bb22 296 return true;
kbhagat6 3:26146a08bb22 297 }
kbhagat6 3:26146a08bb22 298
kbhagat6 3:26146a08bb22 299 /**
kbhagat6 3:26146a08bb22 300 * @brief Ends the proximity sensor on the APDS-9960
kbhagat6 3:26146a08bb22 301 *
kbhagat6 3:26146a08bb22 302 * @return True if sensor disabled correctly. False on error.
kbhagat6 3:26146a08bb22 303 */
kbhagat6 3:26146a08bb22 304 bool glibr::disableProximitySensor()
kbhagat6 3:26146a08bb22 305 {
kbhagat6 3:26146a08bb22 306 if( !setProximityIntEnable(0) ) {
kbhagat6 3:26146a08bb22 307 return false;
kbhagat6 3:26146a08bb22 308 }
kbhagat6 3:26146a08bb22 309 if( !setMode(PROXIMITY, 0) ) {
kbhagat6 3:26146a08bb22 310 return false;
kbhagat6 3:26146a08bb22 311 }
kbhagat6 3:26146a08bb22 312
kbhagat6 3:26146a08bb22 313 return true;
kbhagat6 3:26146a08bb22 314 }
kbhagat6 3:26146a08bb22 315
kbhagat6 3:26146a08bb22 316
kbhagat6 3:26146a08bb22 317 /**
kbhagat6 3:26146a08bb22 318 * @brief Starts the gesture recognition engine on the APDS-9960
kbhagat6 3:26146a08bb22 319 *
kbhagat6 3:26146a08bb22 320 * @param[in] interrupts true to enable hardware external interrupt on gesture
kbhagat6 3:26146a08bb22 321 * @return True if engine enabled correctly. False on error.
kbhagat6 3:26146a08bb22 322 */
kbhagat6 3:26146a08bb22 323 bool glibr::enableGestureSensor(bool interrupts)
kbhagat6 3:26146a08bb22 324 {
kbhagat6 3:26146a08bb22 325
kbhagat6 3:26146a08bb22 326 /* Enable gesture mode
kbhagat6 3:26146a08bb22 327 Set ENABLE to 0 (power off)
kbhagat6 3:26146a08bb22 328 Set WTIME to 0xFF
kbhagat6 3:26146a08bb22 329 Set AUX to LED_BOOST_300
kbhagat6 3:26146a08bb22 330 Enable PON, WEN, PEN, GEN in ENABLE
kbhagat6 3:26146a08bb22 331 */
kbhagat6 3:26146a08bb22 332
kbhagat6 3:26146a08bb22 333 resetGestureParameters();
kbhagat6 3:26146a08bb22 334 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_WTIME, 0xFF) ) {
kbhagat6 3:26146a08bb22 335 return false;
kbhagat6 3:26146a08bb22 336 }
kbhagat6 3:26146a08bb22 337 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PPULSE, DEFAULT_GESTURE_PPULSE) ) {
kbhagat6 3:26146a08bb22 338 return false;
kbhagat6 3:26146a08bb22 339 }
kbhagat6 3:26146a08bb22 340 if( !setLEDBoost(LED_BOOST_300) ) {
kbhagat6 3:26146a08bb22 341 return false;
kbhagat6 3:26146a08bb22 342 }
kbhagat6 3:26146a08bb22 343 if( interrupts ) {
kbhagat6 3:26146a08bb22 344 if( !setGestureIntEnable(1) ) {
kbhagat6 3:26146a08bb22 345 return false;
kbhagat6 3:26146a08bb22 346 }
kbhagat6 3:26146a08bb22 347 } else {
kbhagat6 3:26146a08bb22 348 if( !setGestureIntEnable(0) ) {
kbhagat6 3:26146a08bb22 349 return false;
kbhagat6 3:26146a08bb22 350 }
kbhagat6 3:26146a08bb22 351 }
kbhagat6 3:26146a08bb22 352 if( !setGestureMode(1) ) {
kbhagat6 3:26146a08bb22 353 return false;
kbhagat6 3:26146a08bb22 354 }
kbhagat6 3:26146a08bb22 355 if( !enablePower() ){
kbhagat6 3:26146a08bb22 356 return false;
kbhagat6 3:26146a08bb22 357 }
kbhagat6 3:26146a08bb22 358 if( !setMode(WAIT, 1) ) {
kbhagat6 3:26146a08bb22 359 return false;
kbhagat6 3:26146a08bb22 360 }
kbhagat6 3:26146a08bb22 361 if( !setMode(PROXIMITY, 1) ) {
kbhagat6 3:26146a08bb22 362 return false;
kbhagat6 3:26146a08bb22 363 }
kbhagat6 3:26146a08bb22 364 if( !setMode(GESTURE, 1) ) {
kbhagat6 3:26146a08bb22 365 return false;
kbhagat6 3:26146a08bb22 366 }
kbhagat6 3:26146a08bb22 367
kbhagat6 3:26146a08bb22 368 return true;
kbhagat6 3:26146a08bb22 369 }
kbhagat6 3:26146a08bb22 370
kbhagat6 3:26146a08bb22 371 /**
kbhagat6 3:26146a08bb22 372 * @brief Ends the gesture recognition engine on the APDS-9960
kbhagat6 3:26146a08bb22 373 *
kbhagat6 3:26146a08bb22 374 * @return True if engine disabled correctly. False on error.
kbhagat6 3:26146a08bb22 375 */
kbhagat6 3:26146a08bb22 376 bool glibr::disableGestureSensor()
kbhagat6 3:26146a08bb22 377 {
kbhagat6 3:26146a08bb22 378 resetGestureParameters();
kbhagat6 3:26146a08bb22 379 if( !setGestureIntEnable(0) ) {
kbhagat6 3:26146a08bb22 380 return false;
kbhagat6 3:26146a08bb22 381 }
kbhagat6 3:26146a08bb22 382 if( !setGestureMode(0) ) {
kbhagat6 3:26146a08bb22 383 return false;
kbhagat6 3:26146a08bb22 384 }
kbhagat6 3:26146a08bb22 385 if( !setMode(GESTURE, 0) ) {
kbhagat6 3:26146a08bb22 386 return false;
kbhagat6 3:26146a08bb22 387 }
kbhagat6 3:26146a08bb22 388
kbhagat6 3:26146a08bb22 389 return true;
kbhagat6 3:26146a08bb22 390 }
kbhagat6 3:26146a08bb22 391
kbhagat6 3:26146a08bb22 392
kbhagat6 3:26146a08bb22 393 /**
kbhagat6 3:26146a08bb22 394 * @brief Determines if there is a gesture available for reading
kbhagat6 3:26146a08bb22 395 *
kbhagat6 3:26146a08bb22 396 * @return True if gesture available. False otherwise.
kbhagat6 3:26146a08bb22 397 */
kbhagat6 3:26146a08bb22 398 bool glibr::isGestureAvailable()
kbhagat6 3:26146a08bb22 399 {
kbhagat6 3:26146a08bb22 400 uint8_t val;
kbhagat6 3:26146a08bb22 401
kbhagat6 3:26146a08bb22 402 /* Read value from GSTATUS register */
kbhagat6 4:84545b0e63a9 403 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_GSTATUS);
kbhagat6 5:3eb4f3091bd8 404 if( val==ERROR) {
kbhagat6 3:26146a08bb22 405 return ERROR;
kbhagat6 3:26146a08bb22 406 }
kbhagat6 3:26146a08bb22 407
kbhagat6 3:26146a08bb22 408 /* Shift and mask out GVALID bit */
kbhagat6 3:26146a08bb22 409 val &= APDS9960_GVALID;
kbhagat6 3:26146a08bb22 410
kbhagat6 3:26146a08bb22 411 /* Return true/false based on GVALID bit */
kbhagat6 3:26146a08bb22 412 if( val == 1) {
kbhagat6 3:26146a08bb22 413 return true;
kbhagat6 3:26146a08bb22 414 } else {
kbhagat6 3:26146a08bb22 415 return false;
kbhagat6 3:26146a08bb22 416 }
kbhagat6 3:26146a08bb22 417 }
kbhagat6 3:26146a08bb22 418
kbhagat6 4:84545b0e63a9 419 int glibr::readGesture()
kbhagat6 4:84545b0e63a9 420 {
kbhagat6 4:84545b0e63a9 421 uint8_t fifo_level = 0;
kbhagat6 8:6fa15d4e31fb 422 // uint8_t bytes_expected= 0;
kbhagat6 8:6fa15d4e31fb 423 int check;
kbhagat6 4:84545b0e63a9 424 uint8_t fifo_data[128];
kbhagat6 4:84545b0e63a9 425 uint8_t gstatus;
kbhagat6 4:84545b0e63a9 426 int motion;
kbhagat6 4:84545b0e63a9 427 int i;
kbhagat6 4:84545b0e63a9 428
kbhagat6 4:84545b0e63a9 429 /* Make sure that power and gesture is on and data is valid */
kbhagat6 4:84545b0e63a9 430 if( !isGestureAvailable() || !(getMode() & 0x41) ) {
kbhagat6 4:84545b0e63a9 431 return DIR_NONE;
kbhagat6 4:84545b0e63a9 432 }
kbhagat6 4:84545b0e63a9 433
kbhagat6 4:84545b0e63a9 434 /* Keep looping as long as gesture data is valid */
kbhagat6 4:84545b0e63a9 435 while(1) {
kbhagat6 8:6fa15d4e31fb 436
kbhagat6 4:84545b0e63a9 437 /* Wait some time to collect next batch of FIFO data */
kbhagat6 5:3eb4f3091bd8 438 wait(FIFO_PAUSE_TIME);
kbhagat6 4:84545b0e63a9 439
kbhagat6 4:84545b0e63a9 440 /* Get the contents of the STATUS register. Is data still valid? */
kbhagat6 4:84545b0e63a9 441
kbhagat6 4:84545b0e63a9 442 gstatus=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_GSTATUS);
kbhagat6 4:84545b0e63a9 443 if( gstatus==ERROR ) {
kbhagat6 4:84545b0e63a9 444 return ERROR;
kbhagat6 4:84545b0e63a9 445 }
kbhagat6 4:84545b0e63a9 446 /* If we have valid data, read in FIFO */
kbhagat6 4:84545b0e63a9 447 if( (gstatus & APDS9960_GVALID) == APDS9960_GVALID ) {
kbhagat6 4:84545b0e63a9 448
kbhagat6 4:84545b0e63a9 449 /* Read the current FIFO level */
kbhagat6 8:6fa15d4e31fb 450 fifo_level=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_GFLVL);
kbhagat6 8:6fa15d4e31fb 451 if( fifo_level==ERROR ) {
kbhagat6 4:84545b0e63a9 452 return ERROR;
kbhagat6 4:84545b0e63a9 453 }
kbhagat6 8:6fa15d4e31fb 454
kbhagat6 4:84545b0e63a9 455
kbhagat6 4:84545b0e63a9 456 //#if DEBUG
kbhagat6 4:84545b0e63a9 457 // Serial.print("FIFO Level: ");
kbhagat6 4:84545b0e63a9 458 // Serial.println(fifo_level);
kbhagat6 4:84545b0e63a9 459 //#endif
kbhagat6 4:84545b0e63a9 460
kbhagat6 4:84545b0e63a9 461 /* If there's stuff in the FIFO, read it into our data block */ //NEED TO FIGURE OUT WHAT THIS IS DOING.
kbhagat6 5:3eb4f3091bd8 462
kbhagat6 4:84545b0e63a9 463 if( fifo_level > 0) {
kbhagat6 8:6fa15d4e31fb 464 check = I2CReadDataBlock(APDS9960_I2C_ADDR,APDS9960_GFIFO_U,
kbhagat6 4:84545b0e63a9 465 (uint8_t*)fifo_data,
kbhagat6 4:84545b0e63a9 466 (fifo_level * 4) );
kbhagat6 8:6fa15d4e31fb 467
kbhagat6 8:6fa15d4e31fb 468 if( check == -1 ) {
kbhagat6 4:84545b0e63a9 469 return ERROR;
kbhagat6 4:84545b0e63a9 470 }
kbhagat6 4:84545b0e63a9 471 #if DEBUG
kbhagat6 8:6fa15d4e31fb 472 //Serial.print("FIFO Dump: ");
kbhagat6 8:6fa15d4e31fb 473 for ( i = 0; i < (fifo_level * 4); i++ ) {
kbhagat6 8:6fa15d4e31fb 474 // Serial.print(fifo_data[i]);
kbhagat6 8:6fa15d4e31fb 475 // Serial.print(" ");
kbhagat6 4:84545b0e63a9 476 }
kbhagat6 8:6fa15d4e31fb 477 //Serial.println();
kbhagat6 4:84545b0e63a9 478 #endif
kbhagat6 4:84545b0e63a9 479
kbhagat6 4:84545b0e63a9 480 /* If at least 1 set of data, sort the data into U/D/L/R */
kbhagat6 8:6fa15d4e31fb 481 if((fifo_level * 4) >= 4 ) {
kbhagat6 8:6fa15d4e31fb 482 for( i = 0; i < (fifo_level * 4); i += 4 ) {
kbhagat6 8:6fa15d4e31fb 483 gesture_data_.u_data[gesture_data_.sindex] = \
kbhagat6 4:84545b0e63a9 484 fifo_data[i + 0];
kbhagat6 8:6fa15d4e31fb 485 gesture_data_.d_data[gesture_data_.sindex] = \
kbhagat6 4:84545b0e63a9 486 fifo_data[i + 1];
kbhagat6 8:6fa15d4e31fb 487 gesture_data_.l_data[gesture_data_.sindex] = \
kbhagat6 4:84545b0e63a9 488 fifo_data[i + 2];
kbhagat6 8:6fa15d4e31fb 489 gesture_data_.r_data[gesture_data_.sindex] = \
kbhagat6 4:84545b0e63a9 490 fifo_data[i + 3];
kbhagat6 8:6fa15d4e31fb 491 gesture_data_.sindex++;
kbhagat6 4:84545b0e63a9 492 gesture_data_.total_gestures++;
kbhagat6 4:84545b0e63a9 493 }
kbhagat6 4:84545b0e63a9 494
kbhagat6 4:84545b0e63a9 495 #if DEBUG
kbhagat6 5:3eb4f3091bd8 496 // Serial.print("Up Data: ");
kbhagat6 4:84545b0e63a9 497 for ( i = 0; i < gesture_data_.total_gestures; i++ ) {
kbhagat6 5:3eb4f3091bd8 498 // Serial.print(gesture_data_.u_data[i]);
kbhagat6 5:3eb4f3091bd8 499 // Serial.print(" ");
kbhagat6 4:84545b0e63a9 500 }
kbhagat6 5:3eb4f3091bd8 501 // Serial.println();
kbhagat6 4:84545b0e63a9 502 #endif
kbhagat6 4:84545b0e63a9 503
kbhagat6 4:84545b0e63a9 504 /* Filter and process gesture data. Decode near/far state */
kbhagat6 4:84545b0e63a9 505 if( processGestureData() ) {
kbhagat6 4:84545b0e63a9 506 if( decodeGesture() ) {
kbhagat6 4:84545b0e63a9 507 //***TODO: U-Turn Gestures
kbhagat6 4:84545b0e63a9 508 #if DEBUG
kbhagat6 4:84545b0e63a9 509 //Serial.println(gesture_motion_);
kbhagat6 4:84545b0e63a9 510 #endif
kbhagat6 4:84545b0e63a9 511 }
kbhagat6 4:84545b0e63a9 512 }
kbhagat6 4:84545b0e63a9 513
kbhagat6 4:84545b0e63a9 514 /* Reset data */
kbhagat6 8:6fa15d4e31fb 515 gesture_data_.sindex = 0;
kbhagat6 4:84545b0e63a9 516 gesture_data_.total_gestures = 0;
kbhagat6 4:84545b0e63a9 517 }
kbhagat6 4:84545b0e63a9 518 }
kbhagat6 4:84545b0e63a9 519 } else {
kbhagat6 4:84545b0e63a9 520
kbhagat6 4:84545b0e63a9 521 /* Determine best guessed gesture and clean up */
kbhagat6 5:3eb4f3091bd8 522 wait(FIFO_PAUSE_TIME);
kbhagat6 4:84545b0e63a9 523 decodeGesture();
kbhagat6 4:84545b0e63a9 524 motion = gesture_motion_;
kbhagat6 4:84545b0e63a9 525 #if DEBUG
kbhagat6 5:3eb4f3091bd8 526 // Serial.print("END: ");
kbhagat6 5:3eb4f3091bd8 527 // Serial.println(gesture_motion_);
kbhagat6 4:84545b0e63a9 528 #endif
kbhagat6 4:84545b0e63a9 529 resetGestureParameters();
kbhagat6 4:84545b0e63a9 530 return motion;
kbhagat6 4:84545b0e63a9 531 }
kbhagat6 4:84545b0e63a9 532 }
kbhagat6 4:84545b0e63a9 533 }
kbhagat6 6:687dff16904e 534 /**
kbhagat6 6:687dff16904e 535 * Turn the APDS-9960 on
kbhagat6 6:687dff16904e 536 *
kbhagat6 6:687dff16904e 537 * @return True if operation successful. False otherwise.
kbhagat6 6:687dff16904e 538 */
kbhagat6 6:687dff16904e 539 bool glibr::enablePower()
kbhagat6 6:687dff16904e 540 {
kbhagat6 6:687dff16904e 541 if( !setMode(POWER, 1) ) {
kbhagat6 6:687dff16904e 542 return false;
kbhagat6 6:687dff16904e 543 }
kbhagat6 6:687dff16904e 544
kbhagat6 6:687dff16904e 545 return true;
kbhagat6 6:687dff16904e 546 }
kbhagat6 6:687dff16904e 547
kbhagat6 6:687dff16904e 548 /**
kbhagat6 6:687dff16904e 549 * Turn the APDS-9960 off
kbhagat6 6:687dff16904e 550 *
kbhagat6 6:687dff16904e 551 * @return True if operation successful. False otherwise.
kbhagat6 6:687dff16904e 552 */
kbhagat6 6:687dff16904e 553 bool glibr::disablePower()
kbhagat6 6:687dff16904e 554 {
kbhagat6 6:687dff16904e 555 if( !setMode(POWER, 0) ) {
kbhagat6 6:687dff16904e 556 return false;
kbhagat6 6:687dff16904e 557 }
kbhagat6 6:687dff16904e 558
kbhagat6 6:687dff16904e 559 return true;
kbhagat6 6:687dff16904e 560 }
kbhagat6 6:687dff16904e 561
kbhagat6 6:687dff16904e 562 /*******************************************************************************
kbhagat6 6:687dff16904e 563 * Ambient light and color sensor controls
kbhagat6 6:687dff16904e 564 ******************************************************************************/
kbhagat6 6:687dff16904e 565
kbhagat6 6:687dff16904e 566 /**
kbhagat6 6:687dff16904e 567 * @brief Reads the ambient (clear) light level as a 16-bit value
kbhagat6 6:687dff16904e 568 *
kbhagat6 6:687dff16904e 569 * @param[out] val value of the light sensor.
kbhagat6 6:687dff16904e 570 * @return True if operation successful. False otherwise.
kbhagat6 6:687dff16904e 571 */
kbhagat6 6:687dff16904e 572 bool glibr::readAmbientLight(uint16_t &val)
kbhagat6 6:687dff16904e 573 {
kbhagat6 6:687dff16904e 574 uint8_t val_byte;
kbhagat6 6:687dff16904e 575 val = 0;
kbhagat6 6:687dff16904e 576
kbhagat6 6:687dff16904e 577 /* Read value from clear channel, low byte register */
kbhagat6 6:687dff16904e 578 val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CDATAL);
kbhagat6 8:6fa15d4e31fb 579 if( val_byte==ERROR) {
kbhagat6 6:687dff16904e 580 return false;
kbhagat6 6:687dff16904e 581 }
kbhagat6 6:687dff16904e 582 val = val_byte;
kbhagat6 6:687dff16904e 583
kbhagat6 6:687dff16904e 584 /* Read value from clear channel, high byte register */
kbhagat6 6:687dff16904e 585
kbhagat6 6:687dff16904e 586 val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CDATAH);
kbhagat6 8:6fa15d4e31fb 587 if( val_byte==ERROR) {
kbhagat6 6:687dff16904e 588 return false;
kbhagat6 6:687dff16904e 589 }
kbhagat6 6:687dff16904e 590 val = val + ((uint16_t)val_byte << 8);
kbhagat6 6:687dff16904e 591
kbhagat6 6:687dff16904e 592 return true;
kbhagat6 6:687dff16904e 593 }
kbhagat6 6:687dff16904e 594
kbhagat6 6:687dff16904e 595 /**
kbhagat6 6:687dff16904e 596 * @brief Reads the red light level as a 16-bit value
kbhagat6 6:687dff16904e 597 *
kbhagat6 6:687dff16904e 598 * @param[out] val value of the light sensor.
kbhagat6 6:687dff16904e 599 * @return True if operation successful. False otherwise.
kbhagat6 6:687dff16904e 600 */
kbhagat6 6:687dff16904e 601 bool glibr::readRedLight(uint16_t &val)
kbhagat6 6:687dff16904e 602 {
kbhagat6 6:687dff16904e 603 uint8_t val_byte;
kbhagat6 6:687dff16904e 604 val = 0;
kbhagat6 6:687dff16904e 605
kbhagat6 6:687dff16904e 606 /* Read value from clear channel, low byte register */
kbhagat6 6:687dff16904e 607 val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_RDATAL);
kbhagat6 8:6fa15d4e31fb 608 if( val_byte==ERROR) {
kbhagat6 6:687dff16904e 609 return false;
kbhagat6 6:687dff16904e 610 }
kbhagat6 6:687dff16904e 611
kbhagat6 6:687dff16904e 612 val = val_byte;
kbhagat6 6:687dff16904e 613
kbhagat6 6:687dff16904e 614 /* Read value from clear channel, high byte register */
kbhagat6 6:687dff16904e 615 val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_RDATAH);
kbhagat6 8:6fa15d4e31fb 616 if( val_byte==ERROR) {
kbhagat6 6:687dff16904e 617 return false;
kbhagat6 6:687dff16904e 618 }
kbhagat6 6:687dff16904e 619 val = val + ((uint16_t)val_byte << 8);
kbhagat6 6:687dff16904e 620
kbhagat6 6:687dff16904e 621 return true;
kbhagat6 6:687dff16904e 622 }
kbhagat6 6:687dff16904e 623
kbhagat6 6:687dff16904e 624 /**
kbhagat6 6:687dff16904e 625 * @brief Reads the green light level as a 16-bit value
kbhagat6 6:687dff16904e 626 *
kbhagat6 6:687dff16904e 627 * @param[out] val value of the light sensor.
kbhagat6 6:687dff16904e 628 * @return True if operation successful. False otherwise.
kbhagat6 6:687dff16904e 629 */
kbhagat6 6:687dff16904e 630
kbhagat6 6:687dff16904e 631 bool glibr::readGreenLight(uint16_t &val)
kbhagat6 6:687dff16904e 632 {
kbhagat6 6:687dff16904e 633 uint8_t val_byte;
kbhagat6 6:687dff16904e 634 val = 0;
kbhagat6 6:687dff16904e 635
kbhagat6 6:687dff16904e 636 /* Read value from clear channel, low byte register */
kbhagat6 6:687dff16904e 637 val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_GDATAL);
kbhagat6 8:6fa15d4e31fb 638 if( val_byte==ERROR) {
kbhagat6 6:687dff16904e 639 return false;
kbhagat6 6:687dff16904e 640 }
kbhagat6 6:687dff16904e 641
kbhagat6 6:687dff16904e 642 val = val_byte;
kbhagat6 6:687dff16904e 643
kbhagat6 6:687dff16904e 644 /* Read value from clear channel, high byte register */
kbhagat6 6:687dff16904e 645 val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_GDATAH);
kbhagat6 8:6fa15d4e31fb 646 if( val_byte==ERROR) {
kbhagat6 6:687dff16904e 647 return false;
kbhagat6 6:687dff16904e 648 }
kbhagat6 6:687dff16904e 649 val = val + ((uint16_t)val_byte << 8);
kbhagat6 6:687dff16904e 650
kbhagat6 6:687dff16904e 651 return true;
kbhagat6 6:687dff16904e 652 }
kbhagat6 6:687dff16904e 653
kbhagat6 6:687dff16904e 654 /**
kbhagat6 6:687dff16904e 655 * @brief Reads the red light level as a 16-bit value
kbhagat6 6:687dff16904e 656 *
kbhagat6 6:687dff16904e 657 * @param[out] val value of the light sensor.
kbhagat6 6:687dff16904e 658 * @return True if operation successful. False otherwise.
kbhagat6 6:687dff16904e 659 */
kbhagat6 6:687dff16904e 660
kbhagat6 6:687dff16904e 661 bool glibr::readBlueLight(uint16_t &val)
kbhagat6 6:687dff16904e 662 {
kbhagat6 6:687dff16904e 663 uint8_t val_byte;
kbhagat6 6:687dff16904e 664 val = 0;
kbhagat6 6:687dff16904e 665
kbhagat6 6:687dff16904e 666 /* Read value from clear channel, low byte register */
kbhagat6 6:687dff16904e 667 val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_BDATAL);
kbhagat6 8:6fa15d4e31fb 668 if( val_byte==ERROR) {
kbhagat6 6:687dff16904e 669 return false;
kbhagat6 6:687dff16904e 670 }
kbhagat6 6:687dff16904e 671
kbhagat6 6:687dff16904e 672 val = val_byte;
kbhagat6 6:687dff16904e 673
kbhagat6 6:687dff16904e 674 /* Read value from clear channel, high byte register */
kbhagat6 6:687dff16904e 675 val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_BDATAH);
kbhagat6 8:6fa15d4e31fb 676 if( val_byte==ERROR) {
kbhagat6 6:687dff16904e 677 return false;
kbhagat6 6:687dff16904e 678 }
kbhagat6 6:687dff16904e 679 val = val + ((uint16_t)val_byte << 8);
kbhagat6 6:687dff16904e 680
kbhagat6 6:687dff16904e 681 return true;
kbhagat6 6:687dff16904e 682 }
kbhagat6 6:687dff16904e 683
kbhagat6 6:687dff16904e 684 /*******************************************************************************
kbhagat6 6:687dff16904e 685 * Proximity sensor controls
kbhagat6 6:687dff16904e 686 ******************************************************************************/
kbhagat6 6:687dff16904e 687
kbhagat6 6:687dff16904e 688 /**
kbhagat6 6:687dff16904e 689 * @brief Reads the proximity level as an 8-bit value
kbhagat6 6:687dff16904e 690 *
kbhagat6 6:687dff16904e 691 * @param[out] val value of the proximity sensor.
kbhagat6 6:687dff16904e 692 * @return True if operation successful. False otherwise.
kbhagat6 6:687dff16904e 693 */
kbhagat6 6:687dff16904e 694 bool glibr::readProximity(uint8_t &val)
kbhagat6 6:687dff16904e 695 {
kbhagat6 6:687dff16904e 696 val = 0;
kbhagat6 6:687dff16904e 697
kbhagat6 6:687dff16904e 698 /* Read value from proximity data register */
kbhagat6 6:687dff16904e 699 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_PDATA);
kbhagat6 6:687dff16904e 700
kbhagat6 6:687dff16904e 701 if(val==ERROR){
kbhagat6 6:687dff16904e 702 return false;
kbhagat6 6:687dff16904e 703 }
kbhagat6 6:687dff16904e 704
kbhagat6 6:687dff16904e 705 return true;
kbhagat6 6:687dff16904e 706 }
kbhagat6 6:687dff16904e 707
kbhagat6 6:687dff16904e 708 /*******************************************************************************
kbhagat6 6:687dff16904e 709 * High-level gesture controls
kbhagat6 6:687dff16904e 710 ******************************************************************************/
kbhagat6 6:687dff16904e 711
kbhagat6 6:687dff16904e 712 /**
kbhagat6 6:687dff16904e 713 * @brief Resets all the parameters in the gesture data member
kbhagat6 6:687dff16904e 714 */
kbhagat6 6:687dff16904e 715 void glibr::resetGestureParameters()
kbhagat6 6:687dff16904e 716 {
kbhagat6 8:6fa15d4e31fb 717 gesture_data_.sindex = 0;
kbhagat6 6:687dff16904e 718 gesture_data_.total_gestures = 0;
kbhagat6 6:687dff16904e 719
kbhagat6 6:687dff16904e 720 gesture_ud_delta_ = 0;
kbhagat6 6:687dff16904e 721 gesture_lr_delta_ = 0;
kbhagat6 6:687dff16904e 722
kbhagat6 6:687dff16904e 723 gesture_ud_count_ = 0;
kbhagat6 6:687dff16904e 724 gesture_lr_count_ = 0;
kbhagat6 6:687dff16904e 725
kbhagat6 6:687dff16904e 726 gesture_near_count_ = 0;
kbhagat6 6:687dff16904e 727 gesture_far_count_ = 0;
kbhagat6 6:687dff16904e 728
kbhagat6 6:687dff16904e 729 gesture_state_ = 0;
kbhagat6 6:687dff16904e 730 gesture_motion_ = DIR_NONE;
kbhagat6 6:687dff16904e 731 }
kbhagat6 6:687dff16904e 732
kbhagat6 7:0564c6faf8aa 733 bool glibr::processGestureData()
kbhagat6 7:0564c6faf8aa 734 {
kbhagat6 7:0564c6faf8aa 735 uint8_t u_first = 0;
kbhagat6 7:0564c6faf8aa 736 uint8_t d_first = 0;
kbhagat6 7:0564c6faf8aa 737 uint8_t l_first = 0;
kbhagat6 7:0564c6faf8aa 738 uint8_t r_first = 0;
kbhagat6 7:0564c6faf8aa 739 uint8_t u_last = 0;
kbhagat6 7:0564c6faf8aa 740 uint8_t d_last = 0;
kbhagat6 7:0564c6faf8aa 741 uint8_t l_last = 0;
kbhagat6 7:0564c6faf8aa 742 uint8_t r_last = 0;
kbhagat6 7:0564c6faf8aa 743 int ud_ratio_first;
kbhagat6 7:0564c6faf8aa 744 int lr_ratio_first;
kbhagat6 7:0564c6faf8aa 745 int ud_ratio_last;
kbhagat6 7:0564c6faf8aa 746 int lr_ratio_last;
kbhagat6 7:0564c6faf8aa 747 int ud_delta;
kbhagat6 7:0564c6faf8aa 748 int lr_delta;
kbhagat6 7:0564c6faf8aa 749 int i;
kbhagat6 7:0564c6faf8aa 750
kbhagat6 7:0564c6faf8aa 751 /* If we have less than 4 total gestures, that's not enough */
kbhagat6 7:0564c6faf8aa 752 if( gesture_data_.total_gestures <= 4 ) {
kbhagat6 7:0564c6faf8aa 753 return false;
kbhagat6 7:0564c6faf8aa 754 }
kbhagat6 7:0564c6faf8aa 755
kbhagat6 7:0564c6faf8aa 756 /* Check to make sure our data isn't out of bounds */
kbhagat6 7:0564c6faf8aa 757 if( (gesture_data_.total_gestures <= 32) && \
kbhagat6 7:0564c6faf8aa 758 (gesture_data_.total_gestures > 0) ) {
kbhagat6 7:0564c6faf8aa 759
kbhagat6 7:0564c6faf8aa 760 /* Find the first value in U/D/L/R above the threshold */
kbhagat6 7:0564c6faf8aa 761 for( i = 0; i < gesture_data_.total_gestures; i++ ) {
kbhagat6 7:0564c6faf8aa 762 if( (gesture_data_.u_data[i] > GESTURE_THRESHOLD_OUT) &&
kbhagat6 7:0564c6faf8aa 763 (gesture_data_.d_data[i] > GESTURE_THRESHOLD_OUT) &&
kbhagat6 7:0564c6faf8aa 764 (gesture_data_.l_data[i] > GESTURE_THRESHOLD_OUT) &&
kbhagat6 7:0564c6faf8aa 765 (gesture_data_.r_data[i] > GESTURE_THRESHOLD_OUT) ) {
kbhagat6 7:0564c6faf8aa 766
kbhagat6 7:0564c6faf8aa 767 u_first = gesture_data_.u_data[i];
kbhagat6 7:0564c6faf8aa 768 d_first = gesture_data_.d_data[i];
kbhagat6 7:0564c6faf8aa 769 l_first = gesture_data_.l_data[i];
kbhagat6 7:0564c6faf8aa 770 r_first = gesture_data_.r_data[i];
kbhagat6 7:0564c6faf8aa 771 break;
kbhagat6 7:0564c6faf8aa 772 }
kbhagat6 7:0564c6faf8aa 773 }
kbhagat6 7:0564c6faf8aa 774
kbhagat6 7:0564c6faf8aa 775 /* If one of the _first values is 0, then there is no good data */
kbhagat6 7:0564c6faf8aa 776 if( (u_first == 0) || (d_first == 0) || \
kbhagat6 7:0564c6faf8aa 777 (l_first == 0) || (r_first == 0) ) {
kbhagat6 7:0564c6faf8aa 778
kbhagat6 7:0564c6faf8aa 779 return false;
kbhagat6 7:0564c6faf8aa 780 }
kbhagat6 7:0564c6faf8aa 781 /* Find the last value in U/D/L/R above the threshold */
kbhagat6 7:0564c6faf8aa 782 for( i = gesture_data_.total_gestures - 1; i >= 0; i-- ) {
kbhagat6 7:0564c6faf8aa 783 /* #if DEBUG
kbhagat6 7:0564c6faf8aa 784 Serial.print(F("Finding last: "));
kbhagat6 7:0564c6faf8aa 785 Serial.print(F("U:"));
kbhagat6 7:0564c6faf8aa 786 Serial.print(gesture_data_.u_data[i]);
kbhagat6 7:0564c6faf8aa 787 Serial.print(F(" D:"));
kbhagat6 7:0564c6faf8aa 788 Serial.print(gesture_data_.d_data[i]);
kbhagat6 7:0564c6faf8aa 789 Serial.print(F(" L:"));
kbhagat6 7:0564c6faf8aa 790 Serial.print(gesture_data_.l_data[i]);
kbhagat6 7:0564c6faf8aa 791 Serial.print(F(" R:"));
kbhagat6 7:0564c6faf8aa 792 Serial.println(gesture_data_.r_data[i]);
kbhagat6 7:0564c6faf8aa 793 #endif */
kbhagat6 7:0564c6faf8aa 794 if( (gesture_data_.u_data[i] > GESTURE_THRESHOLD_OUT) &&
kbhagat6 7:0564c6faf8aa 795 (gesture_data_.d_data[i] > GESTURE_THRESHOLD_OUT) &&
kbhagat6 7:0564c6faf8aa 796 (gesture_data_.l_data[i] > GESTURE_THRESHOLD_OUT) &&
kbhagat6 7:0564c6faf8aa 797 (gesture_data_.r_data[i] > GESTURE_THRESHOLD_OUT) ) {
kbhagat6 7:0564c6faf8aa 798
kbhagat6 7:0564c6faf8aa 799 u_last = gesture_data_.u_data[i];
kbhagat6 7:0564c6faf8aa 800 d_last = gesture_data_.d_data[i];
kbhagat6 7:0564c6faf8aa 801 l_last = gesture_data_.l_data[i];
kbhagat6 7:0564c6faf8aa 802 r_last = gesture_data_.r_data[i];
kbhagat6 7:0564c6faf8aa 803 break;
kbhagat6 7:0564c6faf8aa 804 }
kbhagat6 7:0564c6faf8aa 805 }
kbhagat6 7:0564c6faf8aa 806 }
kbhagat6 7:0564c6faf8aa 807
kbhagat6 7:0564c6faf8aa 808 /* Calculate the first vs. last ratio of up/down and left/right */
kbhagat6 7:0564c6faf8aa 809 ud_ratio_first = ((u_first - d_first) * 100) / (u_first + d_first);
kbhagat6 7:0564c6faf8aa 810 lr_ratio_first = ((l_first - r_first) * 100) / (l_first + r_first);
kbhagat6 7:0564c6faf8aa 811 ud_ratio_last = ((u_last - d_last) * 100) / (u_last + d_last);
kbhagat6 7:0564c6faf8aa 812 lr_ratio_last = ((l_last - r_last) * 100) / (l_last + r_last);
kbhagat6 7:0564c6faf8aa 813
kbhagat6 7:0564c6faf8aa 814 /* #if DEBUG
kbhagat6 7:0564c6faf8aa 815 Serial.print(F("Last Values: "));
kbhagat6 7:0564c6faf8aa 816 Serial.print(F("U:"));
kbhagat6 7:0564c6faf8aa 817 Serial.print(u_last);
kbhagat6 7:0564c6faf8aa 818 Serial.print(F(" D:"));
kbhagat6 7:0564c6faf8aa 819 Serial.print(d_last);
kbhagat6 7:0564c6faf8aa 820 Serial.print(F(" L:"));
kbhagat6 7:0564c6faf8aa 821 Serial.print(l_last);
kbhagat6 7:0564c6faf8aa 822 Serial.print(F(" R:"));
kbhagat6 7:0564c6faf8aa 823 Serial.println(r_last);
kbhagat6 7:0564c6faf8aa 824
kbhagat6 7:0564c6faf8aa 825 Serial.print(F("Ratios: "));
kbhagat6 7:0564c6faf8aa 826 Serial.print(F("UD Fi: "));
kbhagat6 7:0564c6faf8aa 827 Serial.print(ud_ratio_first);
kbhagat6 7:0564c6faf8aa 828 Serial.print(F(" UD La: "));
kbhagat6 7:0564c6faf8aa 829 Serial.print(ud_ratio_last);
kbhagat6 7:0564c6faf8aa 830 Serial.print(F(" LR Fi: "));
kbhagat6 7:0564c6faf8aa 831 Serial.print(lr_ratio_first);
kbhagat6 7:0564c6faf8aa 832 Serial.print(F(" LR La: "));
kbhagat6 7:0564c6faf8aa 833 Serial.println(lr_ratio_last);
kbhagat6 7:0564c6faf8aa 834 #endif */
kbhagat6 7:0564c6faf8aa 835
kbhagat6 7:0564c6faf8aa 836 /* Determine the difference between the first and last ratios */
kbhagat6 7:0564c6faf8aa 837 ud_delta = ud_ratio_last - ud_ratio_first;
kbhagat6 7:0564c6faf8aa 838 lr_delta = lr_ratio_last - lr_ratio_first;
kbhagat6 7:0564c6faf8aa 839
kbhagat6 7:0564c6faf8aa 840 /* #if DEBUG
kbhagat6 7:0564c6faf8aa 841 Serial.print("Deltas: ");
kbhagat6 7:0564c6faf8aa 842 Serial.print("UD: ");
kbhagat6 7:0564c6faf8aa 843 Serial.print(ud_delta);
kbhagat6 7:0564c6faf8aa 844 Serial.print(" LR: ");
kbhagat6 7:0564c6faf8aa 845 Serial.println(lr_delta);
kbhagat6 7:0564c6faf8aa 846 #endif */
kbhagat6 7:0564c6faf8aa 847
kbhagat6 7:0564c6faf8aa 848 /* Accumulate the UD and LR delta values */
kbhagat6 7:0564c6faf8aa 849 gesture_ud_delta_ += ud_delta;
kbhagat6 7:0564c6faf8aa 850 gesture_lr_delta_ += lr_delta;
kbhagat6 7:0564c6faf8aa 851
kbhagat6 7:0564c6faf8aa 852 /* #if DEBUG
kbhagat6 7:0564c6faf8aa 853 Serial.print("Accumulations: ");
kbhagat6 7:0564c6faf8aa 854 Serial.print("UD: ");
kbhagat6 7:0564c6faf8aa 855 Serial.print(gesture_ud_delta_);
kbhagat6 7:0564c6faf8aa 856 Serial.print(" LR: ");
kbhagat6 7:0564c6faf8aa 857 Serial.println(gesture_lr_delta_);
kbhagat6 7:0564c6faf8aa 858 #endif */
kbhagat6 7:0564c6faf8aa 859
kbhagat6 7:0564c6faf8aa 860 /* Determine U/D gesture */
kbhagat6 7:0564c6faf8aa 861 if( gesture_ud_delta_ >= GESTURE_SENSITIVITY_1 ) {
kbhagat6 7:0564c6faf8aa 862 gesture_ud_count_ = 1;
kbhagat6 7:0564c6faf8aa 863 } else if( gesture_ud_delta_ <= -GESTURE_SENSITIVITY_1 ) {
kbhagat6 7:0564c6faf8aa 864 gesture_ud_count_ = -1;
kbhagat6 7:0564c6faf8aa 865 } else {
kbhagat6 7:0564c6faf8aa 866 gesture_ud_count_ = 0;
kbhagat6 7:0564c6faf8aa 867 }
kbhagat6 7:0564c6faf8aa 868
kbhagat6 7:0564c6faf8aa 869 /* Determine L/R gesture */
kbhagat6 7:0564c6faf8aa 870 if( gesture_lr_delta_ >= GESTURE_SENSITIVITY_1 ) {
kbhagat6 7:0564c6faf8aa 871 gesture_lr_count_ = 1;
kbhagat6 7:0564c6faf8aa 872 } else if( gesture_lr_delta_ <= -GESTURE_SENSITIVITY_1 ) {
kbhagat6 7:0564c6faf8aa 873 gesture_lr_count_ = -1;
kbhagat6 7:0564c6faf8aa 874 } else {
kbhagat6 7:0564c6faf8aa 875 gesture_lr_count_ = 0;
kbhagat6 7:0564c6faf8aa 876 }
kbhagat6 7:0564c6faf8aa 877
kbhagat6 7:0564c6faf8aa 878 /* Determine Near/Far gesture */
kbhagat6 7:0564c6faf8aa 879 if( (gesture_ud_count_ == 0) && (gesture_lr_count_ == 0) ) {
kbhagat6 7:0564c6faf8aa 880 if( (abs(ud_delta) < GESTURE_SENSITIVITY_2) && \
kbhagat6 7:0564c6faf8aa 881 (abs(lr_delta) < GESTURE_SENSITIVITY_2) ) {
kbhagat6 7:0564c6faf8aa 882
kbhagat6 7:0564c6faf8aa 883 if( (ud_delta == 0) && (lr_delta == 0) ) {
kbhagat6 7:0564c6faf8aa 884 gesture_near_count_++;
kbhagat6 7:0564c6faf8aa 885 } else if( (ud_delta != 0) || (lr_delta != 0) ) {
kbhagat6 7:0564c6faf8aa 886 gesture_far_count_++;
kbhagat6 7:0564c6faf8aa 887 }
kbhagat6 7:0564c6faf8aa 888
kbhagat6 7:0564c6faf8aa 889 if( (gesture_near_count_ >= 10) && (gesture_far_count_ >= 2) ) {
kbhagat6 7:0564c6faf8aa 890 if( (ud_delta == 0) && (lr_delta == 0) ) {
kbhagat6 7:0564c6faf8aa 891 gesture_state_ = NEAR_STATE;
kbhagat6 7:0564c6faf8aa 892 } else if( (ud_delta != 0) && (lr_delta != 0) ) {
kbhagat6 7:0564c6faf8aa 893 gesture_state_ = FAR_STATE;
kbhagat6 7:0564c6faf8aa 894 }
kbhagat6 7:0564c6faf8aa 895 return true;
kbhagat6 7:0564c6faf8aa 896 }
kbhagat6 7:0564c6faf8aa 897 }
kbhagat6 7:0564c6faf8aa 898 } else {
kbhagat6 7:0564c6faf8aa 899 if( (abs(ud_delta) < GESTURE_SENSITIVITY_2) && \
kbhagat6 7:0564c6faf8aa 900 (abs(lr_delta) < GESTURE_SENSITIVITY_2) ) {
kbhagat6 7:0564c6faf8aa 901
kbhagat6 7:0564c6faf8aa 902 if( (ud_delta == 0) && (lr_delta == 0) ) {
kbhagat6 7:0564c6faf8aa 903 gesture_near_count_++;
kbhagat6 7:0564c6faf8aa 904 }
kbhagat6 7:0564c6faf8aa 905
kbhagat6 7:0564c6faf8aa 906 if( gesture_near_count_ >= 10 ) {
kbhagat6 7:0564c6faf8aa 907 gesture_ud_count_ = 0;
kbhagat6 7:0564c6faf8aa 908 gesture_lr_count_ = 0;
kbhagat6 7:0564c6faf8aa 909 gesture_ud_delta_ = 0;
kbhagat6 7:0564c6faf8aa 910 gesture_lr_delta_ = 0;
kbhagat6 7:0564c6faf8aa 911 }
kbhagat6 7:0564c6faf8aa 912 }
kbhagat6 7:0564c6faf8aa 913 }
kbhagat6 7:0564c6faf8aa 914
kbhagat6 7:0564c6faf8aa 915 /* #if DEBUG
kbhagat6 7:0564c6faf8aa 916 Serial.print("UD_CT: ");
kbhagat6 7:0564c6faf8aa 917 Serial.print(gesture_ud_count_);
kbhagat6 7:0564c6faf8aa 918 Serial.print(" LR_CT: ");
kbhagat6 7:0564c6faf8aa 919 Serial.print(gesture_lr_count_);
kbhagat6 7:0564c6faf8aa 920 Serial.print(" NEAR_CT: ");
kbhagat6 7:0564c6faf8aa 921 Serial.print(gesture_near_count_);
kbhagat6 7:0564c6faf8aa 922 Serial.print(" FAR_CT: ");
kbhagat6 7:0564c6faf8aa 923 Serial.println(gesture_far_count_);
kbhagat6 7:0564c6faf8aa 924 Serial.println("----------");
kbhagat6 7:0564c6faf8aa 925 #endif */
kbhagat6 7:0564c6faf8aa 926
kbhagat6 7:0564c6faf8aa 927 return false;
kbhagat6 7:0564c6faf8aa 928 }
kbhagat6 7:0564c6faf8aa 929
kbhagat6 7:0564c6faf8aa 930 /**
kbhagat6 7:0564c6faf8aa 931 * @brief Determines swipe direction or near/far state
kbhagat6 7:0564c6faf8aa 932 *
kbhagat6 7:0564c6faf8aa 933 * @return True if near/far event. False otherwise.
kbhagat6 7:0564c6faf8aa 934 */
kbhagat6 8:6fa15d4e31fb 935 bool glibr::decodeGesture()
kbhagat6 7:0564c6faf8aa 936 {
kbhagat6 7:0564c6faf8aa 937 /* Return if near or far event is detected */
kbhagat6 7:0564c6faf8aa 938 if( gesture_state_ == NEAR_STATE ) {
kbhagat6 7:0564c6faf8aa 939 gesture_motion_ = DIR_NEAR;
kbhagat6 7:0564c6faf8aa 940 return true;
kbhagat6 7:0564c6faf8aa 941 } else if ( gesture_state_ == FAR_STATE ) {
kbhagat6 7:0564c6faf8aa 942 gesture_motion_ = DIR_FAR;
kbhagat6 7:0564c6faf8aa 943 return true;
kbhagat6 7:0564c6faf8aa 944 }
kbhagat6 7:0564c6faf8aa 945
kbhagat6 7:0564c6faf8aa 946 /* Determine swipe direction */
kbhagat6 7:0564c6faf8aa 947 if( (gesture_ud_count_ == -1) && (gesture_lr_count_ == 0) ) {
kbhagat6 7:0564c6faf8aa 948 gesture_motion_ = DIR_UP;
kbhagat6 7:0564c6faf8aa 949 } else if( (gesture_ud_count_ == 1) && (gesture_lr_count_ == 0) ) {
kbhagat6 7:0564c6faf8aa 950 gesture_motion_ = DIR_DOWN;
kbhagat6 7:0564c6faf8aa 951 } else if( (gesture_ud_count_ == 0) && (gesture_lr_count_ == 1) ) {
kbhagat6 7:0564c6faf8aa 952 gesture_motion_ = DIR_RIGHT;
kbhagat6 7:0564c6faf8aa 953 } else if( (gesture_ud_count_ == 0) && (gesture_lr_count_ == -1) ) {
kbhagat6 7:0564c6faf8aa 954 gesture_motion_ = DIR_LEFT;
kbhagat6 7:0564c6faf8aa 955 } else if( (gesture_ud_count_ == -1) && (gesture_lr_count_ == 1) ) {
kbhagat6 7:0564c6faf8aa 956 if( abs(gesture_ud_delta_) > abs(gesture_lr_delta_) ) {
kbhagat6 7:0564c6faf8aa 957 gesture_motion_ = DIR_UP;
kbhagat6 7:0564c6faf8aa 958 } else {
kbhagat6 7:0564c6faf8aa 959 gesture_motion_ = DIR_RIGHT;
kbhagat6 7:0564c6faf8aa 960 }
kbhagat6 7:0564c6faf8aa 961 } else if( (gesture_ud_count_ == 1) && (gesture_lr_count_ == -1) ) {
kbhagat6 7:0564c6faf8aa 962 if( abs(gesture_ud_delta_) > abs(gesture_lr_delta_) ) {
kbhagat6 7:0564c6faf8aa 963 gesture_motion_ = DIR_DOWN;
kbhagat6 7:0564c6faf8aa 964 } else {
kbhagat6 7:0564c6faf8aa 965 gesture_motion_ = DIR_LEFT;
kbhagat6 7:0564c6faf8aa 966 }
kbhagat6 7:0564c6faf8aa 967 } else if( (gesture_ud_count_ == -1) && (gesture_lr_count_ == -1) ) {
kbhagat6 7:0564c6faf8aa 968 if( abs(gesture_ud_delta_) > abs(gesture_lr_delta_) ) {
kbhagat6 7:0564c6faf8aa 969 gesture_motion_ = DIR_UP;
kbhagat6 7:0564c6faf8aa 970 } else {
kbhagat6 7:0564c6faf8aa 971 gesture_motion_ = DIR_LEFT;
kbhagat6 7:0564c6faf8aa 972 }
kbhagat6 7:0564c6faf8aa 973 } else if( (gesture_ud_count_ == 1) && (gesture_lr_count_ == 1) ) {
kbhagat6 7:0564c6faf8aa 974 if( abs(gesture_ud_delta_) > abs(gesture_lr_delta_) ) {
kbhagat6 7:0564c6faf8aa 975 gesture_motion_ = DIR_DOWN;
kbhagat6 7:0564c6faf8aa 976 } else {
kbhagat6 7:0564c6faf8aa 977 gesture_motion_ = DIR_RIGHT;
kbhagat6 7:0564c6faf8aa 978 }
kbhagat6 7:0564c6faf8aa 979 } else {
kbhagat6 7:0564c6faf8aa 980 return false;
kbhagat6 7:0564c6faf8aa 981 }
kbhagat6 7:0564c6faf8aa 982
kbhagat6 7:0564c6faf8aa 983 return true;
kbhagat6 7:0564c6faf8aa 984 }
kbhagat6 7:0564c6faf8aa 985
kbhagat6 7:0564c6faf8aa 986 /*******************************************************************************
kbhagat6 7:0564c6faf8aa 987 * Getters and setters for register values
kbhagat6 7:0564c6faf8aa 988 ******************************************************************************/
kbhagat6 7:0564c6faf8aa 989
kbhagat6 7:0564c6faf8aa 990 /**
kbhagat6 7:0564c6faf8aa 991 * @brief Returns the lower threshold for proximity detection
kbhagat6 7:0564c6faf8aa 992 *
kbhagat6 7:0564c6faf8aa 993 * @return lower threshold
kbhagat6 7:0564c6faf8aa 994 */
kbhagat6 7:0564c6faf8aa 995
kbhagat6 7:0564c6faf8aa 996 uint8_t glibr::getProxIntLowThresh()
kbhagat6 7:0564c6faf8aa 997 {
kbhagat6 7:0564c6faf8aa 998 uint8_t val;
kbhagat6 7:0564c6faf8aa 999
kbhagat6 7:0564c6faf8aa 1000 /* Read value from PILT register */
kbhagat6 7:0564c6faf8aa 1001 /* if( !wireReadDataByte(APDS9960_PILT, val) ) {
kbhagat6 7:0564c6faf8aa 1002 val = 0;
kbhagat6 7:0564c6faf8aa 1003 }*/
kbhagat6 7:0564c6faf8aa 1004 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_PILT);
kbhagat6 7:0564c6faf8aa 1005 if(val==ERROR){
kbhagat6 7:0564c6faf8aa 1006 val=0;
kbhagat6 7:0564c6faf8aa 1007 }
kbhagat6 7:0564c6faf8aa 1008
kbhagat6 7:0564c6faf8aa 1009 return val;
kbhagat6 7:0564c6faf8aa 1010 }
kbhagat6 7:0564c6faf8aa 1011
kbhagat6 7:0564c6faf8aa 1012 bool glibr::setProxIntLowThresh(uint8_t threshold)
kbhagat6 7:0564c6faf8aa 1013 {
kbhagat6 7:0564c6faf8aa 1014 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PILT, threshold) ) {
kbhagat6 7:0564c6faf8aa 1015 return false;
kbhagat6 7:0564c6faf8aa 1016 }
kbhagat6 7:0564c6faf8aa 1017
kbhagat6 7:0564c6faf8aa 1018 return true;
kbhagat6 7:0564c6faf8aa 1019 }
kbhagat6 7:0564c6faf8aa 1020
kbhagat6 7:0564c6faf8aa 1021 bool glibr::setProxIntHighThresh(uint8_t threshold)
kbhagat6 7:0564c6faf8aa 1022 {
kbhagat6 7:0564c6faf8aa 1023
kbhagat6 7:0564c6faf8aa 1024 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PIHT, threshold) ) {
kbhagat6 7:0564c6faf8aa 1025 return false;
kbhagat6 7:0564c6faf8aa 1026 }
kbhagat6 7:0564c6faf8aa 1027
kbhagat6 7:0564c6faf8aa 1028 return true;
kbhagat6 7:0564c6faf8aa 1029 }
kbhagat6 7:0564c6faf8aa 1030
kbhagat6 7:0564c6faf8aa 1031
kbhagat6 7:0564c6faf8aa 1032
kbhagat6 7:0564c6faf8aa 1033 bool glibr::setLEDDrive(uint8_t drive)
kbhagat6 7:0564c6faf8aa 1034 {
kbhagat6 7:0564c6faf8aa 1035 uint8_t val;
kbhagat6 7:0564c6faf8aa 1036
kbhagat6 7:0564c6faf8aa 1037 /* Read value from CONTROL register */
kbhagat6 7:0564c6faf8aa 1038
kbhagat6 7:0564c6faf8aa 1039 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL);
kbhagat6 7:0564c6faf8aa 1040 if(val==ERROR){
kbhagat6 7:0564c6faf8aa 1041 return false;
kbhagat6 7:0564c6faf8aa 1042 }
kbhagat6 7:0564c6faf8aa 1043 /* Set bits in register to given value */
kbhagat6 7:0564c6faf8aa 1044 drive &= 0x03;
kbhagat6 7:0564c6faf8aa 1045 drive = drive << 6;
kbhagat6 7:0564c6faf8aa 1046 //val &= 0b00111111;
kbhagat6 7:0564c6faf8aa 1047 val &= 0x3F;
kbhagat6 7:0564c6faf8aa 1048 val |= drive;
kbhagat6 7:0564c6faf8aa 1049
kbhagat6 7:0564c6faf8aa 1050 /* Write register value back into CONTROL register */
kbhagat6 7:0564c6faf8aa 1051 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONTROL, val) ) {
kbhagat6 7:0564c6faf8aa 1052 return false;
kbhagat6 7:0564c6faf8aa 1053 }
kbhagat6 7:0564c6faf8aa 1054
kbhagat6 7:0564c6faf8aa 1055 return true;
kbhagat6 7:0564c6faf8aa 1056 }
kbhagat6 7:0564c6faf8aa 1057
kbhagat6 6:687dff16904e 1058
kbhagat6 4:84545b0e63a9 1059
kbhagat6 0:437ae08befe3 1060 bool glibr::setProximityGain(uint8_t drive)
kbhagat6 0:437ae08befe3 1061 {
kbhagat6 0:437ae08befe3 1062 uint8_t val;
kbhagat6 0:437ae08befe3 1063
kbhagat6 0:437ae08befe3 1064 /* Read value from CONTROL register */
kbhagat6 1:c7215f5f9a72 1065
kbhagat6 5:3eb4f3091bd8 1066 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL);
kbhagat6 0:437ae08befe3 1067
kbhagat6 1:c7215f5f9a72 1068 if(val==ERROR){
kbhagat6 1:c7215f5f9a72 1069 return false;
kbhagat6 1:c7215f5f9a72 1070 }
kbhagat6 0:437ae08befe3 1071 /* Set bits in register to given value */
kbhagat6 1:c7215f5f9a72 1072 //drive &= 0b00000011;
kbhagat6 1:c7215f5f9a72 1073 drive &=0x03;
kbhagat6 0:437ae08befe3 1074 drive = drive << 2;
kbhagat6 1:c7215f5f9a72 1075 val &= 0xF3;
kbhagat6 0:437ae08befe3 1076 val |= drive;
kbhagat6 0:437ae08befe3 1077
kbhagat6 0:437ae08befe3 1078 /* Write register value back into CONTROL register */
kbhagat6 1:c7215f5f9a72 1079 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONTROL, val) ) {
kbhagat6 1:c7215f5f9a72 1080 return false;
kbhagat6 1:c7215f5f9a72 1081 }
kbhagat6 1:c7215f5f9a72 1082 return true;
kbhagat6 1:c7215f5f9a72 1083 }
kbhagat6 1:c7215f5f9a72 1084
kbhagat6 1:c7215f5f9a72 1085
kbhagat6 1:c7215f5f9a72 1086 bool glibr::setAmbientLightGain(uint8_t drive){
kbhagat6 1:c7215f5f9a72 1087 {
kbhagat6 1:c7215f5f9a72 1088 uint8_t val;
kbhagat6 1:c7215f5f9a72 1089
kbhagat6 1:c7215f5f9a72 1090 /* Read value from CONTROL register */
kbhagat6 1:c7215f5f9a72 1091
kbhagat6 5:3eb4f3091bd8 1092 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL);
kbhagat6 1:c7215f5f9a72 1093
kbhagat6 1:c7215f5f9a72 1094 if(val==ERROR){
kbhagat6 1:c7215f5f9a72 1095 return false;
kbhagat6 1:c7215f5f9a72 1096 }
kbhagat6 1:c7215f5f9a72 1097 /* Set bits in register to given value */
kbhagat6 1:c7215f5f9a72 1098 //drive &= 0b00000011;
kbhagat6 1:c7215f5f9a72 1099 drive &=0x03;
kbhagat6 1:c7215f5f9a72 1100 drive = drive << 2;
kbhagat6 1:c7215f5f9a72 1101 val &= 0xF3;
kbhagat6 1:c7215f5f9a72 1102 val |= drive;
kbhagat6 1:c7215f5f9a72 1103
kbhagat6 1:c7215f5f9a72 1104 /* Write register value back into CONTROL register */
kbhagat6 1:c7215f5f9a72 1105 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONTROL, val) ) {
kbhagat6 1:c7215f5f9a72 1106 return false;
kbhagat6 1:c7215f5f9a72 1107 }
kbhagat6 1:c7215f5f9a72 1108 return true;
kbhagat6 1:c7215f5f9a72 1109 }
kbhagat6 1:c7215f5f9a72 1110
kbhagat6 1:c7215f5f9a72 1111
kbhagat6 1:c7215f5f9a72 1112 bool glibr::setLightIntLowThreshold(uint16_t threshold)
kbhagat6 1:c7215f5f9a72 1113 {
kbhagat6 1:c7215f5f9a72 1114 uint8_t val_low;
kbhagat6 1:c7215f5f9a72 1115 uint8_t val_high;
kbhagat6 1:c7215f5f9a72 1116
kbhagat6 1:c7215f5f9a72 1117 /* Break 16-bit threshold into 2 8-bit values */
kbhagat6 1:c7215f5f9a72 1118 val_low = threshold & 0x00FF;
kbhagat6 1:c7215f5f9a72 1119 val_high = (threshold & 0xFF00) >> 8;
kbhagat6 1:c7215f5f9a72 1120
kbhagat6 1:c7215f5f9a72 1121 /* Write low byte */
kbhagat6 1:c7215f5f9a72 1122 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_AILTL, val_low) ) {
kbhagat6 1:c7215f5f9a72 1123 return false;
kbhagat6 1:c7215f5f9a72 1124 }
kbhagat6 1:c7215f5f9a72 1125
kbhagat6 1:c7215f5f9a72 1126 /* Write high byte */
kbhagat6 1:c7215f5f9a72 1127 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_AILTH, val_high) ) {
kbhagat6 1:c7215f5f9a72 1128 return false;
kbhagat6 1:c7215f5f9a72 1129 }
kbhagat6 1:c7215f5f9a72 1130
kbhagat6 1:c7215f5f9a72 1131 return true;
kbhagat6 1:c7215f5f9a72 1132 }
kbhagat6 1:c7215f5f9a72 1133
kbhagat6 5:3eb4f3091bd8 1134
kbhagat6 5:3eb4f3091bd8 1135
kbhagat6 5:3eb4f3091bd8 1136
kbhagat6 5:3eb4f3091bd8 1137
kbhagat6 5:3eb4f3091bd8 1138 int glibr::I2CwriteByte(char address, char subAddress, char data)
kbhagat6 5:3eb4f3091bd8 1139 {
kbhagat6 5:3eb4f3091bd8 1140 int ret;
kbhagat6 5:3eb4f3091bd8 1141 char cmd[2] = {subAddress, data};
kbhagat6 5:3eb4f3091bd8 1142 ret=i2c.write(address<<1, cmd, 2); //if ret is 1, then not acked.
kbhagat6 5:3eb4f3091bd8 1143 return ret;
kbhagat6 5:3eb4f3091bd8 1144 }
kbhagat6 5:3eb4f3091bd8 1145
kbhagat6 5:3eb4f3091bd8 1146
kbhagat6 5:3eb4f3091bd8 1147
kbhagat6 0:437ae08befe3 1148 uint8_t glibr::I2CreadByte(char address, char subAddress)
kbhagat6 0:437ae08befe3 1149 {
kbhagat6 0:437ae08befe3 1150 char data; // store the register data
kbhagat6 0:437ae08befe3 1151
kbhagat6 0:437ae08befe3 1152 if(i2c.write(address<<1, &subAddress, 1, true)){
kbhagat6 0:437ae08befe3 1153 return ERROR; //7 bit //not acked
kbhagat6 0:437ae08befe3 1154 }
kbhagat6 0:437ae08befe3 1155 i2c.read(address<<1, &data, 1);
kbhagat6 0:437ae08befe3 1156 return data;
kbhagat6 0:437ae08befe3 1157
kbhagat6 0:437ae08befe3 1158 }
kbhagat6 5:3eb4f3091bd8 1159
kbhagat6 5:3eb4f3091bd8 1160 /**
kbhagat6 5:3eb4f3091bd8 1161 * @brief Reads a block (array) of bytes from the I2C device and register
kbhagat6 5:3eb4f3091bd8 1162 *
kbhagat6 5:3eb4f3091bd8 1163 * @param[in] reg the register to read from
kbhagat6 5:3eb4f3091bd8 1164 * @param[out] val pointer to the beginning of the data
kbhagat6 5:3eb4f3091bd8 1165 * @param[in] len number of bytes to read
kbhagat6 5:3eb4f3091bd8 1166 * @return Number of bytes read. -1 on read error.
kbhagat6 5:3eb4f3091bd8 1167 */
kbhagat6 8:6fa15d4e31fb 1168 int glibr::I2CReadDataBlock(char address, char subAddress, uint8_t *val, unsigned int len)
kbhagat6 5:3eb4f3091bd8 1169 {
kbhagat6 5:3eb4f3091bd8 1170 // unsigned char i = 0;
kbhagat6 8:6fa15d4e31fb 1171
kbhagat6 5:3eb4f3091bd8 1172 /* Indicate which register we want to read from */
kbhagat6 5:3eb4f3091bd8 1173
kbhagat6 5:3eb4f3091bd8 1174 if(i2c.write(address<<1, subAddress, 1, true)){
kbhagat6 5:3eb4f3091bd8 1175 return -1; //7 bit //not acked
kbhagat6 8:6fa15d4e31fb 1176 }
kbhagat6 5:3eb4f3091bd8 1177
kbhagat6 5:3eb4f3091bd8 1178 /* Read block data */
kbhagat6 8:6fa15d4e31fb 1179
kbhagat6 8:6fa15d4e31fb 1180 if(i2c.read(address<<1, &val, len)){
kbhagat6 8:6fa15d4e31fb 1181 return -1;
kbhagat6 8:6fa15d4e31fb 1182 }
kbhagat6 5:3eb4f3091bd8 1183
kbhagat6 8:6fa15d4e31fb 1184 return 1;
kbhagat6 5:3eb4f3091bd8 1185 //Wire.requestFrom(APDS9960_I2C_ADDR, len);
kbhagat6 5:3eb4f3091bd8 1186 /*while (Wire.available()) {
kbhagat6 5:3eb4f3091bd8 1187 if (i >= len) {
kbhagat6 5:3eb4f3091bd8 1188 return -1;
kbhagat6 5:3eb4f3091bd8 1189 }
kbhagat6 5:3eb4f3091bd8 1190 val[i] = Wire.read();
kbhagat6 5:3eb4f3091bd8 1191 i++;
kbhagat6 5:3eb4f3091bd8 1192 }*/
kbhagat6 5:3eb4f3091bd8 1193 }