/********************************************************************* * @fn HeartRate_battPerTask * * @brief Perform a periodic task for battery measurement. * * @param none * * @return none */ static void HeartRate_battPerTask(void) { if (gapProfileState == GAPROLE_CONNECTED) { // Perform battery level check. Batt_MeasLevel(); // Restart timer. Util_startClock(&battPerClock); } }
/********************************************************************* * @fn hidDevBattPeriodicTask * * @brief Perform a periodic task for battery measurement. * * @param none * * @return none */ static void hidDevBattPeriodicTask( void ) { if ( hidDevGapState == GAPROLE_CONNECTED ) { // perform battery level check Batt_MeasLevel( ); // Restart timer osal_start_timerEx( hidDevTaskId, BATT_PERIODIC_EVT, DEFAULT_BATT_PERIOD ); } }
/********************************************************************* * @fn heartRateBattPeriodicTask * * @brief Perform a periodic task for battery measurement. * * @param none * * @return none */ static void heartRateBattPeriodicTask( void ) { if (gapProfileState == GAPROLE_CONNECTED) { // perform battery level check Batt_MeasLevel( ); // Restart timer osal_start_timerEx( heartRate_TaskID, BATT_PERIODIC_EVT, DEFAULT_BATT_PERIOD ); } }
/********************************************************************* * @fn performPeriodicTask * * @brief Perform a periodic application task. This function gets * called every five seconds as a result of the SBP_PERIODIC_EVT * OSAL event. In this example, the value of the third * characteristic in the SimpleGATTProfile service is retrieved * from the profile, and then copied into the value of the * the fourth characteristic. * * @param none * * @return none */ static void performPeriodicTask( void ) { uint8 pos = FALSE, txpwr = 0; Batt_MeasLevel( ); GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8), &pos); advertData[11] = Battery2Asc(battLevel); advertData[12] = (P0 & 1<<4) ? 0x31 : 0x30; advertData[13] = (P0 & 1<<5) ? 0x31 : 0x30; advertData[14] = (P0 & 1<<6) ? 0x31 : 0x30; advertData[15] = '0' + advert_internal/100; advertData[16] = '0' + (advert_internal/10)%10; advertData[17] = '0' + advert_internal%10; ProxReporter_GetParameter(PP_TX_POWER_LEVEL, &txpwr); advertData[18] = txpwr + '0'; HCI_LE_SetAdvDataCmd( sizeof( advertData ), advertData ); pos = TRUE; GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8), &pos); }
/********************************************************************* * @fn BattPeriodicTask * * @brief Perform a periodic task for battery measurement. * * @param none * * @return none */ static void BattPeriodicTask( void ) { if (gapProfileState == GAPROLE_CONNECTED) { // perform battery level check Batt_MeasLevel( ); // Restart timer // osal_start_timerEx( ECGSensor_TaskID, BATT_PERIODIC_EVT, DEFAULT_BATT_PERIOD ); } else { // // perform battery level check // Batt_MeasLevel( ); // // // Restart timer // osal_start_timerEx( ECGSensor_TaskID, BATT_PERIODIC_EVT, DEFAULT_BATT_PERIOD ); // // HalUARTWrite(HAL_UART_PORT_1,"100%", 4); // } }
/********************************************************************* * @fn Thermometer_ProcessEvent * * @brief Thermometer Application Task event processor. This function * is called to process all events for the task. Events * include timers, messages and any other user defined events. * * @param task_id - The OSAL assigned task ID. * @param events - events to process. This is a bit map and can * contain more than one event. * * @return events not processed */ uint16 Thermometer_ProcessEvent(uint8 task_id, uint16 events) { struct ther_info *ti = &ther_info; /* message handle */ if ( events & SYS_EVENT_MSG ) { uint8 *msg; if ( (msg = osal_msg_receive(ti->task_id)) != NULL ) { ther_dispatch_msg(ti, (osal_event_hdr_t *)msg); osal_msg_deallocate( msg ); } return (events ^ SYS_EVENT_MSG); } if (events & TH_POWER_ON_EVT) { ther_system_power_on(ti); return (events ^ TH_POWER_ON_EVT); } if (events & TH_POWER_OFF_EVT) { ther_system_power_off_post(ti); return (events ^ TH_POWER_OFF_EVT); } if (events & TH_AUTO_POWER_OFF_EVT) { if (!ti->ble_connect && ((ti->previous_temp >= ti->temp_current && ti->previous_temp - ti->temp_current < 50) || (ti->previous_temp < ti->temp_current && ti->temp_current - ti->previous_temp < 50))) { ti->same_temp_number++; } else { ti->same_temp_number = 0; // ti->previous_temp = ti->temp_current; } print(LOG_DBG, MODULE "auto power off: same_temp_number %d\n", ti->same_temp_number); if (ti->same_temp_number >= AUTO_POWER_OFF_NUMBER_THRESHOLD) { print(LOG_DBG, MODULE "auto power off\n"); ther_system_power_off_pre(ti); } else { osal_start_timerEx( ti->task_id, TH_AUTO_POWER_OFF_EVT, AUTO_POWER_OFF_MEASURE_INTERVAL); } return (events ^ TH_AUTO_POWER_OFF_EVT); } /* batt measure */ if (events & TH_BATT_EVT) { if (ti->mode == NORMAL_MODE) { Batt_MeasLevel(); ti->batt_percentage = ther_batt_get_percentage(FALSE); oled_update_picture(OLED_CONTENT_BATT, TRUE, ti->batt_percentage); print(LOG_DBG, MODULE "batt %d%%\n", ti->batt_percentage); if (!ti->batt_warning_on && ti->batt_percentage < 10) { ti->batt_warning_on = TRUE; osal_start_timerEx( ti->task_id, TH_LOW_BATT_WARNING_EVT, LOW_BATT_WARNING_INTERVAL); } osal_start_timerEx( ti->task_id, TH_BATT_EVT, BATT_MEASURE_INTERVAL); } return (events ^ TH_BATT_EVT); } if (events & TH_LOW_BATT_BLINK_EVT) { if (ti->batt_in_dispaly) { oled_update_picture(OLED_CONTENT_BATT, FALSE, 0); ti->batt_in_dispaly = FALSE; } else { oled_update_picture(OLED_CONTENT_BATT, TRUE, ti->batt_percentage); ti->batt_in_dispaly = TRUE; } if (ti->display_picture != OLED_PICTURE_NONE) osal_start_timerEx(ti->task_id, TH_LOW_BATT_BLINK_EVT, LOW_BATT_BLINK_INTERVAL); return (events ^ TH_LOW_BATT_BLINK_EVT); } if (events & TH_LOW_BATT_WARNING_EVT) { ther_buzzer_start_music(BUZZER_MUSIC_LOW_BATT_WARNING); if (ti->batt_percentage < LOW_BATT_WARNING_THRESHOLD) osal_start_timerEx( ti->task_id, TH_LOW_BATT_WARNING_EVT, LOW_BATT_WARNING_INTERVAL); else ti->batt_warning_on = FALSE; return (events ^ TH_LOW_BATT_WARNING_EVT); } /* temp measure event */ if (events & TH_TEMP_MEASURE_EVT) { if (ti->mode != NORMAL_MODE) { return (events ^ TH_TEMP_MEASURE_EVT); } switch (ti->temp_measure_stage) { case TEMP_STAGE_SETUP: ther_temp_power_on(); osal_start_timerEx( ti->task_id, TH_TEMP_MEASURE_EVT, TEMP_POWER_SETUP_TIME); ti->temp_measure_stage = TEMP_STAGE_MEASURE; break; case TEMP_STAGE_MEASURE: ti->temp_last_saved = ti->temp_current; ti->temp_current = (int16)(ther_read_temp() * 100 + 0.5); ther_temp_power_off(); /* for auto power off, save only once */ if (ti->previous_temp == 0) ti->previous_temp = ti->temp_current; if (ti->temp_max < ti->temp_current) { ti->temp_max = ti->temp_current; print(LOG_DBG, MODULE "update max temp to %d\n", ti->temp_max); oled_update_picture(OLED_CONTENT_MAX_TEMP, TRUE, ti->temp_max); } /* high temp warning */ if (ti->warning_enabled && !ti->temp_warning_on && ti->temp_current >= ti->next_warning_threshold) { ti->temp_warning_on = TRUE; osal_start_timerEx( ti->task_id, TH_HIGH_TEMP_WARNING_EVT, HIGH_TEMP_WARNING_INTERVAL); } if (ti->temp_warning_on && ti->temp_current < ti->next_warning_threshold) { ti->next_warning_threshold -= 50; /* 0.5 C */ if (ti->next_warning_threshold < ti->high_temp_threshold) ti->next_warning_threshold = ti->high_temp_threshold; ti->temp_warning_on = FALSE; osal_stop_timerEx(ti->task_id, TH_HIGH_TEMP_WARNING_EVT); } if (ti->ble_connect) { if (ti->temp_notification_enable) { // ther_send_temp_notify(ti->temp_current); } if (ti->temp_indication_enable) { ther_send_temp_indicate(ti->task_id, ti->temp_current); } } else { ther_save_temp_to_local(ti->temp_current); } if (ti->temp_current != ti->temp_last_saved) { oled_update_picture(OLED_CONTENT_TEMP, TRUE, ti->temp_current); } osal_start_timerEx( ti->task_id, TH_TEMP_MEASURE_EVT, ti->temp_measure_interval); ti->temp_measure_stage = TEMP_STAGE_SETUP; break; default: break; } return (events ^ TH_TEMP_MEASURE_EVT); } if (events & TH_HIGH_TEMP_WARNING_EVT) { ther_buzzer_start_music(BUZZER_MUSIC_HIGH_TEMP_WARNING); osal_start_timerEx( ti->task_id, TH_HIGH_TEMP_WARNING_EVT, HIGH_TEMP_WARNING_INTERVAL); return (events ^ TH_HIGH_TEMP_WARNING_EVT); } if (events & TH_HIS_TEMP_RESTORE_EVT) { if (!ti->his_temp_bundle) { if (ti->temp_notification_enable) { storage_restore_temp((uint8 **)&ti->his_temp_bundle, &ti->his_temp_len); if (ti->his_temp_bundle) { ti->his_temp_offset = 0; osal_start_timerEx(ti->task_id, TH_HIS_TEMP_RESTORE_EVT, HIS_TEMP_UPLOADING_INTERVAL); } else { print(LOG_DBG, MODULE "his temp restore: no more his temp, exit\n"); ti->his_temp_uploading = FALSE; } } else if (!ti->ble_connect) { print(LOG_DBG, MODULE "his temp restore: ble disconnect, exit\n"); ti->his_temp_uploading = FALSE; } else { // print(LOG_DBG, MODULE "his temp restore: wait for notify enable\n"); osal_start_timerEx(ti->task_id, TH_HIS_TEMP_RESTORE_EVT, HIS_TEMP_RESTORE_WAIT_ENABLE); } } else { if (ti->his_temp_offset < ti->his_temp_len) { uint8 *data = ti->his_temp_bundle + ti->his_temp_offset; ther_send_history_temp(ti->task_id, data, sizeof(struct temp_data)); ti->his_temp_offset += sizeof(struct temp_data); } else { ti->his_temp_bundle = NULL; ti->his_temp_offset = 0; ti->his_temp_len = 0; print(LOG_DBG, MODULE "his temp restore: a bundle uploading completed\n"); } osal_start_timerEx(ti->task_id, TH_HIS_TEMP_RESTORE_EVT, HIS_TEMP_UPLOADING_INTERVAL); } return (events ^ TH_HIS_TEMP_RESTORE_EVT); } /* Display event */ if (events & TH_DISPLAY_EVT) { oled_display_state_machine(); return (events ^ TH_DISPLAY_EVT); } /* buzzer event */ if (events & TH_BUZZER_EVT) { ther_buzzer_playing_music(); return (events ^ TH_BUZZER_EVT); } /* button event */ if (events & TH_BUTTON_EVT) { ther_measure_button_time(); return (events ^ TH_BUTTON_EVT); } if (events & TH_WATCHDOG_EVT) { feed_watchdog(); osal_start_timerEx(ti->task_id, TH_WATCHDOG_EVT, WATCHDOG_FEED_INTERVAL); return (events ^ TH_WATCHDOG_EVT); } if (events & TH_TEST_EVT) { { struct display_param param; if (ti->display_picture > OLED_PICTURE_NONE) { print(LOG_DBG, MODULE "ignore button press when picture is %d\n", ti->display_picture); return (events ^ TH_TEST_EVT); } encap_picture_param(ti, ¶m); if (ti->display_picture == OLED_PICTURE_NONE) { oled_show_picture(OLED_PICTURE1, DISPLAY_TIME, ¶m); } else { oled_show_next_picture(DISPLAY_TIME, ¶m); } } osal_start_timerEx(ti->task_id, TH_TEST_EVT, SEC_TO_MS(10 * 60)); return (events ^ TH_TEST_EVT); } return 0; }
/********************************************************************* * @fn KeyFobApp_ProcessEvent * * @brief Key Fob Application Task event processor. This function * is called to process all events for the task. Events * include timers, messages and any other user defined events. * * @param task_id - The OSAL assigned task ID. * @param events - events to process. This is a bit map and can * contain more than one event. * * @return none */ uint16 KeyFobApp_ProcessEvent( uint8 task_id, uint16 events ) { if ( events & SYS_EVENT_MSG ) { uint8 *pMsg; if ( (pMsg = osal_msg_receive( keyfobapp_TaskID )) != NULL ) { keyfobapp_ProcessOSALMsg( (osal_event_hdr_t *)pMsg ); // Release the OSAL message VOID osal_msg_deallocate( pMsg ); } // return unprocessed events return (events ^ SYS_EVENT_MSG); } if ( events & KFD_START_DEVICE_EVT ) { // Start the Device VOID GAPRole_StartDevice( &keyFob_PeripheralCBs ); // Start Bond Manager VOID GAPBondMgr_Register( &keyFob_BondMgrCBs ); // Start the Proximity Profile VOID ProxReporter_RegisterAppCBs( &keyFob_ProximityCBs ); // Set timer for first battery read event osal_start_timerEx( keyfobapp_TaskID, KFD_BATTERY_CHECK_EVT, BATTERY_CHECK_PERIOD ); // Start the Accelerometer Profile VOID Accel_RegisterAppCBs( &keyFob_AccelCBs ); //Set the proximity attribute values to default ProxReporter_SetParameter( PP_LINK_LOSS_ALERT_LEVEL, sizeof ( uint8 ), &keyfobProxLLAlertLevel ); ProxReporter_SetParameter( PP_IM_ALERT_LEVEL, sizeof ( uint8 ), &keyfobProxIMAlertLevel ); ProxReporter_SetParameter( PP_TX_POWER_LEVEL, sizeof ( int8 ), &keyfobProxTxPwrLevel ); // Set LED1 on to give feedback that the power is on, and a timer to turn off HalLedSet( HAL_LED_1, HAL_LED_MODE_ON ); osal_pwrmgr_device( PWRMGR_ALWAYS_ON ); // To keep the LED on continuously. osal_start_timerEx( keyfobapp_TaskID, KFD_POWERON_LED_TIMEOUT_EVT, 1000 ); return ( events ^ KFD_START_DEVICE_EVT ); } if ( events & KFD_POWERON_LED_TIMEOUT_EVT ) { osal_pwrmgr_device( PWRMGR_BATTERY ); // Revert to battery mode after LED off HalLedSet( HAL_LED_1, HAL_LED_MODE_OFF ); return ( events ^ KFD_POWERON_LED_TIMEOUT_EVT ); } if ( events & KFD_ACCEL_READ_EVT ) { bStatus_t status = Accel_GetParameter( ACCEL_ENABLER, &accelEnabler ); if (status == SUCCESS) { if ( accelEnabler ) { // Restart timer if ( ACCEL_READ_PERIOD ) { osal_start_timerEx( keyfobapp_TaskID, KFD_ACCEL_READ_EVT, ACCEL_READ_PERIOD ); } // Read accelerometer data accelRead(); } else { // Stop the acceleromter osal_stop_timerEx( keyfobapp_TaskID, KFD_ACCEL_READ_EVT); } } else { //?? } return (events ^ KFD_ACCEL_READ_EVT); } if ( events & KFD_BATTERY_CHECK_EVT ) { // Restart timer if ( BATTERY_CHECK_PERIOD ) { osal_start_timerEx( keyfobapp_TaskID, KFD_BATTERY_CHECK_EVT, BATTERY_CHECK_PERIOD ); } // perform battery level check Batt_MeasLevel( ); return (events ^ KFD_BATTERY_CHECK_EVT); } if ( events & KFD_TOGGLE_BUZZER_EVT ) { // if this event was triggered while buzzer is on, turn it off, increment beep_count, // check whether max has been reached, and if not set the OSAL timer for next event to // turn buzzer back on. if ( buzzer_state == BUZZER_ON ) { buzzerStop(); buzzer_state = BUZZER_OFF; buzzer_beep_count++; #if defined ( POWER_SAVING ) osal_pwrmgr_device( PWRMGR_BATTERY ); #endif // check to see if buzzer has beeped maximum number of times // if it has, then don't turn it back on if ( ( buzzer_beep_count < BUZZER_MAX_BEEPS ) && ( ( keyfobProximityState == KEYFOB_PROXSTATE_LINK_LOSS ) || ( keyfobProximityState == KEYFOB_PROXSTATE_PATH_LOSS ) ) ) { osal_start_timerEx( keyfobapp_TaskID, KFD_TOGGLE_BUZZER_EVT, 800 ); } } else if ( keyfobAlertState != ALERT_STATE_OFF ) { // if this event was triggered while the buzzer is off then turn it on if appropriate keyfobapp_PerformAlert(); } return (events ^ KFD_TOGGLE_BUZZER_EVT); } #if defined ( PLUS_BROADCASTER ) if ( events & KFD_ADV_IN_CONNECTION_EVT ) { uint8 turnOnAdv = TRUE; // Turn on advertising while in a connection GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &turnOnAdv ); } #endif // Discard unknown events return 0; }
/********************************************************************* * @fn SimpleBLEPeripheral_ProcessEvent * * @brief Simple BLE Peripheral Application Task event processor. This function * is called to process all events for the task. Events * include timers, messages and any other user defined events. * * @param task_id - The OSAL assigned task ID. * @param events - events to process. This is a bit map and can * contain more than one event. * * @return events not processed */ uint16 SimpleBLEPeripheral_ProcessEvent( uint8 task_id, uint16 events ) { VOID task_id; // OSAL required parameter that isn't used in this function if ( events & SYS_EVENT_MSG ) { uint8 *pMsg; if ( (pMsg = osal_msg_receive( simpleBLEPeripheral_TaskID )) != NULL ) { simpleBLEPeripheral_ProcessOSALMsg( (osal_event_hdr_t *)pMsg ); // Release the OSAL message VOID osal_msg_deallocate( pMsg ); } // return unprocessed events return (events ^ SYS_EVENT_MSG); } if ( events & SBP_START_DEVICE_EVT ) { // Start the Device VOID GAPRole_StartDevice( &simpleBLEPeripheral_PeripheralCBs ); // Start Bond Manager VOID GAPBondMgr_Register( &simpleBLEPeripheral_BondMgrCBs ); // sys_config.pwm[0] = 0; //blue // sys_config.pwm[1] = 0;//green // sys_config.pwm[2] = 0;//red // sys_config.pwm[3] = 0;//white ti_pwm_init(2500, sys_config.pwm[0], sys_config.pwm[1], sys_config.pwm[2], sys_config.pwm[3]); osal_start_timerEx( simpleBLEPeripheral_TaskID, SBP_SET_PWM_EVT, 100 ); //if(g_initFlag == FALSE) { // 进行自测 SBP_SELF_TEST_EVT osal_start_timerEx( simpleBLEPeripheral_TaskID, SBP_SELF_TEST_EVT, 200 ); } return ( events ^ SBP_START_DEVICE_EVT ); } if ( events & SBP_SELF_TEST_EVT ) { static uint8 count = 0; if(count%3 == 0) { sys_config.pwm[3] = 0; // 白色 sys_config.pwm[2] = 0; // 红色 sys_config.pwm[1] = 0; // 绿色 sys_config.pwm[0] = 255; // 蓝色 } else if(count%3 == 1) { sys_config.pwm[3] = 0; // 白色 sys_config.pwm[2] = 0; // 红色 sys_config.pwm[1] = 255; // 绿色 sys_config.pwm[0] = 0; // 蓝色 } else if(count%3 == 2) { sys_config.pwm[3] = 0; // 白色 sys_config.pwm[2] = 255; // 红色 sys_config.pwm[1] = 0; // 绿色 sys_config.pwm[0] = 0; // 蓝色 } count++; if(count < 10) { osal_set_event( simpleBLEPeripheral_TaskID, SBP_SET_PWM_EVT); osal_start_timerEx( simpleBLEPeripheral_TaskID, SBP_SELF_TEST_EVT, 800 ); } return (events ^ SBP_SELF_TEST_EVT); } if ( events & SBP_PERIODIC_EVT ) { // Restart timer if ( SBP_PERIODIC_EVT_PERIOD ) { } // Perform periodic application task //performPeriodicTask(); return (events ^ SBP_PERIODIC_EVT); } if ( events & SBP_READ_SENSOR_EVT ) { return (events ^ SBP_READ_SENSOR_EVT); } if ( events & KFD_BATTERY_CHECK_EVT ) { // Restart timer if ( BATTERY_CHECK_PERIOD ) { osal_start_timerEx( simpleBLEPeripheral_TaskID, KFD_BATTERY_CHECK_EVT, BATTERY_CHECK_PERIOD ); } // perform battery level check Batt_MeasLevel( ); return (events ^ KFD_BATTERY_CHECK_EVT); } if ( events & SBP_SET_PWM_EVT ) { ti_pwm_set_period(PWM1_P11, sys_config.pwm[0], 300); ti_pwm_set_period(PWM2_P10, sys_config.pwm[1], 300); ti_pwm_set_period(PWM3_P07, sys_config.pwm[2], 300); ti_pwm_set_period(PWM4_P06, sys_config.pwm[3], 300); return (events ^ SBP_SET_PWM_EVT); } #if defined ( PLUS_BROADCASTER ) if ( events & SBP_ADV_IN_CONNECTION_EVT ) { uint8 turnOnAdv = TRUE; // Turn on advertising while in a connection GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &turnOnAdv ); return (events ^ SBP_ADV_IN_CONNECTION_EVT); } #endif // PLUS_BROADCASTER // Discard unknown events return 0; }