/********************************************************************* * @fn accelRead * * @brief Called by the application to read accelerometer data * and put data in accelerometer profile * * @param none * * @return none */ static void accelRead( void ) { static int8 x, y, z; int8 new_x, new_y, new_z; // Read data for each axis of the accelerometer accReadAcc(&new_x, &new_y, &new_z); // Check if x-axis value has changed by more than the threshold value and // set profile parameter if it has (this will send a notification if enabled) if( (x < (new_x-ACCEL_CHANGE_THRESHOLD)) || (x > (new_x+ACCEL_CHANGE_THRESHOLD)) ) { x = new_x; Accel_SetParameter(ACCEL_X_ATTR, sizeof ( int8 ), &x); } // Check if y-axis value has changed by more than the threshold value and // set profile parameter if it has (this will send a notification if enabled) if( (y < (new_y-ACCEL_CHANGE_THRESHOLD)) || (y > (new_y+ACCEL_CHANGE_THRESHOLD)) ) { y = new_y; Accel_SetParameter(ACCEL_Y_ATTR, sizeof ( int8 ), &y); } // Check if z-axis value has changed by more than the threshold value and // set profile parameter if it has (this will send a notification if enabled) if( (z < (new_z-ACCEL_CHANGE_THRESHOLD)) || (z > (new_z+ACCEL_CHANGE_THRESHOLD)) ) { z = new_z; Accel_SetParameter(ACCEL_Z_ATTR, sizeof ( int8 ), &z); } }
/********************************************************************* * @fn resetCharacteristicValue * * @brief Initialize a characteristic value to zero * * @param servID - service ID (UUID) * * @param paramID - parameter ID of the value is to be cleared * * @param vakue - value to initialise with * * @param paramLen - length of the parameter * * @return none */ static void resetCharacteristicValue(uint16 servUuid, uint8 paramID, uint8 value, uint8 paramLen) { uint8* pData = osal_mem_alloc(paramLen); if (pData == NULL) { return; } osal_memset(pData,value,paramLen); switch(servUuid) { //case IRTEMPERATURE_SERV_UUID: // IRTemp_SetParameter( paramID, paramLen, pData); // break; case ACCELEROMETER_SERV_UUID: Accel_SetParameter( paramID, paramLen, pData); break; //case MAGNETOMETER_SERV_UUID: // Magnetometer_SetParameter( paramID, paramLen, pData); // break; //case HUMIDITY_SERV_UUID: // Humidity_SetParameter( paramID, paramLen, pData); // break; //case BAROMETER_SERV_UUID: // Barometer_SetParameter( paramID, paramLen, pData); // break; case GYROSCOPE_SERV_UUID: Gyro_SetParameter( paramID, paramLen, pData); break; default: // Should not get here break; } osal_mem_free(pData); }
/********************************************************************* * @fn readAccData * * @brief Read accelerometer data * * @param none * * @return none */ static void readAccData(void) { uint8 aData[ACCELEROMETER_DATA_LEN]; if (HalAccRead(aData)) { Accel_SetParameter( SENSOR_DATA, ACCELEROMETER_DATA_LEN, aData); } }
/********************************************************************* * @fn readGyroData * * @brief Read gyroscope data * * @param none * * @return none */ static void readGyroData( void ) { uint8 aData[ACCELEROMETER_DATA_LEN]; uint8 gData[GYROSCOPE_DATA_LEN]; //GAPRole_SendUpdateParam( DEFAULT_DESIRED_MIN_CONN_INTERVAL, DEFAULT_DESIRED_MAX_CONN_INTERVAL, DEFAULT_DESIRED_CONN_TIMEOUT, DEFAULT_DESIRED_CONN_TIMEOUT, GAPROLE_TERMINATE_LINK); if (HalGyroRead(aData,gData)) { Accel_SetParameter( ACCELEROMETER_DATA, ACCELEROMETER_DATA_LEN, aData); Gyro_SetParameter( GYROSCOPE_DATA, GYROSCOPE_DATA_LEN, gData); } }
/********************************************************************* * @fn peripheralStateNotificationCB * * @brief Notification from the profile of a state change. * * @param newState - new state * * @return none */ static void peripheralStateNotificationCB( gaprole_States_t newState ) { uint16 connHandle = INVALID_CONNHANDLE; uint8 valFalse = FALSE; if ( gapProfileState != newState ) { switch( newState ) { case GAPROLE_STARTED: { // Set the system ID from the bd addr uint8 systemId[DEVINFO_SYSTEM_ID_LEN]; GAPRole_GetParameter(GAPROLE_BD_ADDR, systemId); // shift three bytes up systemId[7] = systemId[5]; systemId[6] = systemId[4]; systemId[5] = systemId[3]; // set middle bytes to zero systemId[4] = 0; systemId[3] = 0; DevInfo_SetParameter(DEVINFO_SYSTEM_ID, DEVINFO_SYSTEM_ID_LEN, systemId); } break; //if the state changed to connected, initially assume that keyfob is in range case GAPROLE_ADVERTISING: { // Visual feedback that we are advertising. HalLedSet( HAL_LED_2, HAL_LED_MODE_ON ); } break; //if the state changed to connected, initially assume that keyfob is in range case GAPROLE_CONNECTED: { // set the proximity state to either path loss alert or in range depending // on the value of keyfobProxIMAlertLevel (which was set by proximity monitor) if( keyfobProxIMAlertLevel != PP_ALERT_LEVEL_NO ) { keyfobProximityState = KEYFOB_PROXSTATE_PATH_LOSS; // perform alert keyfobapp_PerformAlert(); buzzer_beep_count = 0; } else // if keyfobProxIMAlertLevel == PP_ALERT_LEVEL_NO { keyfobProximityState = KEYFOB_PROXSTATE_CONNECTED_IN_RANGE; keyfobapp_StopAlert(); } GAPRole_GetParameter( GAPROLE_CONNHANDLE, &connHandle ); #if defined ( PLUS_BROADCASTER ) osal_start_timerEx( keyfobapp_TaskID, KFD_ADV_IN_CONNECTION_EVT, ADV_IN_CONN_WAIT ); #endif // Turn off LED that shows we're advertising HalLedSet( HAL_LED_2, HAL_LED_MODE_OFF ); } break; case GAPROLE_WAITING: { // then the link was terminated intentionally by the slave or master, // or advertising timed out keyfobProximityState = KEYFOB_PROXSTATE_INITIALIZED; // Turn off immediate alert ProxReporter_SetParameter(PP_IM_ALERT_LEVEL, sizeof(valFalse), &valFalse); keyfobProxIMAlertLevel = PP_ALERT_LEVEL_NO; // Change attribute value of Accelerometer Enable to FALSE Accel_SetParameter(ACCEL_ENABLER, sizeof(valFalse), &valFalse); // Stop the acceleromter accelEnablerChangeCB(); // SetParameter does not trigger the callback // Turn off LED that shows we're advertising HalLedSet( HAL_LED_2, HAL_LED_MODE_OFF ); // Stop alert if it was active if( keyfobAlertState != ALERT_STATE_OFF ) { keyfobapp_StopAlert(); } } break; case GAPROLE_WAITING_AFTER_TIMEOUT: { // the link was dropped due to supervision timeout keyfobProximityState = KEYFOB_PROXSTATE_LINK_LOSS; // Turn off immediate alert ProxReporter_SetParameter(PP_IM_ALERT_LEVEL, sizeof(valFalse), &valFalse); keyfobProxIMAlertLevel = PP_ALERT_LEVEL_NO; // Change attribute value of Accelerometer Enable to FALSE Accel_SetParameter(ACCEL_ENABLER, sizeof(valFalse), &valFalse); // Stop the acceleromter accelEnablerChangeCB(); // SetParameter does not trigger the callback // Perform link loss alert if enabled if( keyfobProxLLAlertLevel != PP_ALERT_LEVEL_NO ) { keyfobapp_PerformAlert(); buzzer_beep_count = 0; } } break; default: // do nothing break; } } gapProfileState = newState; }