void Adc_entry(UArg a0, UArg a1) { uint8_t p = 0; uint8_t total = 0; Adc_init(); //1800 1560 1100 x y z // 1800 1200 while(1) { HeartRate_GetParameter(HEARTRATE_COMMAND, &p); if( p == 0) { valx = OneShotADC(ALS_AUX_X) * 43 / 33; valy = OneShotADC(ALS_AUX_Y) * 43 / 33; valz = OneShotADC(ALS_AUX_Z) * 43 / 33; ADXL335_setVal(valx, valy, valz); Svm = ADXL335_Filter(); if(total < 8) { total++; maxSvm = 0; } maxSvm = max(Svm, maxSvm); if( _ADXL335_IsDown( Svm ) ) { p = 1; HeartRate_SetParameter(HEARTRATE_COMMAND, 1, &p); }; Task_sleep(5); } else { Task_sleep(1000); } } }
/********************************************************************* * @fn HeartRate_Init * * @brief Initialization function for the Heart Rate App Task. * This is called during initialization and should contain * any application specific initialization (ie. hardware * initialization/setup, table initialization, power up * notificaiton ... ). * * @param task_id - the ID assigned by OSAL. This ID should be * used to send messages and set timers. * * @return none */ void HeartRate_Init( uint8 task_id ) { heartRate_TaskID = task_id; // Setup the GAP Peripheral Role Profile { // For the CC2540DK-MINI keyfob, device doesn't start advertising until button is pressed uint8 initial_advertising_enable = FALSE; // By setting this to zero, the device will go into the waiting state after // being discoverable for 30.72 second, and will not being advertising again // until the enabler is set back to TRUE uint16 gapRole_AdvertOffTime = 0; uint8 enable_update_request = DEFAULT_ENABLE_UPDATE_REQUEST; uint16 desired_min_interval = DEFAULT_DESIRED_MIN_CONN_INTERVAL; uint16 desired_max_interval = DEFAULT_DESIRED_MAX_CONN_INTERVAL; uint16 desired_slave_latency = DEFAULT_DESIRED_SLAVE_LATENCY; uint16 desired_conn_timeout = DEFAULT_DESIRED_CONN_TIMEOUT; // Set the GAP Role Parameters GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &initial_advertising_enable ); GAPRole_SetParameter( GAPROLE_ADVERT_OFF_TIME, sizeof( uint16 ), &gapRole_AdvertOffTime ); GAPRole_SetParameter( GAPROLE_SCAN_RSP_DATA, sizeof ( scanData ), scanData ); GAPRole_SetParameter( GAPROLE_ADVERT_DATA, sizeof( advertData ), advertData ); GAPRole_SetParameter( GAPROLE_PARAM_UPDATE_ENABLE, sizeof( uint8 ), &enable_update_request ); GAPRole_SetParameter( GAPROLE_MIN_CONN_INTERVAL, sizeof( uint16 ), &desired_min_interval ); GAPRole_SetParameter( GAPROLE_MAX_CONN_INTERVAL, sizeof( uint16 ), &desired_max_interval ); GAPRole_SetParameter( GAPROLE_SLAVE_LATENCY, sizeof( uint16 ), &desired_slave_latency ); GAPRole_SetParameter( GAPROLE_TIMEOUT_MULTIPLIER, sizeof( uint16 ), &desired_conn_timeout ); } // Set the GAP Characteristics GGS_SetParameter( GGS_DEVICE_NAME_ATT, GAP_DEVICE_NAME_LEN, attDeviceName ); // Setup the GAP Bond Manager { uint32 passkey = 0; // passkey "000000" uint8 pairMode = GAPBOND_PAIRING_MODE_WAIT_FOR_REQ; uint8 mitm = FALSE; uint8 ioCap = GAPBOND_IO_CAP_DISPLAY_ONLY; uint8 bonding = TRUE; GAPBondMgr_SetParameter( GAPBOND_DEFAULT_PASSCODE, sizeof ( uint32 ), &passkey ); GAPBondMgr_SetParameter( GAPBOND_PAIRING_MODE, sizeof ( uint8 ), &pairMode ); GAPBondMgr_SetParameter( GAPBOND_MITM_PROTECTION, sizeof ( uint8 ), &mitm ); GAPBondMgr_SetParameter( GAPBOND_IO_CAPABILITIES, sizeof ( uint8 ), &ioCap ); GAPBondMgr_SetParameter( GAPBOND_BONDING_ENABLED, sizeof ( uint8 ), &bonding ); } // Setup the Heart Rate Characteristic Values { uint8 sensLoc = HEARTRATE_SENS_LOC_WRIST; HeartRate_SetParameter( HEARTRATE_SENS_LOC, sizeof ( uint8 ), &sensLoc ); } // Setup Battery Characteristic Values { uint8 critical = DEFAULT_BATT_CRITICAL_LEVEL; Batt_SetParameter( BATT_PARAM_CRITICAL_LEVEL, sizeof (uint8 ), &critical ); } // Initialize GATT attributes GGS_AddService( GATT_ALL_SERVICES ); // GAP GATTServApp_AddService( GATT_ALL_SERVICES ); // GATT attributes HeartRate_AddService( GATT_ALL_SERVICES ); DevInfo_AddService( ); Batt_AddService( ); // Register for Heart Rate service callback HeartRate_Register( heartRateCB ); // Register for Battery service callback; Batt_Register ( heartRateBattCB ); // Register for all key events - This app will handle all key events RegisterForKeys( heartRate_TaskID ); // makes sure LEDs are off HalLedSet( (HAL_LED_1 | HAL_LED_2), HAL_LED_MODE_OFF ); // For keyfob board set GPIO pins into a power-optimized state // Note that there is still some leakage current from the buzzer, // accelerometer, LEDs, and buttons on the PCB. P0SEL = 0; // Configure Port 0 as GPIO P1SEL = 0; // Configure Port 1 as GPIO P2SEL = 0; // Configure Port 2 as GPIO P0DIR = 0xFC; // Port 0 pins P0.0 and P0.1 as input (buttons), // all others (P0.2-P0.7) as output P1DIR = 0xFF; // All port 1 pins (P1.0-P1.7) as output P2DIR = 0x1F; // All port 1 pins (P2.0-P2.4) as output P0 = 0x03; // All pins on port 0 to low except for P0.0 and P0.1 (buttons) P1 = 0; // All pins on port 1 to low P2 = 0; // All pins on port 2 to low // Setup a delayed profile startup osal_set_event( heartRate_TaskID, START_DEVICE_EVT ); }
/********************************************************************* * @fn HeartRate_init * * @brief Initialization function for the Heart Rate application thread. * This is called during initialization and should contain * any application specific initialization (ie. hardware * initialization/setup, table initialization, power up * notification ...). * * @param none * * @return none */ void HeartRate_init(void) { // ****************************************************************** // N0 STACK API CALLS CAN OCCUR BEFORE THIS CALL TO ICall_registerApp // ****************************************************************** // Register the current thread as an ICall dispatcher application // so that the application can send and receive messages. ICall_registerApp(&selfEntity, &sem); // Hard code the DB Address till CC2650 board gets its own IEEE address. //uint8_t bdAddress[B_ADDR_LEN] = { 0x22, 0x22, 0x22, 0x22, 0x22, 0x22 }; //HCI_EXT_SetBDADDRCmd(bdAddress); // Set device's Sleep Clock Accuracy //HCI_EXT_SetSCACmd(40); // Create an RTOS queue for message from profile to be sent to app. appMsgQueue = Util_constructQueue(&appMsg); // Create one-shot clocks for internal periodic events. Util_constructClock(&measPerClock, HeartRate_clockHandler, DEFAULT_HEARTRATE_PERIOD, 0, false, HEARTRATE_MEAS_PERIODIC_EVT); Util_constructClock(&battPerClock, HeartRate_clockHandler, DEFAULT_BATT_PERIOD, 0, false, HEARTRATE_BATT_PERIODIC_EVT); // Initialize keys on SRF06. Board_initKeys(HeartRate_keyPressHandler); // Setup the GAP Peripheral Role Profile. { #if ADVERTISE_WHEN_NOT_CONNECTED uint8_t initial_advertising_enable = TRUE; #else // Press right key to initiate advertising and measurement. uint8_t initial_advertising_enable = FALSE; #endif //ADVERTISE_WHEN_NOT_CONNECTED // By setting this to zero, the device will go into the waiting state after // being discoverable for 30.72 second, and will not being advertising again // until the enabler is set back to TRUE. uint16_t gapRole_AdvertOffTime = 0; uint8_t enable_update_request = DEFAULT_ENABLE_UPDATE_REQUEST; uint16_t desired_min_interval = DEFAULT_DESIRED_MIN_CONN_INTERVAL; uint16_t desired_max_interval = DEFAULT_DESIRED_MAX_CONN_INTERVAL; uint16_t desired_slave_latency = DEFAULT_DESIRED_SLAVE_LATENCY; uint16_t desired_conn_timeout = DEFAULT_DESIRED_CONN_TIMEOUT; // Set the GAP Role Parameters. GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &initial_advertising_enable); GAPRole_SetParameter(GAPROLE_ADVERT_OFF_TIME, sizeof(uint16_t), &gapRole_AdvertOffTime); GAPRole_SetParameter(GAPROLE_SCAN_RSP_DATA, sizeof (scanData), scanData); GAPRole_SetParameter(GAPROLE_ADVERT_DATA, sizeof(advertData), advertData); GAPRole_SetParameter(GAPROLE_PARAM_UPDATE_ENABLE, sizeof(uint8_t), &enable_update_request); GAPRole_SetParameter(GAPROLE_MIN_CONN_INTERVAL, sizeof(uint16_t), &desired_min_interval); GAPRole_SetParameter(GAPROLE_MAX_CONN_INTERVAL, sizeof(uint16_t), &desired_max_interval); GAPRole_SetParameter(GAPROLE_SLAVE_LATENCY, sizeof(uint16_t), &desired_slave_latency); GAPRole_SetParameter(GAPROLE_TIMEOUT_MULTIPLIER, sizeof(uint16_t), &desired_conn_timeout); } // Set the GAP Characteristics. GGS_SetParameter(GGS_DEVICE_NAME_ATT, GAP_DEVICE_NAME_LEN, attDeviceName); // Setup the GAP Bond Manager. { uint32 passkey = 0; // passkey "000000" uint8_t pairMode = GAPBOND_PAIRING_MODE_WAIT_FOR_REQ; uint8_t mitm = FALSE; uint8_t ioCap = GAPBOND_IO_CAP_DISPLAY_ONLY; uint8_t bonding = TRUE; GAPBondMgr_SetParameter(GAPBOND_DEFAULT_PASSCODE, sizeof (uint32), &passkey); GAPBondMgr_SetParameter(GAPBOND_PAIRING_MODE, sizeof (uint8_t), &pairMode); GAPBondMgr_SetParameter(GAPBOND_MITM_PROTECTION, sizeof (uint8_t), &mitm); GAPBondMgr_SetParameter(GAPBOND_IO_CAPABILITIES, sizeof (uint8_t), &ioCap); GAPBondMgr_SetParameter(GAPBOND_BONDING_ENABLED, sizeof (uint8_t), &bonding); } // Initialize GATT attributes. GGS_AddService(GATT_ALL_SERVICES); // GAP GATTServApp_AddService(GATT_ALL_SERVICES); // GATT attributes // Add heart rate service. HeartRate_AddService(GATT_ALL_SERVICES); // Add device info service. DevInfo_AddService(); // Add battery service. Batt_AddService(); // Setup the Heart Rate Characteristic Values. { uint8_t sensLoc = HEARTRATE_SENS_LOC_WRIST; HeartRate_SetParameter(HEARTRATE_SENS_LOC, sizeof (uint8_t), &sensLoc); } // Setup Battery Characteristic Values. { uint8_t critical = DEFAULT_BATT_CRITICAL_LEVEL; Batt_SetParameter(BATT_PARAM_CRITICAL_LEVEL, sizeof(uint8_t), &critical); } // Register for Heart Rate service callback. HeartRate_Register(&HeartRate_serviceCB); // Register for Battery service callback. Batt_Register (&HeartRate_battCB); // Start the Device. GAPRole_StartDevice(&heartRatePeripheralCB); // Start the Bond Manager. GAPBondMgr_Register((gapBondCBs_t *)&heartRateBondCB); }
/********************************************************************* * @fn HeartRateGapStateCB * * @brief Notification from the profile of a state change. * * @param newState - new state * * @return none */ static void HeartRateGapStateCB( gaprole_States_t newState ) { // if connected if (newState == GAPROLE_CONNECTED) { // get connection handle GAPRole_GetParameter(GAPROLE_CONNHANDLE, &gapConnHandle); } // if disconnected else if (gapProfileState == GAPROLE_CONNECTED && newState != GAPROLE_CONNECTED) { uint8 advState = TRUE; // stop periodic measurement osal_stop_timerEx( heartRate_TaskID, HEART_PERIODIC_EVT ); // reset client characteristic configuration descriptors uint16 param = GATT_CFG_NO_OPERATION; HeartRate_SetParameter(HEARTRATE_MEAS_CHAR_CFG, sizeof(uint16), (uint8 *) ¶m); Batt_SetParameter(BATT_PARAM_LVL_ST_CHAR_CFG, sizeof(uint16), (uint8 *) ¶m); if ( newState == GAPROLE_WAITING_AFTER_TIMEOUT ) { // link loss timeout-- use fast advertising GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MIN, DEFAULT_FAST_ADV_INTERVAL ); GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MAX, DEFAULT_FAST_ADV_INTERVAL ); GAP_SetParamValue( TGAP_GEN_DISC_ADV_MIN, DEFAULT_FAST_ADV_DURATION ); } else { // Else use slow advertising GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MIN, DEFAULT_SLOW_ADV_INTERVAL ); GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MAX, DEFAULT_SLOW_ADV_INTERVAL ); GAP_SetParamValue( TGAP_GEN_DISC_ADV_MIN, DEFAULT_SLOW_ADV_DURATION ); } // Enable advertising GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &advState ); } // if advertising stopped else if ( gapProfileState == GAPROLE_ADVERTISING && newState == GAPROLE_WAITING ) { // if advertising stopped by user if ( heartRateAdvCancelled ) { heartRateAdvCancelled = FALSE; } // if fast advertising switch to slow else if ( GAP_GetParamValue( TGAP_GEN_DISC_ADV_INT_MIN ) == DEFAULT_FAST_ADV_INTERVAL ) { uint8 advState = TRUE; GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MIN, DEFAULT_SLOW_ADV_INTERVAL ); GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MAX, DEFAULT_SLOW_ADV_INTERVAL ); GAP_SetParamValue( TGAP_GEN_DISC_ADV_MIN, DEFAULT_SLOW_ADV_DURATION ); GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &advState ); } } // if started else if (newState == 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); } gapProfileState = newState; }