Esempio n. 1
0
/**
* @brief Configures I2C communications and initializes registers to defaults
*
* @return True if initialized successfully. False otherwise.
*/
int gesture_init () {
	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;


	uint8_t id;
	wireReadDataByte(APDS9960_ID, &id);

	if(!(id == APDS9960_ID_1) || (id == APDS9960_ID_2)) {
		return -1;
	}

	setMode(ALL, OFF);

	wireWriteDataByte(APDS9960_ATIME, DEFAULT_ATIME);

	wireWriteDataByte(APDS9960_WTIME, DEFAULT_WTIME);

	wireWriteDataByte(APDS9960_PPULSE, DEFAULT_PROX_PPULSE);

	wireWriteDataByte(APDS9960_POFFSET_UR, DEFAULT_POFFSET_UR);

	wireWriteDataByte(APDS9960_POFFSET_DL, DEFAULT_POFFSET_DL);

	wireWriteDataByte(APDS9960_CONFIG1, DEFAULT_CONFIG1);

	setLEDDrive(DEFAULT_LDRIVE);

	setProximityGain(DEFAULT_PGAIN);

	setAmbientLightGain(DEFAULT_AGAIN);

	setProxIntLowThresh(DEFAULT_PILT);

	setProxIntHighThresh(DEFAULT_PIHT);

	setLightIntLowThreshold(DEFAULT_AILT);

	setLightIntHighThreshold(DEFAULT_AIHT);

	wireWriteDataByte(APDS9960_PERS, DEFAULT_PERS);

	wireWriteDataByte(APDS9960_CONFIG2, DEFAULT_CONFIG2);

	wireWriteDataByte(APDS9960_CONFIG3, DEFAULT_CONFIG3);

	setGestureEnterThresh(DEFAULT_GPENTH);

	setGestureExitThresh(DEFAULT_GEXTH);

	wireWriteDataByte(APDS9960_GCONF1, DEFAULT_GCONF1);

	setGestureGain(DEFAULT_GGAIN);

	setGestureLEDDrive(DEFAULT_GLDRIVE);

	setGestureWaitTime(DEFAULT_GWTIME);

	wireWriteDataByte(APDS9960_GOFFSET_U, DEFAULT_GOFFSET);

	wireWriteDataByte(APDS9960_GOFFSET_D, DEFAULT_GOFFSET);

	wireWriteDataByte(APDS9960_GOFFSET_L, DEFAULT_GOFFSET);

	wireWriteDataByte(APDS9960_GOFFSET_R, DEFAULT_GOFFSET);

	wireWriteDataByte(APDS9960_GPULSE, DEFAULT_GPULSE);

	wireWriteDataByte(APDS9960_GCONF3, DEFAULT_GCONF3);

	setGestureIntEnable(DEFAULT_GIEN);

	return 0;
}
/* ----------------------------------------------------------------------------*
 *
 *  Function Name : apds9960init
 *
 *  Description  : Configures I2C communications and initializes registers to defaults
 *
 *  Input : None
 *
 *  Output : None
 *
 *  Return : True if initialized successfully. False otherwise
 * ----------------------------------------------------------------------------*
 * Authors: Sarath S
 * Date: May 17, 2017
 * ---------------------------------------------------------------------------*/
int apds9960init(void)
{
    uint8_t id;
    //ledSetLeftLed(LED_ON);  

    /* Initialize I2C */
    I2C1_init();
    delayms(700);
    /* Read ID register and check against known values for APDS-9960 */
    if( !i2c1_read(APDS9960_ID, &id,1) ) {
       ledSetRightLed(LED_ON);  
      return false;
        
    }
    ledSetLeftLed(LED_ON);  
    if( !(id == APDS9960_ID_1 || id == APDS9960_ID_2) ) {
        return false;
    }
     
    /* Set ENABLE register to 0 (disable all features) */
    if( !setMode(ALL, OFF) ) {
        return false;
    }
    
    /* Set default values for ambient light and proximity registers */
    if( !i2c1_write(APDS9960_ATIME, DEFAULT_ATIME) ) {
        return false;
    }
    if( !i2c1_write(APDS9960_WTIME, DEFAULT_WTIME) ) {
        return false;
    }
    if( !i2c1_write(APDS9960_PPULSE, DEFAULT_PROX_PPULSE) ) {
        return false;
    }
    if( !i2c1_write(APDS9960_POFFSET_UR, DEFAULT_POFFSET_UR) ) {
        return false;
    }
    if( !i2c1_write(APDS9960_POFFSET_DL, DEFAULT_POFFSET_DL) ) {
        return false;
    }
    if( !i2c1_write(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( !setLightIntHighThreshold(DEFAULT_AIHT) ) {
        return false;
    }
    if( !i2c1_write(APDS9960_PERS, DEFAULT_PERS) ) {
        return false;
    }
    if( !i2c1_write(APDS9960_CONFIG2, DEFAULT_CONFIG2) ) {
        return false;
    }
    if( !i2c1_write(APDS9960_CONFIG3, DEFAULT_CONFIG3) ) {
        return false;
    }
    
    /* Set default values for gesture sense registers */
    if( !setGestureEnterThresh(DEFAULT_GPENTH) ) {
        return false;
    }
    if( !setGestureExitThresh(DEFAULT_GEXTH) ) {
        return false;
    }
    if( !i2c1_write(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( !i2c1_write(APDS9960_GOFFSET_U, DEFAULT_GOFFSET) ) {
        return false;
    }
    if( !i2c1_write(APDS9960_GOFFSET_D, DEFAULT_GOFFSET) ) {
        return false;
    }
    if( !i2c1_write(APDS9960_GOFFSET_L, DEFAULT_GOFFSET) ) {
        return false;
    }
    if( !i2c1_write(APDS9960_GOFFSET_R, DEFAULT_GOFFSET) ) {
        return false;
    }
    if( !i2c1_write(APDS9960_GPULSE, DEFAULT_GPULSE) ) {
        return false;
    }
    if( !i2c1_write(APDS9960_GCONF3, DEFAULT_GCONF3) ) {
        return false;
    }
    if( !setGestureIntEnable(DEFAULT_GIEN) ) {
        return false;
    }
    
#if 0
    /* Gesture config register dump */
    uint8_t reg;
    uint8_t val;
  
    for(reg = 0x80; reg <= 0xAF; reg++) {
        if( (reg != 0x82) && \
            (reg != 0x8A) && \
            (reg != 0x91) && \
            (reg != 0xA8) && \
            (reg != 0xAC) && \
            (reg != 0xAD) )
        {
            i2c1_read(reg, val,1);
            //debugPutChar(reg);
            debugPutString(": 0x");
            //debugPutChar(val);

        }
    }

    for(reg = 0xE4; reg <= 0xE7; reg++) {
        i2c1_read(reg, val,1);
        //debugPutChar(reg);
        debugPutString(": 0x");
        //debugPutChar(val);
    }
#endif

    return true;
}/* End of this function */