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

Committer:
kbhagat6
Date:
2015-03-05
Revision:
1:c7215f5f9a72
Parent:
0:437ae08befe3
Child:
3:26146a08bb22

File content as of revision 1:c7215f5f9a72:

#include "glibr.h"
#include "mbed.h"




glibr::glibr(PinName sda, PinName scl):i2c(sda, scl){
       
}    
   
glibr::~glibr(){
       
} 

 uint8_t  glibr::ginit(){
    uint8_t id;
    
 //   if( !wireReadDataByte(APDS9960_ID, id) ) {
        
     id=I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ID);
    
    if( (!(id == APDS9960_ID_1 || id == APDS9960_ID_2))||id==ERROR) {
         return false;
    }
    
    if(!setMode(ALL, OFF)) {
        return false;
    }
    if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_ATIME, DEFAULT_ATIME)){
        return false;
    }
    if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_WTIME, DEFAULT_WTIME)){
        return false;
    }
    if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PPULSE, DEFAULT_PROX_PPULSE)){
        return false;
    }
    if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_POFFSET_UR, DEFAULT_POFFSET_UR)){
        return false;
    }
    if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_POFFSET_DL, DEFAULT_POFFSET_DL)){
        return false;
    }
    if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONFIG1, DEFAULT_CONFIG1)){
        return false;
    }
    
    if( !setLEDDrive(DEFAULT_LDRIVE) ) {
        return false;
    }
    
    if( !setProximityGain(DEFAULT_PGAIN) ) {
        return false;
    }
    if( !setAmbientLightGain(DEFAULT_AGAIN) ) {
        return false;
    }
    if( !setProxIntLowThresh(DEFAULT_PILT) ) {
        return false;
    }
    if( !setProxIntHighThresh(DEFAULT_PIHT) ) {
        return false;
    }
    if( !setLightIntLowThreshold(DEFAULT_AILT) ) {
        return false;
    }
    if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONFIG2, DEFAULT_CONFIG2) ) {
        return false;
    }
    if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONFIG3, DEFAULT_CONFIG3) ) {
        return false;
    }
    
    if( !setGestureEnterThresh(DEFAULT_GPENTH) ) {
        return false;
    }
    if( !setGestureExitThresh(DEFAULT_GEXTH) ) {
        return false;
    }
    if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GCONF1, DEFAULT_GCONF1) ) {
        return false;
    }
    if( !setGestureGain(DEFAULT_GGAIN) ) {
        return false;
    }
    if( !setGestureLEDDrive(DEFAULT_GLDRIVE) ) {
        return false;
    }
    if( !setGestureWaitTime(DEFAULT_GWTIME) ) {
        return false;
    }
    if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GOFFSET_U, DEFAULT_GOFFSET) ) {
        return false;
    }
    if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GOFFSET_D, DEFAULT_GOFFSET) ) {
        return false;
    }
    if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GOFFSET_L, DEFAULT_GOFFSET) ) {
        return false;
    }
    if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GOFFSET_R, DEFAULT_GOFFSET) ) {
        return false;
    }
    if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GPULSE, DEFAULT_GPULSE) ) {
        return false;
    }
    if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GCONF3, DEFAULT_GCONF3) ) {
        return false;
    }
    if( !setGestureIntEnable(DEFAULT_GIEN) ) {
        return false;
    }
    
    
    
    return id;
}




/**
 * @brief Enables or disables a feature in the APDS-9960
 *
 * @param[in] mode which feature to enable
 * @param[in] enable ON (1) or OFF (0)
 * @return True if operation success. False otherwise.
 */
bool glibr::setMode(uint8_t mode, uint8_t enable)
{
    uint8_t reg_val;

    /* Read current ENABLE register */
    reg_val = getMode();
    if( reg_val == ERROR ) {
        return false;
    }
    
    /* Change bit(s) in ENABLE register */
    enable = enable & 0x01;
    if( mode >= 0 && mode <= 6 ) {
        if (enable) {
            reg_val |= (1 << mode);
        } else {
            reg_val &= ~(1 << mode);
        }
    } else if( mode == ALL ) {
        if (enable) {
            reg_val = 0x7F;
        } else {
            reg_val = 0x00;
        }
    }
    
    /* Write value back to ENABLE register */     
    if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_ENABLE, reg_val)){
        return false;
    }
   

        
    return true;
}

uint8_t glibr::getMode()
{
    return I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ENABLE);
}

bool glibr::setLEDDrive(uint8_t drive)
{
    uint8_t val;
    
    /* Read value from CONTROL register */

    val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL);
    if(val==ERROR){
        return false;   
    }
    /* Set bits in register to given value */
    drive &= 0x03;
    drive = drive << 6;
    //val &= 0b00111111;
    val &= 0x3F;
    val |= drive;
    
    /* Write register value back into CONTROL register */
    if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONTROL, val) ) {
        return false;
    }
    
    return true;
}

bool glibr::setProximityGain(uint8_t drive)
{
    uint8_t val;
    
    /* Read value from CONTROL register */
   
    val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL, val);
    
    if(val==ERROR){
        return false;   
    }
    /* Set bits in register to given value */
    //drive &= 0b00000011;
    drive &=0x03;
    drive = drive << 2;
    val &= 0xF3;
    val |= drive;
    
    /* Write register value back into CONTROL register */
    if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONTROL, val) ) {
        return false;
    }
    return true;
}


bool glibr::setAmbientLightGain(uint8_t drive){
{
    uint8_t val;
    
    /* Read value from CONTROL register */
   
    val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL, val);
    
    if(val==ERROR){
        return false;   
    }
    /* Set bits in register to given value */
    //drive &= 0b00000011;
    drive &=0x03;
    drive = drive << 2;
    val &= 0xF3;
    val |= drive;
    
    /* Write register value back into CONTROL register */
    if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONTROL, val) ) {
        return false;
    }
    return true;
}


bool glibr::setProxIntLowThresh(uint8_t threshold)
{
    if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PILT, threshold) ) {
        return false;
    }
    
    return true;
}

bool glibr::setProxIntHighThresh(uint8_t threshold)
{
   
    if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PIHT, threshold) ) {
        return false;
    }
    
    return true;
}

int glibr::I2CwriteByte(char address, char subAddress, char data)
{   
    int ret;
    char cmd[2] = {subAddress, data};
    ret=i2c.write(address<<1, cmd, 2);  //if ret is 1, then not acked.
    return ret;
}


bool glibr::setLightIntLowThreshold(uint16_t threshold)
{
    uint8_t val_low;
    uint8_t val_high;
    
    /* Break 16-bit threshold into 2 8-bit values */
    val_low = threshold & 0x00FF;
    val_high = (threshold & 0xFF00) >> 8;
    
    /* Write low byte */
    if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_AILTL, val_low) ) {
        return false;
    }
    
    /* Write high byte */
    if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_AILTH, val_high) ) {
        return false;
    }
    
    return true;
}

uint8_t glibr::I2CreadByte(char address, char subAddress)
{
    char data; // store the register data
    
    if(i2c.write(address<<1, &subAddress, 1, true)){
        return ERROR;   //7 bit   //not acked
    } 
    i2c.read(address<<1, &data, 1);
    return data;

}