![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
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
glibr.cpp@9:286d00d9db80, 2015-03-06 (annotated)
- Committer:
- kbhagat6
- Date:
- Fri Mar 06 22:59:40 2015 +0000
- Revision:
- 9:286d00d9db80
- Parent:
- 8:6fa15d4e31fb
- Child:
- 10:e8adab2fb829
fixed/ no errors... now just need to test!
Who changed what in which revision?
User | Revision | Line number | New 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 | 4:84545b0e63a9 | 434 | |
kbhagat6 | 4:84545b0e63a9 | 435 | /* Keep looping as long as gesture data is valid */ |
kbhagat6 | 4:84545b0e63a9 | 436 | while(1) { |
kbhagat6 | 8:6fa15d4e31fb | 437 | |
kbhagat6 | 4:84545b0e63a9 | 438 | /* Wait some time to collect next batch of FIFO data */ |
kbhagat6 | 5:3eb4f3091bd8 | 439 | wait(FIFO_PAUSE_TIME); |
kbhagat6 | 4:84545b0e63a9 | 440 | |
kbhagat6 | 4:84545b0e63a9 | 441 | /* Get the contents of the STATUS register. Is data still valid? */ |
kbhagat6 | 4:84545b0e63a9 | 442 | |
kbhagat6 | 4:84545b0e63a9 | 443 | gstatus=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_GSTATUS); |
kbhagat6 | 4:84545b0e63a9 | 444 | if( gstatus==ERROR ) { |
kbhagat6 | 4:84545b0e63a9 | 445 | return ERROR; |
kbhagat6 | 4:84545b0e63a9 | 446 | } |
kbhagat6 | 4:84545b0e63a9 | 447 | /* If we have valid data, read in FIFO */ |
kbhagat6 | 4:84545b0e63a9 | 448 | if( (gstatus & APDS9960_GVALID) == APDS9960_GVALID ) { |
kbhagat6 | 4:84545b0e63a9 | 449 | |
kbhagat6 | 4:84545b0e63a9 | 450 | /* Read the current FIFO level */ |
kbhagat6 | 8:6fa15d4e31fb | 451 | fifo_level=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_GFLVL); |
kbhagat6 | 8:6fa15d4e31fb | 452 | if( fifo_level==ERROR ) { |
kbhagat6 | 4:84545b0e63a9 | 453 | return ERROR; |
kbhagat6 | 4:84545b0e63a9 | 454 | } |
kbhagat6 | 8:6fa15d4e31fb | 455 | |
kbhagat6 | 4:84545b0e63a9 | 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 | 4:84545b0e63a9 | 472 | #if DEBUG |
kbhagat6 | 8:6fa15d4e31fb | 473 | //Serial.print("FIFO Dump: "); |
kbhagat6 | 8:6fa15d4e31fb | 474 | for ( i = 0; i < (fifo_level * 4); i++ ) { |
kbhagat6 | 8:6fa15d4e31fb | 475 | // Serial.print(fifo_data[i]); |
kbhagat6 | 8:6fa15d4e31fb | 476 | // Serial.print(" "); |
kbhagat6 | 4:84545b0e63a9 | 477 | } |
kbhagat6 | 8:6fa15d4e31fb | 478 | //Serial.println(); |
kbhagat6 | 4:84545b0e63a9 | 479 | #endif |
kbhagat6 | 4:84545b0e63a9 | 480 | |
kbhagat6 | 4:84545b0e63a9 | 481 | /* If at least 1 set of data, sort the data into U/D/L/R */ |
kbhagat6 | 8:6fa15d4e31fb | 482 | if((fifo_level * 4) >= 4 ) { |
kbhagat6 | 8:6fa15d4e31fb | 483 | for( i = 0; i < (fifo_level * 4); i += 4 ) { |
kbhagat6 | 8:6fa15d4e31fb | 484 | gesture_data_.u_data[gesture_data_.sindex] = \ |
kbhagat6 | 4:84545b0e63a9 | 485 | fifo_data[i + 0]; |
kbhagat6 | 8:6fa15d4e31fb | 486 | gesture_data_.d_data[gesture_data_.sindex] = \ |
kbhagat6 | 4:84545b0e63a9 | 487 | fifo_data[i + 1]; |
kbhagat6 | 8:6fa15d4e31fb | 488 | gesture_data_.l_data[gesture_data_.sindex] = \ |
kbhagat6 | 4:84545b0e63a9 | 489 | fifo_data[i + 2]; |
kbhagat6 | 8:6fa15d4e31fb | 490 | gesture_data_.r_data[gesture_data_.sindex] = \ |
kbhagat6 | 4:84545b0e63a9 | 491 | fifo_data[i + 3]; |
kbhagat6 | 8:6fa15d4e31fb | 492 | gesture_data_.sindex++; |
kbhagat6 | 4:84545b0e63a9 | 493 | gesture_data_.total_gestures++; |
kbhagat6 | 4:84545b0e63a9 | 494 | } |
kbhagat6 | 4:84545b0e63a9 | 495 | |
kbhagat6 | 4:84545b0e63a9 | 496 | #if DEBUG |
kbhagat6 | 5:3eb4f3091bd8 | 497 | // Serial.print("Up Data: "); |
kbhagat6 | 4:84545b0e63a9 | 498 | for ( i = 0; i < gesture_data_.total_gestures; i++ ) { |
kbhagat6 | 5:3eb4f3091bd8 | 499 | // Serial.print(gesture_data_.u_data[i]); |
kbhagat6 | 5:3eb4f3091bd8 | 500 | // Serial.print(" "); |
kbhagat6 | 4:84545b0e63a9 | 501 | } |
kbhagat6 | 5:3eb4f3091bd8 | 502 | // Serial.println(); |
kbhagat6 | 4:84545b0e63a9 | 503 | #endif |
kbhagat6 | 4:84545b0e63a9 | 504 | |
kbhagat6 | 4:84545b0e63a9 | 505 | /* Filter and process gesture data. Decode near/far state */ |
kbhagat6 | 4:84545b0e63a9 | 506 | if( processGestureData() ) { |
kbhagat6 | 4:84545b0e63a9 | 507 | if( decodeGesture() ) { |
kbhagat6 | 4:84545b0e63a9 | 508 | //***TODO: U-Turn Gestures |
kbhagat6 | 4:84545b0e63a9 | 509 | #if DEBUG |
kbhagat6 | 4:84545b0e63a9 | 510 | //Serial.println(gesture_motion_); |
kbhagat6 | 4:84545b0e63a9 | 511 | #endif |
kbhagat6 | 4:84545b0e63a9 | 512 | } |
kbhagat6 | 4:84545b0e63a9 | 513 | } |
kbhagat6 | 4:84545b0e63a9 | 514 | |
kbhagat6 | 4:84545b0e63a9 | 515 | /* Reset data */ |
kbhagat6 | 8:6fa15d4e31fb | 516 | gesture_data_.sindex = 0; |
kbhagat6 | 4:84545b0e63a9 | 517 | gesture_data_.total_gestures = 0; |
kbhagat6 | 4:84545b0e63a9 | 518 | } |
kbhagat6 | 4:84545b0e63a9 | 519 | } |
kbhagat6 | 4:84545b0e63a9 | 520 | } else { |
kbhagat6 | 4:84545b0e63a9 | 521 | |
kbhagat6 | 4:84545b0e63a9 | 522 | /* Determine best guessed gesture and clean up */ |
kbhagat6 | 5:3eb4f3091bd8 | 523 | wait(FIFO_PAUSE_TIME); |
kbhagat6 | 4:84545b0e63a9 | 524 | decodeGesture(); |
kbhagat6 | 4:84545b0e63a9 | 525 | motion = gesture_motion_; |
kbhagat6 | 4:84545b0e63a9 | 526 | #if DEBUG |
kbhagat6 | 5:3eb4f3091bd8 | 527 | // Serial.print("END: "); |
kbhagat6 | 5:3eb4f3091bd8 | 528 | // Serial.println(gesture_motion_); |
kbhagat6 | 4:84545b0e63a9 | 529 | #endif |
kbhagat6 | 4:84545b0e63a9 | 530 | resetGestureParameters(); |
kbhagat6 | 4:84545b0e63a9 | 531 | return motion; |
kbhagat6 | 4:84545b0e63a9 | 532 | } |
kbhagat6 | 4:84545b0e63a9 | 533 | } |
kbhagat6 | 9:286d00d9db80 | 534 | // delete fptr; |
kbhagat6 | 4:84545b0e63a9 | 535 | } |
kbhagat6 | 6:687dff16904e | 536 | /** |
kbhagat6 | 6:687dff16904e | 537 | * Turn the APDS-9960 on |
kbhagat6 | 6:687dff16904e | 538 | * |
kbhagat6 | 6:687dff16904e | 539 | * @return True if operation successful. False otherwise. |
kbhagat6 | 6:687dff16904e | 540 | */ |
kbhagat6 | 6:687dff16904e | 541 | bool glibr::enablePower() |
kbhagat6 | 6:687dff16904e | 542 | { |
kbhagat6 | 6:687dff16904e | 543 | if( !setMode(POWER, 1) ) { |
kbhagat6 | 6:687dff16904e | 544 | return false; |
kbhagat6 | 6:687dff16904e | 545 | } |
kbhagat6 | 6:687dff16904e | 546 | |
kbhagat6 | 6:687dff16904e | 547 | return true; |
kbhagat6 | 6:687dff16904e | 548 | } |
kbhagat6 | 6:687dff16904e | 549 | |
kbhagat6 | 6:687dff16904e | 550 | /** |
kbhagat6 | 6:687dff16904e | 551 | * Turn the APDS-9960 off |
kbhagat6 | 6:687dff16904e | 552 | * |
kbhagat6 | 6:687dff16904e | 553 | * @return True if operation successful. False otherwise. |
kbhagat6 | 6:687dff16904e | 554 | */ |
kbhagat6 | 6:687dff16904e | 555 | bool glibr::disablePower() |
kbhagat6 | 6:687dff16904e | 556 | { |
kbhagat6 | 6:687dff16904e | 557 | if( !setMode(POWER, 0) ) { |
kbhagat6 | 6:687dff16904e | 558 | return false; |
kbhagat6 | 6:687dff16904e | 559 | } |
kbhagat6 | 6:687dff16904e | 560 | |
kbhagat6 | 6:687dff16904e | 561 | return true; |
kbhagat6 | 6:687dff16904e | 562 | } |
kbhagat6 | 6:687dff16904e | 563 | |
kbhagat6 | 6:687dff16904e | 564 | /******************************************************************************* |
kbhagat6 | 6:687dff16904e | 565 | * Ambient light and color sensor controls |
kbhagat6 | 6:687dff16904e | 566 | ******************************************************************************/ |
kbhagat6 | 6:687dff16904e | 567 | |
kbhagat6 | 6:687dff16904e | 568 | /** |
kbhagat6 | 6:687dff16904e | 569 | * @brief Reads the ambient (clear) light level as a 16-bit value |
kbhagat6 | 6:687dff16904e | 570 | * |
kbhagat6 | 6:687dff16904e | 571 | * @param[out] val value of the light sensor. |
kbhagat6 | 6:687dff16904e | 572 | * @return True if operation successful. False otherwise. |
kbhagat6 | 6:687dff16904e | 573 | */ |
kbhagat6 | 6:687dff16904e | 574 | bool glibr::readAmbientLight(uint16_t &val) |
kbhagat6 | 6:687dff16904e | 575 | { |
kbhagat6 | 6:687dff16904e | 576 | uint8_t val_byte; |
kbhagat6 | 6:687dff16904e | 577 | val = 0; |
kbhagat6 | 6:687dff16904e | 578 | |
kbhagat6 | 6:687dff16904e | 579 | /* Read value from clear channel, low byte register */ |
kbhagat6 | 6:687dff16904e | 580 | val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CDATAL); |
kbhagat6 | 8:6fa15d4e31fb | 581 | if( val_byte==ERROR) { |
kbhagat6 | 6:687dff16904e | 582 | return false; |
kbhagat6 | 6:687dff16904e | 583 | } |
kbhagat6 | 6:687dff16904e | 584 | val = val_byte; |
kbhagat6 | 6:687dff16904e | 585 | |
kbhagat6 | 6:687dff16904e | 586 | /* Read value from clear channel, high byte register */ |
kbhagat6 | 6:687dff16904e | 587 | |
kbhagat6 | 6:687dff16904e | 588 | val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CDATAH); |
kbhagat6 | 8:6fa15d4e31fb | 589 | if( val_byte==ERROR) { |
kbhagat6 | 6:687dff16904e | 590 | return false; |
kbhagat6 | 6:687dff16904e | 591 | } |
kbhagat6 | 6:687dff16904e | 592 | val = val + ((uint16_t)val_byte << 8); |
kbhagat6 | 6:687dff16904e | 593 | |
kbhagat6 | 6:687dff16904e | 594 | return true; |
kbhagat6 | 6:687dff16904e | 595 | } |
kbhagat6 | 6:687dff16904e | 596 | |
kbhagat6 | 6:687dff16904e | 597 | /** |
kbhagat6 | 6:687dff16904e | 598 | * @brief Reads the red light level as a 16-bit value |
kbhagat6 | 6:687dff16904e | 599 | * |
kbhagat6 | 6:687dff16904e | 600 | * @param[out] val value of the light sensor. |
kbhagat6 | 6:687dff16904e | 601 | * @return True if operation successful. False otherwise. |
kbhagat6 | 6:687dff16904e | 602 | */ |
kbhagat6 | 6:687dff16904e | 603 | bool glibr::readRedLight(uint16_t &val) |
kbhagat6 | 6:687dff16904e | 604 | { |
kbhagat6 | 6:687dff16904e | 605 | uint8_t val_byte; |
kbhagat6 | 6:687dff16904e | 606 | val = 0; |
kbhagat6 | 6:687dff16904e | 607 | |
kbhagat6 | 6:687dff16904e | 608 | /* Read value from clear channel, low byte register */ |
kbhagat6 | 6:687dff16904e | 609 | val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_RDATAL); |
kbhagat6 | 8:6fa15d4e31fb | 610 | if( val_byte==ERROR) { |
kbhagat6 | 6:687dff16904e | 611 | return false; |
kbhagat6 | 6:687dff16904e | 612 | } |
kbhagat6 | 6:687dff16904e | 613 | |
kbhagat6 | 6:687dff16904e | 614 | val = val_byte; |
kbhagat6 | 6:687dff16904e | 615 | |
kbhagat6 | 6:687dff16904e | 616 | /* Read value from clear channel, high byte register */ |
kbhagat6 | 6:687dff16904e | 617 | val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_RDATAH); |
kbhagat6 | 8:6fa15d4e31fb | 618 | if( val_byte==ERROR) { |
kbhagat6 | 6:687dff16904e | 619 | return false; |
kbhagat6 | 6:687dff16904e | 620 | } |
kbhagat6 | 6:687dff16904e | 621 | val = val + ((uint16_t)val_byte << 8); |
kbhagat6 | 6:687dff16904e | 622 | |
kbhagat6 | 6:687dff16904e | 623 | return true; |
kbhagat6 | 6:687dff16904e | 624 | } |
kbhagat6 | 6:687dff16904e | 625 | |
kbhagat6 | 6:687dff16904e | 626 | /** |
kbhagat6 | 6:687dff16904e | 627 | * @brief Reads the green light level as a 16-bit value |
kbhagat6 | 6:687dff16904e | 628 | * |
kbhagat6 | 6:687dff16904e | 629 | * @param[out] val value of the light sensor. |
kbhagat6 | 6:687dff16904e | 630 | * @return True if operation successful. False otherwise. |
kbhagat6 | 6:687dff16904e | 631 | */ |
kbhagat6 | 6:687dff16904e | 632 | |
kbhagat6 | 6:687dff16904e | 633 | bool glibr::readGreenLight(uint16_t &val) |
kbhagat6 | 6:687dff16904e | 634 | { |
kbhagat6 | 6:687dff16904e | 635 | uint8_t val_byte; |
kbhagat6 | 6:687dff16904e | 636 | val = 0; |
kbhagat6 | 6:687dff16904e | 637 | |
kbhagat6 | 6:687dff16904e | 638 | /* Read value from clear channel, low byte register */ |
kbhagat6 | 6:687dff16904e | 639 | val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_GDATAL); |
kbhagat6 | 8:6fa15d4e31fb | 640 | if( val_byte==ERROR) { |
kbhagat6 | 6:687dff16904e | 641 | return false; |
kbhagat6 | 6:687dff16904e | 642 | } |
kbhagat6 | 6:687dff16904e | 643 | |
kbhagat6 | 6:687dff16904e | 644 | val = val_byte; |
kbhagat6 | 6:687dff16904e | 645 | |
kbhagat6 | 6:687dff16904e | 646 | /* Read value from clear channel, high byte register */ |
kbhagat6 | 6:687dff16904e | 647 | val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_GDATAH); |
kbhagat6 | 8:6fa15d4e31fb | 648 | if( val_byte==ERROR) { |
kbhagat6 | 6:687dff16904e | 649 | return false; |
kbhagat6 | 6:687dff16904e | 650 | } |
kbhagat6 | 6:687dff16904e | 651 | val = val + ((uint16_t)val_byte << 8); |
kbhagat6 | 6:687dff16904e | 652 | |
kbhagat6 | 6:687dff16904e | 653 | return true; |
kbhagat6 | 6:687dff16904e | 654 | } |
kbhagat6 | 6:687dff16904e | 655 | |
kbhagat6 | 6:687dff16904e | 656 | /** |
kbhagat6 | 6:687dff16904e | 657 | * @brief Reads the red light level as a 16-bit value |
kbhagat6 | 6:687dff16904e | 658 | * |
kbhagat6 | 6:687dff16904e | 659 | * @param[out] val value of the light sensor. |
kbhagat6 | 6:687dff16904e | 660 | * @return True if operation successful. False otherwise. |
kbhagat6 | 6:687dff16904e | 661 | */ |
kbhagat6 | 6:687dff16904e | 662 | |
kbhagat6 | 6:687dff16904e | 663 | bool glibr::readBlueLight(uint16_t &val) |
kbhagat6 | 6:687dff16904e | 664 | { |
kbhagat6 | 6:687dff16904e | 665 | uint8_t val_byte; |
kbhagat6 | 6:687dff16904e | 666 | val = 0; |
kbhagat6 | 6:687dff16904e | 667 | |
kbhagat6 | 6:687dff16904e | 668 | /* Read value from clear channel, low byte register */ |
kbhagat6 | 6:687dff16904e | 669 | val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_BDATAL); |
kbhagat6 | 8:6fa15d4e31fb | 670 | if( val_byte==ERROR) { |
kbhagat6 | 6:687dff16904e | 671 | return false; |
kbhagat6 | 6:687dff16904e | 672 | } |
kbhagat6 | 6:687dff16904e | 673 | |
kbhagat6 | 6:687dff16904e | 674 | val = val_byte; |
kbhagat6 | 6:687dff16904e | 675 | |
kbhagat6 | 6:687dff16904e | 676 | /* Read value from clear channel, high byte register */ |
kbhagat6 | 6:687dff16904e | 677 | val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_BDATAH); |
kbhagat6 | 8:6fa15d4e31fb | 678 | if( val_byte==ERROR) { |
kbhagat6 | 6:687dff16904e | 679 | return false; |
kbhagat6 | 6:687dff16904e | 680 | } |
kbhagat6 | 6:687dff16904e | 681 | val = val + ((uint16_t)val_byte << 8); |
kbhagat6 | 6:687dff16904e | 682 | |
kbhagat6 | 6:687dff16904e | 683 | return true; |
kbhagat6 | 6:687dff16904e | 684 | } |
kbhagat6 | 6:687dff16904e | 685 | |
kbhagat6 | 6:687dff16904e | 686 | /******************************************************************************* |
kbhagat6 | 6:687dff16904e | 687 | * Proximity sensor controls |
kbhagat6 | 6:687dff16904e | 688 | ******************************************************************************/ |
kbhagat6 | 6:687dff16904e | 689 | |
kbhagat6 | 6:687dff16904e | 690 | /** |
kbhagat6 | 6:687dff16904e | 691 | * @brief Reads the proximity level as an 8-bit value |
kbhagat6 | 6:687dff16904e | 692 | * |
kbhagat6 | 6:687dff16904e | 693 | * @param[out] val value of the proximity sensor. |
kbhagat6 | 6:687dff16904e | 694 | * @return True if operation successful. False otherwise. |
kbhagat6 | 6:687dff16904e | 695 | */ |
kbhagat6 | 6:687dff16904e | 696 | bool glibr::readProximity(uint8_t &val) |
kbhagat6 | 6:687dff16904e | 697 | { |
kbhagat6 | 6:687dff16904e | 698 | val = 0; |
kbhagat6 | 6:687dff16904e | 699 | |
kbhagat6 | 6:687dff16904e | 700 | /* Read value from proximity data register */ |
kbhagat6 | 6:687dff16904e | 701 | val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_PDATA); |
kbhagat6 | 6:687dff16904e | 702 | |
kbhagat6 | 6:687dff16904e | 703 | if(val==ERROR){ |
kbhagat6 | 6:687dff16904e | 704 | return false; |
kbhagat6 | 6:687dff16904e | 705 | } |
kbhagat6 | 6:687dff16904e | 706 | |
kbhagat6 | 6:687dff16904e | 707 | return true; |
kbhagat6 | 6:687dff16904e | 708 | } |
kbhagat6 | 6:687dff16904e | 709 | |
kbhagat6 | 6:687dff16904e | 710 | /******************************************************************************* |
kbhagat6 | 6:687dff16904e | 711 | * High-level gesture controls |
kbhagat6 | 6:687dff16904e | 712 | ******************************************************************************/ |
kbhagat6 | 6:687dff16904e | 713 | |
kbhagat6 | 6:687dff16904e | 714 | /** |
kbhagat6 | 6:687dff16904e | 715 | * @brief Resets all the parameters in the gesture data member |
kbhagat6 | 6:687dff16904e | 716 | */ |
kbhagat6 | 6:687dff16904e | 717 | void glibr::resetGestureParameters() |
kbhagat6 | 6:687dff16904e | 718 | { |
kbhagat6 | 8:6fa15d4e31fb | 719 | gesture_data_.sindex = 0; |
kbhagat6 | 6:687dff16904e | 720 | gesture_data_.total_gestures = 0; |
kbhagat6 | 6:687dff16904e | 721 | |
kbhagat6 | 6:687dff16904e | 722 | gesture_ud_delta_ = 0; |
kbhagat6 | 6:687dff16904e | 723 | gesture_lr_delta_ = 0; |
kbhagat6 | 6:687dff16904e | 724 | |
kbhagat6 | 6:687dff16904e | 725 | gesture_ud_count_ = 0; |
kbhagat6 | 6:687dff16904e | 726 | gesture_lr_count_ = 0; |
kbhagat6 | 6:687dff16904e | 727 | |
kbhagat6 | 6:687dff16904e | 728 | gesture_near_count_ = 0; |
kbhagat6 | 6:687dff16904e | 729 | gesture_far_count_ = 0; |
kbhagat6 | 6:687dff16904e | 730 | |
kbhagat6 | 6:687dff16904e | 731 | gesture_state_ = 0; |
kbhagat6 | 6:687dff16904e | 732 | gesture_motion_ = DIR_NONE; |
kbhagat6 | 6:687dff16904e | 733 | } |
kbhagat6 | 6:687dff16904e | 734 | |
kbhagat6 | 7:0564c6faf8aa | 735 | bool glibr::processGestureData() |
kbhagat6 | 7:0564c6faf8aa | 736 | { |
kbhagat6 | 7:0564c6faf8aa | 737 | uint8_t u_first = 0; |
kbhagat6 | 7:0564c6faf8aa | 738 | uint8_t d_first = 0; |
kbhagat6 | 7:0564c6faf8aa | 739 | uint8_t l_first = 0; |
kbhagat6 | 7:0564c6faf8aa | 740 | uint8_t r_first = 0; |
kbhagat6 | 7:0564c6faf8aa | 741 | uint8_t u_last = 0; |
kbhagat6 | 7:0564c6faf8aa | 742 | uint8_t d_last = 0; |
kbhagat6 | 7:0564c6faf8aa | 743 | uint8_t l_last = 0; |
kbhagat6 | 7:0564c6faf8aa | 744 | uint8_t r_last = 0; |
kbhagat6 | 7:0564c6faf8aa | 745 | int ud_ratio_first; |
kbhagat6 | 7:0564c6faf8aa | 746 | int lr_ratio_first; |
kbhagat6 | 7:0564c6faf8aa | 747 | int ud_ratio_last; |
kbhagat6 | 7:0564c6faf8aa | 748 | int lr_ratio_last; |
kbhagat6 | 7:0564c6faf8aa | 749 | int ud_delta; |
kbhagat6 | 7:0564c6faf8aa | 750 | int lr_delta; |
kbhagat6 | 7:0564c6faf8aa | 751 | int i; |
kbhagat6 | 7:0564c6faf8aa | 752 | |
kbhagat6 | 7:0564c6faf8aa | 753 | /* If we have less than 4 total gestures, that's not enough */ |
kbhagat6 | 7:0564c6faf8aa | 754 | if( gesture_data_.total_gestures <= 4 ) { |
kbhagat6 | 7:0564c6faf8aa | 755 | return false; |
kbhagat6 | 7:0564c6faf8aa | 756 | } |
kbhagat6 | 7:0564c6faf8aa | 757 | |
kbhagat6 | 7:0564c6faf8aa | 758 | /* Check to make sure our data isn't out of bounds */ |
kbhagat6 | 7:0564c6faf8aa | 759 | if( (gesture_data_.total_gestures <= 32) && \ |
kbhagat6 | 7:0564c6faf8aa | 760 | (gesture_data_.total_gestures > 0) ) { |
kbhagat6 | 7:0564c6faf8aa | 761 | |
kbhagat6 | 7:0564c6faf8aa | 762 | /* Find the first value in U/D/L/R above the threshold */ |
kbhagat6 | 7:0564c6faf8aa | 763 | for( i = 0; i < gesture_data_.total_gestures; i++ ) { |
kbhagat6 | 7:0564c6faf8aa | 764 | if( (gesture_data_.u_data[i] > GESTURE_THRESHOLD_OUT) && |
kbhagat6 | 7:0564c6faf8aa | 765 | (gesture_data_.d_data[i] > GESTURE_THRESHOLD_OUT) && |
kbhagat6 | 7:0564c6faf8aa | 766 | (gesture_data_.l_data[i] > GESTURE_THRESHOLD_OUT) && |
kbhagat6 | 7:0564c6faf8aa | 767 | (gesture_data_.r_data[i] > GESTURE_THRESHOLD_OUT) ) { |
kbhagat6 | 7:0564c6faf8aa | 768 | |
kbhagat6 | 7:0564c6faf8aa | 769 | u_first = gesture_data_.u_data[i]; |
kbhagat6 | 7:0564c6faf8aa | 770 | d_first = gesture_data_.d_data[i]; |
kbhagat6 | 7:0564c6faf8aa | 771 | l_first = gesture_data_.l_data[i]; |
kbhagat6 | 7:0564c6faf8aa | 772 | r_first = gesture_data_.r_data[i]; |
kbhagat6 | 7:0564c6faf8aa | 773 | break; |
kbhagat6 | 7:0564c6faf8aa | 774 | } |
kbhagat6 | 7:0564c6faf8aa | 775 | } |
kbhagat6 | 7:0564c6faf8aa | 776 | |
kbhagat6 | 7:0564c6faf8aa | 777 | /* If one of the _first values is 0, then there is no good data */ |
kbhagat6 | 7:0564c6faf8aa | 778 | if( (u_first == 0) || (d_first == 0) || \ |
kbhagat6 | 7:0564c6faf8aa | 779 | (l_first == 0) || (r_first == 0) ) { |
kbhagat6 | 7:0564c6faf8aa | 780 | |
kbhagat6 | 7:0564c6faf8aa | 781 | return false; |
kbhagat6 | 7:0564c6faf8aa | 782 | } |
kbhagat6 | 7:0564c6faf8aa | 783 | /* Find the last value in U/D/L/R above the threshold */ |
kbhagat6 | 7:0564c6faf8aa | 784 | for( i = gesture_data_.total_gestures - 1; i >= 0; i-- ) { |
kbhagat6 | 7:0564c6faf8aa | 785 | /* #if DEBUG |
kbhagat6 | 7:0564c6faf8aa | 786 | Serial.print(F("Finding last: ")); |
kbhagat6 | 7:0564c6faf8aa | 787 | Serial.print(F("U:")); |
kbhagat6 | 7:0564c6faf8aa | 788 | Serial.print(gesture_data_.u_data[i]); |
kbhagat6 | 7:0564c6faf8aa | 789 | Serial.print(F(" D:")); |
kbhagat6 | 7:0564c6faf8aa | 790 | Serial.print(gesture_data_.d_data[i]); |
kbhagat6 | 7:0564c6faf8aa | 791 | Serial.print(F(" L:")); |
kbhagat6 | 7:0564c6faf8aa | 792 | Serial.print(gesture_data_.l_data[i]); |
kbhagat6 | 7:0564c6faf8aa | 793 | Serial.print(F(" R:")); |
kbhagat6 | 7:0564c6faf8aa | 794 | Serial.println(gesture_data_.r_data[i]); |
kbhagat6 | 7:0564c6faf8aa | 795 | #endif */ |
kbhagat6 | 7:0564c6faf8aa | 796 | if( (gesture_data_.u_data[i] > GESTURE_THRESHOLD_OUT) && |
kbhagat6 | 7:0564c6faf8aa | 797 | (gesture_data_.d_data[i] > GESTURE_THRESHOLD_OUT) && |
kbhagat6 | 7:0564c6faf8aa | 798 | (gesture_data_.l_data[i] > GESTURE_THRESHOLD_OUT) && |
kbhagat6 | 7:0564c6faf8aa | 799 | (gesture_data_.r_data[i] > GESTURE_THRESHOLD_OUT) ) { |
kbhagat6 | 7:0564c6faf8aa | 800 | |
kbhagat6 | 7:0564c6faf8aa | 801 | u_last = gesture_data_.u_data[i]; |
kbhagat6 | 7:0564c6faf8aa | 802 | d_last = gesture_data_.d_data[i]; |
kbhagat6 | 7:0564c6faf8aa | 803 | l_last = gesture_data_.l_data[i]; |
kbhagat6 | 7:0564c6faf8aa | 804 | r_last = gesture_data_.r_data[i]; |
kbhagat6 | 7:0564c6faf8aa | 805 | break; |
kbhagat6 | 7:0564c6faf8aa | 806 | } |
kbhagat6 | 7:0564c6faf8aa | 807 | } |
kbhagat6 | 7:0564c6faf8aa | 808 | } |
kbhagat6 | 7:0564c6faf8aa | 809 | |
kbhagat6 | 7:0564c6faf8aa | 810 | /* Calculate the first vs. last ratio of up/down and left/right */ |
kbhagat6 | 7:0564c6faf8aa | 811 | ud_ratio_first = ((u_first - d_first) * 100) / (u_first + d_first); |
kbhagat6 | 7:0564c6faf8aa | 812 | lr_ratio_first = ((l_first - r_first) * 100) / (l_first + r_first); |
kbhagat6 | 7:0564c6faf8aa | 813 | ud_ratio_last = ((u_last - d_last) * 100) / (u_last + d_last); |
kbhagat6 | 7:0564c6faf8aa | 814 | lr_ratio_last = ((l_last - r_last) * 100) / (l_last + r_last); |
kbhagat6 | 7:0564c6faf8aa | 815 | |
kbhagat6 | 7:0564c6faf8aa | 816 | /* #if DEBUG |
kbhagat6 | 7:0564c6faf8aa | 817 | Serial.print(F("Last Values: ")); |
kbhagat6 | 7:0564c6faf8aa | 818 | Serial.print(F("U:")); |
kbhagat6 | 7:0564c6faf8aa | 819 | Serial.print(u_last); |
kbhagat6 | 7:0564c6faf8aa | 820 | Serial.print(F(" D:")); |
kbhagat6 | 7:0564c6faf8aa | 821 | Serial.print(d_last); |
kbhagat6 | 7:0564c6faf8aa | 822 | Serial.print(F(" L:")); |
kbhagat6 | 7:0564c6faf8aa | 823 | Serial.print(l_last); |
kbhagat6 | 7:0564c6faf8aa | 824 | Serial.print(F(" R:")); |
kbhagat6 | 7:0564c6faf8aa | 825 | Serial.println(r_last); |
kbhagat6 | 7:0564c6faf8aa | 826 | |
kbhagat6 | 7:0564c6faf8aa | 827 | Serial.print(F("Ratios: ")); |
kbhagat6 | 7:0564c6faf8aa | 828 | Serial.print(F("UD Fi: ")); |
kbhagat6 | 7:0564c6faf8aa | 829 | Serial.print(ud_ratio_first); |
kbhagat6 | 7:0564c6faf8aa | 830 | Serial.print(F(" UD La: ")); |
kbhagat6 | 7:0564c6faf8aa | 831 | Serial.print(ud_ratio_last); |
kbhagat6 | 7:0564c6faf8aa | 832 | Serial.print(F(" LR Fi: ")); |
kbhagat6 | 7:0564c6faf8aa | 833 | Serial.print(lr_ratio_first); |
kbhagat6 | 7:0564c6faf8aa | 834 | Serial.print(F(" LR La: ")); |
kbhagat6 | 7:0564c6faf8aa | 835 | Serial.println(lr_ratio_last); |
kbhagat6 | 7:0564c6faf8aa | 836 | #endif */ |
kbhagat6 | 7:0564c6faf8aa | 837 | |
kbhagat6 | 7:0564c6faf8aa | 838 | /* Determine the difference between the first and last ratios */ |
kbhagat6 | 7:0564c6faf8aa | 839 | ud_delta = ud_ratio_last - ud_ratio_first; |
kbhagat6 | 7:0564c6faf8aa | 840 | lr_delta = lr_ratio_last - lr_ratio_first; |
kbhagat6 | 7:0564c6faf8aa | 841 | |
kbhagat6 | 7:0564c6faf8aa | 842 | /* #if DEBUG |
kbhagat6 | 7:0564c6faf8aa | 843 | Serial.print("Deltas: "); |
kbhagat6 | 7:0564c6faf8aa | 844 | Serial.print("UD: "); |
kbhagat6 | 7:0564c6faf8aa | 845 | Serial.print(ud_delta); |
kbhagat6 | 7:0564c6faf8aa | 846 | Serial.print(" LR: "); |
kbhagat6 | 7:0564c6faf8aa | 847 | Serial.println(lr_delta); |
kbhagat6 | 7:0564c6faf8aa | 848 | #endif */ |
kbhagat6 | 7:0564c6faf8aa | 849 | |
kbhagat6 | 7:0564c6faf8aa | 850 | /* Accumulate the UD and LR delta values */ |
kbhagat6 | 7:0564c6faf8aa | 851 | gesture_ud_delta_ += ud_delta; |
kbhagat6 | 7:0564c6faf8aa | 852 | gesture_lr_delta_ += lr_delta; |
kbhagat6 | 7:0564c6faf8aa | 853 | |
kbhagat6 | 7:0564c6faf8aa | 854 | /* #if DEBUG |
kbhagat6 | 7:0564c6faf8aa | 855 | Serial.print("Accumulations: "); |
kbhagat6 | 7:0564c6faf8aa | 856 | Serial.print("UD: "); |
kbhagat6 | 7:0564c6faf8aa | 857 | Serial.print(gesture_ud_delta_); |
kbhagat6 | 7:0564c6faf8aa | 858 | Serial.print(" LR: "); |
kbhagat6 | 7:0564c6faf8aa | 859 | Serial.println(gesture_lr_delta_); |
kbhagat6 | 7:0564c6faf8aa | 860 | #endif */ |
kbhagat6 | 7:0564c6faf8aa | 861 | |
kbhagat6 | 7:0564c6faf8aa | 862 | /* Determine U/D gesture */ |
kbhagat6 | 7:0564c6faf8aa | 863 | if( gesture_ud_delta_ >= GESTURE_SENSITIVITY_1 ) { |
kbhagat6 | 7:0564c6faf8aa | 864 | gesture_ud_count_ = 1; |
kbhagat6 | 7:0564c6faf8aa | 865 | } else if( gesture_ud_delta_ <= -GESTURE_SENSITIVITY_1 ) { |
kbhagat6 | 7:0564c6faf8aa | 866 | gesture_ud_count_ = -1; |
kbhagat6 | 7:0564c6faf8aa | 867 | } else { |
kbhagat6 | 7:0564c6faf8aa | 868 | gesture_ud_count_ = 0; |
kbhagat6 | 7:0564c6faf8aa | 869 | } |
kbhagat6 | 7:0564c6faf8aa | 870 | |
kbhagat6 | 7:0564c6faf8aa | 871 | /* Determine L/R gesture */ |
kbhagat6 | 7:0564c6faf8aa | 872 | if( gesture_lr_delta_ >= GESTURE_SENSITIVITY_1 ) { |
kbhagat6 | 7:0564c6faf8aa | 873 | gesture_lr_count_ = 1; |
kbhagat6 | 7:0564c6faf8aa | 874 | } else if( gesture_lr_delta_ <= -GESTURE_SENSITIVITY_1 ) { |
kbhagat6 | 7:0564c6faf8aa | 875 | gesture_lr_count_ = -1; |
kbhagat6 | 7:0564c6faf8aa | 876 | } else { |
kbhagat6 | 7:0564c6faf8aa | 877 | gesture_lr_count_ = 0; |
kbhagat6 | 7:0564c6faf8aa | 878 | } |
kbhagat6 | 7:0564c6faf8aa | 879 | |
kbhagat6 | 7:0564c6faf8aa | 880 | /* Determine Near/Far gesture */ |
kbhagat6 | 7:0564c6faf8aa | 881 | if( (gesture_ud_count_ == 0) && (gesture_lr_count_ == 0) ) { |
kbhagat6 | 7:0564c6faf8aa | 882 | if( (abs(ud_delta) < GESTURE_SENSITIVITY_2) && \ |
kbhagat6 | 7:0564c6faf8aa | 883 | (abs(lr_delta) < GESTURE_SENSITIVITY_2) ) { |
kbhagat6 | 7:0564c6faf8aa | 884 | |
kbhagat6 | 7:0564c6faf8aa | 885 | if( (ud_delta == 0) && (lr_delta == 0) ) { |
kbhagat6 | 7:0564c6faf8aa | 886 | gesture_near_count_++; |
kbhagat6 | 7:0564c6faf8aa | 887 | } else if( (ud_delta != 0) || (lr_delta != 0) ) { |
kbhagat6 | 7:0564c6faf8aa | 888 | gesture_far_count_++; |
kbhagat6 | 7:0564c6faf8aa | 889 | } |
kbhagat6 | 7:0564c6faf8aa | 890 | |
kbhagat6 | 7:0564c6faf8aa | 891 | if( (gesture_near_count_ >= 10) && (gesture_far_count_ >= 2) ) { |
kbhagat6 | 7:0564c6faf8aa | 892 | if( (ud_delta == 0) && (lr_delta == 0) ) { |
kbhagat6 | 7:0564c6faf8aa | 893 | gesture_state_ = NEAR_STATE; |
kbhagat6 | 7:0564c6faf8aa | 894 | } else if( (ud_delta != 0) && (lr_delta != 0) ) { |
kbhagat6 | 7:0564c6faf8aa | 895 | gesture_state_ = FAR_STATE; |
kbhagat6 | 7:0564c6faf8aa | 896 | } |
kbhagat6 | 7:0564c6faf8aa | 897 | return true; |
kbhagat6 | 7:0564c6faf8aa | 898 | } |
kbhagat6 | 7:0564c6faf8aa | 899 | } |
kbhagat6 | 7:0564c6faf8aa | 900 | } else { |
kbhagat6 | 7:0564c6faf8aa | 901 | if( (abs(ud_delta) < GESTURE_SENSITIVITY_2) && \ |
kbhagat6 | 7:0564c6faf8aa | 902 | (abs(lr_delta) < GESTURE_SENSITIVITY_2) ) { |
kbhagat6 | 7:0564c6faf8aa | 903 | |
kbhagat6 | 7:0564c6faf8aa | 904 | if( (ud_delta == 0) && (lr_delta == 0) ) { |
kbhagat6 | 7:0564c6faf8aa | 905 | gesture_near_count_++; |
kbhagat6 | 7:0564c6faf8aa | 906 | } |
kbhagat6 | 7:0564c6faf8aa | 907 | |
kbhagat6 | 7:0564c6faf8aa | 908 | if( gesture_near_count_ >= 10 ) { |
kbhagat6 | 7:0564c6faf8aa | 909 | gesture_ud_count_ = 0; |
kbhagat6 | 7:0564c6faf8aa | 910 | gesture_lr_count_ = 0; |
kbhagat6 | 7:0564c6faf8aa | 911 | gesture_ud_delta_ = 0; |
kbhagat6 | 7:0564c6faf8aa | 912 | gesture_lr_delta_ = 0; |
kbhagat6 | 7:0564c6faf8aa | 913 | } |
kbhagat6 | 7:0564c6faf8aa | 914 | } |
kbhagat6 | 7:0564c6faf8aa | 915 | } |
kbhagat6 | 7:0564c6faf8aa | 916 | |
kbhagat6 | 7:0564c6faf8aa | 917 | /* #if DEBUG |
kbhagat6 | 7:0564c6faf8aa | 918 | Serial.print("UD_CT: "); |
kbhagat6 | 7:0564c6faf8aa | 919 | Serial.print(gesture_ud_count_); |
kbhagat6 | 7:0564c6faf8aa | 920 | Serial.print(" LR_CT: "); |
kbhagat6 | 7:0564c6faf8aa | 921 | Serial.print(gesture_lr_count_); |
kbhagat6 | 7:0564c6faf8aa | 922 | Serial.print(" NEAR_CT: "); |
kbhagat6 | 7:0564c6faf8aa | 923 | Serial.print(gesture_near_count_); |
kbhagat6 | 7:0564c6faf8aa | 924 | Serial.print(" FAR_CT: "); |
kbhagat6 | 7:0564c6faf8aa | 925 | Serial.println(gesture_far_count_); |
kbhagat6 | 7:0564c6faf8aa | 926 | Serial.println("----------"); |
kbhagat6 | 7:0564c6faf8aa | 927 | #endif */ |
kbhagat6 | 7:0564c6faf8aa | 928 | |
kbhagat6 | 7:0564c6faf8aa | 929 | return false; |
kbhagat6 | 7:0564c6faf8aa | 930 | } |
kbhagat6 | 7:0564c6faf8aa | 931 | |
kbhagat6 | 7:0564c6faf8aa | 932 | /** |
kbhagat6 | 7:0564c6faf8aa | 933 | * @brief Determines swipe direction or near/far state |
kbhagat6 | 7:0564c6faf8aa | 934 | * |
kbhagat6 | 7:0564c6faf8aa | 935 | * @return True if near/far event. False otherwise. |
kbhagat6 | 7:0564c6faf8aa | 936 | */ |
kbhagat6 | 8:6fa15d4e31fb | 937 | bool glibr::decodeGesture() |
kbhagat6 | 7:0564c6faf8aa | 938 | { |
kbhagat6 | 7:0564c6faf8aa | 939 | /* Return if near or far event is detected */ |
kbhagat6 | 7:0564c6faf8aa | 940 | if( gesture_state_ == NEAR_STATE ) { |
kbhagat6 | 7:0564c6faf8aa | 941 | gesture_motion_ = DIR_NEAR; |
kbhagat6 | 7:0564c6faf8aa | 942 | return true; |
kbhagat6 | 7:0564c6faf8aa | 943 | } else if ( gesture_state_ == FAR_STATE ) { |
kbhagat6 | 7:0564c6faf8aa | 944 | gesture_motion_ = DIR_FAR; |
kbhagat6 | 7:0564c6faf8aa | 945 | return true; |
kbhagat6 | 7:0564c6faf8aa | 946 | } |
kbhagat6 | 7:0564c6faf8aa | 947 | |
kbhagat6 | 7:0564c6faf8aa | 948 | /* Determine swipe direction */ |
kbhagat6 | 7:0564c6faf8aa | 949 | if( (gesture_ud_count_ == -1) && (gesture_lr_count_ == 0) ) { |
kbhagat6 | 7:0564c6faf8aa | 950 | gesture_motion_ = DIR_UP; |
kbhagat6 | 7:0564c6faf8aa | 951 | } else if( (gesture_ud_count_ == 1) && (gesture_lr_count_ == 0) ) { |
kbhagat6 | 7:0564c6faf8aa | 952 | gesture_motion_ = DIR_DOWN; |
kbhagat6 | 7:0564c6faf8aa | 953 | } else if( (gesture_ud_count_ == 0) && (gesture_lr_count_ == 1) ) { |
kbhagat6 | 7:0564c6faf8aa | 954 | gesture_motion_ = DIR_RIGHT; |
kbhagat6 | 7:0564c6faf8aa | 955 | } else if( (gesture_ud_count_ == 0) && (gesture_lr_count_ == -1) ) { |
kbhagat6 | 7:0564c6faf8aa | 956 | gesture_motion_ = DIR_LEFT; |
kbhagat6 | 7:0564c6faf8aa | 957 | } else if( (gesture_ud_count_ == -1) && (gesture_lr_count_ == 1) ) { |
kbhagat6 | 7:0564c6faf8aa | 958 | if( abs(gesture_ud_delta_) > abs(gesture_lr_delta_) ) { |
kbhagat6 | 7:0564c6faf8aa | 959 | gesture_motion_ = DIR_UP; |
kbhagat6 | 7:0564c6faf8aa | 960 | } else { |
kbhagat6 | 7:0564c6faf8aa | 961 | gesture_motion_ = DIR_RIGHT; |
kbhagat6 | 7:0564c6faf8aa | 962 | } |
kbhagat6 | 7:0564c6faf8aa | 963 | } else if( (gesture_ud_count_ == 1) && (gesture_lr_count_ == -1) ) { |
kbhagat6 | 7:0564c6faf8aa | 964 | if( abs(gesture_ud_delta_) > abs(gesture_lr_delta_) ) { |
kbhagat6 | 7:0564c6faf8aa | 965 | gesture_motion_ = DIR_DOWN; |
kbhagat6 | 7:0564c6faf8aa | 966 | } else { |
kbhagat6 | 7:0564c6faf8aa | 967 | gesture_motion_ = DIR_LEFT; |
kbhagat6 | 7:0564c6faf8aa | 968 | } |
kbhagat6 | 7:0564c6faf8aa | 969 | } else if( (gesture_ud_count_ == -1) && (gesture_lr_count_ == -1) ) { |
kbhagat6 | 7:0564c6faf8aa | 970 | if( abs(gesture_ud_delta_) > abs(gesture_lr_delta_) ) { |
kbhagat6 | 7:0564c6faf8aa | 971 | gesture_motion_ = DIR_UP; |
kbhagat6 | 7:0564c6faf8aa | 972 | } else { |
kbhagat6 | 7:0564c6faf8aa | 973 | gesture_motion_ = DIR_LEFT; |
kbhagat6 | 7:0564c6faf8aa | 974 | } |
kbhagat6 | 7:0564c6faf8aa | 975 | } else if( (gesture_ud_count_ == 1) && (gesture_lr_count_ == 1) ) { |
kbhagat6 | 7:0564c6faf8aa | 976 | if( abs(gesture_ud_delta_) > abs(gesture_lr_delta_) ) { |
kbhagat6 | 7:0564c6faf8aa | 977 | gesture_motion_ = DIR_DOWN; |
kbhagat6 | 7:0564c6faf8aa | 978 | } else { |
kbhagat6 | 7:0564c6faf8aa | 979 | gesture_motion_ = DIR_RIGHT; |
kbhagat6 | 7:0564c6faf8aa | 980 | } |
kbhagat6 | 7:0564c6faf8aa | 981 | } else { |
kbhagat6 | 7:0564c6faf8aa | 982 | return false; |
kbhagat6 | 7:0564c6faf8aa | 983 | } |
kbhagat6 | 7:0564c6faf8aa | 984 | |
kbhagat6 | 7:0564c6faf8aa | 985 | return true; |
kbhagat6 | 7:0564c6faf8aa | 986 | } |
kbhagat6 | 7:0564c6faf8aa | 987 | |
kbhagat6 | 7:0564c6faf8aa | 988 | /******************************************************************************* |
kbhagat6 | 7:0564c6faf8aa | 989 | * Getters and setters for register values |
kbhagat6 | 7:0564c6faf8aa | 990 | ******************************************************************************/ |
kbhagat6 | 7:0564c6faf8aa | 991 | |
kbhagat6 | 7:0564c6faf8aa | 992 | /** |
kbhagat6 | 7:0564c6faf8aa | 993 | * @brief Returns the lower threshold for proximity detection |
kbhagat6 | 7:0564c6faf8aa | 994 | * |
kbhagat6 | 7:0564c6faf8aa | 995 | * @return lower threshold |
kbhagat6 | 7:0564c6faf8aa | 996 | */ |
kbhagat6 | 7:0564c6faf8aa | 997 | |
kbhagat6 | 7:0564c6faf8aa | 998 | uint8_t glibr::getProxIntLowThresh() |
kbhagat6 | 7:0564c6faf8aa | 999 | { |
kbhagat6 | 7:0564c6faf8aa | 1000 | uint8_t val; |
kbhagat6 | 7:0564c6faf8aa | 1001 | |
kbhagat6 | 7:0564c6faf8aa | 1002 | /* Read value from PILT register */ |
kbhagat6 | 7:0564c6faf8aa | 1003 | /* if( !wireReadDataByte(APDS9960_PILT, val) ) { |
kbhagat6 | 7:0564c6faf8aa | 1004 | val = 0; |
kbhagat6 | 7:0564c6faf8aa | 1005 | }*/ |
kbhagat6 | 7:0564c6faf8aa | 1006 | val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_PILT); |
kbhagat6 | 7:0564c6faf8aa | 1007 | if(val==ERROR){ |
kbhagat6 | 7:0564c6faf8aa | 1008 | val=0; |
kbhagat6 | 7:0564c6faf8aa | 1009 | } |
kbhagat6 | 7:0564c6faf8aa | 1010 | |
kbhagat6 | 7:0564c6faf8aa | 1011 | return val; |
kbhagat6 | 7:0564c6faf8aa | 1012 | } |
kbhagat6 | 7:0564c6faf8aa | 1013 | |
kbhagat6 | 9:286d00d9db80 | 1014 | /** |
kbhagat6 | 9:286d00d9db80 | 1015 | * @brief Sets the lower threshold for proximity detection |
kbhagat6 | 9:286d00d9db80 | 1016 | * |
kbhagat6 | 9:286d00d9db80 | 1017 | * @param[in] threshold the lower proximity threshold |
kbhagat6 | 9:286d00d9db80 | 1018 | * @return True if operation successful. False otherwise. |
kbhagat6 | 9:286d00d9db80 | 1019 | */ |
kbhagat6 | 7:0564c6faf8aa | 1020 | bool glibr::setProxIntLowThresh(uint8_t threshold) |
kbhagat6 | 7:0564c6faf8aa | 1021 | { |
kbhagat6 | 7:0564c6faf8aa | 1022 | if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PILT, threshold) ) { |
kbhagat6 | 7:0564c6faf8aa | 1023 | return false; |
kbhagat6 | 7:0564c6faf8aa | 1024 | } |
kbhagat6 | 7:0564c6faf8aa | 1025 | |
kbhagat6 | 7:0564c6faf8aa | 1026 | return true; |
kbhagat6 | 7:0564c6faf8aa | 1027 | } |
kbhagat6 | 7:0564c6faf8aa | 1028 | |
kbhagat6 | 9:286d00d9db80 | 1029 | /** |
kbhagat6 | 9:286d00d9db80 | 1030 | * @brief Returns the high threshold for proximity detection |
kbhagat6 | 9:286d00d9db80 | 1031 | * |
kbhagat6 | 9:286d00d9db80 | 1032 | * @return high threshold |
kbhagat6 | 9:286d00d9db80 | 1033 | */ |
kbhagat6 | 9:286d00d9db80 | 1034 | uint8_t glibr::getProxIntHighThresh() |
kbhagat6 | 9:286d00d9db80 | 1035 | { |
kbhagat6 | 9:286d00d9db80 | 1036 | uint8_t val; |
kbhagat6 | 9:286d00d9db80 | 1037 | |
kbhagat6 | 9:286d00d9db80 | 1038 | /* Read value from PIHT register */ |
kbhagat6 | 9:286d00d9db80 | 1039 | val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_PILT); |
kbhagat6 | 9:286d00d9db80 | 1040 | if( val==ERROR ) { |
kbhagat6 | 9:286d00d9db80 | 1041 | val = 0; |
kbhagat6 | 9:286d00d9db80 | 1042 | } |
kbhagat6 | 9:286d00d9db80 | 1043 | |
kbhagat6 | 9:286d00d9db80 | 1044 | return val; |
kbhagat6 | 9:286d00d9db80 | 1045 | } |
kbhagat6 | 9:286d00d9db80 | 1046 | |
kbhagat6 | 9:286d00d9db80 | 1047 | /** |
kbhagat6 | 9:286d00d9db80 | 1048 | * @brief Sets the high threshold for proximity detection |
kbhagat6 | 9:286d00d9db80 | 1049 | * |
kbhagat6 | 9:286d00d9db80 | 1050 | * @param[in] threshold the high proximity threshold |
kbhagat6 | 9:286d00d9db80 | 1051 | * @return True if operation successful. False otherwise. |
kbhagat6 | 9:286d00d9db80 | 1052 | */ |
kbhagat6 | 7:0564c6faf8aa | 1053 | bool glibr::setProxIntHighThresh(uint8_t threshold) |
kbhagat6 | 7:0564c6faf8aa | 1054 | { |
kbhagat6 | 7:0564c6faf8aa | 1055 | |
kbhagat6 | 7:0564c6faf8aa | 1056 | if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PIHT, threshold) ) { |
kbhagat6 | 7:0564c6faf8aa | 1057 | return false; |
kbhagat6 | 7:0564c6faf8aa | 1058 | } |
kbhagat6 | 7:0564c6faf8aa | 1059 | |
kbhagat6 | 7:0564c6faf8aa | 1060 | return true; |
kbhagat6 | 7:0564c6faf8aa | 1061 | } |
kbhagat6 | 7:0564c6faf8aa | 1062 | |
kbhagat6 | 9:286d00d9db80 | 1063 | /** |
kbhagat6 | 9:286d00d9db80 | 1064 | * @brief Returns LED drive strength for proximity and ALS |
kbhagat6 | 9:286d00d9db80 | 1065 | * |
kbhagat6 | 9:286d00d9db80 | 1066 | * Value LED Current |
kbhagat6 | 9:286d00d9db80 | 1067 | * 0 100 mA |
kbhagat6 | 9:286d00d9db80 | 1068 | * 1 50 mA |
kbhagat6 | 9:286d00d9db80 | 1069 | * 2 25 mA |
kbhagat6 | 9:286d00d9db80 | 1070 | * 3 12.5 mA |
kbhagat6 | 9:286d00d9db80 | 1071 | * |
kbhagat6 | 9:286d00d9db80 | 1072 | * @return the value of the LED drive strength. 0xFF on failure. |
kbhagat6 | 9:286d00d9db80 | 1073 | */ |
kbhagat6 | 9:286d00d9db80 | 1074 | uint8_t glibr::getLEDDrive() |
kbhagat6 | 9:286d00d9db80 | 1075 | { |
kbhagat6 | 9:286d00d9db80 | 1076 | uint8_t val; |
kbhagat6 | 9:286d00d9db80 | 1077 | |
kbhagat6 | 9:286d00d9db80 | 1078 | /* Read value from CONTROL register */ |
kbhagat6 | 9:286d00d9db80 | 1079 | val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL); |
kbhagat6 | 9:286d00d9db80 | 1080 | if( val == ERROR ){//!wireReadDataByte(APDS9960_CONTROL, val) ) { |
kbhagat6 | 9:286d00d9db80 | 1081 | return ERROR; |
kbhagat6 | 9:286d00d9db80 | 1082 | } |
kbhagat6 | 9:286d00d9db80 | 1083 | |
kbhagat6 | 9:286d00d9db80 | 1084 | /* Shift and mask out LED drive bits */ |
kbhagat6 | 9:286d00d9db80 | 1085 | val = (val >> 6) & 0x03;//0b00000011; |
kbhagat6 | 9:286d00d9db80 | 1086 | |
kbhagat6 | 9:286d00d9db80 | 1087 | return val; |
kbhagat6 | 9:286d00d9db80 | 1088 | } |
kbhagat6 | 7:0564c6faf8aa | 1089 | |
kbhagat6 | 9:286d00d9db80 | 1090 | /** |
kbhagat6 | 9:286d00d9db80 | 1091 | * @brief Sets the LED drive strength for proximity and ALS |
kbhagat6 | 9:286d00d9db80 | 1092 | * |
kbhagat6 | 9:286d00d9db80 | 1093 | * Value LED Current |
kbhagat6 | 9:286d00d9db80 | 1094 | * 0 100 mA |
kbhagat6 | 9:286d00d9db80 | 1095 | * 1 50 mA |
kbhagat6 | 9:286d00d9db80 | 1096 | * 2 25 mA |
kbhagat6 | 9:286d00d9db80 | 1097 | * 3 12.5 mA |
kbhagat6 | 9:286d00d9db80 | 1098 | * |
kbhagat6 | 9:286d00d9db80 | 1099 | * @param[in] drive the value (0-3) for the LED drive strength |
kbhagat6 | 9:286d00d9db80 | 1100 | * @return True if operation successful. False otherwise. |
kbhagat6 | 9:286d00d9db80 | 1101 | */ |
kbhagat6 | 7:0564c6faf8aa | 1102 | |
kbhagat6 | 7:0564c6faf8aa | 1103 | bool glibr::setLEDDrive(uint8_t drive) |
kbhagat6 | 7:0564c6faf8aa | 1104 | { |
kbhagat6 | 7:0564c6faf8aa | 1105 | uint8_t val; |
kbhagat6 | 7:0564c6faf8aa | 1106 | |
kbhagat6 | 7:0564c6faf8aa | 1107 | /* Read value from CONTROL register */ |
kbhagat6 | 7:0564c6faf8aa | 1108 | val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL); |
kbhagat6 | 7:0564c6faf8aa | 1109 | if(val==ERROR){ |
kbhagat6 | 7:0564c6faf8aa | 1110 | return false; |
kbhagat6 | 7:0564c6faf8aa | 1111 | } |
kbhagat6 | 7:0564c6faf8aa | 1112 | /* Set bits in register to given value */ |
kbhagat6 | 9:286d00d9db80 | 1113 | //drive &= 0b00000011 |
kbhagat6 | 7:0564c6faf8aa | 1114 | drive &= 0x03; |
kbhagat6 | 7:0564c6faf8aa | 1115 | drive = drive << 6; |
kbhagat6 | 7:0564c6faf8aa | 1116 | //val &= 0b00111111; |
kbhagat6 | 7:0564c6faf8aa | 1117 | val &= 0x3F; |
kbhagat6 | 7:0564c6faf8aa | 1118 | val |= drive; |
kbhagat6 | 7:0564c6faf8aa | 1119 | |
kbhagat6 | 7:0564c6faf8aa | 1120 | /* Write register value back into CONTROL register */ |
kbhagat6 | 7:0564c6faf8aa | 1121 | if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONTROL, val) ) { |
kbhagat6 | 7:0564c6faf8aa | 1122 | return false; |
kbhagat6 | 7:0564c6faf8aa | 1123 | } |
kbhagat6 | 7:0564c6faf8aa | 1124 | |
kbhagat6 | 7:0564c6faf8aa | 1125 | return true; |
kbhagat6 | 7:0564c6faf8aa | 1126 | } |
kbhagat6 | 7:0564c6faf8aa | 1127 | |
kbhagat6 | 9:286d00d9db80 | 1128 | /** |
kbhagat6 | 9:286d00d9db80 | 1129 | * @brief Returns receiver gain for proximity detection |
kbhagat6 | 9:286d00d9db80 | 1130 | * |
kbhagat6 | 9:286d00d9db80 | 1131 | * Value Gain |
kbhagat6 | 9:286d00d9db80 | 1132 | * 0 1x |
kbhagat6 | 9:286d00d9db80 | 1133 | * 1 2x |
kbhagat6 | 9:286d00d9db80 | 1134 | * 2 4x |
kbhagat6 | 9:286d00d9db80 | 1135 | * 3 8x |
kbhagat6 | 9:286d00d9db80 | 1136 | * |
kbhagat6 | 9:286d00d9db80 | 1137 | * @return the value of the proximity gain. 0xFF on failure. |
kbhagat6 | 9:286d00d9db80 | 1138 | */ |
kbhagat6 | 9:286d00d9db80 | 1139 | uint8_t glibr::getProximityGain() |
kbhagat6 | 9:286d00d9db80 | 1140 | { |
kbhagat6 | 9:286d00d9db80 | 1141 | uint8_t val; |
kbhagat6 | 9:286d00d9db80 | 1142 | |
kbhagat6 | 9:286d00d9db80 | 1143 | /* Read value from CONTROL register */ |
kbhagat6 | 9:286d00d9db80 | 1144 | val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL); |
kbhagat6 | 9:286d00d9db80 | 1145 | if( val == ERROR){//!wireReadDataByte(APDS9960_CONTROL, val) ) { |
kbhagat6 | 9:286d00d9db80 | 1146 | return ERROR; |
kbhagat6 | 9:286d00d9db80 | 1147 | } |
kbhagat6 | 9:286d00d9db80 | 1148 | |
kbhagat6 | 9:286d00d9db80 | 1149 | /* Shift and mask out PDRIVE bits */ |
kbhagat6 | 9:286d00d9db80 | 1150 | val = (val >> 2) & 0x03;//0b00000011; |
kbhagat6 | 9:286d00d9db80 | 1151 | |
kbhagat6 | 9:286d00d9db80 | 1152 | return val; |
kbhagat6 | 9:286d00d9db80 | 1153 | } |
kbhagat6 | 6:687dff16904e | 1154 | |
kbhagat6 | 9:286d00d9db80 | 1155 | /** |
kbhagat6 | 9:286d00d9db80 | 1156 | * @brief Sets the receiver gain for proximity detection |
kbhagat6 | 9:286d00d9db80 | 1157 | * |
kbhagat6 | 9:286d00d9db80 | 1158 | * Value Gain |
kbhagat6 | 9:286d00d9db80 | 1159 | * 0 1x |
kbhagat6 | 9:286d00d9db80 | 1160 | * 1 2x |
kbhagat6 | 9:286d00d9db80 | 1161 | * 2 4x |
kbhagat6 | 9:286d00d9db80 | 1162 | * 3 8x |
kbhagat6 | 9:286d00d9db80 | 1163 | * |
kbhagat6 | 9:286d00d9db80 | 1164 | * @param[in] drive the value (0-3) for the gain |
kbhagat6 | 9:286d00d9db80 | 1165 | * @return True if operation successful. False otherwise. |
kbhagat6 | 9:286d00d9db80 | 1166 | */ |
kbhagat6 | 0:437ae08befe3 | 1167 | bool glibr::setProximityGain(uint8_t drive) |
kbhagat6 | 0:437ae08befe3 | 1168 | { |
kbhagat6 | 0:437ae08befe3 | 1169 | uint8_t val; |
kbhagat6 | 0:437ae08befe3 | 1170 | |
kbhagat6 | 0:437ae08befe3 | 1171 | /* Read value from CONTROL register */ |
kbhagat6 | 1:c7215f5f9a72 | 1172 | |
kbhagat6 | 5:3eb4f3091bd8 | 1173 | val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL); |
kbhagat6 | 0:437ae08befe3 | 1174 | |
kbhagat6 | 1:c7215f5f9a72 | 1175 | if(val==ERROR){ |
kbhagat6 | 1:c7215f5f9a72 | 1176 | return false; |
kbhagat6 | 1:c7215f5f9a72 | 1177 | } |
kbhagat6 | 0:437ae08befe3 | 1178 | /* Set bits in register to given value */ |
kbhagat6 | 1:c7215f5f9a72 | 1179 | //drive &= 0b00000011; |
kbhagat6 | 1:c7215f5f9a72 | 1180 | drive &=0x03; |
kbhagat6 | 0:437ae08befe3 | 1181 | drive = drive << 2; |
kbhagat6 | 9:286d00d9db80 | 1182 | //val &= 0b11110011 |
kbhagat6 | 1:c7215f5f9a72 | 1183 | val &= 0xF3; |
kbhagat6 | 0:437ae08befe3 | 1184 | val |= drive; |
kbhagat6 | 0:437ae08befe3 | 1185 | |
kbhagat6 | 0:437ae08befe3 | 1186 | /* Write register value back into CONTROL register */ |
kbhagat6 | 1:c7215f5f9a72 | 1187 | if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONTROL, val) ) { |
kbhagat6 | 1:c7215f5f9a72 | 1188 | return false; |
kbhagat6 | 1:c7215f5f9a72 | 1189 | } |
kbhagat6 | 1:c7215f5f9a72 | 1190 | return true; |
kbhagat6 | 1:c7215f5f9a72 | 1191 | } |
kbhagat6 | 1:c7215f5f9a72 | 1192 | |
kbhagat6 | 9:286d00d9db80 | 1193 | /** |
kbhagat6 | 9:286d00d9db80 | 1194 | * @brief Returns receiver gain for the ambient light sensor (ALS) |
kbhagat6 | 9:286d00d9db80 | 1195 | * |
kbhagat6 | 9:286d00d9db80 | 1196 | * Value Gain |
kbhagat6 | 9:286d00d9db80 | 1197 | * 0 1x |
kbhagat6 | 9:286d00d9db80 | 1198 | * 1 4x |
kbhagat6 | 9:286d00d9db80 | 1199 | * 2 16x |
kbhagat6 | 9:286d00d9db80 | 1200 | * 3 64x |
kbhagat6 | 9:286d00d9db80 | 1201 | * |
kbhagat6 | 9:286d00d9db80 | 1202 | * @return the value of the ALS gain. 0xFF on failure. |
kbhagat6 | 9:286d00d9db80 | 1203 | */ |
kbhagat6 | 9:286d00d9db80 | 1204 | uint8_t glibr::getAmbientLightGain() |
kbhagat6 | 9:286d00d9db80 | 1205 | { |
kbhagat6 | 9:286d00d9db80 | 1206 | uint8_t val; |
kbhagat6 | 9:286d00d9db80 | 1207 | |
kbhagat6 | 9:286d00d9db80 | 1208 | /* Read value from CONTROL register */ |
kbhagat6 | 9:286d00d9db80 | 1209 | val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL); |
kbhagat6 | 9:286d00d9db80 | 1210 | if( val == ERROR){//!wireReadDataByte(APDS9960_CONTROL, val) ) { |
kbhagat6 | 9:286d00d9db80 | 1211 | return ERROR; |
kbhagat6 | 9:286d00d9db80 | 1212 | } |
kbhagat6 | 9:286d00d9db80 | 1213 | |
kbhagat6 | 9:286d00d9db80 | 1214 | /* Shift and mask out ADRIVE bits */ |
kbhagat6 | 9:286d00d9db80 | 1215 | val &= 0x03;//0b00000011; |
kbhagat6 | 9:286d00d9db80 | 1216 | |
kbhagat6 | 9:286d00d9db80 | 1217 | return val; |
kbhagat6 | 9:286d00d9db80 | 1218 | } |
kbhagat6 | 1:c7215f5f9a72 | 1219 | |
kbhagat6 | 9:286d00d9db80 | 1220 | /** |
kbhagat6 | 9:286d00d9db80 | 1221 | * @brief Sets the receiver gain for the ambient light sensor (ALS) |
kbhagat6 | 9:286d00d9db80 | 1222 | * |
kbhagat6 | 9:286d00d9db80 | 1223 | * Value Gain |
kbhagat6 | 9:286d00d9db80 | 1224 | * 0 1x |
kbhagat6 | 9:286d00d9db80 | 1225 | * 1 4x |
kbhagat6 | 9:286d00d9db80 | 1226 | * 2 16x |
kbhagat6 | 9:286d00d9db80 | 1227 | * 3 64x |
kbhagat6 | 9:286d00d9db80 | 1228 | * |
kbhagat6 | 9:286d00d9db80 | 1229 | * @param[in] drive the value (0-3) for the gain |
kbhagat6 | 9:286d00d9db80 | 1230 | * @return True if operation successful. False otherwise. |
kbhagat6 | 9:286d00d9db80 | 1231 | */ |
kbhagat6 | 1:c7215f5f9a72 | 1232 | bool glibr::setAmbientLightGain(uint8_t drive){ |
kbhagat6 | 9:286d00d9db80 | 1233 | |
kbhagat6 | 1:c7215f5f9a72 | 1234 | uint8_t val; |
kbhagat6 | 1:c7215f5f9a72 | 1235 | |
kbhagat6 | 1:c7215f5f9a72 | 1236 | /* Read value from CONTROL register */ |
kbhagat6 | 1:c7215f5f9a72 | 1237 | |
kbhagat6 | 5:3eb4f3091bd8 | 1238 | val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL); |
kbhagat6 | 1:c7215f5f9a72 | 1239 | |
kbhagat6 | 1:c7215f5f9a72 | 1240 | if(val==ERROR){ |
kbhagat6 | 1:c7215f5f9a72 | 1241 | return false; |
kbhagat6 | 1:c7215f5f9a72 | 1242 | } |
kbhagat6 | 1:c7215f5f9a72 | 1243 | /* Set bits in register to given value */ |
kbhagat6 | 1:c7215f5f9a72 | 1244 | //drive &= 0b00000011; |
kbhagat6 | 1:c7215f5f9a72 | 1245 | drive &=0x03; |
kbhagat6 | 1:c7215f5f9a72 | 1246 | drive = drive << 2; |
kbhagat6 | 9:286d00d9db80 | 1247 | //val &=0b11111100 |
kbhagat6 | 1:c7215f5f9a72 | 1248 | val &= 0xF3; |
kbhagat6 | 1:c7215f5f9a72 | 1249 | val |= drive; |
kbhagat6 | 1:c7215f5f9a72 | 1250 | |
kbhagat6 | 1:c7215f5f9a72 | 1251 | /* Write register value back into CONTROL register */ |
kbhagat6 | 1:c7215f5f9a72 | 1252 | if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONTROL, val) ) { |
kbhagat6 | 1:c7215f5f9a72 | 1253 | return false; |
kbhagat6 | 1:c7215f5f9a72 | 1254 | } |
kbhagat6 | 1:c7215f5f9a72 | 1255 | return true; |
kbhagat6 | 1:c7215f5f9a72 | 1256 | } |
kbhagat6 | 1:c7215f5f9a72 | 1257 | |
kbhagat6 | 9:286d00d9db80 | 1258 | /** |
kbhagat6 | 9:286d00d9db80 | 1259 | * @brief Get the current LED boost value |
kbhagat6 | 9:286d00d9db80 | 1260 | * |
kbhagat6 | 9:286d00d9db80 | 1261 | * Value Boost Current |
kbhagat6 | 9:286d00d9db80 | 1262 | * 0 100% |
kbhagat6 | 9:286d00d9db80 | 1263 | * 1 150% |
kbhagat6 | 9:286d00d9db80 | 1264 | * 2 200% |
kbhagat6 | 9:286d00d9db80 | 1265 | * 3 300% |
kbhagat6 | 9:286d00d9db80 | 1266 | * |
kbhagat6 | 9:286d00d9db80 | 1267 | * @return The LED boost value. 0xFF on failure. |
kbhagat6 | 9:286d00d9db80 | 1268 | */ |
kbhagat6 | 9:286d00d9db80 | 1269 | uint8_t glibr::getLEDBoost() { |
kbhagat6 | 9:286d00d9db80 | 1270 | uint8_t val; |
kbhagat6 | 9:286d00d9db80 | 1271 | |
kbhagat6 | 9:286d00d9db80 | 1272 | /* Read value from CONFIG2 register */ |
kbhagat6 | 9:286d00d9db80 | 1273 | val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_CONFIG2); |
kbhagat6 | 9:286d00d9db80 | 1274 | if( val == ERROR){//!wireReadDataByte(APDS9960_CONFIG2, val) ) { |
kbhagat6 | 9:286d00d9db80 | 1275 | return ERROR; |
kbhagat6 | 9:286d00d9db80 | 1276 | } |
kbhagat6 | 9:286d00d9db80 | 1277 | |
kbhagat6 | 9:286d00d9db80 | 1278 | /* Shift and mask out LED_BOOST bits */ |
kbhagat6 | 9:286d00d9db80 | 1279 | val = (val >> 4) & 0x03;//0b00000011; |
kbhagat6 | 9:286d00d9db80 | 1280 | |
kbhagat6 | 9:286d00d9db80 | 1281 | return val; |
kbhagat6 | 9:286d00d9db80 | 1282 | } |
kbhagat6 | 1:c7215f5f9a72 | 1283 | |
kbhagat6 | 9:286d00d9db80 | 1284 | /** |
kbhagat6 | 9:286d00d9db80 | 1285 | * @brief Sets the LED current boost value |
kbhagat6 | 9:286d00d9db80 | 1286 | * |
kbhagat6 | 9:286d00d9db80 | 1287 | * Value Boost Current |
kbhagat6 | 9:286d00d9db80 | 1288 | * 0 100% |
kbhagat6 | 9:286d00d9db80 | 1289 | * 1 150% |
kbhagat6 | 9:286d00d9db80 | 1290 | * 2 200% |
kbhagat6 | 9:286d00d9db80 | 1291 | * 3 300% |
kbhagat6 | 9:286d00d9db80 | 1292 | * |
kbhagat6 | 9:286d00d9db80 | 1293 | * @param[in] drive the value (0-3) for current boost (100-300%) |
kbhagat6 | 9:286d00d9db80 | 1294 | * @return True if operation successful. False otherwise. |
kbhagat6 | 9:286d00d9db80 | 1295 | */ |
kbhagat6 | 9:286d00d9db80 | 1296 | bool glibr::setLEDBoost(uint8_t boost) |
kbhagat6 | 9:286d00d9db80 | 1297 | { |
kbhagat6 | 9:286d00d9db80 | 1298 | uint8_t val; |
kbhagat6 | 9:286d00d9db80 | 1299 | |
kbhagat6 | 9:286d00d9db80 | 1300 | /* Read value from CONFIG2 register */ |
kbhagat6 | 9:286d00d9db80 | 1301 | val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_CONFIG2); |
kbhagat6 | 9:286d00d9db80 | 1302 | if( val == ERROR){//!wireReadDataByte(APDS9960_CONFIG2, val) ) { |
kbhagat6 | 9:286d00d9db80 | 1303 | return false; |
kbhagat6 | 9:286d00d9db80 | 1304 | } |
kbhagat6 | 9:286d00d9db80 | 1305 | |
kbhagat6 | 9:286d00d9db80 | 1306 | /* Set bits in register to given value */ |
kbhagat6 | 9:286d00d9db80 | 1307 | boost &= 0x03;//0b00000011; |
kbhagat6 | 9:286d00d9db80 | 1308 | boost = boost << 4; |
kbhagat6 | 9:286d00d9db80 | 1309 | val &= 0xCF;//0b11001111; |
kbhagat6 | 9:286d00d9db80 | 1310 | val |= boost; |
kbhagat6 | 9:286d00d9db80 | 1311 | |
kbhagat6 | 9:286d00d9db80 | 1312 | /* Write register value back into CONFIG2 register */ |
kbhagat6 | 9:286d00d9db80 | 1313 | |
kbhagat6 | 9:286d00d9db80 | 1314 | if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONFIG2, val)){//!wireWriteDataByte(APDS9960_CONFIG2, val) ) { |
kbhagat6 | 9:286d00d9db80 | 1315 | return false; |
kbhagat6 | 9:286d00d9db80 | 1316 | } |
kbhagat6 | 9:286d00d9db80 | 1317 | |
kbhagat6 | 9:286d00d9db80 | 1318 | return true; |
kbhagat6 | 9:286d00d9db80 | 1319 | } |
kbhagat6 | 9:286d00d9db80 | 1320 | |
kbhagat6 | 9:286d00d9db80 | 1321 | /** |
kbhagat6 | 9:286d00d9db80 | 1322 | * @brief Gets proximity gain compensation enable |
kbhagat6 | 9:286d00d9db80 | 1323 | * |
kbhagat6 | 9:286d00d9db80 | 1324 | * @return 1 if compensation is enabled. 0 if not. 0xFF on error. |
kbhagat6 | 9:286d00d9db80 | 1325 | */ |
kbhagat6 | 9:286d00d9db80 | 1326 | uint8_t glibr::getProxGainCompEnable() |
kbhagat6 | 9:286d00d9db80 | 1327 | { |
kbhagat6 | 9:286d00d9db80 | 1328 | uint8_t val; |
kbhagat6 | 9:286d00d9db80 | 1329 | |
kbhagat6 | 9:286d00d9db80 | 1330 | /* Read value from CONFIG3 register */ |
kbhagat6 | 9:286d00d9db80 | 1331 | val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_CONFIG3); |
kbhagat6 | 9:286d00d9db80 | 1332 | if( val == ERROR){//!wireReadDataByte(APDS9960_CONFIG3, val) ) { |
kbhagat6 | 9:286d00d9db80 | 1333 | return ERROR; |
kbhagat6 | 9:286d00d9db80 | 1334 | } |
kbhagat6 | 9:286d00d9db80 | 1335 | |
kbhagat6 | 9:286d00d9db80 | 1336 | /* Shift and mask out PCMP bits */ |
kbhagat6 | 9:286d00d9db80 | 1337 | val = (val >> 5) & 0x01;//0b00000001; |
kbhagat6 | 9:286d00d9db80 | 1338 | |
kbhagat6 | 9:286d00d9db80 | 1339 | return val; |
kbhagat6 | 9:286d00d9db80 | 1340 | } |
kbhagat6 | 9:286d00d9db80 | 1341 | |
kbhagat6 | 9:286d00d9db80 | 1342 | /** |
kbhagat6 | 9:286d00d9db80 | 1343 | * @brief Sets the proximity gain compensation enable |
kbhagat6 | 9:286d00d9db80 | 1344 | * |
kbhagat6 | 9:286d00d9db80 | 1345 | * @param[in] enable 1 to enable compensation. 0 to disable compensation. |
kbhagat6 | 9:286d00d9db80 | 1346 | * @return True if operation successful. False otherwise. |
kbhagat6 | 9:286d00d9db80 | 1347 | */ |
kbhagat6 | 9:286d00d9db80 | 1348 | bool glibr::setProxGainCompEnable(uint8_t enable) |
kbhagat6 | 9:286d00d9db80 | 1349 | { |
kbhagat6 | 9:286d00d9db80 | 1350 | uint8_t val; |
kbhagat6 | 9:286d00d9db80 | 1351 | |
kbhagat6 | 9:286d00d9db80 | 1352 | /* Read value from CONFIG3 register */ |
kbhagat6 | 9:286d00d9db80 | 1353 | val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_CONFIG3); |
kbhagat6 | 9:286d00d9db80 | 1354 | if( val == ERROR){//!wireReadDataByte(APDS9960_CONFIG3, val) ) { |
kbhagat6 | 9:286d00d9db80 | 1355 | return false; |
kbhagat6 | 9:286d00d9db80 | 1356 | } |
kbhagat6 | 9:286d00d9db80 | 1357 | |
kbhagat6 | 9:286d00d9db80 | 1358 | /* Set bits in register to given value */ |
kbhagat6 | 9:286d00d9db80 | 1359 | enable &= 0x01;//0b00000001; |
kbhagat6 | 9:286d00d9db80 | 1360 | enable = enable << 5; |
kbhagat6 | 9:286d00d9db80 | 1361 | val &= 0xCF;//0b11011111; |
kbhagat6 | 9:286d00d9db80 | 1362 | val |= enable; |
kbhagat6 | 9:286d00d9db80 | 1363 | |
kbhagat6 | 9:286d00d9db80 | 1364 | /* Write register value back into CONFIG3 register */ |
kbhagat6 | 9:286d00d9db80 | 1365 | |
kbhagat6 | 9:286d00d9db80 | 1366 | if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONFIG3, val)){//!wireWriteDataByte(APDS9960_CONFIG3, val) ) { |
kbhagat6 | 9:286d00d9db80 | 1367 | return false; |
kbhagat6 | 9:286d00d9db80 | 1368 | } |
kbhagat6 | 9:286d00d9db80 | 1369 | |
kbhagat6 | 9:286d00d9db80 | 1370 | return true; |
kbhagat6 | 9:286d00d9db80 | 1371 | } |
kbhagat6 | 9:286d00d9db80 | 1372 | |
kbhagat6 | 9:286d00d9db80 | 1373 | /** |
kbhagat6 | 9:286d00d9db80 | 1374 | * @brief Gets the current mask for enabled/disabled proximity photodiodes |
kbhagat6 | 9:286d00d9db80 | 1375 | * |
kbhagat6 | 9:286d00d9db80 | 1376 | * 1 = disabled, 0 = enabled |
kbhagat6 | 9:286d00d9db80 | 1377 | * Bit Photodiode |
kbhagat6 | 9:286d00d9db80 | 1378 | * 3 UP |
kbhagat6 | 9:286d00d9db80 | 1379 | * 2 DOWN |
kbhagat6 | 9:286d00d9db80 | 1380 | * 1 LEFT |
kbhagat6 | 9:286d00d9db80 | 1381 | * 0 RIGHT |
kbhagat6 | 9:286d00d9db80 | 1382 | * |
kbhagat6 | 9:286d00d9db80 | 1383 | * @return Current proximity mask for photodiodes. 0xFF on error. |
kbhagat6 | 9:286d00d9db80 | 1384 | */ |
kbhagat6 | 9:286d00d9db80 | 1385 | uint8_t glibr::getProxPhotoMask() |
kbhagat6 | 9:286d00d9db80 | 1386 | { |
kbhagat6 | 9:286d00d9db80 | 1387 | uint8_t val; |
kbhagat6 | 9:286d00d9db80 | 1388 | |
kbhagat6 | 9:286d00d9db80 | 1389 | /* Read value from CONFIG3 register */ |
kbhagat6 | 9:286d00d9db80 | 1390 | val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_CONFIG3); |
kbhagat6 | 9:286d00d9db80 | 1391 | if( val == ERROR){//!wireReadDataByte(APDS9960_CONFIG3, val) ) { |
kbhagat6 | 9:286d00d9db80 | 1392 | return ERROR; |
kbhagat6 | 9:286d00d9db80 | 1393 | } |
kbhagat6 | 9:286d00d9db80 | 1394 | |
kbhagat6 | 9:286d00d9db80 | 1395 | /* Mask out photodiode enable mask bits */ |
kbhagat6 | 9:286d00d9db80 | 1396 | val &= 0x0F;//0b00001111; |
kbhagat6 | 9:286d00d9db80 | 1397 | |
kbhagat6 | 9:286d00d9db80 | 1398 | return val; |
kbhagat6 | 9:286d00d9db80 | 1399 | } |
kbhagat6 | 9:286d00d9db80 | 1400 | |
kbhagat6 | 9:286d00d9db80 | 1401 | /** |
kbhagat6 | 9:286d00d9db80 | 1402 | * @brief Sets the mask for enabling/disabling proximity photodiodes |
kbhagat6 | 9:286d00d9db80 | 1403 | * |
kbhagat6 | 9:286d00d9db80 | 1404 | * 1 = disabled, 0 = enabled |
kbhagat6 | 9:286d00d9db80 | 1405 | * Bit Photodiode |
kbhagat6 | 9:286d00d9db80 | 1406 | * 3 UP |
kbhagat6 | 9:286d00d9db80 | 1407 | * 2 DOWN |
kbhagat6 | 9:286d00d9db80 | 1408 | * 1 LEFT |
kbhagat6 | 9:286d00d9db80 | 1409 | * 0 RIGHT |
kbhagat6 | 9:286d00d9db80 | 1410 | * |
kbhagat6 | 9:286d00d9db80 | 1411 | * @param[in] mask 4-bit mask value |
kbhagat6 | 9:286d00d9db80 | 1412 | * @return True if operation successful. False otherwise. |
kbhagat6 | 9:286d00d9db80 | 1413 | */ |
kbhagat6 | 9:286d00d9db80 | 1414 | bool glibr::setProxPhotoMask(uint8_t mask) |
kbhagat6 | 9:286d00d9db80 | 1415 | { |
kbhagat6 | 9:286d00d9db80 | 1416 | uint8_t val; |
kbhagat6 | 9:286d00d9db80 | 1417 | |
kbhagat6 | 9:286d00d9db80 | 1418 | /* Read value from CONFIG3 register */ |
kbhagat6 | 9:286d00d9db80 | 1419 | val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_CONFIG3); |
kbhagat6 | 9:286d00d9db80 | 1420 | if( val == ERROR){//!wireReadDataByte(APDS9960_CONFIG3, val) ) { |
kbhagat6 | 9:286d00d9db80 | 1421 | return false; |
kbhagat6 | 9:286d00d9db80 | 1422 | } |
kbhagat6 | 9:286d00d9db80 | 1423 | |
kbhagat6 | 9:286d00d9db80 | 1424 | /* Set bits in register to given value */ |
kbhagat6 | 9:286d00d9db80 | 1425 | mask &= 0x0F;//0b00001111; |
kbhagat6 | 9:286d00d9db80 | 1426 | val &= 0xF0;//0b11110000; |
kbhagat6 | 9:286d00d9db80 | 1427 | val |= mask; |
kbhagat6 | 9:286d00d9db80 | 1428 | |
kbhagat6 | 9:286d00d9db80 | 1429 | /* Write register value back into CONFIG3 register */ |
kbhagat6 | 9:286d00d9db80 | 1430 | I2CwriteByte(APDS9960_I2C_ADDR, APDS9960_CONFIG3, val); |
kbhagat6 | 9:286d00d9db80 | 1431 | if( val == ERROR){//!wireWriteDataByte(APDS9960_CONFIG3, val) ) { |
kbhagat6 | 9:286d00d9db80 | 1432 | return false; |
kbhagat6 | 9:286d00d9db80 | 1433 | } |
kbhagat6 | 9:286d00d9db80 | 1434 | |
kbhagat6 | 9:286d00d9db80 | 1435 | return true; |
kbhagat6 | 9:286d00d9db80 | 1436 | } |
kbhagat6 | 9:286d00d9db80 | 1437 | |
kbhagat6 | 9:286d00d9db80 | 1438 | /** |
kbhagat6 | 9:286d00d9db80 | 1439 | * @brief Gets the entry proximity threshold for gesture sensing |
kbhagat6 | 9:286d00d9db80 | 1440 | * |
kbhagat6 | 9:286d00d9db80 | 1441 | * @return Current entry proximity threshold. |
kbhagat6 | 9:286d00d9db80 | 1442 | */ |
kbhagat6 | 9:286d00d9db80 | 1443 | uint8_t glibr::getGestureEnterThresh() |
kbhagat6 | 9:286d00d9db80 | 1444 | { |
kbhagat6 | 9:286d00d9db80 | 1445 | uint8_t val; |
kbhagat6 | 9:286d00d9db80 | 1446 | |
kbhagat6 | 9:286d00d9db80 | 1447 | /* Read value from GPENTH register */ |
kbhagat6 | 9:286d00d9db80 | 1448 | val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GPENTH); |
kbhagat6 | 9:286d00d9db80 | 1449 | if( val == ERROR){//!wireReadDataByte(APDS9960_GPENTH, val) ) { |
kbhagat6 | 9:286d00d9db80 | 1450 | val = 0; |
kbhagat6 | 9:286d00d9db80 | 1451 | } |
kbhagat6 | 9:286d00d9db80 | 1452 | |
kbhagat6 | 9:286d00d9db80 | 1453 | return val; |
kbhagat6 | 9:286d00d9db80 | 1454 | } |
kbhagat6 | 9:286d00d9db80 | 1455 | |
kbhagat6 | 9:286d00d9db80 | 1456 | /** |
kbhagat6 | 9:286d00d9db80 | 1457 | * @brief Sets the entry proximity threshold for gesture sensing |
kbhagat6 | 9:286d00d9db80 | 1458 | * |
kbhagat6 | 9:286d00d9db80 | 1459 | * @param[in] threshold proximity value needed to start gesture mode |
kbhagat6 | 9:286d00d9db80 | 1460 | * @return True if operation successful. False otherwise. |
kbhagat6 | 9:286d00d9db80 | 1461 | */ |
kbhagat6 | 9:286d00d9db80 | 1462 | bool glibr::setGestureEnterThresh(uint8_t threshold) |
kbhagat6 | 9:286d00d9db80 | 1463 | { |
kbhagat6 | 9:286d00d9db80 | 1464 | |
kbhagat6 | 9:286d00d9db80 | 1465 | if( I2CwriteByte(APDS9960_I2C_ADDR, APDS9960_GPENTH, threshold)){;//!wireWriteDataByte(APDS9960_GPENTH, threshold) ) { |
kbhagat6 | 9:286d00d9db80 | 1466 | return false; |
kbhagat6 | 9:286d00d9db80 | 1467 | } |
kbhagat6 | 9:286d00d9db80 | 1468 | |
kbhagat6 | 9:286d00d9db80 | 1469 | return true; |
kbhagat6 | 9:286d00d9db80 | 1470 | } |
kbhagat6 | 9:286d00d9db80 | 1471 | |
kbhagat6 | 9:286d00d9db80 | 1472 | /** |
kbhagat6 | 9:286d00d9db80 | 1473 | * @brief Gets the exit proximity threshold for gesture sensing |
kbhagat6 | 9:286d00d9db80 | 1474 | * |
kbhagat6 | 9:286d00d9db80 | 1475 | * @return Current exit proximity threshold. |
kbhagat6 | 9:286d00d9db80 | 1476 | */ |
kbhagat6 | 9:286d00d9db80 | 1477 | uint8_t glibr::getGestureExitThresh() |
kbhagat6 | 9:286d00d9db80 | 1478 | { |
kbhagat6 | 9:286d00d9db80 | 1479 | uint8_t val; |
kbhagat6 | 9:286d00d9db80 | 1480 | |
kbhagat6 | 9:286d00d9db80 | 1481 | /* Read value from GEXTH register */ |
kbhagat6 | 9:286d00d9db80 | 1482 | val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GEXTH); |
kbhagat6 | 9:286d00d9db80 | 1483 | if( val == ERROR){//!wireReadDataByte(APDS9960_GEXTH, val) ) { |
kbhagat6 | 9:286d00d9db80 | 1484 | val = 0; |
kbhagat6 | 9:286d00d9db80 | 1485 | } |
kbhagat6 | 9:286d00d9db80 | 1486 | |
kbhagat6 | 9:286d00d9db80 | 1487 | return val; |
kbhagat6 | 9:286d00d9db80 | 1488 | } |
kbhagat6 | 9:286d00d9db80 | 1489 | |
kbhagat6 | 9:286d00d9db80 | 1490 | /** |
kbhagat6 | 9:286d00d9db80 | 1491 | * @brief Sets the exit proximity threshold for gesture sensing |
kbhagat6 | 9:286d00d9db80 | 1492 | * |
kbhagat6 | 9:286d00d9db80 | 1493 | * @param[in] threshold proximity value needed to end gesture mode |
kbhagat6 | 9:286d00d9db80 | 1494 | * @return True if operation successful. False otherwise. |
kbhagat6 | 9:286d00d9db80 | 1495 | */ |
kbhagat6 | 9:286d00d9db80 | 1496 | bool glibr::setGestureExitThresh(uint8_t threshold) |
kbhagat6 | 9:286d00d9db80 | 1497 | { |
kbhagat6 | 9:286d00d9db80 | 1498 | if( I2CwriteByte(APDS9960_I2C_ADDR, APDS9960_GEXTH, threshold)){//!wireWriteDataByte(APDS9960_GEXTH, threshold) ) { |
kbhagat6 | 9:286d00d9db80 | 1499 | return false; |
kbhagat6 | 9:286d00d9db80 | 1500 | } |
kbhagat6 | 9:286d00d9db80 | 1501 | |
kbhagat6 | 9:286d00d9db80 | 1502 | return true; |
kbhagat6 | 9:286d00d9db80 | 1503 | } |
kbhagat6 | 9:286d00d9db80 | 1504 | |
kbhagat6 | 9:286d00d9db80 | 1505 | /** |
kbhagat6 | 9:286d00d9db80 | 1506 | * @brief Gets the gain of the photodiode during gesture mode |
kbhagat6 | 9:286d00d9db80 | 1507 | * |
kbhagat6 | 9:286d00d9db80 | 1508 | * Value Gain |
kbhagat6 | 9:286d00d9db80 | 1509 | * 0 1x |
kbhagat6 | 9:286d00d9db80 | 1510 | * 1 2x |
kbhagat6 | 9:286d00d9db80 | 1511 | * 2 4x |
kbhagat6 | 9:286d00d9db80 | 1512 | * 3 8x |
kbhagat6 | 9:286d00d9db80 | 1513 | * |
kbhagat6 | 9:286d00d9db80 | 1514 | * @return the current photodiode gain. 0xFF on error. |
kbhagat6 | 9:286d00d9db80 | 1515 | */ |
kbhagat6 | 9:286d00d9db80 | 1516 | uint8_t glibr::getGestureGain() |
kbhagat6 | 9:286d00d9db80 | 1517 | { |
kbhagat6 | 9:286d00d9db80 | 1518 | uint8_t val; |
kbhagat6 | 9:286d00d9db80 | 1519 | |
kbhagat6 | 9:286d00d9db80 | 1520 | /* Read value from GCONF2 register */ |
kbhagat6 | 9:286d00d9db80 | 1521 | val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF2); |
kbhagat6 | 9:286d00d9db80 | 1522 | if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF2, val) ) { |
kbhagat6 | 9:286d00d9db80 | 1523 | return ERROR; |
kbhagat6 | 9:286d00d9db80 | 1524 | } |
kbhagat6 | 9:286d00d9db80 | 1525 | |
kbhagat6 | 9:286d00d9db80 | 1526 | /* Shift and mask out GGAIN bits */ |
kbhagat6 | 9:286d00d9db80 | 1527 | val = (val >> 5) & 0x03;//0b00000011; |
kbhagat6 | 9:286d00d9db80 | 1528 | |
kbhagat6 | 9:286d00d9db80 | 1529 | return val; |
kbhagat6 | 9:286d00d9db80 | 1530 | } |
kbhagat6 | 9:286d00d9db80 | 1531 | |
kbhagat6 | 9:286d00d9db80 | 1532 | /** |
kbhagat6 | 9:286d00d9db80 | 1533 | * @brief Sets the gain of the photodiode during gesture mode |
kbhagat6 | 9:286d00d9db80 | 1534 | * |
kbhagat6 | 9:286d00d9db80 | 1535 | * Value Gain |
kbhagat6 | 9:286d00d9db80 | 1536 | * 0 1x |
kbhagat6 | 9:286d00d9db80 | 1537 | * 1 2x |
kbhagat6 | 9:286d00d9db80 | 1538 | * 2 4x |
kbhagat6 | 9:286d00d9db80 | 1539 | * 3 8x |
kbhagat6 | 9:286d00d9db80 | 1540 | * |
kbhagat6 | 9:286d00d9db80 | 1541 | * @param[in] gain the value for the photodiode gain |
kbhagat6 | 9:286d00d9db80 | 1542 | * @return True if operation successful. False otherwise. |
kbhagat6 | 9:286d00d9db80 | 1543 | */ |
kbhagat6 | 9:286d00d9db80 | 1544 | bool glibr::setGestureGain(uint8_t gain) |
kbhagat6 | 9:286d00d9db80 | 1545 | { |
kbhagat6 | 9:286d00d9db80 | 1546 | uint8_t val; |
kbhagat6 | 9:286d00d9db80 | 1547 | |
kbhagat6 | 9:286d00d9db80 | 1548 | /* Read value from GCONF2 register */ |
kbhagat6 | 9:286d00d9db80 | 1549 | val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF2); |
kbhagat6 | 9:286d00d9db80 | 1550 | if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF2, val) ) { |
kbhagat6 | 9:286d00d9db80 | 1551 | return false; |
kbhagat6 | 9:286d00d9db80 | 1552 | } |
kbhagat6 | 9:286d00d9db80 | 1553 | |
kbhagat6 | 9:286d00d9db80 | 1554 | /* Set bits in register to given value */ |
kbhagat6 | 9:286d00d9db80 | 1555 | gain &= 0x03;//0b00000011; |
kbhagat6 | 9:286d00d9db80 | 1556 | gain = gain << 5; |
kbhagat6 | 9:286d00d9db80 | 1557 | val &= 0x9F;//0b10011111; |
kbhagat6 | 9:286d00d9db80 | 1558 | val |= gain; |
kbhagat6 | 9:286d00d9db80 | 1559 | |
kbhagat6 | 9:286d00d9db80 | 1560 | /* Write register value back into GCONF2 register */ |
kbhagat6 | 9:286d00d9db80 | 1561 | if( I2CwriteByte(APDS9960_I2C_ADDR, APDS9960_GCONF2, val)){//!wireWriteDataByte(APDS9960_GCONF2, val) ) { |
kbhagat6 | 9:286d00d9db80 | 1562 | return false; |
kbhagat6 | 9:286d00d9db80 | 1563 | } |
kbhagat6 | 9:286d00d9db80 | 1564 | |
kbhagat6 | 9:286d00d9db80 | 1565 | return true; |
kbhagat6 | 9:286d00d9db80 | 1566 | } |
kbhagat6 | 9:286d00d9db80 | 1567 | |
kbhagat6 | 9:286d00d9db80 | 1568 | /** |
kbhagat6 | 9:286d00d9db80 | 1569 | * @brief Gets the drive current of the LED during gesture mode |
kbhagat6 | 9:286d00d9db80 | 1570 | * |
kbhagat6 | 9:286d00d9db80 | 1571 | * Value LED Current |
kbhagat6 | 9:286d00d9db80 | 1572 | * 0 100 mA |
kbhagat6 | 9:286d00d9db80 | 1573 | * 1 50 mA |
kbhagat6 | 9:286d00d9db80 | 1574 | * 2 25 mA |
kbhagat6 | 9:286d00d9db80 | 1575 | * 3 12.5 mA |
kbhagat6 | 9:286d00d9db80 | 1576 | * |
kbhagat6 | 9:286d00d9db80 | 1577 | * @return the LED drive current value. 0xFF on error. |
kbhagat6 | 9:286d00d9db80 | 1578 | */ |
kbhagat6 | 9:286d00d9db80 | 1579 | uint8_t glibr::getGestureLEDDrive() |
kbhagat6 | 9:286d00d9db80 | 1580 | { |
kbhagat6 | 9:286d00d9db80 | 1581 | uint8_t val; |
kbhagat6 | 9:286d00d9db80 | 1582 | |
kbhagat6 | 9:286d00d9db80 | 1583 | /* Read value from GCONF2 register */ |
kbhagat6 | 9:286d00d9db80 | 1584 | val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF2); |
kbhagat6 | 9:286d00d9db80 | 1585 | if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF2, val) ) { |
kbhagat6 | 9:286d00d9db80 | 1586 | return ERROR; |
kbhagat6 | 9:286d00d9db80 | 1587 | } |
kbhagat6 | 9:286d00d9db80 | 1588 | |
kbhagat6 | 9:286d00d9db80 | 1589 | /* Shift and mask out GLDRIVE bits */ |
kbhagat6 | 9:286d00d9db80 | 1590 | val = (val >> 3) & 0x03;//0b00000011; |
kbhagat6 | 9:286d00d9db80 | 1591 | |
kbhagat6 | 9:286d00d9db80 | 1592 | return val; |
kbhagat6 | 9:286d00d9db80 | 1593 | } |
kbhagat6 | 9:286d00d9db80 | 1594 | |
kbhagat6 | 9:286d00d9db80 | 1595 | /** |
kbhagat6 | 9:286d00d9db80 | 1596 | * @brief Sets the LED drive current during gesture mode |
kbhagat6 | 9:286d00d9db80 | 1597 | * |
kbhagat6 | 9:286d00d9db80 | 1598 | * Value LED Current |
kbhagat6 | 9:286d00d9db80 | 1599 | * 0 100 mA |
kbhagat6 | 9:286d00d9db80 | 1600 | * 1 50 mA |
kbhagat6 | 9:286d00d9db80 | 1601 | * 2 25 mA |
kbhagat6 | 9:286d00d9db80 | 1602 | * 3 12.5 mA |
kbhagat6 | 9:286d00d9db80 | 1603 | * |
kbhagat6 | 9:286d00d9db80 | 1604 | * @param[in] drive the value for the LED drive current |
kbhagat6 | 9:286d00d9db80 | 1605 | * @return True if operation successful. False otherwise. |
kbhagat6 | 9:286d00d9db80 | 1606 | */ |
kbhagat6 | 9:286d00d9db80 | 1607 | bool glibr::setGestureLEDDrive(uint8_t drive) |
kbhagat6 | 9:286d00d9db80 | 1608 | { |
kbhagat6 | 9:286d00d9db80 | 1609 | uint8_t val; |
kbhagat6 | 9:286d00d9db80 | 1610 | |
kbhagat6 | 9:286d00d9db80 | 1611 | /* Read value from GCONF2 register */ |
kbhagat6 | 9:286d00d9db80 | 1612 | val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF2); |
kbhagat6 | 9:286d00d9db80 | 1613 | if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF2, val) ) { |
kbhagat6 | 9:286d00d9db80 | 1614 | return false; |
kbhagat6 | 9:286d00d9db80 | 1615 | } |
kbhagat6 | 9:286d00d9db80 | 1616 | |
kbhagat6 | 9:286d00d9db80 | 1617 | /* Set bits in register to given value */ |
kbhagat6 | 9:286d00d9db80 | 1618 | drive &= 0x03;//0b00000011; |
kbhagat6 | 9:286d00d9db80 | 1619 | drive = drive << 3; |
kbhagat6 | 9:286d00d9db80 | 1620 | val &= 0xE7;//0b11100111; |
kbhagat6 | 9:286d00d9db80 | 1621 | val |= drive; |
kbhagat6 | 9:286d00d9db80 | 1622 | |
kbhagat6 | 9:286d00d9db80 | 1623 | /* Write register value back into GCONF2 register */ |
kbhagat6 | 9:286d00d9db80 | 1624 | if( I2CwriteByte(APDS9960_I2C_ADDR, APDS9960_GCONF2, val)){//!wireWriteDataByte(APDS9960_GCONF2, val) ) { |
kbhagat6 | 9:286d00d9db80 | 1625 | return false; |
kbhagat6 | 9:286d00d9db80 | 1626 | } |
kbhagat6 | 9:286d00d9db80 | 1627 | |
kbhagat6 | 9:286d00d9db80 | 1628 | return true; |
kbhagat6 | 9:286d00d9db80 | 1629 | } |
kbhagat6 | 9:286d00d9db80 | 1630 | |
kbhagat6 | 9:286d00d9db80 | 1631 | /** |
kbhagat6 | 9:286d00d9db80 | 1632 | * @brief Gets the time in low power mode between gesture detections |
kbhagat6 | 9:286d00d9db80 | 1633 | * |
kbhagat6 | 9:286d00d9db80 | 1634 | * Value Wait time |
kbhagat6 | 9:286d00d9db80 | 1635 | * 0 0 ms |
kbhagat6 | 9:286d00d9db80 | 1636 | * 1 2.8 ms |
kbhagat6 | 9:286d00d9db80 | 1637 | * 2 5.6 ms |
kbhagat6 | 9:286d00d9db80 | 1638 | * 3 8.4 ms |
kbhagat6 | 9:286d00d9db80 | 1639 | * 4 14.0 ms |
kbhagat6 | 9:286d00d9db80 | 1640 | * 5 22.4 ms |
kbhagat6 | 9:286d00d9db80 | 1641 | * 6 30.8 ms |
kbhagat6 | 9:286d00d9db80 | 1642 | * 7 39.2 ms |
kbhagat6 | 9:286d00d9db80 | 1643 | * |
kbhagat6 | 9:286d00d9db80 | 1644 | * @return the current wait time between gestures. 0xFF on error. |
kbhagat6 | 9:286d00d9db80 | 1645 | */ |
kbhagat6 | 9:286d00d9db80 | 1646 | uint8_t glibr::getGestureWaitTime() |
kbhagat6 | 9:286d00d9db80 | 1647 | { |
kbhagat6 | 9:286d00d9db80 | 1648 | uint8_t val; |
kbhagat6 | 9:286d00d9db80 | 1649 | |
kbhagat6 | 9:286d00d9db80 | 1650 | /* Read value from GCONF2 register */ |
kbhagat6 | 9:286d00d9db80 | 1651 | val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF2); |
kbhagat6 | 9:286d00d9db80 | 1652 | if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF2, val) ) { |
kbhagat6 | 9:286d00d9db80 | 1653 | return ERROR; |
kbhagat6 | 9:286d00d9db80 | 1654 | } |
kbhagat6 | 9:286d00d9db80 | 1655 | |
kbhagat6 | 9:286d00d9db80 | 1656 | /* Mask out GWTIME bits */ |
kbhagat6 | 9:286d00d9db80 | 1657 | val &= 0x07;//0b00000111; |
kbhagat6 | 9:286d00d9db80 | 1658 | |
kbhagat6 | 9:286d00d9db80 | 1659 | return val; |
kbhagat6 | 9:286d00d9db80 | 1660 | } |
kbhagat6 | 9:286d00d9db80 | 1661 | |
kbhagat6 | 9:286d00d9db80 | 1662 | /* |
kbhagat6 | 9:286d00d9db80 | 1663 | * |
kbhagat6 | 9:286d00d9db80 | 1664 | * |
kbhagat6 | 9:286d00d9db80 | 1665 | * |
kbhagat6 | 9:286d00d9db80 | 1666 | *LEFT OFF HERE AT 3:47PM ON 3/6/15 |
kbhagat6 | 9:286d00d9db80 | 1667 | * |
kbhagat6 | 9:286d00d9db80 | 1668 | * |
kbhagat6 | 9:286d00d9db80 | 1669 | * |
kbhagat6 | 9:286d00d9db80 | 1670 | * |
kbhagat6 | 9:286d00d9db80 | 1671 | */ |
kbhagat6 | 9:286d00d9db80 | 1672 | |
kbhagat6 | 9:286d00d9db80 | 1673 | |
kbhagat6 | 9:286d00d9db80 | 1674 | /** |
kbhagat6 | 9:286d00d9db80 | 1675 | * @brief Sets the time in low power mode between gesture detections |
kbhagat6 | 9:286d00d9db80 | 1676 | * |
kbhagat6 | 9:286d00d9db80 | 1677 | * Value Wait time |
kbhagat6 | 9:286d00d9db80 | 1678 | * 0 0 ms |
kbhagat6 | 9:286d00d9db80 | 1679 | * 1 2.8 ms |
kbhagat6 | 9:286d00d9db80 | 1680 | * 2 5.6 ms |
kbhagat6 | 9:286d00d9db80 | 1681 | * 3 8.4 ms |
kbhagat6 | 9:286d00d9db80 | 1682 | * 4 14.0 ms |
kbhagat6 | 9:286d00d9db80 | 1683 | * 5 22.4 ms |
kbhagat6 | 9:286d00d9db80 | 1684 | * 6 30.8 ms |
kbhagat6 | 9:286d00d9db80 | 1685 | * 7 39.2 ms |
kbhagat6 | 9:286d00d9db80 | 1686 | * |
kbhagat6 | 9:286d00d9db80 | 1687 | * @param[in] the value for the wait time |
kbhagat6 | 9:286d00d9db80 | 1688 | * @return True if operation successful. False otherwise. |
kbhagat6 | 9:286d00d9db80 | 1689 | */ |
kbhagat6 | 9:286d00d9db80 | 1690 | bool glibr::setGestureWaitTime(uint8_t time) |
kbhagat6 | 9:286d00d9db80 | 1691 | { |
kbhagat6 | 9:286d00d9db80 | 1692 | uint8_t val; |
kbhagat6 | 9:286d00d9db80 | 1693 | |
kbhagat6 | 9:286d00d9db80 | 1694 | /* Read value from GCONF2 register */ |
kbhagat6 | 9:286d00d9db80 | 1695 | val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF2); |
kbhagat6 | 9:286d00d9db80 | 1696 | if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF2, val) ) { |
kbhagat6 | 9:286d00d9db80 | 1697 | return false; |
kbhagat6 | 9:286d00d9db80 | 1698 | } |
kbhagat6 | 9:286d00d9db80 | 1699 | /* if( !wireReadDataByte(APDS9960_GCONF2, val) ) { |
kbhagat6 | 9:286d00d9db80 | 1700 | return false; |
kbhagat6 | 9:286d00d9db80 | 1701 | } */ |
kbhagat6 | 9:286d00d9db80 | 1702 | |
kbhagat6 | 9:286d00d9db80 | 1703 | /* Set bits in register to given value */ |
kbhagat6 | 9:286d00d9db80 | 1704 | time &= 0x07;//0b00000111; |
kbhagat6 | 9:286d00d9db80 | 1705 | val &= 0xF8;//0b11111000; |
kbhagat6 | 9:286d00d9db80 | 1706 | val |= time; |
kbhagat6 | 9:286d00d9db80 | 1707 | |
kbhagat6 | 9:286d00d9db80 | 1708 | /* Write register value back into GCONF2 register */ |
kbhagat6 | 9:286d00d9db80 | 1709 | if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GCONF2,val)){//!wireWriteDataByte(APDS9960_GCONF2, val) ) { |
kbhagat6 | 9:286d00d9db80 | 1710 | return false; |
kbhagat6 | 9:286d00d9db80 | 1711 | } |
kbhagat6 | 9:286d00d9db80 | 1712 | /*if( !wireWriteDataByte(APDS9960_GCONF2, val) ) { |
kbhagat6 | 9:286d00d9db80 | 1713 | return false; |
kbhagat6 | 9:286d00d9db80 | 1714 | }*/ |
kbhagat6 | 9:286d00d9db80 | 1715 | return true; |
kbhagat6 | 9:286d00d9db80 | 1716 | } |
kbhagat6 | 9:286d00d9db80 | 1717 | |
kbhagat6 | 9:286d00d9db80 | 1718 | /** |
kbhagat6 | 9:286d00d9db80 | 1719 | * @brief Gets the low threshold for ambient light interrupts |
kbhagat6 | 9:286d00d9db80 | 1720 | * |
kbhagat6 | 9:286d00d9db80 | 1721 | * @param[out] threshold current low threshold stored on the APDS-9960 |
kbhagat6 | 9:286d00d9db80 | 1722 | * @return True if operation successful. False otherwise. |
kbhagat6 | 9:286d00d9db80 | 1723 | */ |
kbhagat6 | 9:286d00d9db80 | 1724 | bool glibr::getLightIntLowThreshold(uint16_t &threshold) |
kbhagat6 | 9:286d00d9db80 | 1725 | { |
kbhagat6 | 9:286d00d9db80 | 1726 | uint8_t val_byte; |
kbhagat6 | 9:286d00d9db80 | 1727 | threshold = 0; |
kbhagat6 | 9:286d00d9db80 | 1728 | |
kbhagat6 | 9:286d00d9db80 | 1729 | /* Read value from ambient light low threshold, low byte register */ |
kbhagat6 | 9:286d00d9db80 | 1730 | val_byte = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_AILTL); |
kbhagat6 | 9:286d00d9db80 | 1731 | if( val_byte == ERROR){//!wireReadDataByte(APDS9960_AILTL, val_byte) ) { |
kbhagat6 | 9:286d00d9db80 | 1732 | return false; |
kbhagat6 | 9:286d00d9db80 | 1733 | } |
kbhagat6 | 9:286d00d9db80 | 1734 | threshold = val_byte; |
kbhagat6 | 9:286d00d9db80 | 1735 | |
kbhagat6 | 9:286d00d9db80 | 1736 | /* Read value from ambient light low threshold, high byte register */ |
kbhagat6 | 9:286d00d9db80 | 1737 | val_byte = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_AILTH); |
kbhagat6 | 9:286d00d9db80 | 1738 | if( val_byte == ERROR){//!wireReadDataByte(APDS9960_AILTH, val_byte) ) { |
kbhagat6 | 9:286d00d9db80 | 1739 | return false; |
kbhagat6 | 9:286d00d9db80 | 1740 | } |
kbhagat6 | 9:286d00d9db80 | 1741 | threshold = threshold + ((uint16_t)val_byte << 8); |
kbhagat6 | 9:286d00d9db80 | 1742 | |
kbhagat6 | 9:286d00d9db80 | 1743 | return true; |
kbhagat6 | 9:286d00d9db80 | 1744 | } |
kbhagat6 | 9:286d00d9db80 | 1745 | |
kbhagat6 | 9:286d00d9db80 | 1746 | /** |
kbhagat6 | 9:286d00d9db80 | 1747 | * @brief Sets the low threshold for ambient light interrupts |
kbhagat6 | 9:286d00d9db80 | 1748 | * |
kbhagat6 | 9:286d00d9db80 | 1749 | * @param[in] threshold low threshold value for interrupt to trigger |
kbhagat6 | 9:286d00d9db80 | 1750 | * @return True if operation successful. False otherwise. |
kbhagat6 | 9:286d00d9db80 | 1751 | */ |
kbhagat6 | 1:c7215f5f9a72 | 1752 | bool glibr::setLightIntLowThreshold(uint16_t threshold) |
kbhagat6 | 1:c7215f5f9a72 | 1753 | { |
kbhagat6 | 1:c7215f5f9a72 | 1754 | uint8_t val_low; |
kbhagat6 | 1:c7215f5f9a72 | 1755 | uint8_t val_high; |
kbhagat6 | 1:c7215f5f9a72 | 1756 | |
kbhagat6 | 1:c7215f5f9a72 | 1757 | /* Break 16-bit threshold into 2 8-bit values */ |
kbhagat6 | 1:c7215f5f9a72 | 1758 | val_low = threshold & 0x00FF; |
kbhagat6 | 1:c7215f5f9a72 | 1759 | val_high = (threshold & 0xFF00) >> 8; |
kbhagat6 | 1:c7215f5f9a72 | 1760 | |
kbhagat6 | 1:c7215f5f9a72 | 1761 | /* Write low byte */ |
kbhagat6 | 9:286d00d9db80 | 1762 | if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_AILTL,val_low)){//!wireWriteDataByte(APDS9960_AILTL, val_low) ) { |
kbhagat6 | 9:286d00d9db80 | 1763 | return false; |
kbhagat6 | 9:286d00d9db80 | 1764 | } |
kbhagat6 | 9:286d00d9db80 | 1765 | |
kbhagat6 | 9:286d00d9db80 | 1766 | /* Write high byte */ |
kbhagat6 | 9:286d00d9db80 | 1767 | if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_AILTH,val_high)){//!wireWriteDataByte(APDS9960_AILTH, val_high) ) { |
kbhagat6 | 9:286d00d9db80 | 1768 | return false; |
kbhagat6 | 9:286d00d9db80 | 1769 | } |
kbhagat6 | 9:286d00d9db80 | 1770 | |
kbhagat6 | 9:286d00d9db80 | 1771 | return true; |
kbhagat6 | 9:286d00d9db80 | 1772 | } |
kbhagat6 | 9:286d00d9db80 | 1773 | |
kbhagat6 | 9:286d00d9db80 | 1774 | /** |
kbhagat6 | 9:286d00d9db80 | 1775 | * @brief Gets the high threshold for ambient light interrupts |
kbhagat6 | 9:286d00d9db80 | 1776 | * |
kbhagat6 | 9:286d00d9db80 | 1777 | * @param[out] threshold current low threshold stored on the APDS-9960 |
kbhagat6 | 9:286d00d9db80 | 1778 | * @return True if operation successful. False otherwise. |
kbhagat6 | 9:286d00d9db80 | 1779 | */ |
kbhagat6 | 9:286d00d9db80 | 1780 | bool glibr::getLightIntHighThreshold(uint16_t &threshold) |
kbhagat6 | 9:286d00d9db80 | 1781 | { |
kbhagat6 | 9:286d00d9db80 | 1782 | uint8_t val_byte; |
kbhagat6 | 9:286d00d9db80 | 1783 | threshold = 0; |
kbhagat6 | 9:286d00d9db80 | 1784 | |
kbhagat6 | 9:286d00d9db80 | 1785 | /* Read value from ambient light high threshold, low byte register */ |
kbhagat6 | 9:286d00d9db80 | 1786 | val_byte = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_AIHTL); |
kbhagat6 | 9:286d00d9db80 | 1787 | if( val_byte == ERROR){//!wireReadDataByte(APDS9960_AIHTL, val_byte) ) { |
kbhagat6 | 9:286d00d9db80 | 1788 | return false; |
kbhagat6 | 9:286d00d9db80 | 1789 | } |
kbhagat6 | 9:286d00d9db80 | 1790 | threshold = val_byte; |
kbhagat6 | 9:286d00d9db80 | 1791 | |
kbhagat6 | 9:286d00d9db80 | 1792 | /* Read value from ambient light high threshold, high byte register */ |
kbhagat6 | 9:286d00d9db80 | 1793 | val_byte = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_AIHTH); |
kbhagat6 | 9:286d00d9db80 | 1794 | if( val_byte == ERROR){//!wireReadDataByte(APDS9960_AIHTH, val_byte) ) { |
kbhagat6 | 9:286d00d9db80 | 1795 | return false; |
kbhagat6 | 9:286d00d9db80 | 1796 | } |
kbhagat6 | 9:286d00d9db80 | 1797 | threshold = threshold + ((uint16_t)val_byte << 8); |
kbhagat6 | 9:286d00d9db80 | 1798 | |
kbhagat6 | 9:286d00d9db80 | 1799 | return true; |
kbhagat6 | 9:286d00d9db80 | 1800 | } |
kbhagat6 | 9:286d00d9db80 | 1801 | |
kbhagat6 | 9:286d00d9db80 | 1802 | /** |
kbhagat6 | 9:286d00d9db80 | 1803 | * @brief Sets the high threshold for ambient light interrupts |
kbhagat6 | 9:286d00d9db80 | 1804 | * |
kbhagat6 | 9:286d00d9db80 | 1805 | * @param[in] threshold high threshold value for interrupt to trigger |
kbhagat6 | 9:286d00d9db80 | 1806 | * @return True if operation successful. False otherwise. |
kbhagat6 | 9:286d00d9db80 | 1807 | */ |
kbhagat6 | 9:286d00d9db80 | 1808 | bool glibr::setLightIntHighThreshold(uint16_t threshold) |
kbhagat6 | 9:286d00d9db80 | 1809 | { |
kbhagat6 | 9:286d00d9db80 | 1810 | uint8_t val_low; |
kbhagat6 | 9:286d00d9db80 | 1811 | uint8_t val_high; |
kbhagat6 | 9:286d00d9db80 | 1812 | |
kbhagat6 | 9:286d00d9db80 | 1813 | /* Break 16-bit threshold into 2 8-bit values */ |
kbhagat6 | 9:286d00d9db80 | 1814 | val_low = threshold & 0x00FF; |
kbhagat6 | 9:286d00d9db80 | 1815 | val_high = (threshold & 0xFF00) >> 8; |
kbhagat6 | 9:286d00d9db80 | 1816 | |
kbhagat6 | 9:286d00d9db80 | 1817 | /* Write low byte */ |
kbhagat6 | 9:286d00d9db80 | 1818 | if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_AIHTL,val_low)){//!wireWriteDataByte(APDS9960_AIHTL, val_low) ) { |
kbhagat6 | 1:c7215f5f9a72 | 1819 | return false; |
kbhagat6 | 1:c7215f5f9a72 | 1820 | } |
kbhagat6 | 1:c7215f5f9a72 | 1821 | |
kbhagat6 | 1:c7215f5f9a72 | 1822 | /* Write high byte */ |
kbhagat6 | 9:286d00d9db80 | 1823 | if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_AIHTH,val_high)){//!wireWriteDataByte(APDS9960_AIHTH, val_high) ) { |
kbhagat6 | 9:286d00d9db80 | 1824 | return false; |
kbhagat6 | 9:286d00d9db80 | 1825 | } |
kbhagat6 | 9:286d00d9db80 | 1826 | |
kbhagat6 | 9:286d00d9db80 | 1827 | return true; |
kbhagat6 | 9:286d00d9db80 | 1828 | } |
kbhagat6 | 9:286d00d9db80 | 1829 | |
kbhagat6 | 9:286d00d9db80 | 1830 | /** |
kbhagat6 | 9:286d00d9db80 | 1831 | * @brief Gets the low threshold for proximity interrupts |
kbhagat6 | 9:286d00d9db80 | 1832 | * |
kbhagat6 | 9:286d00d9db80 | 1833 | * @param[out] threshold current low threshold stored on the APDS-9960 |
kbhagat6 | 9:286d00d9db80 | 1834 | * @return True if operation successful. False otherwise. |
kbhagat6 | 9:286d00d9db80 | 1835 | */ |
kbhagat6 | 9:286d00d9db80 | 1836 | bool glibr::getProximityIntLowThreshold(uint8_t &threshold) |
kbhagat6 | 9:286d00d9db80 | 1837 | { |
kbhagat6 | 9:286d00d9db80 | 1838 | threshold = 0; |
kbhagat6 | 9:286d00d9db80 | 1839 | |
kbhagat6 | 9:286d00d9db80 | 1840 | /* Read value from proximity low threshold register */ |
kbhagat6 | 9:286d00d9db80 | 1841 | threshold = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_PILT); |
kbhagat6 | 9:286d00d9db80 | 1842 | if( threshold == ERROR){//!wireReadDataByte(APDS9960_PILT, threshold) ) { |
kbhagat6 | 9:286d00d9db80 | 1843 | return false; |
kbhagat6 | 9:286d00d9db80 | 1844 | } |
kbhagat6 | 9:286d00d9db80 | 1845 | |
kbhagat6 | 9:286d00d9db80 | 1846 | return true; |
kbhagat6 | 9:286d00d9db80 | 1847 | } |
kbhagat6 | 9:286d00d9db80 | 1848 | |
kbhagat6 | 9:286d00d9db80 | 1849 | /** |
kbhagat6 | 9:286d00d9db80 | 1850 | * @brief Sets the low threshold for proximity interrupts |
kbhagat6 | 9:286d00d9db80 | 1851 | * |
kbhagat6 | 9:286d00d9db80 | 1852 | * @param[in] threshold low threshold value for interrupt to trigger |
kbhagat6 | 9:286d00d9db80 | 1853 | * @return True if operation successful. False otherwise. |
kbhagat6 | 9:286d00d9db80 | 1854 | */ |
kbhagat6 | 9:286d00d9db80 | 1855 | bool glibr::setProximityIntLowThreshold(uint8_t threshold) |
kbhagat6 | 9:286d00d9db80 | 1856 | { |
kbhagat6 | 9:286d00d9db80 | 1857 | |
kbhagat6 | 9:286d00d9db80 | 1858 | /* Write threshold value to register */ |
kbhagat6 | 9:286d00d9db80 | 1859 | if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PILT,threshold)){//!wireWriteDataByte(APDS9960_PILT, threshold) ) { |
kbhagat6 | 9:286d00d9db80 | 1860 | return false; |
kbhagat6 | 9:286d00d9db80 | 1861 | } |
kbhagat6 | 9:286d00d9db80 | 1862 | |
kbhagat6 | 9:286d00d9db80 | 1863 | return true; |
kbhagat6 | 9:286d00d9db80 | 1864 | } |
kbhagat6 | 9:286d00d9db80 | 1865 | |
kbhagat6 | 9:286d00d9db80 | 1866 | /** |
kbhagat6 | 9:286d00d9db80 | 1867 | * @brief Gets the high threshold for proximity interrupts |
kbhagat6 | 9:286d00d9db80 | 1868 | * |
kbhagat6 | 9:286d00d9db80 | 1869 | * @param[out] threshold current low threshold stored on the APDS-9960 |
kbhagat6 | 9:286d00d9db80 | 1870 | * @return True if operation successful. False otherwise. |
kbhagat6 | 9:286d00d9db80 | 1871 | */ |
kbhagat6 | 9:286d00d9db80 | 1872 | bool glibr::getProximityIntHighThreshold(uint8_t &threshold) |
kbhagat6 | 9:286d00d9db80 | 1873 | { |
kbhagat6 | 9:286d00d9db80 | 1874 | threshold = 0; |
kbhagat6 | 9:286d00d9db80 | 1875 | |
kbhagat6 | 9:286d00d9db80 | 1876 | /* Read value from proximity low threshold register */ |
kbhagat6 | 9:286d00d9db80 | 1877 | threshold = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_PIHT); |
kbhagat6 | 9:286d00d9db80 | 1878 | if( threshold == ERROR){//!wireReadDataByte(APDS9960_PIHT, threshold) ) { |
kbhagat6 | 9:286d00d9db80 | 1879 | return false; |
kbhagat6 | 9:286d00d9db80 | 1880 | } |
kbhagat6 | 9:286d00d9db80 | 1881 | |
kbhagat6 | 9:286d00d9db80 | 1882 | return true; |
kbhagat6 | 9:286d00d9db80 | 1883 | } |
kbhagat6 | 9:286d00d9db80 | 1884 | |
kbhagat6 | 9:286d00d9db80 | 1885 | /** |
kbhagat6 | 9:286d00d9db80 | 1886 | * @brief Sets the high threshold for proximity interrupts |
kbhagat6 | 9:286d00d9db80 | 1887 | * |
kbhagat6 | 9:286d00d9db80 | 1888 | * @param[in] threshold high threshold value for interrupt to trigger |
kbhagat6 | 9:286d00d9db80 | 1889 | * @return True if operation successful. False otherwise. |
kbhagat6 | 9:286d00d9db80 | 1890 | */ |
kbhagat6 | 9:286d00d9db80 | 1891 | bool glibr::setProximityIntHighThreshold(uint8_t threshold) |
kbhagat6 | 9:286d00d9db80 | 1892 | { |
kbhagat6 | 9:286d00d9db80 | 1893 | |
kbhagat6 | 9:286d00d9db80 | 1894 | /* Write threshold value to register */ |
kbhagat6 | 9:286d00d9db80 | 1895 | if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PIHT,threshold)){//!wireWriteDataByte(APDS9960_PIHT, threshold) ) { |
kbhagat6 | 9:286d00d9db80 | 1896 | return false; |
kbhagat6 | 9:286d00d9db80 | 1897 | } |
kbhagat6 | 9:286d00d9db80 | 1898 | |
kbhagat6 | 9:286d00d9db80 | 1899 | return true; |
kbhagat6 | 9:286d00d9db80 | 1900 | } |
kbhagat6 | 9:286d00d9db80 | 1901 | |
kbhagat6 | 9:286d00d9db80 | 1902 | /** |
kbhagat6 | 9:286d00d9db80 | 1903 | * @brief Gets if ambient light interrupts are enabled or not |
kbhagat6 | 9:286d00d9db80 | 1904 | * |
kbhagat6 | 9:286d00d9db80 | 1905 | * @return 1 if interrupts are enabled, 0 if not. 0xFF on error. |
kbhagat6 | 9:286d00d9db80 | 1906 | */ |
kbhagat6 | 9:286d00d9db80 | 1907 | uint8_t glibr::getAmbientLightIntEnable() |
kbhagat6 | 9:286d00d9db80 | 1908 | { |
kbhagat6 | 9:286d00d9db80 | 1909 | uint8_t val; |
kbhagat6 | 9:286d00d9db80 | 1910 | |
kbhagat6 | 9:286d00d9db80 | 1911 | /* Read value from ENABLE register */ |
kbhagat6 | 9:286d00d9db80 | 1912 | val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ENABLE); |
kbhagat6 | 9:286d00d9db80 | 1913 | if( val == ERROR){//!wireReadDataByte(APDS9960_ENABLE, val) ) { |
kbhagat6 | 9:286d00d9db80 | 1914 | return ERROR; |
kbhagat6 | 9:286d00d9db80 | 1915 | } |
kbhagat6 | 9:286d00d9db80 | 1916 | |
kbhagat6 | 9:286d00d9db80 | 1917 | /* Shift and mask out AIEN bit */ |
kbhagat6 | 9:286d00d9db80 | 1918 | val = (val >> 4) & 0x01;//0b00000001; |
kbhagat6 | 9:286d00d9db80 | 1919 | |
kbhagat6 | 9:286d00d9db80 | 1920 | return val; |
kbhagat6 | 9:286d00d9db80 | 1921 | } |
kbhagat6 | 9:286d00d9db80 | 1922 | |
kbhagat6 | 9:286d00d9db80 | 1923 | /** |
kbhagat6 | 9:286d00d9db80 | 1924 | * @brief Turns ambient light interrupts on or off |
kbhagat6 | 9:286d00d9db80 | 1925 | * |
kbhagat6 | 9:286d00d9db80 | 1926 | * @param[in] enable 1 to enable interrupts, 0 to turn them off |
kbhagat6 | 9:286d00d9db80 | 1927 | * @return True if operation successful. False otherwise. |
kbhagat6 | 9:286d00d9db80 | 1928 | */ |
kbhagat6 | 9:286d00d9db80 | 1929 | bool glibr::setAmbientLightIntEnable(uint8_t enable) |
kbhagat6 | 9:286d00d9db80 | 1930 | { |
kbhagat6 | 9:286d00d9db80 | 1931 | uint8_t val; |
kbhagat6 | 9:286d00d9db80 | 1932 | |
kbhagat6 | 9:286d00d9db80 | 1933 | /* Read value from ENABLE register */ |
kbhagat6 | 9:286d00d9db80 | 1934 | val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ENABLE); |
kbhagat6 | 9:286d00d9db80 | 1935 | if( val == ERROR){//!wireReadDataByte(APDS9960_ENABLE, val) ) { |
kbhagat6 | 9:286d00d9db80 | 1936 | return false; |
kbhagat6 | 9:286d00d9db80 | 1937 | } |
kbhagat6 | 9:286d00d9db80 | 1938 | |
kbhagat6 | 9:286d00d9db80 | 1939 | /* Set bits in register to given value */ |
kbhagat6 | 9:286d00d9db80 | 1940 | enable &= 0x01;//0b00000001; |
kbhagat6 | 9:286d00d9db80 | 1941 | enable = enable << 4; |
kbhagat6 | 9:286d00d9db80 | 1942 | val &= 0xEF;//0b11101111; |
kbhagat6 | 9:286d00d9db80 | 1943 | val |= enable; |
kbhagat6 | 9:286d00d9db80 | 1944 | |
kbhagat6 | 9:286d00d9db80 | 1945 | /* Write register value back into ENABLE register */ |
kbhagat6 | 9:286d00d9db80 | 1946 | if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_ENABLE,val)){//!wireWriteDataByte(APDS9960_ENABLE, val) ) { |
kbhagat6 | 9:286d00d9db80 | 1947 | return false; |
kbhagat6 | 9:286d00d9db80 | 1948 | } |
kbhagat6 | 9:286d00d9db80 | 1949 | |
kbhagat6 | 9:286d00d9db80 | 1950 | return true; |
kbhagat6 | 9:286d00d9db80 | 1951 | } |
kbhagat6 | 9:286d00d9db80 | 1952 | |
kbhagat6 | 9:286d00d9db80 | 1953 | /** |
kbhagat6 | 9:286d00d9db80 | 1954 | * @brief Gets if proximity interrupts are enabled or not |
kbhagat6 | 9:286d00d9db80 | 1955 | * |
kbhagat6 | 9:286d00d9db80 | 1956 | * @return 1 if interrupts are enabled, 0 if not. 0xFF on error. |
kbhagat6 | 9:286d00d9db80 | 1957 | */ |
kbhagat6 | 9:286d00d9db80 | 1958 | uint8_t glibr::getProximityIntEnable() |
kbhagat6 | 9:286d00d9db80 | 1959 | { |
kbhagat6 | 9:286d00d9db80 | 1960 | uint8_t val; |
kbhagat6 | 9:286d00d9db80 | 1961 | |
kbhagat6 | 9:286d00d9db80 | 1962 | /* Read value from ENABLE register */ |
kbhagat6 | 9:286d00d9db80 | 1963 | val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ENABLE); |
kbhagat6 | 9:286d00d9db80 | 1964 | if( val == ERROR){//!wireReadDataByte(APDS9960_ENABLE, val) ) { |
kbhagat6 | 9:286d00d9db80 | 1965 | return ERROR; |
kbhagat6 | 9:286d00d9db80 | 1966 | } |
kbhagat6 | 9:286d00d9db80 | 1967 | |
kbhagat6 | 9:286d00d9db80 | 1968 | /* Shift and mask out PIEN bit */ |
kbhagat6 | 9:286d00d9db80 | 1969 | val = (val >> 5) & 0x01;//0b00000001; |
kbhagat6 | 9:286d00d9db80 | 1970 | |
kbhagat6 | 9:286d00d9db80 | 1971 | return val; |
kbhagat6 | 9:286d00d9db80 | 1972 | } |
kbhagat6 | 9:286d00d9db80 | 1973 | |
kbhagat6 | 9:286d00d9db80 | 1974 | /** |
kbhagat6 | 9:286d00d9db80 | 1975 | * @brief Turns proximity interrupts on or off |
kbhagat6 | 9:286d00d9db80 | 1976 | * |
kbhagat6 | 9:286d00d9db80 | 1977 | * @param[in] enable 1 to enable interrupts, 0 to turn them off |
kbhagat6 | 9:286d00d9db80 | 1978 | * @return True if operation successful. False otherwise. |
kbhagat6 | 9:286d00d9db80 | 1979 | */ |
kbhagat6 | 9:286d00d9db80 | 1980 | bool glibr::setProximityIntEnable(uint8_t enable) |
kbhagat6 | 9:286d00d9db80 | 1981 | { |
kbhagat6 | 9:286d00d9db80 | 1982 | uint8_t val; |
kbhagat6 | 9:286d00d9db80 | 1983 | |
kbhagat6 | 9:286d00d9db80 | 1984 | /* Read value from ENABLE register */ |
kbhagat6 | 9:286d00d9db80 | 1985 | val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ENABLE); |
kbhagat6 | 9:286d00d9db80 | 1986 | if( val == ERROR){//!wireReadDataByte(APDS9960_ENABLE, val) ) { |
kbhagat6 | 9:286d00d9db80 | 1987 | return false; |
kbhagat6 | 9:286d00d9db80 | 1988 | } |
kbhagat6 | 9:286d00d9db80 | 1989 | |
kbhagat6 | 9:286d00d9db80 | 1990 | /* Set bits in register to given value */ |
kbhagat6 | 9:286d00d9db80 | 1991 | enable &= 0x01;//0b00000001; |
kbhagat6 | 9:286d00d9db80 | 1992 | enable = enable << 5; |
kbhagat6 | 9:286d00d9db80 | 1993 | val &= 0xDF;//0b11011111; |
kbhagat6 | 9:286d00d9db80 | 1994 | val |= enable; |
kbhagat6 | 9:286d00d9db80 | 1995 | |
kbhagat6 | 9:286d00d9db80 | 1996 | /* Write register value back into ENABLE register */ |
kbhagat6 | 9:286d00d9db80 | 1997 | if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_ENABLE,val)){//!wireWriteDataByte(APDS9960_ENABLE, val) ) { |
kbhagat6 | 9:286d00d9db80 | 1998 | return false; |
kbhagat6 | 9:286d00d9db80 | 1999 | } |
kbhagat6 | 9:286d00d9db80 | 2000 | |
kbhagat6 | 9:286d00d9db80 | 2001 | return true; |
kbhagat6 | 9:286d00d9db80 | 2002 | } |
kbhagat6 | 9:286d00d9db80 | 2003 | |
kbhagat6 | 9:286d00d9db80 | 2004 | /** |
kbhagat6 | 9:286d00d9db80 | 2005 | * @brief Gets if gesture interrupts are enabled or not |
kbhagat6 | 9:286d00d9db80 | 2006 | * |
kbhagat6 | 9:286d00d9db80 | 2007 | * @return 1 if interrupts are enabled, 0 if not. 0xFF on error. |
kbhagat6 | 9:286d00d9db80 | 2008 | */ |
kbhagat6 | 9:286d00d9db80 | 2009 | uint8_t glibr::getGestureIntEnable() |
kbhagat6 | 9:286d00d9db80 | 2010 | { |
kbhagat6 | 9:286d00d9db80 | 2011 | uint8_t val; |
kbhagat6 | 9:286d00d9db80 | 2012 | |
kbhagat6 | 9:286d00d9db80 | 2013 | /* Read value from GCONF4 register */ |
kbhagat6 | 9:286d00d9db80 | 2014 | val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF4); |
kbhagat6 | 9:286d00d9db80 | 2015 | if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF4, val) ) { |
kbhagat6 | 9:286d00d9db80 | 2016 | return ERROR; |
kbhagat6 | 9:286d00d9db80 | 2017 | } |
kbhagat6 | 9:286d00d9db80 | 2018 | |
kbhagat6 | 9:286d00d9db80 | 2019 | /* Shift and mask out GIEN bit */ |
kbhagat6 | 9:286d00d9db80 | 2020 | val = (val >> 1) & 0x01;//0b00000001; |
kbhagat6 | 9:286d00d9db80 | 2021 | |
kbhagat6 | 9:286d00d9db80 | 2022 | return val; |
kbhagat6 | 9:286d00d9db80 | 2023 | } |
kbhagat6 | 9:286d00d9db80 | 2024 | |
kbhagat6 | 9:286d00d9db80 | 2025 | /** |
kbhagat6 | 9:286d00d9db80 | 2026 | * @brief Turns gesture-related interrupts on or off |
kbhagat6 | 9:286d00d9db80 | 2027 | * |
kbhagat6 | 9:286d00d9db80 | 2028 | * @param[in] enable 1 to enable interrupts, 0 to turn them off |
kbhagat6 | 9:286d00d9db80 | 2029 | * @return True if operation successful. False otherwise. |
kbhagat6 | 9:286d00d9db80 | 2030 | */ |
kbhagat6 | 9:286d00d9db80 | 2031 | bool glibr::setGestureIntEnable(uint8_t enable) |
kbhagat6 | 9:286d00d9db80 | 2032 | { |
kbhagat6 | 9:286d00d9db80 | 2033 | uint8_t val; |
kbhagat6 | 9:286d00d9db80 | 2034 | |
kbhagat6 | 9:286d00d9db80 | 2035 | /* Read value from GCONF4 register */ |
kbhagat6 | 9:286d00d9db80 | 2036 | val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF4); |
kbhagat6 | 9:286d00d9db80 | 2037 | if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF4, val) ) { |
kbhagat6 | 9:286d00d9db80 | 2038 | return false; |
kbhagat6 | 9:286d00d9db80 | 2039 | } |
kbhagat6 | 9:286d00d9db80 | 2040 | |
kbhagat6 | 9:286d00d9db80 | 2041 | /* Set bits in register to given value */ |
kbhagat6 | 9:286d00d9db80 | 2042 | enable &= 0x01;//0b00000001; |
kbhagat6 | 9:286d00d9db80 | 2043 | enable = enable << 1; |
kbhagat6 | 9:286d00d9db80 | 2044 | val &= 0xFD;//0b11111101; |
kbhagat6 | 9:286d00d9db80 | 2045 | val |= enable; |
kbhagat6 | 9:286d00d9db80 | 2046 | |
kbhagat6 | 9:286d00d9db80 | 2047 | /* Write register value back into GCONF4 register */ |
kbhagat6 | 9:286d00d9db80 | 2048 | if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GCONF4,val)){//!wireWriteDataByte(APDS9960_GCONF4, val) ) { |
kbhagat6 | 9:286d00d9db80 | 2049 | return false; |
kbhagat6 | 9:286d00d9db80 | 2050 | } |
kbhagat6 | 9:286d00d9db80 | 2051 | |
kbhagat6 | 9:286d00d9db80 | 2052 | return true; |
kbhagat6 | 9:286d00d9db80 | 2053 | } |
kbhagat6 | 9:286d00d9db80 | 2054 | |
kbhagat6 | 9:286d00d9db80 | 2055 | /** |
kbhagat6 | 9:286d00d9db80 | 2056 | * @brief Clears the ambient light interrupt |
kbhagat6 | 9:286d00d9db80 | 2057 | * |
kbhagat6 | 9:286d00d9db80 | 2058 | * @return True if operation completed successfully. False otherwise. |
kbhagat6 | 9:286d00d9db80 | 2059 | */ |
kbhagat6 | 9:286d00d9db80 | 2060 | bool glibr::clearAmbientLightInt() |
kbhagat6 | 9:286d00d9db80 | 2061 | { |
kbhagat6 | 9:286d00d9db80 | 2062 | uint8_t throwaway; |
kbhagat6 | 9:286d00d9db80 | 2063 | throwaway = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_AICLEAR); |
kbhagat6 | 9:286d00d9db80 | 2064 | if( throwaway == ERROR){//!wireReadDataByte(APDS9960_AICLEAR, throwaway) ) { |
kbhagat6 | 9:286d00d9db80 | 2065 | return false; |
kbhagat6 | 9:286d00d9db80 | 2066 | } |
kbhagat6 | 9:286d00d9db80 | 2067 | |
kbhagat6 | 9:286d00d9db80 | 2068 | return true; |
kbhagat6 | 9:286d00d9db80 | 2069 | } |
kbhagat6 | 9:286d00d9db80 | 2070 | |
kbhagat6 | 9:286d00d9db80 | 2071 | /** |
kbhagat6 | 9:286d00d9db80 | 2072 | * @brief Clears the proximity interrupt |
kbhagat6 | 9:286d00d9db80 | 2073 | * |
kbhagat6 | 9:286d00d9db80 | 2074 | * @return True if operation completed successfully. False otherwise. |
kbhagat6 | 9:286d00d9db80 | 2075 | */ |
kbhagat6 | 9:286d00d9db80 | 2076 | bool glibr::clearProximityInt() |
kbhagat6 | 9:286d00d9db80 | 2077 | { |
kbhagat6 | 9:286d00d9db80 | 2078 | uint8_t throwaway; |
kbhagat6 | 9:286d00d9db80 | 2079 | throwaway = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_PICLEAR); |
kbhagat6 | 9:286d00d9db80 | 2080 | if( throwaway == ERROR){//!wireReadDataByte(APDS9960_PICLEAR, throwaway) ) { |
kbhagat6 | 9:286d00d9db80 | 2081 | return false; |
kbhagat6 | 9:286d00d9db80 | 2082 | } |
kbhagat6 | 9:286d00d9db80 | 2083 | |
kbhagat6 | 9:286d00d9db80 | 2084 | return true; |
kbhagat6 | 9:286d00d9db80 | 2085 | } |
kbhagat6 | 9:286d00d9db80 | 2086 | |
kbhagat6 | 9:286d00d9db80 | 2087 | /** |
kbhagat6 | 9:286d00d9db80 | 2088 | * @brief Tells if the gesture state machine is currently running |
kbhagat6 | 9:286d00d9db80 | 2089 | * |
kbhagat6 | 9:286d00d9db80 | 2090 | * @return 1 if gesture state machine is running, 0 if not. 0xFF on error. |
kbhagat6 | 9:286d00d9db80 | 2091 | */ |
kbhagat6 | 9:286d00d9db80 | 2092 | uint8_t glibr::getGestureMode() |
kbhagat6 | 9:286d00d9db80 | 2093 | { |
kbhagat6 | 9:286d00d9db80 | 2094 | uint8_t val; |
kbhagat6 | 9:286d00d9db80 | 2095 | |
kbhagat6 | 9:286d00d9db80 | 2096 | /* Read value from GCONF4 register */ |
kbhagat6 | 9:286d00d9db80 | 2097 | val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF4); |
kbhagat6 | 9:286d00d9db80 | 2098 | if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF4, val) ) { |
kbhagat6 | 9:286d00d9db80 | 2099 | return ERROR; |
kbhagat6 | 9:286d00d9db80 | 2100 | } |
kbhagat6 | 9:286d00d9db80 | 2101 | |
kbhagat6 | 9:286d00d9db80 | 2102 | /* Mask out GMODE bit */ |
kbhagat6 | 9:286d00d9db80 | 2103 | val &= 0x01;//0b00000001; |
kbhagat6 | 9:286d00d9db80 | 2104 | |
kbhagat6 | 9:286d00d9db80 | 2105 | return val; |
kbhagat6 | 9:286d00d9db80 | 2106 | } |
kbhagat6 | 9:286d00d9db80 | 2107 | |
kbhagat6 | 9:286d00d9db80 | 2108 | /** |
kbhagat6 | 9:286d00d9db80 | 2109 | * @brief Tells the state machine to either enter or exit gesture state machine |
kbhagat6 | 9:286d00d9db80 | 2110 | * |
kbhagat6 | 9:286d00d9db80 | 2111 | * @param[in] mode 1 to enter gesture state machine, 0 to exit. |
kbhagat6 | 9:286d00d9db80 | 2112 | * @return True if operation successful. False otherwise. |
kbhagat6 | 9:286d00d9db80 | 2113 | */ |
kbhagat6 | 9:286d00d9db80 | 2114 | bool glibr::setGestureMode(uint8_t mode) |
kbhagat6 | 9:286d00d9db80 | 2115 | { |
kbhagat6 | 9:286d00d9db80 | 2116 | uint8_t val; |
kbhagat6 | 9:286d00d9db80 | 2117 | |
kbhagat6 | 9:286d00d9db80 | 2118 | /* Read value from GCONF4 register */ |
kbhagat6 | 9:286d00d9db80 | 2119 | val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF4); |
kbhagat6 | 9:286d00d9db80 | 2120 | if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF4, val) ) { |
kbhagat6 | 9:286d00d9db80 | 2121 | return false; |
kbhagat6 | 9:286d00d9db80 | 2122 | } |
kbhagat6 | 9:286d00d9db80 | 2123 | |
kbhagat6 | 9:286d00d9db80 | 2124 | /* Set bits in register to given value */ |
kbhagat6 | 9:286d00d9db80 | 2125 | mode &= 0x01;//0b00000001; |
kbhagat6 | 9:286d00d9db80 | 2126 | val &= 0xFE;//0b11111110; |
kbhagat6 | 9:286d00d9db80 | 2127 | val |= mode; |
kbhagat6 | 9:286d00d9db80 | 2128 | |
kbhagat6 | 9:286d00d9db80 | 2129 | /* Write register value back into GCONF4 register */ |
kbhagat6 | 9:286d00d9db80 | 2130 | if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GCONF4,val)){//!wireWriteDataByte(APDS9960_GCONF4, val) ) { |
kbhagat6 | 1:c7215f5f9a72 | 2131 | return false; |
kbhagat6 | 1:c7215f5f9a72 | 2132 | } |
kbhagat6 | 1:c7215f5f9a72 | 2133 | |
kbhagat6 | 1:c7215f5f9a72 | 2134 | return true; |
kbhagat6 | 1:c7215f5f9a72 | 2135 | } |
kbhagat6 | 1:c7215f5f9a72 | 2136 | |
kbhagat6 | 5:3eb4f3091bd8 | 2137 | |
kbhagat6 | 5:3eb4f3091bd8 | 2138 | |
kbhagat6 | 5:3eb4f3091bd8 | 2139 | |
kbhagat6 | 5:3eb4f3091bd8 | 2140 | |
kbhagat6 | 5:3eb4f3091bd8 | 2141 | int glibr::I2CwriteByte(char address, char subAddress, char data) |
kbhagat6 | 5:3eb4f3091bd8 | 2142 | { |
kbhagat6 | 5:3eb4f3091bd8 | 2143 | int ret; |
kbhagat6 | 5:3eb4f3091bd8 | 2144 | char cmd[2] = {subAddress, data}; |
kbhagat6 | 5:3eb4f3091bd8 | 2145 | ret=i2c.write(address<<1, cmd, 2); //if ret is 1, then not acked. |
kbhagat6 | 5:3eb4f3091bd8 | 2146 | return ret; |
kbhagat6 | 5:3eb4f3091bd8 | 2147 | } |
kbhagat6 | 5:3eb4f3091bd8 | 2148 | |
kbhagat6 | 5:3eb4f3091bd8 | 2149 | |
kbhagat6 | 5:3eb4f3091bd8 | 2150 | |
kbhagat6 | 0:437ae08befe3 | 2151 | uint8_t glibr::I2CreadByte(char address, char subAddress) |
kbhagat6 | 0:437ae08befe3 | 2152 | { |
kbhagat6 | 0:437ae08befe3 | 2153 | char data; // store the register data |
kbhagat6 | 0:437ae08befe3 | 2154 | |
kbhagat6 | 0:437ae08befe3 | 2155 | if(i2c.write(address<<1, &subAddress, 1, true)){ |
kbhagat6 | 0:437ae08befe3 | 2156 | return ERROR; //7 bit //not acked |
kbhagat6 | 0:437ae08befe3 | 2157 | } |
kbhagat6 | 0:437ae08befe3 | 2158 | i2c.read(address<<1, &data, 1); |
kbhagat6 | 0:437ae08befe3 | 2159 | return data; |
kbhagat6 | 0:437ae08befe3 | 2160 | |
kbhagat6 | 0:437ae08befe3 | 2161 | } |
kbhagat6 | 5:3eb4f3091bd8 | 2162 | |
kbhagat6 | 9:286d00d9db80 | 2163 | |
kbhagat6 | 9:286d00d9db80 | 2164 | // * @brief Reads a block (array) of bytes from the I2C device and register |
kbhagat6 | 9:286d00d9db80 | 2165 | // * |
kbhagat6 | 9:286d00d9db80 | 2166 | // * @param[in] reg the register to read from |
kbhagat6 | 9:286d00d9db80 | 2167 | // * @param[out] val pointer to the beginning of the data |
kbhagat6 | 9:286d00d9db80 | 2168 | // * @param[in] len number of bytes to read |
kbhagat6 | 9:286d00d9db80 | 2169 | // * @return Number of bytes read. -1 on read error. |
kbhagat6 | 9:286d00d9db80 | 2170 | // */ |
kbhagat6 | 9:286d00d9db80 | 2171 | int glibr::I2CReadDataBlock(char address, char subAddress, char *data, unsigned int len) |
kbhagat6 | 5:3eb4f3091bd8 | 2172 | { |
kbhagat6 | 5:3eb4f3091bd8 | 2173 | // unsigned char i = 0; |
kbhagat6 | 8:6fa15d4e31fb | 2174 | |
kbhagat6 | 5:3eb4f3091bd8 | 2175 | /* Indicate which register we want to read from */ |
kbhagat6 | 5:3eb4f3091bd8 | 2176 | |
kbhagat6 | 9:286d00d9db80 | 2177 | if(i2c.write(address<<1, &subAddress, 1, true)){ |
kbhagat6 | 5:3eb4f3091bd8 | 2178 | return -1; //7 bit //not acked |
kbhagat6 | 8:6fa15d4e31fb | 2179 | } |
kbhagat6 | 5:3eb4f3091bd8 | 2180 | |
kbhagat6 | 5:3eb4f3091bd8 | 2181 | /* Read block data */ |
kbhagat6 | 8:6fa15d4e31fb | 2182 | |
kbhagat6 | 9:286d00d9db80 | 2183 | if(i2c.read(address<<1, data, len)){ |
kbhagat6 | 8:6fa15d4e31fb | 2184 | return -1; |
kbhagat6 | 8:6fa15d4e31fb | 2185 | } |
kbhagat6 | 5:3eb4f3091bd8 | 2186 | |
kbhagat6 | 8:6fa15d4e31fb | 2187 | return 1; |
kbhagat6 | 5:3eb4f3091bd8 | 2188 | //Wire.requestFrom(APDS9960_I2C_ADDR, len); |
kbhagat6 | 5:3eb4f3091bd8 | 2189 | /*while (Wire.available()) { |
kbhagat6 | 5:3eb4f3091bd8 | 2190 | if (i >= len) { |
kbhagat6 | 5:3eb4f3091bd8 | 2191 | return -1; |
kbhagat6 | 5:3eb4f3091bd8 | 2192 | } |
kbhagat6 | 5:3eb4f3091bd8 | 2193 | val[i] = Wire.read(); |
kbhagat6 | 5:3eb4f3091bd8 | 2194 | i++; |
kbhagat6 | 5:3eb4f3091bd8 | 2195 | }*/ |
kbhagat6 | 5:3eb4f3091bd8 | 2196 | } |