/*************************************************************************************************** * @fn HalLedSet * * @brief Tun ON/OFF/TOGGLE given LEDs * * @param led - bit mask value of leds to be turned ON/OFF/TOGGLE * mode - BLINK, FLASH, TOGGLE, ON, OFF * @return None ***************************************************************************************************/ uint8 HalLedSet ( uint8 leds, uint8 mode ) { #if (defined (BLINK_LEDS)) && (HAL_LED == TRUE) uint8 led; HalLedControl_t *sts; switch (mode) { case HAL_LED_MODE_BLINK: // Default blink, 1 time, D% duty cycle HalLedBlink( leds, 1, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME ); break; case HAL_LED_MODE_FLASH: // Default flash, N times, D% duty cycle HalLedBlink( leds, HAL_LED_DEFAULT_FLASH_COUNT, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME ); break; case HAL_LED_MODE_ON: case HAL_LED_MODE_OFF: case HAL_LED_MODE_TOGGLE: led = HAL_LED_1; leds &= HAL_LED_ALL; sts = HalLedStatusTable; while ( leds ) { if ( leds & led ) { if ( mode != HAL_LED_MODE_TOGGLE ) sts->mode = mode; // ON or OFF else sts->mode ^= HAL_LED_MODE_ON; // Toggle HalLedOnOff( led, sts->mode ); leds ^= led; } led <<= 1; sts++; } break; default: break; } #elif (HAL_LED == TRUE) HalLedOnOff( leds, mode ); #endif /* BLINK_LEDS && HAL_LED */ return ( ledState ); }
/***************************************************************************** * @fn zb_HandleOsalEvent * * @brief The zb_HandleOsalEvent function is called by the operating * system when a task event is set * * @param event - Bitmask containing the events that have been set * * @return none */ void zb_HandleOsalEvent( uint16 event ) { if(event & SYS_EVENT_MSG) { } if( event & ZB_ENTRY_EVENT ) { // blind LED 1 to indicate joining a network #ifndef SYS_DEBUG_SH HalLedBlink ( HAL_LED_1, 0, 50, 500 ); #else HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON); #endif // Start the device zb_StartRequest(); } if ( event & MY_REPORT_EVT ) { if ( appState == APP_REPORT ) { sendReport(); osal_start_timerEx( sapi_TaskID, MY_REPORT_EVT, myReportPeriod ); } } if ( event & MY_FIND_COLLECTOR_EVT ) { // Delete previous binding if ( appState==APP_REPORT ) { zb_BindDevice( FALSE, SENSOR_REPORT_CMD_ID, (uint8 *)NULL ); } appState = APP_BIND; // blind LED 2 to indicate discovery and binding #ifndef SYS_DEBUG_SH HalLedBlink ( HAL_LED_2, 0, 50, 500 ); #else HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON); #endif // Find and bind to a collector device zb_BindDevice( TRUE, SENSOR_REPORT_CMD_ID, (uint8 *)NULL ); } }
bool CurrentDetectionT1_SendHeartBeatMessage(uint8* sendToG1Data) { SET_HEART_BIT_STATUS(sendToG1Data , HEART_BIT_STATUS_REALTIME_MASK, 1); if (nv_number() || nv_mem_number()) { SET_HEART_BIT_STATUS(sendToG1Data, HEART_BIT_STATUS_RECOVER_MASK, 1); } else { SET_HEART_BIT_STATUS(sendToG1Data , HEART_BIT_STATUS_RECOVER_MASK, 0); } ShowHeartBeatInfo(sendToG1Data); ShowRssiInfo(); if (AF_DataRequest( &CurrentDetectionT1_Periodic_DstAddr, &CurrentDetectionT1_epDesc, DTCT_PERIODIC_CLUSTERID, HEART_BIT_MSG_LEN, sendToG1Data, &CurrentDetectionT1_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS) == afStatus_SUCCESS) { HalLedBlink(HAL_LED_2, 2, 20, 50); return true; } return false; }
void writeUart(uint8 whichport, cmd_msg_t* command) { if ( HalUARTWrite ( whichport, command->controlmsg, command->length)) { HalLedBlink( HAL_LED_1, 2, 25, 50); } }
/****************************************************************************** * @fn zb_HandleOsalEvent * * @brief The zb_HandleOsalEvent function is called by the operating * system when a task event is set * * @param event - Bitmask containing the events that have been set * * @return none */ void zb_HandleOsalEvent( uint16 event ) { uint8 logicalType; if(event & SYS_EVENT_MSG) { } if( event & ZB_ENTRY_EVENT ) { // Initialise UART initUart(uartRxCB); // blind LED 1 to indicate starting/joining a network #ifndef SYS_DEBUG_SH HalLedBlink ( HAL_LED_1, 0, 50, 500 ); #endif HalLedSet( HAL_LED_2, HAL_LED_MODE_OFF ); // Read logical device type from NV zb_ReadConfiguration(ZCD_NV_LOGICAL_TYPE, sizeof(uint8), &logicalType); // Start the device zb_StartRequest(); #ifdef SYS_DEBUG_SH // Turn ON Allow Bind mode infinitly zb_AllowBind( 0xFF ); HalLedSet( HAL_LED_2, HAL_LED_MODE_ON ); #endif } if ( event & MY_START_EVT ) { zb_StartRequest(); } if ( event & MY_REPORT_EVT ) { if (isGateWay) { osal_start_timerEx( sapi_TaskID, MY_REPORT_EVT, myReportPeriod ); } else if (appState == APP_BINDED) { sendDummyReport(); osal_start_timerEx( sapi_TaskID, MY_REPORT_EVT, myReportPeriod ); } } if ( event & MY_FIND_COLLECTOR_EVT ) { // Find and bind to a gateway device (if this node is not gateway) if (!isGateWay) { zb_BindDevice( TRUE, DUMMY_REPORT_CMD_ID, (uint8 *)NULL ); } } }
/********************************************************************* * @fn pct_LoadControlEventCB * * @brief Callback from the ZCL SE Profile Load Contro Cluster Library when * it received a Load Control Event Command for * this application. * * @param pCmd - pointer to load control event command * @param srcAddr - pointer to source address * @param status - event status * @param seqNum - sequence number of this command * * @return none */ static void pct_LoadControlEventCB( zclCCLoadControlEvent_t *pCmd, afAddrType_t *srcAddr, uint8 status, uint8 seqNum) { #if defined ( ZCL_LOAD_CONTROL ) // According to the Smart Metering Specification, upon receipt // of the Load Control Event command, the receiving device shall // send Report Event Status command back. uint8 eventStatus; if ( status == ZCL_STATUS_INVALID_FIELD ) { // If the incoming message has invalid fields in it // Send response back with status: rejected eventStatus = EVENT_STATUS_LOAD_CONTROL_EVENT_REJECTED; } else { // Send response back with status: received eventStatus = EVENT_STATUS_LOAD_CONTROL_EVENT_RECEIVED; } // Send response back pct_SendReportEventStatus( srcAddr, seqNum, pCmd->issuerEvent, pCmd->startTime, eventStatus, pCmd->criticalityLevel, pCmd->eventControl); if ( status != ZCL_STATUS_INVALID_FIELD ) { // Start the Load Control Event if ( pCmd->issuerEvent == LOADCONTROL_EVENT_ID ) { if ( pCmd->startTime == START_TIME_NOW ) // start time = NOW { // send back status event = load control event started eventStatus = EVENT_STATUS_LOAD_CONTROL_EVENT_STARTED; pct_SendReportEventStatus( srcAddr, seqNum, pCmd->issuerEvent, pCmd->startTime, eventStatus, pCmd->criticalityLevel, pCmd->eventControl); if ( pCmd->deviceGroupClass == ONOFF_LOAD_DEVICE_CLASS ) // is this one for residential on/off load? { HalLcdWriteString("Load Evt Started", HAL_LCD_LINE_3); } else if ( pCmd->deviceGroupClass == HVAC_DEVICE_CLASS ) // is this one for HVAC compressor/furnace? { HalLcdWriteString("PCT Evt Started", HAL_LCD_LINE_3); } HalLedBlink ( HAL_LED_4, 0, 50, 500 ); osal_start_timerEx( pctTaskID, PCT_LOAD_CTRL_EVT, (PCT_LOAD_CTRL_PERIOD * (pCmd->durationInMinutes)) ); } } } #endif // ZCL_LOAD_CONTROL }
/***************************************************************************** * @fn zb_HandleOsalEvent * * @brief The zb_HandleOsalEvent function is called by the operating * system when a task event is set * * @param event - Bitmask containing the events that have been set * * @return none */ void zb_HandleOsalEvent( uint16 event ) { if( event & SYS_EVENT_MSG ) { } if( event & ZB_ENTRY_EVENT ) { // blind LED 1 to indicate joining a network HalLedBlink ( HAL_LED_1, 0, 50, 500 ); // Start the device zb_StartRequest(); MCU_IO_DIR_OUTPUT(LED_PORT, LED_PIN); MCU_IO_SET_LOW(LED_PORT, LED_PIN); } if ( event & MY_START_EVT ) { zb_StartRequest(); } if ( event & MY_REPORT_EVT ) { if ( appState == APP_REPORT ) { osal_start_timerEx( sapi_TaskID, MY_REPORT_EVT, myReportPeriod ); } } if ( event & MY_FIND_COLLECTOR_EVT ) { // blind LED 2 to indicate discovery and binding HalLedBlink ( HAL_LED_2, 0, 50, 500 ); appState = APP_BIND; // Find and bind to a collector device zb_BindDevice( TRUE, ROUTER_REPORT_CMD_ID, (uint8 *)NULL ); } }
/** * @brief Esta función es llamada por el SO cuando se debe atender un * evento de la tarea. * @param event Mascara de bits conteniendo los eventos a atender */ void zb_HandleOsalEvent(uint16 event) { uint8 logicalType; if (event & SYS_EVENT_MSG) { } if (event & ZB_ENTRY_EVENT) { // inicializa UART initUart(uartRxCB); // blick LED 1 para indicar que se está uniendo a la red HalLedBlink(HAL_LED_1, 0, 50, 500 ); HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF); // lee tipo de logical device desde la memoria NV zb_ReadConfiguration(ZCD_NV_LOGICAL_TYPE, sizeof(uint8), &logicalType); // inicia la red zb_StartRequest(); } if (event & MY_START_EVT) { // inicia la red zb_StartRequest(); } if (event & SEND_UART_MSG) { // envia mensaje por UART a la PC //HalLcdWriteString("UART_EVT", HAL_LCD_LINE_1); sendUartMessage(&msgReport, &alarmFlags); } if (event & MY_TEST_EVT) { // crea mensaje de prueba msgReport.msgType = MSG_TYPE_REPORT; msgReport.sequence++; msgReport.lenData = MSG_LEN_DATA_REPORT; for (uint8 i = 0; i < 8; i++) msgReport.mac[i] = i; // datos de prueba static double d[10] = {220.5, 0.354, 85.12, 88.33, 85.06, 0.98, 1.23, 3.25, 0.97, 4.55}; for (int i = 0; i < 10; i++) memcpy(&(msgReport.data[i*4]), &(d[i]), sizeof(double)); // flags de prueba uint16 flags = 0; // imprime info en LCD HalLcdWriteString("TEST_EVT", HAL_LCD_LINE_1); HalLcdWriteStringValue("#", msgReport.sequence, 10, HAL_LCD_LINE_2); // envia mensaje por UART sendUartMessage(&msgReport, &flags); // crea evento nuevamente osal_start_timerEx(sapi_TaskID, MY_TEST_EVT, 5000); } }
/********************************************************************* * @fn zclRouterVersion1_ProcessIdentifyTimeChange * * @brief Called to process any change to the IdentifyTime attribute. * * @param none * * @return none */ static void zclRouterVersion1_ProcessIdentifyTimeChange( void ) { if ( zclRouterVersion1_IdentifyTime > 0 ) { osal_start_timerEx( zclRouterVersion1_TaskID, ROUTERVERSION1_IDENTIFY_TIMEOUT_EVT, 1000 ); HalLedBlink ( HAL_LED_4, 0xFF, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME ); } else { osal_stop_timerEx( zclRouterVersion1_TaskID, ROUTERVERSION1_IDENTIFY_TIMEOUT_EVT ); } }
/********************************************************************* * @fn OTA_Dongle_ProcessIdentifyTimeChange * * @brief Called to process any change to the IdentifyTime attribute. * * @param none * * @return none */ static void OTA_Dongle_ProcessIdentifyTimeChange( void ) { if ( OTA_Dongle_IdentifyTime > 0 ) { osal_start_timerEx( OTA_Dongle_TaskID, OTA_DONGLE_IDENTIFY_TIMEOUT_EVT, 1000 ); HalLedBlink ( HAL_LED_4, 0xFF, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME ); } else { osal_stop_timerEx( OTA_Dongle_TaskID, OTA_DONGLE_IDENTIFY_TIMEOUT_EVT ); } }
/********************************************************************* * @fn rangeext_ProcessIdentifyTimeChange * * @brief Called to blink led for specified IdentifyTime attribute value * * @param none * * @return none */ static void rangeext_ProcessIdentifyTimeChange( void ) { if ( rangeExtIdentifyTime > 0 ) { osal_start_timerEx( rangeExtTaskID, RANGEEXT_IDENTIFY_TIMEOUT_EVT, 1000 ); HalLedBlink ( HAL_LED_4, 0xFF, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME ); } else { HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF ); osal_stop_timerEx( rangeExtTaskID, RANGEEXT_IDENTIFY_TIMEOUT_EVT ); } }
/********************************************************************* * @fn loadcontrol_ProcessIdentifyTimeChange * * @brief Called to blink led for specified IdentifyTime attribute value * * @param none * * @return none */ static void loadcontrol_ProcessIdentifyTimeChange( void ) { if ( loadControlIdentifyTime > 0 ) { osal_start_timerEx( loadControlTaskID, LOADCONTROL_IDENTIFY_TIMEOUT_EVT, 1000 ); HalLedBlink ( HAL_LED_4, 0xFF, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME ); } else { HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF ); osal_stop_timerEx( loadControlTaskID, LOADCONTROL_IDENTIFY_TIMEOUT_EVT ); } }
/********************************************************************* * @fn SampleApp_MessageMSGCB * * @brief Data message processor callback. This function processes * any incoming data - probably from other devices. So, based * on cluster ID, perform the intended action. * * @param none * * @return none */ void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt ) { uint16 flashTime; switch ( pkt->clusterId ) { case SAMPLEAPP_PERIODIC_CLUSTERID: break; case SAMPLEAPP_FLASH_CLUSTERID: flashTime = BUILD_UINT16(pkt->cmd.Data[1], pkt->cmd.Data[2] ); HalLedBlink( HAL_LED_4, 4, 50, (flashTime / 4) ); break; } }
void SampleApp_HandleKeys( uint8 shift, uint8 keys ) { if ( keys & HAL_KEY_SW_6 ) { //mpleApp_SendFlashMessage_Endevice_Df( SAMPLEAPP_FLASH_DURATION ); SampleApp_SendFlashMessage_Coordinator_Ob( SAMPLEAPP_FLASH_DURATION ); //SampleApp_SendFlashMessage3( SAMPLEAPP_FLASH_DURATION ); } else if ( keys & HAL_KEY_SW_5 ) { HalLedBlink( HAL_LED_4, 4, 50, 500 ); //SampleApp_SendFlashMessage_Endevice_Ob( SAMPLEAPP_FLASH_DURATION ); } }
/********************************************************************* * @fn zclSampleSw_ProcessIdentifyTimeChange * * @brief Called to process any change to the IdentifyTime attribute. * * @param none * * @return none */ static void zclSampleSw_ProcessIdentifyTimeChange( void ) { if ( zclSampleSw_IdentifyTime > 0 ) { osal_start_timerEx( zclSampleSw_TaskID, SAMPLESW_IDENTIFY_TIMEOUT_EVT, 1000 ); HalLedBlink ( HAL_LED_4, 0xFF, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME ); } else { if ( zclSampleSw_OnOff ) HalLedSet ( HAL_LED_4, HAL_LED_MODE_ON ); else HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF ); osal_stop_timerEx( zclSampleSw_TaskID, SAMPLESW_IDENTIFY_TIMEOUT_EVT ); } }
/********************************************************************* * @fn AT_ZCL_ONOFF_SWITCH_ProcessIdentifyTimeChange * * @brief Called to process any change to the IdentifyTime attribute. * * @param none * * @return none */ static void AT_ZCL_ONOFF_SWITCH_ProcessIdentifyTimeChange( void ) { if ( AT_ZCL_ONOFF_SWITCH_IdentifyTime > 0 ) { osal_start_timerEx( AT_ZCL_ONOFF_SWITCH_TaskID, AT_ZCL_ONOFF_SWITCH_IDENTIFY_TIMEOUT_EVT, 1000 ); HalLedBlink ( HAL_LED_2, 0xFF, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME ); } else { if ( AT_ZCL_ONOFF_SWITCH_OnOff ) HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON ); else HalLedSet ( HAL_LED_2, HAL_LED_MODE_OFF ); osal_stop_timerEx( AT_ZCL_ONOFF_SWITCH_TaskID, AT_ZCL_ONOFF_SWITCH_IDENTIFY_TIMEOUT_EVT ); } }
/***************************************** 广播,由协调器通过按键或启动时, 向全部终端节点发起测试请求 ******************************************/ void SampleApp_SendBroadcastMessage( void ) { uint8 data[]="+REBOT#"; if(AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_PERIODIC_CLUSTERID, 7, data, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ) == afStatus_SUCCESS ) { //SUCCESS! HalLedBlink( HAL_LED_2, 2,50, 500 ); } }
/********************************************************************* * @fn zllSampleBridge_ProcessIdentifyTimeChange * * @brief Called to process any change to the IdentifyTime attribute. * * @param none * * @return none */ static void zllSampleBridge_ProcessIdentifyTimeChange( void ) { if ( zllSampleBridge_IdentifyTime > 0 ) { osal_start_timerEx( zllSampleBridge_TaskID, SAMPLEBRIDGE_IDENTIFY_TIMEOUT_EVT, 1000 ); #if ( HAL_LED == TRUE ) HalLedBlink ( HAL_LED_4, 0xFF, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME ); #endif } else { #if ( HAL_LED == TRUE ) HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF ); #endif osal_stop_timerEx( zllSampleBridge_TaskID, SAMPLEBRIDGE_IDENTIFY_TIMEOUT_EVT ); } }
void checkLedStatus(void) { //P2_0: NeedRepair Switch, when it is 1: Need Repair, blink Red Led and turn off Green Led. 0: Don't Need Repair //P1_2: Red Led LED1 //P1_1: Green Led, Green Led blink when device is not in network LED2 HalLedSet(HAL_LED_1, HAL_LED_MODE_OFF); if (!inNetwork) { HalLedBlink(HAL_LED_2, 4, 50, 190); } else { HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF); } return; }
/****************************************************************************** * @fn zb_HandleKeys * * @brief Handles all key events for this device. * * @param shift - true if in shift/alt. * @param keys - bit field for key events. Valid entries: * EVAL_SW4 * EVAL_SW3 * EVAL_SW2 * EVAL_SW1 * * @return none */ void zb_HandleKeys( uint8 shift, uint8 keys ) { // Shift is used to make each button/switch dual purpose. if ( shift ) { if ( keys & HAL_KEY_SW_1 ) { } if ( keys & HAL_KEY_SW_2 ) { } if ( keys & HAL_KEY_SW_3 ) { } if ( keys & HAL_KEY_SW_4 ) { } } else { if ( keys & HAL_KEY_SW_1 ) { // Start reporting if ( reportState == FALSE ) { osal_set_event( sapi_TaskID, MY_REPORT_EVT ); reportState = TRUE; // blink LED 2 to indicate reporting HalLedBlink ( HAL_LED_2, 0, 50, 500 ); } } if ( keys & HAL_KEY_SW_2 ) { MCU_IO_OUTPUT_PREP(0, 0, 1); } if ( keys & HAL_KEY_SW_3 ) { } if ( keys & HAL_KEY_SW_4 ) { } } }
void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt ) { uint16 flashTime; switch ( pkt->clusterId ) { case SAMPLEAPP_PERIODIC_CLUSTERID: f=pkt->cmd.Data[0];//检测其panid,记录在参数f里面。 if(State_data[f]=='0'){State_data[f]='1';}//将其状态置为在线 // HalUARTWrite(0, State_data,3); //十六进制发给PC机 // HalUARTWrite(0, "\n",1); break; case SAMPLEAPP_FLASH_CLUSTERID: flashTime = BUILD_UINT16(pkt->cmd.Data[1], pkt->cmd.Data[2] ); HalLedBlink( HAL_LED_4, 4, 50, (flashTime / 4) ); break; } }
bool CurrentDetectionT1_HandleSendRepairMessage(uint8 * sendToG1Data) { static bool needResendRepair = false; static uint32 repairTime_h; static uint32 repairTime_l; if (needResendRepair || CurrentDetectionT1_CheckNeedSendRepairMessage()) { if (!needResendRepair) { repairTime_l = lastNvTime_l + get_osal_SystemClock(); repairTime_h = lastNvTime_h; } memcpy(sendToG1Data + SN_LEN, &repairTime_h, sizeof(repairTime_h)); memcpy(sendToG1Data + SN_LEN + TIME_LEN, &repairTime_l, sizeof(repairTime_l)); swapUint32Bytes(sendToG1Data + SN_LEN); swapUint32Bytes(sendToG1Data + SN_LEN + TIME_LEN); SET_HEART_BIT_STATUS(sendToG1Data , HEART_BIT_STATUS_REPAIR_MASK, 1); if (AF_DataRequest( &CurrentDetectionT1_Periodic_DstAddr, &CurrentDetectionT1_epDesc, DTCT_PERIODIC_CLUSTERID, HEART_BIT_MSG_LEN, sendToG1Data, &CurrentDetectionT1_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS) == afStatus_SUCCESS) { HalLedBlink(HAL_LED_2, 2, 50, 50); needResendRepair = false; } else { needResendRepair = true; } } return (P2_0); }
/********************************************************************* * @fn zclSampleTemperatureSensor_ProcessIdentifyTimeChange * * @brief Called to process any change to the IdentifyTime attribute. * * @param none * * @return none */ static void zclSampleTemperatureSensor_ProcessIdentifyTimeChange( void ) { if ( zclSampleTemperatureSensor_IdentifyTime > 0 ) { osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT, 1000 ); HalLedBlink ( HAL_LED_4, 0xFF, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME ); } else { if ( zclSampleTemperatureSensor_OnOff ) { HalLedSet ( HAL_LED_4, HAL_LED_MODE_ON ); } else { HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF ); } osal_stop_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT ); } }
/********************************************************************* * @fn SampleApp_HandleKeys * * @brief Handles all key events for this device. * * @param shift - true if in shift/alt. * @param keys - bit field for key events. Valid entries: * HAL_KEY_SW_2 * HAL_KEY_SW_1 * * @return none */ void SampleApp_HandleKeys( uint8 shift, uint8 keys ) { (void)shift; // Intentionally unreferenced parameter if ( keys & HAL_KEY_SW_1 ) { /* This key sends the Flash Command is sent to Group 1. * This device will not receive the Flash Command from this * device (even if it belongs to group 1). */ SampleApp_SendFlashMessage( SAMPLEAPP_FLASH_DURATION ); } if ( keys & HAL_KEY_SW_2 ) { /* The Flashr Command is sent to Group 1. * This key toggles this device in and out of group 1. * If this device doesn't belong to group 1, this application * will not receive the Flash command sent to group 1. */ aps_Group_t *grp; grp = aps_FindGroup( SAMPLEAPP_ENDPOINT, SAMPLEAPP_FLASH_GROUP ); if ( grp ) { // Remove from the group aps_RemoveGroup( SAMPLEAPP_ENDPOINT, SAMPLEAPP_FLASH_GROUP ); } else { // Add to the flash group aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group ); } } if (keys & HAL_KEY_SW_6) { HalUARTWrite(0, "In Handlekey\n", sizeof("In HandleKey\n")-1); HalLedBlink(HAL_LED_1, 2, 50, 500); } }
/********************************************************************* * @fn SampleApp_MessageMSGCB * * @brief Data message processor callback. This function processes * any incoming data - probably from other devices. So, based * on cluster ID, perform the intended action. * * @param none * * @return none */ void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt ) //接收數據 { // uint16 flashTime; switch ( pkt->clusterId ) { case SAMPLEAPP_P2P_CLUSTERID: osal_memcpy(periodicData, pkt->cmd.Data, 27); LedState=LedState+1; if(LedState == 1) HalLedSet(HAL_LED_3, HAL_LED_MODE_ON); else if(LedState == 2) {HalLedSet(HAL_LED_3, HAL_LED_MODE_OFF); LedState=0;} break; case SAMPLEAPP_PERIODIC_CLUSTERID: // osal_memset(periodicData, 0 , 15); osal_memcpy(periodicData, pkt->cmd.Data, 27); LedState=LedState+1; // HalUARTWrite(0, "Rx:", 3); //提示信息 // HalUARTWrite(0, pkt->cmd.Data, pkt->cmd.DataLength); //輸出接收到的數據 // HalUARTWrite(0, "\n", 1); //迴車換行 if(LedState == 1) HalLedSet(HAL_LED_3, HAL_LED_MODE_ON); else if(LedState == 2) {HalLedSet(HAL_LED_3, HAL_LED_MODE_OFF); LedState=0;} break; case SAMPLEAPP_FLASH_CLUSTERID: //此實驗沒有使用,到後面實驗詳解 // flashTime = BUILD_UINT16(pkt->cmd.Data[1], pkt->cmd.Data[2] ); HalLedBlink( HAL_LED_3, 2, 25 ,100 ); break; } }
/****************************************************************************** * @fn zb_BindConfirm * * @brief The zb_BindConfirm callback is called by the ZigBee stack * after a bind operation completes. * * @param commandId - The command ID of the binding being confirmed. * status - The status of the bind operation. * * @return none */ void zb_BindConfirm( uint16 commandId, uint8 status ) { if( status == ZB_SUCCESS ) { appState = APP_REPORT; HalLedSet( HAL_LED_2, HAL_LED_MODE_OFF ); // After failure reporting start automatically when the device // is binded to a new gateway if ( reportState ) { // blink LED 2 to indicate reporting HalLedBlink ( HAL_LED_2, 0, 50, 500 ); // Start reporting osal_set_event( sapi_TaskID, MY_REPORT_EVT ); } } else { osal_start_timerEx( sapi_TaskID, MY_FIND_COLLECTOR_EVT, myBindRetryDelay ); } }
/************************************************************************************************** * @fn main * * @brief Start of application. * * @param none * * @return none ************************************************************************************************** */ int main(void) { /* Initialize hardware */ HAL_BOARD_INIT(); /* Initialze the HAL driver */ HalDriverInit(); /* Initialize MAC */ MAC_Init(); /* Initialize the operating system */ osal_init_system(); #ifdef HAL_BOARD_CC2538 /* Master enable interrupts */ IntMasterEnable(); /* Setup SysTick to generate interrupt every 1 ms */ SysTickSetup(); #endif /* HAL_BOARD_CC2538 */ /* Enable interrupts */ HAL_ENABLE_INTERRUPTS(); /* Setup Keyboard callback */ HalKeyConfig(MSA_KEY_INT_ENABLED, MSA_Main_KeyCallback); /* Blink LED on startup */ HalLedBlink (HAL_LED_4, 0, 40, 200); /* Start OSAL */ OSAL_START_SYSTEM(); return 0; }
void SampleApp_SendFlashMessage_Coordinator_C( uint16 flashTime, char *id, uint8 button ) { uint8 id_int = atoi(id); if(id_int == 1) HalLedBlink( HAL_LED_4, 4, 50, 500 ); uint8 cmd[9]; cmd[0] = id_int; cmd[1] = button; if ( AF_DataRequest( &SampleApp_Flash_DstAddr, &SampleApp_epDesc, SAMPLEAPP_FLASH_CLUSTERID, 2, cmd, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ) == afStatus_SUCCESS ) { } else { } }
/*************************************************************************************************** * @fn HalLedSet * * @brief Tun ON/OFF/TOGGLE given LEDs * * @param led - bit mask value of leds to be turned ON/OFF/TOGGLE * mode - BLINK, FLASH, TOGGLE, ON, OFF * @return None ***************************************************************************************************/ uint8 HalLedSet (uint8 leds, uint8 mode) { #if (defined (BLINK_LEDS)) && (HAL_LED == TRUE) uint8 led; HalLedControl_t *sts; switch (mode) { case HAL_LED_MODE_BLINK: /* Default blink, 1 time, D% duty cycle */ HalLedBlink (leds, 1, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME); break; case HAL_LED_MODE_FLASH: /* Default flash, N times, D% duty cycle */ HalLedBlink (leds, HAL_LED_DEFAULT_FLASH_COUNT, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME); break; case HAL_LED_MODE_ON: case HAL_LED_MODE_OFF: case HAL_LED_MODE_TOGGLE: led = HAL_LED_1; leds &= HAL_LED_ALL; sts = HalLedStatusControl.HalLedControlTable; while (leds) { if (leds & led) { if (mode != HAL_LED_MODE_TOGGLE) { sts->mode = mode; /* ON or OFF */ } else { sts->mode ^= HAL_LED_MODE_ON; /* Toggle */ } HalLedOnOff (led, sts->mode); leds ^= led; } led <<= 1; sts++; } break; default: break; } #elif (HAL_LED == TRUE) LedOnOff(leds, mode); #else // HAL LED is disabled, suppress unused argument warnings (void) leds; (void) mode; #endif /* BLINK_LEDS && HAL_LED */ return ( HalLedState ); }
/********************************************************************* * @fn SAPI_ProcessEvent * * @brief Simple API 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 SAPI_ProcessEvent( byte task_id, UINT16 events ) { osal_event_hdr_t *pMsg; afIncomingMSGPacket_t *pMSGpkt; afDataConfirm_t *pDataConfirm; if ( events & SYS_EVENT_MSG ) { pMsg = (osal_event_hdr_t *) osal_msg_receive( task_id ); while ( pMsg ) { switch ( pMsg->event ) { case ZDO_CB_MSG: SAPI_ProcessZDOMsgs( (zdoIncomingMsg_t *)pMsg ); break; case AF_DATA_CONFIRM_CMD: // This message is received as a confirmation of a data packet sent. // The status is of ZStatus_t type [defined in ZComDef.h] // The message fields are defined in AF.h pDataConfirm = (afDataConfirm_t *) pMsg; SAPI_SendDataConfirm( pDataConfirm->transID, pDataConfirm->hdr.status ); break; case AF_INCOMING_MSG_CMD: pMSGpkt = (afIncomingMSGPacket_t *) pMsg; SAPI_ReceiveDataIndication( pMSGpkt->srcAddr.addr.shortAddr, pMSGpkt->clusterId, pMSGpkt->cmd.DataLength, pMSGpkt->cmd.Data); break; case ZDO_STATE_CHANGE: // If the device has started up, notify the application if (pMsg->status == DEV_END_DEVICE || pMsg->status == DEV_ROUTER || pMsg->status == DEV_ZB_COORD ) { SAPI_StartConfirm( ZB_SUCCESS ); } else if (pMsg->status == DEV_HOLD || pMsg->status == DEV_INIT) { SAPI_StartConfirm( ZB_INIT ); } break; case ZDO_MATCH_DESC_RSP_SENT: SAPI_AllowBindConfirm( ((ZDO_MatchDescRspSent_t *)pMsg)->nwkAddr ); break; case KEY_CHANGE: #if ( SAPI_CB_FUNC ) zb_HandleKeys( ((keyChange_t *)pMsg)->state, ((keyChange_t *)pMsg)->keys ); #endif break; case SAPICB_DATA_CNF: SAPI_SendDataConfirm( (uint8)((sapi_CbackEvent_t *)pMsg)->data, ((sapi_CbackEvent_t *)pMsg)->hdr.status ); break; case SAPICB_BIND_CNF: SAPI_BindConfirm( ((sapi_CbackEvent_t *)pMsg)->data, ((sapi_CbackEvent_t *)pMsg)->hdr.status ); break; case SAPICB_START_CNF: SAPI_StartConfirm( ((sapi_CbackEvent_t *)pMsg)->hdr.status ); break; default: // User messages should be handled by user or passed to the application if ( pMsg->event >= ZB_USER_MSG ) { } break; } // Release the memory osal_msg_deallocate( (uint8 *) pMsg ); // Next pMsg = (osal_event_hdr_t *) osal_msg_receive( task_id ); } // Return unprocessed events return (events ^ SYS_EVENT_MSG); } if ( events & ZB_ALLOW_BIND_TIMER ) { afSetMatch(sapi_epDesc.simpleDesc->EndPoint, FALSE); return (events ^ ZB_ALLOW_BIND_TIMER); } if ( events & ZB_BIND_TIMER ) { // Send bind confirm callback to application SAPI_BindConfirm( sapi_bindInProgress, ZB_TIMEOUT ); sapi_bindInProgress = 0xffff; return (events ^ ZB_BIND_TIMER); } if ( events & ZB_ENTRY_EVENT ) { uint8 startOptions; // Give indication to application of device startup #if ( SAPI_CB_FUNC ) zb_HandleOsalEvent( ZB_ENTRY_EVENT ); #endif // LED off cancels HOLD_AUTO_START blink set in the stack HalLedSet (HAL_LED_4, HAL_LED_MODE_OFF); zb_ReadConfiguration( ZCD_NV_STARTUP_OPTION, sizeof(uint8), &startOptions ); if ( startOptions & ZCD_STARTOPT_AUTO_START ) { zb_StartRequest(); } else { // blink leds and wait for external input to config and restart HalLedBlink(HAL_LED_2, 0, 50, 500); } return (events ^ ZB_ENTRY_EVENT ); } // This must be the last event to be processed if ( events & ( ZB_USER_EVENTS ) ) { // User events are passed to the application #if ( SAPI_CB_FUNC ) zb_HandleOsalEvent( events ); #endif // Do not return here, return 0 later } // Discard unknown events return 0; }