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

Dependencies:   mbed SDFileSystem wave_player

Committer:
kbhagat6
Date:
Thu Mar 05 22:42:17 2015 +0000
Revision:
4:84545b0e63a9
Parent:
3:26146a08bb22
Child:
5:3eb4f3091bd8
more functions added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kbhagat6 0:437ae08befe3 1 #include "glibr.h"
kbhagat6 0:437ae08befe3 2 #include "mbed.h"
kbhagat6 0:437ae08befe3 3
kbhagat6 0:437ae08befe3 4
kbhagat6 0:437ae08befe3 5
kbhagat6 0:437ae08befe3 6
kbhagat6 0:437ae08befe3 7 glibr::glibr(PinName sda, PinName scl):i2c(sda, scl){
kbhagat6 0:437ae08befe3 8
kbhagat6 0:437ae08befe3 9 }
kbhagat6 0:437ae08befe3 10
kbhagat6 0:437ae08befe3 11 glibr::~glibr(){
kbhagat6 0:437ae08befe3 12
kbhagat6 0:437ae08befe3 13 }
kbhagat6 0:437ae08befe3 14
kbhagat6 0:437ae08befe3 15 uint8_t glibr::ginit(){
kbhagat6 0:437ae08befe3 16 uint8_t id;
kbhagat6 0:437ae08befe3 17
kbhagat6 0:437ae08befe3 18 // if( !wireReadDataByte(APDS9960_ID, id) ) {
kbhagat6 0:437ae08befe3 19
kbhagat6 0:437ae08befe3 20 id=I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ID);
kbhagat6 0:437ae08befe3 21
kbhagat6 0:437ae08befe3 22 if( (!(id == APDS9960_ID_1 || id == APDS9960_ID_2))||id==ERROR) {
kbhagat6 0:437ae08befe3 23 return false;
kbhagat6 0:437ae08befe3 24 }
kbhagat6 0:437ae08befe3 25
kbhagat6 0:437ae08befe3 26 if(!setMode(ALL, OFF)) {
kbhagat6 0:437ae08befe3 27 return false;
kbhagat6 0:437ae08befe3 28 }
kbhagat6 0:437ae08befe3 29 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_ATIME, DEFAULT_ATIME)){
kbhagat6 0:437ae08befe3 30 return false;
kbhagat6 0:437ae08befe3 31 }
kbhagat6 0:437ae08befe3 32 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_WTIME, DEFAULT_WTIME)){
kbhagat6 0:437ae08befe3 33 return false;
kbhagat6 0:437ae08befe3 34 }
kbhagat6 0:437ae08befe3 35 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PPULSE, DEFAULT_PROX_PPULSE)){
kbhagat6 0:437ae08befe3 36 return false;
kbhagat6 0:437ae08befe3 37 }
kbhagat6 0:437ae08befe3 38 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_POFFSET_UR, DEFAULT_POFFSET_UR)){
kbhagat6 0:437ae08befe3 39 return false;
kbhagat6 0:437ae08befe3 40 }
kbhagat6 0:437ae08befe3 41 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_POFFSET_DL, DEFAULT_POFFSET_DL)){
kbhagat6 0:437ae08befe3 42 return false;
kbhagat6 0:437ae08befe3 43 }
kbhagat6 0:437ae08befe3 44 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONFIG1, DEFAULT_CONFIG1)){
kbhagat6 0:437ae08befe3 45 return false;
kbhagat6 0:437ae08befe3 46 }
kbhagat6 0:437ae08befe3 47
kbhagat6 0:437ae08befe3 48 if( !setLEDDrive(DEFAULT_LDRIVE) ) {
kbhagat6 0:437ae08befe3 49 return false;
kbhagat6 0:437ae08befe3 50 }
kbhagat6 0:437ae08befe3 51
kbhagat6 0:437ae08befe3 52 if( !setProximityGain(DEFAULT_PGAIN) ) {
kbhagat6 0:437ae08befe3 53 return false;
kbhagat6 0:437ae08befe3 54 }
kbhagat6 0:437ae08befe3 55 if( !setAmbientLightGain(DEFAULT_AGAIN) ) {
kbhagat6 0:437ae08befe3 56 return false;
kbhagat6 0:437ae08befe3 57 }
kbhagat6 0:437ae08befe3 58 if( !setProxIntLowThresh(DEFAULT_PILT) ) {
kbhagat6 0:437ae08befe3 59 return false;
kbhagat6 0:437ae08befe3 60 }
kbhagat6 1:c7215f5f9a72 61 if( !setProxIntHighThresh(DEFAULT_PIHT) ) {
kbhagat6 1:c7215f5f9a72 62 return false;
kbhagat6 1:c7215f5f9a72 63 }
kbhagat6 1:c7215f5f9a72 64 if( !setLightIntLowThreshold(DEFAULT_AILT) ) {
kbhagat6 1:c7215f5f9a72 65 return false;
kbhagat6 1:c7215f5f9a72 66 }
kbhagat6 1:c7215f5f9a72 67 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONFIG2, DEFAULT_CONFIG2) ) {
kbhagat6 1:c7215f5f9a72 68 return false;
kbhagat6 1:c7215f5f9a72 69 }
kbhagat6 1:c7215f5f9a72 70 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONFIG3, DEFAULT_CONFIG3) ) {
kbhagat6 1:c7215f5f9a72 71 return false;
kbhagat6 1:c7215f5f9a72 72 }
kbhagat6 1:c7215f5f9a72 73
kbhagat6 1:c7215f5f9a72 74 if( !setGestureEnterThresh(DEFAULT_GPENTH) ) {
kbhagat6 1:c7215f5f9a72 75 return false;
kbhagat6 1:c7215f5f9a72 76 }
kbhagat6 1:c7215f5f9a72 77 if( !setGestureExitThresh(DEFAULT_GEXTH) ) {
kbhagat6 1:c7215f5f9a72 78 return false;
kbhagat6 1:c7215f5f9a72 79 }
kbhagat6 1:c7215f5f9a72 80 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GCONF1, DEFAULT_GCONF1) ) {
kbhagat6 1:c7215f5f9a72 81 return false;
kbhagat6 1:c7215f5f9a72 82 }
kbhagat6 1:c7215f5f9a72 83 if( !setGestureGain(DEFAULT_GGAIN) ) {
kbhagat6 1:c7215f5f9a72 84 return false;
kbhagat6 1:c7215f5f9a72 85 }
kbhagat6 1:c7215f5f9a72 86 if( !setGestureLEDDrive(DEFAULT_GLDRIVE) ) {
kbhagat6 1:c7215f5f9a72 87 return false;
kbhagat6 1:c7215f5f9a72 88 }
kbhagat6 1:c7215f5f9a72 89 if( !setGestureWaitTime(DEFAULT_GWTIME) ) {
kbhagat6 1:c7215f5f9a72 90 return false;
kbhagat6 1:c7215f5f9a72 91 }
kbhagat6 1:c7215f5f9a72 92 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GOFFSET_U, DEFAULT_GOFFSET) ) {
kbhagat6 1:c7215f5f9a72 93 return false;
kbhagat6 1:c7215f5f9a72 94 }
kbhagat6 1:c7215f5f9a72 95 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GOFFSET_D, DEFAULT_GOFFSET) ) {
kbhagat6 1:c7215f5f9a72 96 return false;
kbhagat6 1:c7215f5f9a72 97 }
kbhagat6 1:c7215f5f9a72 98 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GOFFSET_L, DEFAULT_GOFFSET) ) {
kbhagat6 1:c7215f5f9a72 99 return false;
kbhagat6 1:c7215f5f9a72 100 }
kbhagat6 1:c7215f5f9a72 101 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GOFFSET_R, DEFAULT_GOFFSET) ) {
kbhagat6 1:c7215f5f9a72 102 return false;
kbhagat6 1:c7215f5f9a72 103 }
kbhagat6 1:c7215f5f9a72 104 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GPULSE, DEFAULT_GPULSE) ) {
kbhagat6 1:c7215f5f9a72 105 return false;
kbhagat6 1:c7215f5f9a72 106 }
kbhagat6 1:c7215f5f9a72 107 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GCONF3, DEFAULT_GCONF3) ) {
kbhagat6 1:c7215f5f9a72 108 return false;
kbhagat6 1:c7215f5f9a72 109 }
kbhagat6 1:c7215f5f9a72 110 if( !setGestureIntEnable(DEFAULT_GIEN) ) {
kbhagat6 1:c7215f5f9a72 111 return false;
kbhagat6 1:c7215f5f9a72 112 }
kbhagat6 0:437ae08befe3 113
kbhagat6 0:437ae08befe3 114
kbhagat6 0:437ae08befe3 115
kbhagat6 0:437ae08befe3 116 return id;
kbhagat6 0:437ae08befe3 117 }
kbhagat6 0:437ae08befe3 118
kbhagat6 0:437ae08befe3 119
kbhagat6 0:437ae08befe3 120
kbhagat6 0:437ae08befe3 121
kbhagat6 0:437ae08befe3 122 /**
kbhagat6 0:437ae08befe3 123 * @brief Enables or disables a feature in the APDS-9960
kbhagat6 0:437ae08befe3 124 *
kbhagat6 0:437ae08befe3 125 * @param[in] mode which feature to enable
kbhagat6 0:437ae08befe3 126 * @param[in] enable ON (1) or OFF (0)
kbhagat6 0:437ae08befe3 127 * @return True if operation success. False otherwise.
kbhagat6 0:437ae08befe3 128 */
kbhagat6 0:437ae08befe3 129 bool glibr::setMode(uint8_t mode, uint8_t enable)
kbhagat6 0:437ae08befe3 130 {
kbhagat6 0:437ae08befe3 131 uint8_t reg_val;
kbhagat6 0:437ae08befe3 132
kbhagat6 0:437ae08befe3 133 /* Read current ENABLE register */
kbhagat6 0:437ae08befe3 134 reg_val = getMode();
kbhagat6 0:437ae08befe3 135 if( reg_val == ERROR ) {
kbhagat6 0:437ae08befe3 136 return false;
kbhagat6 0:437ae08befe3 137 }
kbhagat6 0:437ae08befe3 138
kbhagat6 0:437ae08befe3 139 /* Change bit(s) in ENABLE register */
kbhagat6 0:437ae08befe3 140 enable = enable & 0x01;
kbhagat6 0:437ae08befe3 141 if( mode >= 0 && mode <= 6 ) {
kbhagat6 0:437ae08befe3 142 if (enable) {
kbhagat6 0:437ae08befe3 143 reg_val |= (1 << mode);
kbhagat6 0:437ae08befe3 144 } else {
kbhagat6 0:437ae08befe3 145 reg_val &= ~(1 << mode);
kbhagat6 0:437ae08befe3 146 }
kbhagat6 0:437ae08befe3 147 } else if( mode == ALL ) {
kbhagat6 0:437ae08befe3 148 if (enable) {
kbhagat6 0:437ae08befe3 149 reg_val = 0x7F;
kbhagat6 0:437ae08befe3 150 } else {
kbhagat6 0:437ae08befe3 151 reg_val = 0x00;
kbhagat6 0:437ae08befe3 152 }
kbhagat6 0:437ae08befe3 153 }
kbhagat6 0:437ae08befe3 154
kbhagat6 0:437ae08befe3 155 /* Write value back to ENABLE register */
kbhagat6 0:437ae08befe3 156 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_ENABLE, reg_val)){
kbhagat6 0:437ae08befe3 157 return false;
kbhagat6 0:437ae08befe3 158 }
kbhagat6 0:437ae08befe3 159
kbhagat6 0:437ae08befe3 160
kbhagat6 0:437ae08befe3 161
kbhagat6 0:437ae08befe3 162 return true;
kbhagat6 0:437ae08befe3 163 }
kbhagat6 0:437ae08befe3 164
kbhagat6 0:437ae08befe3 165 uint8_t glibr::getMode()
kbhagat6 0:437ae08befe3 166 {
kbhagat6 0:437ae08befe3 167 return I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ENABLE);
kbhagat6 0:437ae08befe3 168 }
kbhagat6 0:437ae08befe3 169
kbhagat6 0:437ae08befe3 170 bool glibr::setLEDDrive(uint8_t drive)
kbhagat6 0:437ae08befe3 171 {
kbhagat6 0:437ae08befe3 172 uint8_t val;
kbhagat6 0:437ae08befe3 173
kbhagat6 0:437ae08befe3 174 /* Read value from CONTROL register */
kbhagat6 0:437ae08befe3 175
kbhagat6 0:437ae08befe3 176 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL);
kbhagat6 0:437ae08befe3 177 if(val==ERROR){
kbhagat6 0:437ae08befe3 178 return false;
kbhagat6 0:437ae08befe3 179 }
kbhagat6 0:437ae08befe3 180 /* Set bits in register to given value */
kbhagat6 0:437ae08befe3 181 drive &= 0x03;
kbhagat6 0:437ae08befe3 182 drive = drive << 6;
kbhagat6 0:437ae08befe3 183 //val &= 0b00111111;
kbhagat6 0:437ae08befe3 184 val &= 0x3F;
kbhagat6 0:437ae08befe3 185 val |= drive;
kbhagat6 0:437ae08befe3 186
kbhagat6 0:437ae08befe3 187 /* Write register value back into CONTROL register */
kbhagat6 0:437ae08befe3 188 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONTROL, val) ) {
kbhagat6 0:437ae08befe3 189 return false;
kbhagat6 0:437ae08befe3 190 }
kbhagat6 0:437ae08befe3 191
kbhagat6 0:437ae08befe3 192 return true;
kbhagat6 0:437ae08befe3 193 }
kbhagat6 0:437ae08befe3 194
kbhagat6 3:26146a08bb22 195 bool glibr::enableLightSensor(bool interrupts)
kbhagat6 3:26146a08bb22 196 {
kbhagat6 3:26146a08bb22 197
kbhagat6 3:26146a08bb22 198 /* Set default gain, interrupts, enable power, and enable sensor */
kbhagat6 3:26146a08bb22 199 if( !setAmbientLightGain(DEFAULT_AGAIN) ) {
kbhagat6 3:26146a08bb22 200 return false;
kbhagat6 3:26146a08bb22 201 }
kbhagat6 3:26146a08bb22 202 if( interrupts ) {
kbhagat6 3:26146a08bb22 203 if( !setAmbientLightIntEnable(1) ) {
kbhagat6 3:26146a08bb22 204 return false;
kbhagat6 3:26146a08bb22 205 }
kbhagat6 3:26146a08bb22 206 } else {
kbhagat6 3:26146a08bb22 207 if( !setAmbientLightIntEnable(0) ) {
kbhagat6 3:26146a08bb22 208 return false;
kbhagat6 3:26146a08bb22 209 }
kbhagat6 3:26146a08bb22 210 }
kbhagat6 3:26146a08bb22 211 if( !enablePower() ){
kbhagat6 3:26146a08bb22 212 return false;
kbhagat6 3:26146a08bb22 213 }
kbhagat6 3:26146a08bb22 214 if( !setMode(AMBIENT_LIGHT, 1) ) {
kbhagat6 3:26146a08bb22 215 return false;
kbhagat6 3:26146a08bb22 216 }
kbhagat6 3:26146a08bb22 217
kbhagat6 3:26146a08bb22 218 return true;
kbhagat6 3:26146a08bb22 219
kbhagat6 3:26146a08bb22 220 }
kbhagat6 3:26146a08bb22 221
kbhagat6 3:26146a08bb22 222 /**
kbhagat6 3:26146a08bb22 223 * @brief Ends the light sensor on the APDS-9960
kbhagat6 3:26146a08bb22 224 *
kbhagat6 3:26146a08bb22 225 * @return True if sensor disabled correctly. False on error.
kbhagat6 3:26146a08bb22 226 */
kbhagat6 3:26146a08bb22 227 bool glibr::disableLightSensor()
kbhagat6 3:26146a08bb22 228 {
kbhagat6 3:26146a08bb22 229 if( !setAmbientLightIntEnable(0) ) {
kbhagat6 3:26146a08bb22 230 return false;
kbhagat6 3:26146a08bb22 231 }
kbhagat6 3:26146a08bb22 232 if( !setMode(AMBIENT_LIGHT, 0) ) {
kbhagat6 3:26146a08bb22 233 return false;
kbhagat6 3:26146a08bb22 234 }
kbhagat6 3:26146a08bb22 235
kbhagat6 3:26146a08bb22 236 return true;
kbhagat6 3:26146a08bb22 237 }
kbhagat6 3:26146a08bb22 238
kbhagat6 3:26146a08bb22 239 /**
kbhagat6 3:26146a08bb22 240 * @brief Starts the proximity sensor on the APDS-9960
kbhagat6 3:26146a08bb22 241 *
kbhagat6 3:26146a08bb22 242 * @param[in] interrupts true to enable hardware external interrupt on proximity
kbhagat6 3:26146a08bb22 243 * @return True if sensor enabled correctly. False on error.
kbhagat6 3:26146a08bb22 244 */
kbhagat6 3:26146a08bb22 245 bool glibr::enableProximitySensor(bool interrupts)
kbhagat6 3:26146a08bb22 246 {
kbhagat6 3:26146a08bb22 247 /* Set default gain, LED, interrupts, enable power, and enable sensor */
kbhagat6 3:26146a08bb22 248 if( !setProximityGain(DEFAULT_PGAIN) ) {
kbhagat6 3:26146a08bb22 249 return false;
kbhagat6 3:26146a08bb22 250 }
kbhagat6 3:26146a08bb22 251 if( !setLEDDrive(DEFAULT_LDRIVE) ) {
kbhagat6 3:26146a08bb22 252 return false;
kbhagat6 3:26146a08bb22 253 }
kbhagat6 3:26146a08bb22 254 if( interrupts ) {
kbhagat6 3:26146a08bb22 255 if( !setProximityIntEnable(1) ) {
kbhagat6 3:26146a08bb22 256 return false;
kbhagat6 3:26146a08bb22 257 }
kbhagat6 3:26146a08bb22 258 } else {
kbhagat6 3:26146a08bb22 259 if( !setProximityIntEnable(0) ) {
kbhagat6 3:26146a08bb22 260 return false;
kbhagat6 3:26146a08bb22 261 }
kbhagat6 3:26146a08bb22 262 }
kbhagat6 3:26146a08bb22 263 if( !enablePower() ){
kbhagat6 3:26146a08bb22 264 return false;
kbhagat6 3:26146a08bb22 265 }
kbhagat6 3:26146a08bb22 266 if( !setMode(PROXIMITY, 1) ) {
kbhagat6 3:26146a08bb22 267 return false;
kbhagat6 3:26146a08bb22 268 }
kbhagat6 3:26146a08bb22 269
kbhagat6 3:26146a08bb22 270 return true;
kbhagat6 3:26146a08bb22 271 }
kbhagat6 3:26146a08bb22 272
kbhagat6 3:26146a08bb22 273 /**
kbhagat6 3:26146a08bb22 274 * @brief Ends the proximity sensor on the APDS-9960
kbhagat6 3:26146a08bb22 275 *
kbhagat6 3:26146a08bb22 276 * @return True if sensor disabled correctly. False on error.
kbhagat6 3:26146a08bb22 277 */
kbhagat6 3:26146a08bb22 278 bool glibr::disableProximitySensor()
kbhagat6 3:26146a08bb22 279 {
kbhagat6 3:26146a08bb22 280 if( !setProximityIntEnable(0) ) {
kbhagat6 3:26146a08bb22 281 return false;
kbhagat6 3:26146a08bb22 282 }
kbhagat6 3:26146a08bb22 283 if( !setMode(PROXIMITY, 0) ) {
kbhagat6 3:26146a08bb22 284 return false;
kbhagat6 3:26146a08bb22 285 }
kbhagat6 3:26146a08bb22 286
kbhagat6 3:26146a08bb22 287 return true;
kbhagat6 3:26146a08bb22 288 }
kbhagat6 3:26146a08bb22 289
kbhagat6 3:26146a08bb22 290
kbhagat6 3:26146a08bb22 291 /**
kbhagat6 3:26146a08bb22 292 * @brief Starts the gesture recognition engine on the APDS-9960
kbhagat6 3:26146a08bb22 293 *
kbhagat6 3:26146a08bb22 294 * @param[in] interrupts true to enable hardware external interrupt on gesture
kbhagat6 3:26146a08bb22 295 * @return True if engine enabled correctly. False on error.
kbhagat6 3:26146a08bb22 296 */
kbhagat6 3:26146a08bb22 297 bool glibr::enableGestureSensor(bool interrupts)
kbhagat6 3:26146a08bb22 298 {
kbhagat6 3:26146a08bb22 299
kbhagat6 3:26146a08bb22 300 /* Enable gesture mode
kbhagat6 3:26146a08bb22 301 Set ENABLE to 0 (power off)
kbhagat6 3:26146a08bb22 302 Set WTIME to 0xFF
kbhagat6 3:26146a08bb22 303 Set AUX to LED_BOOST_300
kbhagat6 3:26146a08bb22 304 Enable PON, WEN, PEN, GEN in ENABLE
kbhagat6 3:26146a08bb22 305 */
kbhagat6 3:26146a08bb22 306
kbhagat6 3:26146a08bb22 307 resetGestureParameters();
kbhagat6 3:26146a08bb22 308 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_WTIME, 0xFF) ) {
kbhagat6 3:26146a08bb22 309 return false;
kbhagat6 3:26146a08bb22 310 }
kbhagat6 3:26146a08bb22 311 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PPULSE, DEFAULT_GESTURE_PPULSE) ) {
kbhagat6 3:26146a08bb22 312 return false;
kbhagat6 3:26146a08bb22 313 }
kbhagat6 3:26146a08bb22 314 if( !setLEDBoost(LED_BOOST_300) ) {
kbhagat6 3:26146a08bb22 315 return false;
kbhagat6 3:26146a08bb22 316 }
kbhagat6 3:26146a08bb22 317 if( interrupts ) {
kbhagat6 3:26146a08bb22 318 if( !setGestureIntEnable(1) ) {
kbhagat6 3:26146a08bb22 319 return false;
kbhagat6 3:26146a08bb22 320 }
kbhagat6 3:26146a08bb22 321 } else {
kbhagat6 3:26146a08bb22 322 if( !setGestureIntEnable(0) ) {
kbhagat6 3:26146a08bb22 323 return false;
kbhagat6 3:26146a08bb22 324 }
kbhagat6 3:26146a08bb22 325 }
kbhagat6 3:26146a08bb22 326 if( !setGestureMode(1) ) {
kbhagat6 3:26146a08bb22 327 return false;
kbhagat6 3:26146a08bb22 328 }
kbhagat6 3:26146a08bb22 329 if( !enablePower() ){
kbhagat6 3:26146a08bb22 330 return false;
kbhagat6 3:26146a08bb22 331 }
kbhagat6 3:26146a08bb22 332 if( !setMode(WAIT, 1) ) {
kbhagat6 3:26146a08bb22 333 return false;
kbhagat6 3:26146a08bb22 334 }
kbhagat6 3:26146a08bb22 335 if( !setMode(PROXIMITY, 1) ) {
kbhagat6 3:26146a08bb22 336 return false;
kbhagat6 3:26146a08bb22 337 }
kbhagat6 3:26146a08bb22 338 if( !setMode(GESTURE, 1) ) {
kbhagat6 3:26146a08bb22 339 return false;
kbhagat6 3:26146a08bb22 340 }
kbhagat6 3:26146a08bb22 341
kbhagat6 3:26146a08bb22 342 return true;
kbhagat6 3:26146a08bb22 343 }
kbhagat6 3:26146a08bb22 344
kbhagat6 3:26146a08bb22 345 /**
kbhagat6 3:26146a08bb22 346 * @brief Ends the gesture recognition engine on the APDS-9960
kbhagat6 3:26146a08bb22 347 *
kbhagat6 3:26146a08bb22 348 * @return True if engine disabled correctly. False on error.
kbhagat6 3:26146a08bb22 349 */
kbhagat6 3:26146a08bb22 350 bool glibr::disableGestureSensor()
kbhagat6 3:26146a08bb22 351 {
kbhagat6 3:26146a08bb22 352 resetGestureParameters();
kbhagat6 3:26146a08bb22 353 if( !setGestureIntEnable(0) ) {
kbhagat6 3:26146a08bb22 354 return false;
kbhagat6 3:26146a08bb22 355 }
kbhagat6 3:26146a08bb22 356 if( !setGestureMode(0) ) {
kbhagat6 3:26146a08bb22 357 return false;
kbhagat6 3:26146a08bb22 358 }
kbhagat6 3:26146a08bb22 359 if( !setMode(GESTURE, 0) ) {
kbhagat6 3:26146a08bb22 360 return false;
kbhagat6 3:26146a08bb22 361 }
kbhagat6 3:26146a08bb22 362
kbhagat6 3:26146a08bb22 363 return true;
kbhagat6 3:26146a08bb22 364 }
kbhagat6 3:26146a08bb22 365
kbhagat6 3:26146a08bb22 366
kbhagat6 3:26146a08bb22 367 /**
kbhagat6 3:26146a08bb22 368 * @brief Determines if there is a gesture available for reading
kbhagat6 3:26146a08bb22 369 *
kbhagat6 3:26146a08bb22 370 * @return True if gesture available. False otherwise.
kbhagat6 3:26146a08bb22 371 */
kbhagat6 3:26146a08bb22 372 bool glibr::isGestureAvailable()
kbhagat6 3:26146a08bb22 373 {
kbhagat6 3:26146a08bb22 374 uint8_t val;
kbhagat6 3:26146a08bb22 375
kbhagat6 3:26146a08bb22 376 /* Read value from GSTATUS register */
kbhagat6 4:84545b0e63a9 377 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_GSTATUS);
kbhagat6 4:84545b0e63a9 378 if( val==ERROR) ) {
kbhagat6 3:26146a08bb22 379 return ERROR;
kbhagat6 3:26146a08bb22 380 }
kbhagat6 3:26146a08bb22 381
kbhagat6 3:26146a08bb22 382 /* Shift and mask out GVALID bit */
kbhagat6 3:26146a08bb22 383 val &= APDS9960_GVALID;
kbhagat6 3:26146a08bb22 384
kbhagat6 3:26146a08bb22 385 /* Return true/false based on GVALID bit */
kbhagat6 3:26146a08bb22 386 if( val == 1) {
kbhagat6 3:26146a08bb22 387 return true;
kbhagat6 3:26146a08bb22 388 } else {
kbhagat6 3:26146a08bb22 389 return false;
kbhagat6 3:26146a08bb22 390 }
kbhagat6 3:26146a08bb22 391 }
kbhagat6 3:26146a08bb22 392
kbhagat6 4:84545b0e63a9 393 int glibr::readGesture()
kbhagat6 4:84545b0e63a9 394 {
kbhagat6 4:84545b0e63a9 395 uint8_t fifo_level = 0;
kbhagat6 4:84545b0e63a9 396 uint8_t bytes_read = 0;
kbhagat6 4:84545b0e63a9 397 uint8_t fifo_data[128];
kbhagat6 4:84545b0e63a9 398 uint8_t gstatus;
kbhagat6 4:84545b0e63a9 399 int motion;
kbhagat6 4:84545b0e63a9 400 int i;
kbhagat6 4:84545b0e63a9 401
kbhagat6 4:84545b0e63a9 402 /* Make sure that power and gesture is on and data is valid */
kbhagat6 4:84545b0e63a9 403 if( !isGestureAvailable() || !(getMode() & 0x41) ) {
kbhagat6 4:84545b0e63a9 404 return DIR_NONE;
kbhagat6 4:84545b0e63a9 405 }
kbhagat6 4:84545b0e63a9 406
kbhagat6 4:84545b0e63a9 407 /* Keep looping as long as gesture data is valid */
kbhagat6 4:84545b0e63a9 408 while(1) {
kbhagat6 4:84545b0e63a9 409
kbhagat6 4:84545b0e63a9 410 /* Wait some time to collect next batch of FIFO data */
kbhagat6 4:84545b0e63a9 411 delay(FIFO_PAUSE_TIME);
kbhagat6 4:84545b0e63a9 412
kbhagat6 4:84545b0e63a9 413 /* Get the contents of the STATUS register. Is data still valid? */
kbhagat6 4:84545b0e63a9 414
kbhagat6 4:84545b0e63a9 415 gstatus=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_GSTATUS);
kbhagat6 4:84545b0e63a9 416 if( gstatus==ERROR ) {
kbhagat6 4:84545b0e63a9 417 return ERROR;
kbhagat6 4:84545b0e63a9 418 }
kbhagat6 4:84545b0e63a9 419 /* If we have valid data, read in FIFO */
kbhagat6 4:84545b0e63a9 420 if( (gstatus & APDS9960_GVALID) == APDS9960_GVALID ) {
kbhagat6 4:84545b0e63a9 421
kbhagat6 4:84545b0e63a9 422 /* Read the current FIFO level */
kbhagat6 4:84545b0e63a9 423 fifolevel=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_GFLVL);
kbhagat6 4:84545b0e63a9 424 if( fifolevel==ERROR ) {
kbhagat6 4:84545b0e63a9 425 return ERROR;
kbhagat6 4:84545b0e63a9 426 }
kbhagat6 4:84545b0e63a9 427
kbhagat6 4:84545b0e63a9 428 //#if DEBUG
kbhagat6 4:84545b0e63a9 429 // Serial.print("FIFO Level: ");
kbhagat6 4:84545b0e63a9 430 // Serial.println(fifo_level);
kbhagat6 4:84545b0e63a9 431 //#endif
kbhagat6 4:84545b0e63a9 432
kbhagat6 4:84545b0e63a9 433 /* If there's stuff in the FIFO, read it into our data block */ //NEED TO FIGURE OUT WHAT THIS IS DOING.
kbhagat6 4:84545b0e63a9 434 if( fifo_level > 0) {
kbhagat6 4:84545b0e63a9 435 bytes_read = wireReadDataBlock( APDS9960_GFIFO_U,
kbhagat6 4:84545b0e63a9 436 (uint8_t*)fifo_data,
kbhagat6 4:84545b0e63a9 437 (fifo_level * 4) );
kbhagat6 4:84545b0e63a9 438 if( bytes_read == -1 ) {
kbhagat6 4:84545b0e63a9 439 return ERROR;
kbhagat6 4:84545b0e63a9 440 }
kbhagat6 4:84545b0e63a9 441 #if DEBUG
kbhagat6 4:84545b0e63a9 442 Serial.print("FIFO Dump: ");
kbhagat6 4:84545b0e63a9 443 for ( i = 0; i < bytes_read; i++ ) {
kbhagat6 4:84545b0e63a9 444 Serial.print(fifo_data[i]);
kbhagat6 4:84545b0e63a9 445 Serial.print(" ");
kbhagat6 4:84545b0e63a9 446 }
kbhagat6 4:84545b0e63a9 447 Serial.println();
kbhagat6 4:84545b0e63a9 448 #endif
kbhagat6 4:84545b0e63a9 449
kbhagat6 4:84545b0e63a9 450 /* If at least 1 set of data, sort the data into U/D/L/R */
kbhagat6 4:84545b0e63a9 451 if( bytes_read >= 4 ) {
kbhagat6 4:84545b0e63a9 452 for( i = 0; i < bytes_read; i += 4 ) {
kbhagat6 4:84545b0e63a9 453 gesture_data_.u_data[gesture_data_.index] = \
kbhagat6 4:84545b0e63a9 454 fifo_data[i + 0];
kbhagat6 4:84545b0e63a9 455 gesture_data_.d_data[gesture_data_.index] = \
kbhagat6 4:84545b0e63a9 456 fifo_data[i + 1];
kbhagat6 4:84545b0e63a9 457 gesture_data_.l_data[gesture_data_.index] = \
kbhagat6 4:84545b0e63a9 458 fifo_data[i + 2];
kbhagat6 4:84545b0e63a9 459 gesture_data_.r_data[gesture_data_.index] = \
kbhagat6 4:84545b0e63a9 460 fifo_data[i + 3];
kbhagat6 4:84545b0e63a9 461 gesture_data_.index++;
kbhagat6 4:84545b0e63a9 462 gesture_data_.total_gestures++;
kbhagat6 4:84545b0e63a9 463 }
kbhagat6 4:84545b0e63a9 464
kbhagat6 4:84545b0e63a9 465 #if DEBUG
kbhagat6 4:84545b0e63a9 466 Serial.print("Up Data: ");
kbhagat6 4:84545b0e63a9 467 for ( i = 0; i < gesture_data_.total_gestures; i++ ) {
kbhagat6 4:84545b0e63a9 468 Serial.print(gesture_data_.u_data[i]);
kbhagat6 4:84545b0e63a9 469 Serial.print(" ");
kbhagat6 4:84545b0e63a9 470 }
kbhagat6 4:84545b0e63a9 471 Serial.println();
kbhagat6 4:84545b0e63a9 472 #endif
kbhagat6 4:84545b0e63a9 473
kbhagat6 4:84545b0e63a9 474 /* Filter and process gesture data. Decode near/far state */
kbhagat6 4:84545b0e63a9 475 if( processGestureData() ) {
kbhagat6 4:84545b0e63a9 476 if( decodeGesture() ) {
kbhagat6 4:84545b0e63a9 477 //***TODO: U-Turn Gestures
kbhagat6 4:84545b0e63a9 478 #if DEBUG
kbhagat6 4:84545b0e63a9 479 //Serial.println(gesture_motion_);
kbhagat6 4:84545b0e63a9 480 #endif
kbhagat6 4:84545b0e63a9 481 }
kbhagat6 4:84545b0e63a9 482 }
kbhagat6 4:84545b0e63a9 483
kbhagat6 4:84545b0e63a9 484 /* Reset data */
kbhagat6 4:84545b0e63a9 485 gesture_data_.index = 0;
kbhagat6 4:84545b0e63a9 486 gesture_data_.total_gestures = 0;
kbhagat6 4:84545b0e63a9 487 }
kbhagat6 4:84545b0e63a9 488 }
kbhagat6 4:84545b0e63a9 489 } else {
kbhagat6 4:84545b0e63a9 490
kbhagat6 4:84545b0e63a9 491 /* Determine best guessed gesture and clean up */
kbhagat6 4:84545b0e63a9 492 delay(FIFO_PAUSE_TIME);
kbhagat6 4:84545b0e63a9 493 decodeGesture();
kbhagat6 4:84545b0e63a9 494 motion = gesture_motion_;
kbhagat6 4:84545b0e63a9 495 #if DEBUG
kbhagat6 4:84545b0e63a9 496 Serial.print("END: ");
kbhagat6 4:84545b0e63a9 497 Serial.println(gesture_motion_);
kbhagat6 4:84545b0e63a9 498 #endif
kbhagat6 4:84545b0e63a9 499 resetGestureParameters();
kbhagat6 4:84545b0e63a9 500 return motion;
kbhagat6 4:84545b0e63a9 501 }
kbhagat6 4:84545b0e63a9 502 }
kbhagat6 4:84545b0e63a9 503 }
kbhagat6 4:84545b0e63a9 504
kbhagat6 0:437ae08befe3 505 bool glibr::setProximityGain(uint8_t drive)
kbhagat6 0:437ae08befe3 506 {
kbhagat6 0:437ae08befe3 507 uint8_t val;
kbhagat6 0:437ae08befe3 508
kbhagat6 0:437ae08befe3 509 /* Read value from CONTROL register */
kbhagat6 1:c7215f5f9a72 510
kbhagat6 1:c7215f5f9a72 511 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL, val);
kbhagat6 0:437ae08befe3 512
kbhagat6 1:c7215f5f9a72 513 if(val==ERROR){
kbhagat6 1:c7215f5f9a72 514 return false;
kbhagat6 1:c7215f5f9a72 515 }
kbhagat6 0:437ae08befe3 516 /* Set bits in register to given value */
kbhagat6 1:c7215f5f9a72 517 //drive &= 0b00000011;
kbhagat6 1:c7215f5f9a72 518 drive &=0x03;
kbhagat6 0:437ae08befe3 519 drive = drive << 2;
kbhagat6 1:c7215f5f9a72 520 val &= 0xF3;
kbhagat6 0:437ae08befe3 521 val |= drive;
kbhagat6 0:437ae08befe3 522
kbhagat6 0:437ae08befe3 523 /* Write register value back into CONTROL register */
kbhagat6 1:c7215f5f9a72 524 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONTROL, val) ) {
kbhagat6 1:c7215f5f9a72 525 return false;
kbhagat6 1:c7215f5f9a72 526 }
kbhagat6 1:c7215f5f9a72 527 return true;
kbhagat6 1:c7215f5f9a72 528 }
kbhagat6 1:c7215f5f9a72 529
kbhagat6 1:c7215f5f9a72 530
kbhagat6 1:c7215f5f9a72 531 bool glibr::setAmbientLightGain(uint8_t drive){
kbhagat6 1:c7215f5f9a72 532 {
kbhagat6 1:c7215f5f9a72 533 uint8_t val;
kbhagat6 1:c7215f5f9a72 534
kbhagat6 1:c7215f5f9a72 535 /* Read value from CONTROL register */
kbhagat6 1:c7215f5f9a72 536
kbhagat6 1:c7215f5f9a72 537 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL, val);
kbhagat6 1:c7215f5f9a72 538
kbhagat6 1:c7215f5f9a72 539 if(val==ERROR){
kbhagat6 1:c7215f5f9a72 540 return false;
kbhagat6 1:c7215f5f9a72 541 }
kbhagat6 1:c7215f5f9a72 542 /* Set bits in register to given value */
kbhagat6 1:c7215f5f9a72 543 //drive &= 0b00000011;
kbhagat6 1:c7215f5f9a72 544 drive &=0x03;
kbhagat6 1:c7215f5f9a72 545 drive = drive << 2;
kbhagat6 1:c7215f5f9a72 546 val &= 0xF3;
kbhagat6 1:c7215f5f9a72 547 val |= drive;
kbhagat6 1:c7215f5f9a72 548
kbhagat6 1:c7215f5f9a72 549 /* Write register value back into CONTROL register */
kbhagat6 1:c7215f5f9a72 550 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONTROL, val) ) {
kbhagat6 1:c7215f5f9a72 551 return false;
kbhagat6 1:c7215f5f9a72 552 }
kbhagat6 1:c7215f5f9a72 553 return true;
kbhagat6 1:c7215f5f9a72 554 }
kbhagat6 1:c7215f5f9a72 555
kbhagat6 1:c7215f5f9a72 556
kbhagat6 1:c7215f5f9a72 557 bool glibr::setProxIntLowThresh(uint8_t threshold)
kbhagat6 1:c7215f5f9a72 558 {
kbhagat6 1:c7215f5f9a72 559 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PILT, threshold) ) {
kbhagat6 0:437ae08befe3 560 return false;
kbhagat6 0:437ae08befe3 561 }
kbhagat6 0:437ae08befe3 562
kbhagat6 0:437ae08befe3 563 return true;
kbhagat6 0:437ae08befe3 564 }
kbhagat6 0:437ae08befe3 565
kbhagat6 1:c7215f5f9a72 566 bool glibr::setProxIntHighThresh(uint8_t threshold)
kbhagat6 1:c7215f5f9a72 567 {
kbhagat6 1:c7215f5f9a72 568
kbhagat6 1:c7215f5f9a72 569 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PIHT, threshold) ) {
kbhagat6 1:c7215f5f9a72 570 return false;
kbhagat6 1:c7215f5f9a72 571 }
kbhagat6 1:c7215f5f9a72 572
kbhagat6 1:c7215f5f9a72 573 return true;
kbhagat6 1:c7215f5f9a72 574 }
kbhagat6 0:437ae08befe3 575
kbhagat6 3:26146a08bb22 576
kbhagat6 3:26146a08bb22 577
kbhagat6 3:26146a08bb22 578
kbhagat6 3:26146a08bb22 579
kbhagat6 3:26146a08bb22 580
kbhagat6 3:26146a08bb22 581
kbhagat6 3:26146a08bb22 582
kbhagat6 0:437ae08befe3 583 int glibr::I2CwriteByte(char address, char subAddress, char data)
kbhagat6 0:437ae08befe3 584 {
kbhagat6 0:437ae08befe3 585 int ret;
kbhagat6 0:437ae08befe3 586 char cmd[2] = {subAddress, data};
kbhagat6 0:437ae08befe3 587 ret=i2c.write(address<<1, cmd, 2); //if ret is 1, then not acked.
kbhagat6 0:437ae08befe3 588 return ret;
kbhagat6 0:437ae08befe3 589 }
kbhagat6 0:437ae08befe3 590
kbhagat6 1:c7215f5f9a72 591
kbhagat6 1:c7215f5f9a72 592 bool glibr::setLightIntLowThreshold(uint16_t threshold)
kbhagat6 1:c7215f5f9a72 593 {
kbhagat6 1:c7215f5f9a72 594 uint8_t val_low;
kbhagat6 1:c7215f5f9a72 595 uint8_t val_high;
kbhagat6 1:c7215f5f9a72 596
kbhagat6 1:c7215f5f9a72 597 /* Break 16-bit threshold into 2 8-bit values */
kbhagat6 1:c7215f5f9a72 598 val_low = threshold & 0x00FF;
kbhagat6 1:c7215f5f9a72 599 val_high = (threshold & 0xFF00) >> 8;
kbhagat6 1:c7215f5f9a72 600
kbhagat6 1:c7215f5f9a72 601 /* Write low byte */
kbhagat6 1:c7215f5f9a72 602 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_AILTL, val_low) ) {
kbhagat6 1:c7215f5f9a72 603 return false;
kbhagat6 1:c7215f5f9a72 604 }
kbhagat6 1:c7215f5f9a72 605
kbhagat6 1:c7215f5f9a72 606 /* Write high byte */
kbhagat6 1:c7215f5f9a72 607 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_AILTH, val_high) ) {
kbhagat6 1:c7215f5f9a72 608 return false;
kbhagat6 1:c7215f5f9a72 609 }
kbhagat6 1:c7215f5f9a72 610
kbhagat6 1:c7215f5f9a72 611 return true;
kbhagat6 1:c7215f5f9a72 612 }
kbhagat6 1:c7215f5f9a72 613
kbhagat6 0:437ae08befe3 614 uint8_t glibr::I2CreadByte(char address, char subAddress)
kbhagat6 0:437ae08befe3 615 {
kbhagat6 0:437ae08befe3 616 char data; // store the register data
kbhagat6 0:437ae08befe3 617
kbhagat6 0:437ae08befe3 618 if(i2c.write(address<<1, &subAddress, 1, true)){
kbhagat6 0:437ae08befe3 619 return ERROR; //7 bit //not acked
kbhagat6 0:437ae08befe3 620 }
kbhagat6 0:437ae08befe3 621 i2c.read(address<<1, &data, 1);
kbhagat6 0:437ae08befe3 622 return data;
kbhagat6 0:437ae08befe3 623
kbhagat6 0:437ae08befe3 624 }