/* ----------------------------------------------------------------------------*
 *
 *  Function Name : enableGestureSensor(int interrupts)
 *
 *  Description  :Starts the gesture recognition engine on the APDS-9960
 *
 *  Input : interrupts true to enable hardware external interrupt on gesture
 *
 *  Output : None
 *
 *  Return : True if engine enabled correctly. False on error.
 * ----------------------------------------------------------------------------*
 * Authors: Sarath S
 * Date: May 17, 2017
 * ---------------------------------------------------------------------------*/
int enableGestureSensor(int interrupts)
{
    
    /* Enable gesture mode
       Set ENABLE to 0 (power off)
       Set WTIME to 0xFF
       Set AUX to LED_BOOST_300
       Enable PON, WEN, PEN, GEN in ENABLE 
    */
    resetGestureParameters();
    if( !i2c1_write(APDS9960_WTIME, 0xFF) ) {
        return false;
    }
    if( !i2c1_write(APDS9960_PPULSE, DEFAULT_GESTURE_PPULSE) ) {
        return false;
    }
    if( !setLEDBoost(LED_BOOST_300) ) {
        return false;
    }
    if( interrupts ) {
        if( !setGestureIntEnable(1) ) {
            return false;
        }
    } else {
        if( !setGestureIntEnable(0) ) {
            return false;
        }
    }
    if( !setGestureMode(1) ) {
        return false;
    }
    if( !enablePower() ){
        return false;
    }
    if( !setMode(WAIT, 1) ) {
        return false;
    }
    if( !setMode(PROXIMITY, 1) ) {
        return false;
    }
    if( !setMode(GESTURE, 1) ) {
        return false;
    }
    
    return true;
}/* End of this function */
Exemplo n.º 2
0
void enable_gesture() {
	resetGestureParameters();

	wireWriteDataByte(APDS9960_WTIME, 0xFF);

	wireWriteDataByte(APDS9960_PPULSE, DEFAULT_GESTURE_PPULSE);

	setLEDBoost(LED_BOOST_300);

	setGestureIntEnable(1);

	setGestureMode(1);

	enablePower();

	setMode(WAIT, 1);

	setMode(PROXIMITY, 1);

	setMode(GESTURE, 1);
}
Exemplo n.º 3
0
int readGesture() {
	uint8_t fifo_level = 0;
	uint8_t bytes_read = 0;
	uint8_t fifo_data[128];
	uint8_t gstatus;
	int motion;
	int i;

	/* Make sure that power and gesture is on and data is valid */
	if( !isGestureAvailable() || !(getMode() & 0b01000001) ) {
		return DIR_NONE;
	}

	/* Keep looping as long as gesture data is valid */
	while(1) {
		/* Wait some time to collect next batch of FIFO data */
		//sleep(2000);

		/* Get the contents of the STATUS register. Is data still valid? */
		wireReadDataByte(APDS9960_GSTATUS, &gstatus);


		/* If we have valid data, read in FIFO */
		if( (gstatus & APDS9960_GVALID) == APDS9960_GVALID ) {

			/* Read the current FIFO level */
			wireReadDataByte(APDS9960_GFLVL, &fifo_level);


#if DEBUG
			Serial.print("FIFO Level: ");
			Serial.println(fifo_level);
#endif

			/* If there's stuff in the FIFO, read it into our data block */
			if( fifo_level > 0) {
				bytes_read = wireReadDataBlock(  APDS9960_GFIFO_U,(uint8_t*)fifo_data,(fifo_level * 4) );
				if( bytes_read == -1 ) {
					return ERROR;
				}
#if DEBUG
				Serial.print("FIFO Dump: ");
				for ( i = 0; i < bytes_read; i++ ) {
					Serial.print(fifo_data[i]);
					Serial.print(" ");
				}
				Serial.println();
#endif

				/* If at least 1 set of data, sort the data into U/D/L/R */
				if( bytes_read >= 4 ) {
					for( i = 0; i < bytes_read; i += 4 ) {
						gesture_data_.u_data[gesture_data_.index] = fifo_data[i + 0];
						gesture_data_.d_data[gesture_data_.index] = fifo_data[i + 1];
						gesture_data_.l_data[gesture_data_.index] = fifo_data[i + 2];
						gesture_data_.r_data[gesture_data_.index] = fifo_data[i + 3];
						gesture_data_.index++;
						gesture_data_.total_gestures++;
					}

#if DEBUG
					Serial.print("Up Data: ");
					for ( i = 0; i < gesture_data_.total_gestures; i++ ) {
						Serial.print(gesture_data_.u_data[i]);
						Serial.print(" ");
					}
					Serial.println();
#endif

					/* Filter and process gesture data. Decode near/far state */
					if( processGestureData() ) {
						if( decodeGesture() ) {
							//***TODO: U-Turn Gestures
#if DEBUG
							//Serial.println(gesture_motion_);
#endif
						}
					}

					/* Reset data */
					gesture_data_.index = 0;
					gesture_data_.total_gestures = 0;
				}
			}
		} else {

			/* Determine best guessed gesture and clean up */
			// delay(FIFO_PAUSE_TIME);
			decodeGesture();
			motion = gesture_motion_;
#if DEBUG
			Serial.print("END: ");
			Serial.println(gesture_motion_);
#endif
			resetGestureParameters();
			return motion;
		}
	}
}