int Si1147_MeasureUVAndObjectPresent(I2C_TypeDef *i2c, uint8_t addr, uint16_t *uvIndex, int *objectDetect) { uint16_t data; int retval = 0; int gestureMode; *objectDetect = 0; si114x_handle->addr = addr; si114x_handle->i2c = i2c; Si1147_GetInterruptOutputEnable(i2c,addr,&gestureMode); if ( !gestureMode ) /* Force only if not already running swipe detection. */ { Si114xPsAlsForce(si114x_handle); while ((Si114xReadFromRegister(si114x_handle, REG_IRQ_STATUS) & 1) == 0) ; /*wait for measurement data */ } data = Si114xReadFromRegister(si114x_handle, REG_AUX_DATA0); /*read sample data from si114x */ data |= Si114xReadFromRegister(si114x_handle, REG_AUX_DATA1) << 8; /*round to nearest*/ *uvIndex = data + 50; *uvIndex /= 100; if ( !gestureMode ) /* Check for object only if not already running swipe detection. */ { data = Si114xReadFromRegister(si114x_handle, REG_PS1_DATA0); /*read sample data from si114x */ data |= Si114xReadFromRegister(si114x_handle, REG_PS1_DATA1) << 8; if (data > PS_HOVER_THRESHOLD) { *objectDetect = 1; } } /*clear irq*/ Si114xWriteToRegister(si114x_handle, REG_IRQ_STATUS, 0xff); return retval; }
/**************************************************************************//** * @brief * Reads the UV index measurement data from the * Si114x. * @param[out] uvIndex * The UV index read from the sensor * @return * Returns 0. *****************************************************************************/ int Si114x_MeasureUVIndex(u16 *uvIndex) { u16 data; volatile u8 regval; int retval = 0; Si114xPsAlsForce(si114x_handle); regval = Si114xReadFromRegister(si114x_handle, REG_AUX_DATA0); /*read sample data from si114x */ data = regval; regval = Si114xReadFromRegister(si114x_handle, REG_AUX_DATA1); data |= regval << 8; //check for saturation after the forced measurement and clear it if found //otherwise the next si114x cmd will not be performed. Also this must be //done after reading the AUX_DATA register regval = Si114xReadFromRegister(si114x_handle, REG_RESPONSE); while((regval & 0x80) != 0) //response == 0x8x means saturation occured { // Send the NOP Command to clear the error...we cannot use Si114xNop() // because it first checks if REG_RESPONSE < 0 and if so it does not // perform the cmd. Since we have a saturation REG_RESPONSE will be <0 Si114xWriteToRegister(si114x_handle, REG_COMMAND, 0x00); regval = Si114xReadFromRegister(si114x_handle, REG_RESPONSE); } /*round to nearest*/ *uvIndex = data + 50; *uvIndex /= 100; /*clear irq*/ if(*uvIndex > 10) *uvIndex = data + 50; Si114xWriteToRegister(si114x_handle, REG_IRQ_STATUS, 0xff); return retval; }
int main() { // variable declarations int usr_sel; int s16_current = 0x0bbb; int s16_tasklist = -1; int s16_measrate = 0x84; int gest_cnt = 0; s16 SI114x_ret_val = -1; u8 SI114x_write_val; u8 SI114x_ret_val_array[8]; u8* SI114x_ptr = &SI114x_ret_val_array; // data structure declarations SI114X_IRQ_SAMPLE samples; gesture_t gest = NONE; HANDLE si114x_handle; si114x_handle = 0; // main program printf("\nGesture Sensor program starting... Please wait...\n\n"); // initializing the I2C bus @ addr = I2C_OPENCORES_0_BASE printf("Initializing I2C bus...\n"); I2C_init(I2C_OPENCORES_0_BASE, (alt_32) 50000000, (alt_32) 200000); // reset the Si1143 Gesture IC printf("Resetting Si1143 Gesture Sensor...\n"); SI114x_ret_val = Si114xReset(si114x_handle); if ( SI114x_ret_val != 0 ) // if reset is successful... { printf("Si1143 Reset completed successfully!\n"); // set IR LED current values to a default level printf("Setting LED current values...\n"); u8 i21, i3; i21 = 0xbb; // current = LEDI_202; i3 = 0x0b; SI114x_ret_val+=Si114xWriteToRegister(si114x_handle, REG_PS_LED21, i21); SI114x_ret_val+=Si114xWriteToRegister(si114x_handle, REG_PS_LED3 , i3); } else // if reset fails, exit { printf("Si1143 Reset failed... Exiting...\n"); return -1; } // gesture recognition loop printf("Entering main loop...\n Loop exits after 32 gestures are recognized.\n"); // have Si1143 measure all three proximity channels, the visual channel, and the IR channel s16_tasklist = PS1_TASK + PS2_TASK + PS3_TASK + ALS_VIS_TASK + ALS_IR_TASK; SI114x_ret_val+=Si114xParamSet(si114x_handle, PARAM_CH_LIST, s16_tasklist); while ( gest_cnt <= 32 ){ // make measurement SI114x_ret_val = Si114xPsAlsForce(si114x_handle); //usleep(10000); // uncomment if you need a pause in measurements if ( SI114x_ret_val > 0 ){ // if measurement is successful... // retrieve data from sensor and put it in struct "samples" DECA_si1143_irq_pop(si114x_handle, &samples); // process data in gesture algorithm gest = ProcessSi1143Samples(&samples); // report the gesture and increment counter if ( gest != NONE ) gest_cnt++; if ( gest == UP ) printf("UP swipe detected!\n"); if ( gest == DOWN ) printf("DOWN swipe detected!\n"); if ( gest == LEFT ) printf("LEFT swipe detected!\n"); if ( gest == RIGHT ) printf("RIGHT swipe detected!\n"); if ( gest == TAP ) printf("TAP detected!\n"); } else printf("Unable to force measurement...\n"); } return 0; }