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
Diff: glibr.cpp
- Revision:
- 6:687dff16904e
- Parent:
- 5:3eb4f3091bd8
- Child:
- 7:0564c6faf8aa
diff -r 3eb4f3091bd8 -r 687dff16904e glibr.cpp --- a/glibr.cpp Fri Mar 06 02:06:15 2015 +0000 +++ b/glibr.cpp Fri Mar 06 07:20:24 2015 +0000 @@ -5,7 +5,17 @@ glibr::glibr(PinName sda, PinName scl):i2c(sda, scl){ - + gesture_ud_delta_ = 0; + gesture_lr_delta_ = 0; + + gesture_ud_count_ = 0; + gesture_lr_count_ = 0; + + gesture_near_count_ = 0; + gesture_far_count_ = 0; + + gesture_state_ = 0; + gesture_motion_ = DIR_NONE; } glibr::~glibr(){ @@ -164,7 +174,12 @@ uint8_t glibr::getMode() { - return I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ENABLE); + uint8_t val; + val= I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ENABLE); + if(val==ERROR){ + return ERROR; + } + return val; } bool glibr::setLEDDrive(uint8_t drive) @@ -502,6 +517,206 @@ } } } +/** + * Turn the APDS-9960 on + * + * @return True if operation successful. False otherwise. + */ +bool glibr::enablePower() +{ + if( !setMode(POWER, 1) ) { + return false; + } + + return true; +} + +/** + * Turn the APDS-9960 off + * + * @return True if operation successful. False otherwise. + */ +bool glibr::disablePower() +{ + if( !setMode(POWER, 0) ) { + return false; + } + + return true; +} + +/******************************************************************************* + * Ambient light and color sensor controls + ******************************************************************************/ + +/** + * @brief Reads the ambient (clear) light level as a 16-bit value + * + * @param[out] val value of the light sensor. + * @return True if operation successful. False otherwise. + */ +bool glibr::readAmbientLight(uint16_t &val) +{ + uint8_t val_byte; + val = 0; + + /* Read value from clear channel, low byte register */ + val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CDATAL); + if( valbyte==ERROR) { + return false; + } + val = val_byte; + + /* Read value from clear channel, high byte register */ + + val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CDATAH); + if( valbyte==ERROR) { + return false; + } + val = val + ((uint16_t)val_byte << 8); + + return true; +} + +/** + * @brief Reads the red light level as a 16-bit value + * + * @param[out] val value of the light sensor. + * @return True if operation successful. False otherwise. + */ +bool glibr::readRedLight(uint16_t &val) +{ + uint8_t val_byte; + val = 0; + + /* Read value from clear channel, low byte register */ + val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_RDATAL); + if( valbyte==ERROR) { + return false; + } + + val = val_byte; + + /* Read value from clear channel, high byte register */ + val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_RDATAH); + if( valbyte==ERROR) { + return false; + } + val = val + ((uint16_t)val_byte << 8); + + return true; +} + +/** + * @brief Reads the green light level as a 16-bit value + * + * @param[out] val value of the light sensor. + * @return True if operation successful. False otherwise. + */ + +bool glibr::readGreenLight(uint16_t &val) +{ + uint8_t val_byte; + val = 0; + + /* Read value from clear channel, low byte register */ + val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_GDATAL); + if( valbyte==ERROR) { + return false; + } + + val = val_byte; + + /* Read value from clear channel, high byte register */ + val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_GDATAH); + if( valbyte==ERROR) { + return false; + } + val = val + ((uint16_t)val_byte << 8); + + return true; +} + +/** + * @brief Reads the red light level as a 16-bit value + * + * @param[out] val value of the light sensor. + * @return True if operation successful. False otherwise. +*/ + +bool glibr::readBlueLight(uint16_t &val) +{ + uint8_t val_byte; + val = 0; + + /* Read value from clear channel, low byte register */ + val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_BDATAL); + if( valbyte==ERROR) { + return false; + } + + val = val_byte; + + /* Read value from clear channel, high byte register */ + val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_BDATAH); + if( valbyte==ERROR) { + return false; + } + val = val + ((uint16_t)val_byte << 8); + + return true; +} + +/******************************************************************************* + * Proximity sensor controls + ******************************************************************************/ + +/** + * @brief Reads the proximity level as an 8-bit value + * + * @param[out] val value of the proximity sensor. + * @return True if operation successful. False otherwise. + */ +bool glibr::readProximity(uint8_t &val) +{ + val = 0; + + /* Read value from proximity data register */ + val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_PDATA); + + if(val==ERROR){ + return false; + } + + return true; +} + +/******************************************************************************* + * High-level gesture controls + ******************************************************************************/ + +/** + * @brief Resets all the parameters in the gesture data member + */ +void glibr::resetGestureParameters() +{ + gesture_data_.index = 0; + gesture_data_.total_gestures = 0; + + gesture_ud_delta_ = 0; + gesture_lr_delta_ = 0; + + gesture_ud_count_ = 0; + gesture_lr_count_ = 0; + + gesture_near_count_ = 0; + gesture_far_count_ = 0; + + gesture_state_ = 0; + gesture_motion_ = DIR_NONE; +} + + bool glibr::setProximityGain(uint8_t drive) {