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 06:44:27 2015 +0000
Revision:
1:c7215f5f9a72
Parent:
0:437ae08befe3
Child:
3:26146a08bb22
still working in bool init function, and adding the functions it calls.

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 0:437ae08befe3 195 bool glibr::setProximityGain(uint8_t drive)
kbhagat6 0:437ae08befe3 196 {
kbhagat6 0:437ae08befe3 197 uint8_t val;
kbhagat6 0:437ae08befe3 198
kbhagat6 0:437ae08befe3 199 /* Read value from CONTROL register */
kbhagat6 1:c7215f5f9a72 200
kbhagat6 1:c7215f5f9a72 201 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL, val);
kbhagat6 0:437ae08befe3 202
kbhagat6 1:c7215f5f9a72 203 if(val==ERROR){
kbhagat6 1:c7215f5f9a72 204 return false;
kbhagat6 1:c7215f5f9a72 205 }
kbhagat6 0:437ae08befe3 206 /* Set bits in register to given value */
kbhagat6 1:c7215f5f9a72 207 //drive &= 0b00000011;
kbhagat6 1:c7215f5f9a72 208 drive &=0x03;
kbhagat6 0:437ae08befe3 209 drive = drive << 2;
kbhagat6 1:c7215f5f9a72 210 val &= 0xF3;
kbhagat6 0:437ae08befe3 211 val |= drive;
kbhagat6 0:437ae08befe3 212
kbhagat6 0:437ae08befe3 213 /* Write register value back into CONTROL register */
kbhagat6 1:c7215f5f9a72 214 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONTROL, val) ) {
kbhagat6 1:c7215f5f9a72 215 return false;
kbhagat6 1:c7215f5f9a72 216 }
kbhagat6 1:c7215f5f9a72 217 return true;
kbhagat6 1:c7215f5f9a72 218 }
kbhagat6 1:c7215f5f9a72 219
kbhagat6 1:c7215f5f9a72 220
kbhagat6 1:c7215f5f9a72 221 bool glibr::setAmbientLightGain(uint8_t drive){
kbhagat6 1:c7215f5f9a72 222 {
kbhagat6 1:c7215f5f9a72 223 uint8_t val;
kbhagat6 1:c7215f5f9a72 224
kbhagat6 1:c7215f5f9a72 225 /* Read value from CONTROL register */
kbhagat6 1:c7215f5f9a72 226
kbhagat6 1:c7215f5f9a72 227 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL, val);
kbhagat6 1:c7215f5f9a72 228
kbhagat6 1:c7215f5f9a72 229 if(val==ERROR){
kbhagat6 1:c7215f5f9a72 230 return false;
kbhagat6 1:c7215f5f9a72 231 }
kbhagat6 1:c7215f5f9a72 232 /* Set bits in register to given value */
kbhagat6 1:c7215f5f9a72 233 //drive &= 0b00000011;
kbhagat6 1:c7215f5f9a72 234 drive &=0x03;
kbhagat6 1:c7215f5f9a72 235 drive = drive << 2;
kbhagat6 1:c7215f5f9a72 236 val &= 0xF3;
kbhagat6 1:c7215f5f9a72 237 val |= drive;
kbhagat6 1:c7215f5f9a72 238
kbhagat6 1:c7215f5f9a72 239 /* Write register value back into CONTROL register */
kbhagat6 1:c7215f5f9a72 240 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONTROL, val) ) {
kbhagat6 1:c7215f5f9a72 241 return false;
kbhagat6 1:c7215f5f9a72 242 }
kbhagat6 1:c7215f5f9a72 243 return true;
kbhagat6 1:c7215f5f9a72 244 }
kbhagat6 1:c7215f5f9a72 245
kbhagat6 1:c7215f5f9a72 246
kbhagat6 1:c7215f5f9a72 247 bool glibr::setProxIntLowThresh(uint8_t threshold)
kbhagat6 1:c7215f5f9a72 248 {
kbhagat6 1:c7215f5f9a72 249 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PILT, threshold) ) {
kbhagat6 0:437ae08befe3 250 return false;
kbhagat6 0:437ae08befe3 251 }
kbhagat6 0:437ae08befe3 252
kbhagat6 0:437ae08befe3 253 return true;
kbhagat6 0:437ae08befe3 254 }
kbhagat6 0:437ae08befe3 255
kbhagat6 1:c7215f5f9a72 256 bool glibr::setProxIntHighThresh(uint8_t threshold)
kbhagat6 1:c7215f5f9a72 257 {
kbhagat6 1:c7215f5f9a72 258
kbhagat6 1:c7215f5f9a72 259 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PIHT, threshold) ) {
kbhagat6 1:c7215f5f9a72 260 return false;
kbhagat6 1:c7215f5f9a72 261 }
kbhagat6 1:c7215f5f9a72 262
kbhagat6 1:c7215f5f9a72 263 return true;
kbhagat6 1:c7215f5f9a72 264 }
kbhagat6 0:437ae08befe3 265
kbhagat6 0:437ae08befe3 266 int glibr::I2CwriteByte(char address, char subAddress, char data)
kbhagat6 0:437ae08befe3 267 {
kbhagat6 0:437ae08befe3 268 int ret;
kbhagat6 0:437ae08befe3 269 char cmd[2] = {subAddress, data};
kbhagat6 0:437ae08befe3 270 ret=i2c.write(address<<1, cmd, 2); //if ret is 1, then not acked.
kbhagat6 0:437ae08befe3 271 return ret;
kbhagat6 0:437ae08befe3 272 }
kbhagat6 0:437ae08befe3 273
kbhagat6 1:c7215f5f9a72 274
kbhagat6 1:c7215f5f9a72 275 bool glibr::setLightIntLowThreshold(uint16_t threshold)
kbhagat6 1:c7215f5f9a72 276 {
kbhagat6 1:c7215f5f9a72 277 uint8_t val_low;
kbhagat6 1:c7215f5f9a72 278 uint8_t val_high;
kbhagat6 1:c7215f5f9a72 279
kbhagat6 1:c7215f5f9a72 280 /* Break 16-bit threshold into 2 8-bit values */
kbhagat6 1:c7215f5f9a72 281 val_low = threshold & 0x00FF;
kbhagat6 1:c7215f5f9a72 282 val_high = (threshold & 0xFF00) >> 8;
kbhagat6 1:c7215f5f9a72 283
kbhagat6 1:c7215f5f9a72 284 /* Write low byte */
kbhagat6 1:c7215f5f9a72 285 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_AILTL, val_low) ) {
kbhagat6 1:c7215f5f9a72 286 return false;
kbhagat6 1:c7215f5f9a72 287 }
kbhagat6 1:c7215f5f9a72 288
kbhagat6 1:c7215f5f9a72 289 /* Write high byte */
kbhagat6 1:c7215f5f9a72 290 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_AILTH, val_high) ) {
kbhagat6 1:c7215f5f9a72 291 return false;
kbhagat6 1:c7215f5f9a72 292 }
kbhagat6 1:c7215f5f9a72 293
kbhagat6 1:c7215f5f9a72 294 return true;
kbhagat6 1:c7215f5f9a72 295 }
kbhagat6 1:c7215f5f9a72 296
kbhagat6 0:437ae08befe3 297 uint8_t glibr::I2CreadByte(char address, char subAddress)
kbhagat6 0:437ae08befe3 298 {
kbhagat6 0:437ae08befe3 299 char data; // store the register data
kbhagat6 0:437ae08befe3 300
kbhagat6 0:437ae08befe3 301 if(i2c.write(address<<1, &subAddress, 1, true)){
kbhagat6 0:437ae08befe3 302 return ERROR; //7 bit //not acked
kbhagat6 0:437ae08befe3 303 }
kbhagat6 0:437ae08befe3 304 i2c.read(address<<1, &data, 1);
kbhagat6 0:437ae08befe3 305 return data;
kbhagat6 0:437ae08befe3 306
kbhagat6 0:437ae08befe3 307 }