This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

temperature sensor in cc2530dk kit

Other Parts Discussed in Thread: Z-STACK, CC2530EM, CC2530

Warning[Pe177]: variable "zclSampleTemperatureSensor_MeasuredValue" was declared but never referenced E:\PROJECT\13$11\Z-Stack Home 1.2.1\Projects\zstack\HomeAutomation\SampleTemperatureSensor\Source\zcl_sampletemperaturesensor.c 452
Linking
Error[e46]: Undefined external "zclSampleTemperatureSensor_MeasuredValue" referred in zcl_sampletemperaturesensor ( E:\PROJECT\13$11\Z-Stack Home
1.2.1\Projects\zstack\HomeAutomation\SampleTemperatureSensor\CC2530DB\EndDeviceEB\Obj\zcl_sampletemperaturesensor.r51 )
Error while running Linker
Total number of errors: 1
Total number of warnings: 3

i used the adc to get data from internal temperature sensor and transmit it via zigbee and display it in samplethermostat code dumped board

  • SampleHeatingCoolingUnit is a Zigbee thermostat.

  • sir but i am using the progrm called sample thermotat  in the coordinator node sir !!! 

  • It is the same steps to use SampleThermostat as coordinator and SampleTemperatureSensor as end device. Do you follow the steps?

  • so sir ... what u r saying is that my code is correct and the binding steps i have been using was wrong ??

    and is it possible to declare as a variable

    int16 zclSampleTemperatureSensor_MeasuredValue = 2200; // 22.00C in the zcl_temperaturesensor_data.c 

    the above zclSampleTemperatureSensor_MeasuredValue is declared as a constant in that .c file ... s there any possibility that i can change it to a variable if i try like this 

    int16 aadc==3000;

    zclSampleTemperatureSensor_MeasuredValue = aadc ;

    means it is showing the error of 

    Error[Pe028]: expression must have a constant value E:\PROJECT\17$11\new idea\Z-Stack Home 1.2.1\Projects\zstack\HomeAutomation\SampleTemperatureSensor\Source\zcl_sampletemperaturesensor_data.c 99
    Error while running C/C++ Compiler

  • What are you talking about? zclSampleTemperatureSensor_MeasuredValue is not declared as const in the original source code, it is like

    int16 zclSampleTemperatureSensor_MeasuredValue = 2200;  // 22.00C

    Of course you can declare int16 aadc=3000 (not int16 aadc==3000) and assign it to zclSampleTemperatureSensor_MeasuredValue

  • Wow... You really need to study more hard on programming.... You can't declare a variable like

    int16 zclSampleTemperatureSensor_MeasuredValue = aadc;


    You can only assign a variable to another variable in code. for example:

      if (events & SAMPLETEMPERATURESENSOR_ADC)
      {
        zclSampleTemperatureSensor_MeasuredValue = aadc ;
        zclSampleTemperatureSensor_LcdDisplayUpdate();
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
        return ( events ^ SAMPLETEMPERATURESENSOR_ADC );
      }

  • ohhh thank u sir .. i know i jst began programming now .... 

    /**************************************************************************************************
      Filename:       zcl_sampletemperaturesensor.c
      Revised:        $Date: 2013-10-18 11:49:27 -0700 (Fri, 18 Oct 2013) $
      Revision:       $Revision: 35718 $
    
      Description:    Zigbee Cluster Library - sample device application.
    
    
      Copyright 2013 Texas Instruments Incorporated. All rights reserved.
    
      IMPORTANT: Your use of this Software is limited to those specific rights
      granted under the terms of a software license agreement between the user
      who downloaded the software, his/her employer (which must be your employer)
      and Texas Instruments Incorporated (the "License").  You may not use this
      Software unless you agree to abide by the terms of the License. The License
      limits your use, and you acknowledge, that the Software may not be modified,
      copied or distributed unless embedded on a Texas Instruments microcontroller
      or used solely and exclusively in conjunction with a Texas Instruments radio
      frequency transceiver, which is integrated into your product.  Other than for
      the foregoing purpose, you may not use, reproduce, copy, prepare derivative
      works of, modify, distribute, perform, display or sell this Software and/or
      its documentation for any purpose.
    
      YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
      PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
      INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
      NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
      TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
      NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
      LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
      INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
      OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
      OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
      (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
    
      Should you have any questions regarding your right to use this Software,
      contact Texas Instruments Incorporated at www.TI.com.
    **************************************************************************************************/
    
    /*********************************************************************
      This device will act as a temperature sensor. It updates the current
      temperature on the thermostat when the user sends the desired
      temperature using SW1.
    
      SCREEN MODES
      ----------------------------------------
      Main:
        - SW1: Send current temperature
        - SW2: Invoke EZMode
        - SW3: Adjust temperature
        - SW5: Go to Help screen
    
      Temperature:
        - SW1: Increase temperature
        - SW3: Decrease temperature
        - SW5: Enter temperature
      ----------------------------------------
    *********************************************************************/
    
    /*********************************************************************
     * INCLUDES
     */
    #include "ZComDef.h"
    #include "OSAL.h"
    #include "AF.h"
    #include "ZDApp.h"
    #include "ZDObject.h"
    #include "ZDProfile.h"
    #include "MT_SYS.h"
    
    #include "zcl.h"
    #include "zcl_general.h"
    #include "zcl_ha.h"
    #include "zcl_ezmode.h"
    #include "zcl_ms.h"
    #include "hal_adc.h"
    
    #include "zcl_sampletemperaturesensor.h"
    
    #include "onboard.h"
    
    /* HAL */
    #include "hal_lcd.h"
    #include "hal_led.h"
    #include "hal_key.h"
    //#include "zcl_sampletemperaturesensor_data.c"
    
    /*********************************************************************
     * MACROS
     */
    
    // how often to report temperature
    #define SAMPLETEMPERATURESENSOR_REPORT_INTERVAL   3000
    
    /*********************************************************************
     * CONSTANTS
     */
    
    /*********************************************************************
     * TYPEDEFS
     */
    /*#ifdef __IAR_SYSTEMS_ICC__
    #define  CODE   __code
    #define  XDATA  __xdata
    */
    
    /*********************************************************************
     * GLOBAL VARIABLES
     */
    byte zclSampleTemperatureSensor_TaskID;
    
    uint8 zclSampleTemperatureSensorSeqNum;
    
    static byte gPermitDuration = 0x00;
    /*void initTempSensor(void){
       DISABLE_ALL_INTERRUPTS();            //??????
       InitClock();                         //????????32M
       *((BYTE XDATA*) 0x624B) = 0x01;    //???????
       *((BYTE XDATA*) 0x61BD) = 0x01;    //???????ADC????
    }*/
    
    /*********************************************************************
     * GLOBAL FUNCTIONS
     */
    
    /*********************************************************************
     * LOCAL VARIABLES
     */
    afAddrType_t zclSampleTemperatureSensor_DstAddr;
    
    #ifdef ZCL_EZMODE
    static void zclSampleTemperatureSensor_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg );
    static void zclSampleTemperatureSensor_EZModeCB( zlcEZMode_State_t state, zclEZMode_CBData_t *pData );
    
    static const zclEZMode_RegisterData_t zclSampleTemperatureSensor_RegisterEZModeData =
    {
      &zclSampleTemperatureSensor_TaskID,
      SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT,
      SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT,
      &zclSampleTemperatureSensorSeqNum,
      zclSampleTemperatureSensor_EZModeCB
    };
    
    // NOT ZCL_EZMODE, Use EndDeviceBind
    #else
    
    static cId_t bindingOutClusters[] =
    {
      ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT
    };
    #define ZCLSAMPLETEMPERATURESENSOR_BINDINGLIST        1
    #endif
    
    devStates_t zclSampleTemperatureSensor_NwkState = DEV_INIT;
    
    uint8 giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;   // display main screen mode first
    
    static uint8 aProcessCmd[] = { 1, 0, 0, 0 }; // used for reset command, { length + cmd0 + cmd1 + data }
    
    // Test Endpoint to allow SYS_APP_MSGs
    static endPointDesc_t sampleTemperatureSensor_TestEp =
    {
      20,                                 // Test endpoint
      &zclSampleTemperatureSensor_TaskID,
      (SimpleDescriptionFormat_t *)NULL,  // No Simple description for this test endpoint
      (afNetworkLatencyReq_t)0            // No Network Latency req
    };
    
    
    /*********************************************************************
     * LOCAL FUNCTIONS
     */
    static void zclSampleTemperatureSensor_HandleKeys( byte shift, byte keys );
    static void zclSampleTemperatureSensor_BasicResetCB( void );
    static void zclSampleTemperatureSensor_IdentifyCB( zclIdentify_t *pCmd );
    static void zclSampleTemperatureSensor_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp );
    static void zclSampleTemperatureSensor_ProcessIdentifyTimeChange( void );
    //void getTemperature(void);
    // app display functions
    void zclSampleTemperatureSensor_LcdDisplayUpdate(void);
    void zclSampleTemperatureSensor_LcdDisplayMainMode(void);
    void zclSampleTemperatureSensor_LcdDisplayTempMode(void);
    void zclSampleTemperatureSensor_LcdDisplayHelpMode(void);
    //int16 getTemperature(void);
    int16 zclSampleTemperatureSensor_MeasuredValue;
    
    static void zclSampleTemperatureSensor_SendTemp(void);
    
    // Functions to process ZCL Foundation incoming Command/Response messages
    static void zclSampleTemperatureSensor_ProcessIncomingMsg( zclIncomingMsg_t *msg );
    #ifdef ZCL_READ
    static uint8 zclSampleTemperatureSensor_ProcessInReadRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    #ifdef ZCL_WRITE
    static uint8 zclSampleTemperatureSensor_ProcessInWriteRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    static uint8 zclSampleTemperatureSensor_ProcessInDefaultRspCmd( zclIncomingMsg_t *pInMsg );
    #ifdef ZCL_DISCOVER
    static uint8 zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( zclIncomingMsg_t *pInMsg );
    #endif // ZCL_DISCOVER
    
    /*********************************************************************
     * STATUS STRINGS
     */
    #ifdef LCD_SUPPORTED
    const char sClearLine[]    = " ";
    const char sDeviceName[]   = "  Temp Sensor";
    const char sSwTempUp[]     = "SW1: Raise Temp";
    const char sSwEZMode[]     = "SW2: EZ-Mode";
    const char sSwTempDown[]   = "SW3: Lower Temp";
    const char sSwHelp[]       = "SW5: Help";
    #endif
    
    /*********************************************************************
     * ZCL General Profile Callback table
     */
    static zclGeneral_AppCallbacks_t zclSampleTemperatureSensor_CmdCallbacks =
    {
      zclSampleTemperatureSensor_BasicResetCB,        // Basic Cluster Reset command
      zclSampleTemperatureSensor_IdentifyCB,          // Identify command
    #ifdef ZCL_EZMODE
      NULL,                                           // Identify EZ-Mode Invoke command
      NULL,                                           // Identify Update Commission State command
    #endif
      NULL,                                           // Identify Trigger Effect command
      zclSampleTemperatureSensor_IdentifyQueryRspCB,  // Identify Query Response command
      NULL,             				                      // On/Off cluster command
      NULL,                                           // On/Off cluster enhanced command Off with Effect
      NULL,                                           // On/Off cluster enhanced command On with Recall Global Scene
      NULL,                                           // On/Off cluster enhanced command On with Timed Off
    #ifdef ZCL_LEVEL_CTRL
      NULL,                                           // Level Control Move to Level command
      NULL,                                           // Level Control Move command
      NULL,                                           // Level Control Step command
      NULL,                                           // Level Control Stop command
    #endif
    #ifdef ZCL_GROUPS
      NULL,                                           // Group Response commands
    #endif
    #ifdef ZCL_SCENES
      NULL,                                           // Scene Store Request command
      NULL,                                           // Scene Recall Request command
      NULL,                                           // Scene Response command
    #endif
    #ifdef ZCL_ALARMS
      NULL,                                           // Alarm (Response) commands
    #endif
    #ifdef SE_UK_EXT
      NULL,                                           // Get Event Log command
      NULL,                                           // Publish Event Log command
    #endif
      NULL,                                           // RSSI Location command
      NULL                                            // RSSI Location Response command
    };
    
    /*********************************************************************
     * @fn          zclSampleTemperatureSensor_Init
     *
     * @brief       Initialization function for the zclGeneral layer.
     *
     * @param       none
     *
     * @return      none
     */
    void zclSampleTemperatureSensor_Init( byte task_id )
    {
      zclSampleTemperatureSensor_TaskID = task_id;
    
      // Set destination address to indirect
      zclSampleTemperatureSensor_DstAddr.addrMode = (afAddrMode_t)AddrNotPresent;
      zclSampleTemperatureSensor_DstAddr.endPoint = 0;
      zclSampleTemperatureSensor_DstAddr.addr.shortAddr = 0;
    
      // This app is part of the Home Automation Profile
      zclHA_Init( &zclSampleTemperatureSensor_SimpleDesc );
    
      // Register the ZCL General Cluster Library callback functions
      zclGeneral_RegisterCmdCallbacks( SAMPLETEMPERATURESENSOR_ENDPOINT, &zclSampleTemperatureSensor_CmdCallbacks );
    
      // Register the application's attribute list
      zcl_registerAttrList( SAMPLETEMPERATURESENSOR_ENDPOINT, SAMPLETEMPERATURESENSOR_MAX_ATTRIBUTES, zclSampleTemperatureSensor_Attrs );
    
      // Register the Application to receive the unprocessed Foundation command/response messages
      zcl_registerForMsg( zclSampleTemperatureSensor_TaskID );
    
    #ifdef ZCL_EZMODE
      // Register EZ-Mode
      zcl_RegisterEZMode( &zclSampleTemperatureSensor_RegisterEZModeData );
    
      // Register with the ZDO to receive Match Descriptor Responses
      ZDO_RegisterForZDOMsg(task_id, Match_Desc_rsp);
    #endif
    
      // Register for all key events - This app will handle all key events
      RegisterForKeys( zclSampleTemperatureSensor_TaskID );
    
      // Register for a test endpoint
      afRegister( &sampleTemperatureSensor_TestEp );
    
    #ifdef LCD_SUPPORTED
      // display the device name
      HalLcdWriteString( (char *)sDeviceName, HAL_LCD_LINE_3 );
    #endif
      osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
    
    }
    
    /*********************************************************************
     * @fn          zclSample_event_loop
     *
     * @brief       Event Loop Processor for zclGeneral.
     *
     * @param       none
     *
     * @return      none
     */
    uint16 zclSampleTemperatureSensor_event_loop( uint8 task_id, uint16 events )
    {
      afIncomingMSGPacket_t *MSGpkt;
    
      (void)task_id;  // Intentionally unreferenced parameter
    
      if ( events & SYS_EVENT_MSG )
      {
        while ( (MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( zclSampleTemperatureSensor_TaskID )) )
        {
          switch ( MSGpkt->hdr.event )
          {
    #ifdef ZCL_EZMODE
            case ZDO_CB_MSG:
              zclSampleTemperatureSensor_ProcessZDOMsgs( (zdoIncomingMsg_t *)MSGpkt );
              break;
    #endif
    
            case ZCL_INCOMING_MSG:
              // Incoming ZCL Foundation command/response messages
              zclSampleTemperatureSensor_ProcessIncomingMsg( (zclIncomingMsg_t *)MSGpkt );
              break;
    
            case KEY_CHANGE:
              zclSampleTemperatureSensor_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
              break;
    
            case ZDO_STATE_CHANGE:
              zclSampleTemperatureSensor_NwkState = (devStates_t)(MSGpkt->hdr.status);
    
    
              // now on the network
              if ( (zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD) ||
                   (zclSampleTemperatureSensor_NwkState == DEV_ROUTER)   ||
                   (zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE) )
              {
    #ifndef HOLD_AUTO_START
                giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
                zclSampleTemperatureSensor_LcdDisplayUpdate();
    #endif
    #ifdef ZCL_EZMODE
                zcl_EZModeAction( EZMODE_ACTION_NETWORK_STARTED, NULL );
    #endif // ZCL_EZMODE
              }
              break;
    
            default:
              break;
          }
    
          // Release the memory
          osal_msg_deallocate( (uint8 *)MSGpkt );
        }
    
        // return unprocessed events
        return (events ^ SYS_EVENT_MSG);
      }
    
      if ( events & SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT )
      {
        if ( zclSampleTemperatureSensor_IdentifyTime > 0 )
          zclSampleTemperatureSensor_IdentifyTime--;
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    
        return ( events ^ SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT );
      }
    
    #ifdef ZCL_EZMODE
      // going on to next state
      if ( events & SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT )
      {
        zcl_EZModeAction ( EZMODE_ACTION_PROCESS, NULL );   // going on to next state
        return ( events ^ SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT );
      }
    
      // the overall EZMode timer expired, so we timed out
      if ( events & SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT )
      {
        zcl_EZModeAction ( EZMODE_ACTION_TIMED_OUT, NULL ); // EZ-Mode timed out
        return ( events ^ SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT );
      }
    #endif // ZLC_EZMODE
    
      if ( events & SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
        zclSampleTemperatureSensor_LcdDisplayUpdate();
    
        return ( events ^ SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT );
      }
    
      if (events & SAMPLETEMPERATURESENSOR_ADC)
      {
       int16 AvgTemp;
      uint8   i;
      uint16  AdcValue;
      uint16  value;
      uint16  value1;
    //char i;
            //char TempValue[10]; 
            //initTempSensor(); 
      AdcValue = 0;
      for( i = 0; i < 4; i++ )
      {
    ADCIF = 0;
        ADCCON3 = (HAL_ADC_REF_125V | HAL_ADC_RESOLUTION_14 | HAL_ADC_CHN_TEMP);    // ??1.25V????,12????,AD??:?????
            while ( !ADCIF );                                              //?????ADC
                     
        value =  ADCL >> 2;                        
        value |= (((UINT16)ADCH) << 6);    
        AdcValue += value;                          
      }
      value = AdcValue >> 2;                        //????4,?????
      value1 =   (((value) >> 4) - 335);
                 //??AD?,????????
     // while(1)
      //      {
             AvgTemp = 0;
             for(i = 0 ; i < 64 ; i++)
              {
                AvgTemp += value1;              
                AvgTemp >>= 1;                              //??????2.
              }
              
        //    }
        zclSampleTemperatureSensor_MeasuredValue = AvgTemp ;
        zclSampleTemperatureSensor_LcdDisplayUpdate();
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
        return ( events ^ SAMPLETEMPERATURESENSOR_ADC );
      }
    
      if ( events & SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT )
      {
        zclSampleTemperatureSensor_SendTemp();
    
        // report current temperature reading every 10 seconds
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
    
        return ( events ^ SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT );
      }
    
      // Discard unknown events
      return 0;
    }
    
    /*int16 getTemperature(void)
    {
      int16 AvgTemp;
      uint8   i;
      uint16  AdcValue;
      uint16  value;
      uint16  value1;
    //char i;
            //char TempValue[10]; 
            //initTempSensor(); 
      AdcValue = 0;
      for( i = 0; i < 4; i++ )
      {
    ADCIF = 0;
        ADCCON3 = (HAL_ADC_REF_125V | HAL_ADC_RESOLUTION_14 | HAL_ADC_CHN_TEMP);    // ??1.25V????,12????,AD??:?????
            while ( !ADCIF );                                              //?????ADC
                     
        value =  ADCL >> 2;                        
        value |= (((UINT16)ADCH) << 6);    
        AdcValue += value;                          
      }
      value = AdcValue >> 2;                        //????4,?????
      value1 =   (((value) >> 4) - 335);
                 //??AD?,????????
      while(1)
            {
              AvgTemp = 0;
              for(i = 0 ; i < 64 ; i++)
              {
                AvgTemp += value1;              
                AvgTemp >>= 1;                              //??????2.
              }
              
            }
      //zclSampleTemperatureSensor_MeasuredValue = AvgTemp ; 
      return AvgTemp;
    }*/
    
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_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_5
     *                 HAL_KEY_SW_4
     *                 HAL_KEY_SW_3
     *                 HAL_KEY_SW_2
     *                 HAL_KEY_SW_1
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_HandleKeys( byte shift, byte keys )
    {
      if ( keys & HAL_KEY_SW_1 )
      {
        // increase temperature
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        if ( zclSampleTemperatureSensor_MeasuredValue < zclSampleTemperatureSensor_MaxMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MeasuredValue + 100;  // considering using whole number value
        }
        else if ( zclSampleTemperatureSensor_MeasuredValue >= zclSampleTemperatureSensor_MaxMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MaxMeasuredValue;
        }
    
        // Send temperature information
        zclSampleTemperatureSensor_SendTemp();
      }
    
      if ( keys & HAL_KEY_SW_2 )
      {
        if ( ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE ) ||
            ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE ) )
        {
          giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
    #ifdef ZCL_EZMODE
          zclEZMode_InvokeData_t ezModeData;
          static uint16 clusterIDs[] = { ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT };   // only bind on the Temperature Measurement cluster
    
          // Invoke EZ-Mode
          ezModeData.endpoint = SAMPLETEMPERATURESENSOR_ENDPOINT; // endpoint on which to invoke EZ-Mode
          if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
               ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER )   ||
               ( zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE ) )
          {
            ezModeData.onNetwork = TRUE;      // node is already on the network
          }
          else
          {
            ezModeData.onNetwork = FALSE;     // node is not yet on the network
          }
          ezModeData.initiator = TRUE;        // Temperature Sensor is an initiator
          ezModeData.numActiveInClusters = 1;
          ezModeData.pActiveInClusterIDs = clusterIDs;
          ezModeData.numActiveOutClusters = 0;   // active output cluster
          ezModeData.pActiveOutClusterIDs = NULL;
          zcl_InvokeEZMode( &ezModeData );
    
    #ifdef LCD_SUPPORTED
          HalLcdWriteString( "EZMode", HAL_LCD_LINE_2 );
    #endif
    
          // NOT ZCL_EZMODE, Use EndDeviceBind
    #else
          {
            zAddrType_t dstAddr;
            dstAddr.addrMode = Addr16Bit;
            dstAddr.addr.shortAddr = 0;   // Coordinator makes the EDB match
    
            // Initiate an End Device Bind Request, this bind request will
            // only use a cluster list that is important to binding.
            HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
            ZDP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(),
                                  SAMPLETEMPERATURESENSOR_ENDPOINT,
                                  ZCL_HA_PROFILE_ID,
                                  0, NULL,
                                  ZCLSAMPLETEMPERATURESENSOR_BINDINGLIST, bindingOutClusters,
                                  FALSE );
          }
    #endif // ZCL_EZMODE
        }
      }
    
      if ( keys & HAL_KEY_SW_3 )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        // decrease the temperature
        if ( zclSampleTemperatureSensor_MeasuredValue > zclSampleTemperatureSensor_MinMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MeasuredValue - 100;  // considering using whole number value
        }
        else if ( zclSampleTemperatureSensor_MeasuredValue >= zclSampleTemperatureSensor_MinMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MinMeasuredValue;
        }
    
        // Send temperature information
        zclSampleTemperatureSensor_SendTemp();
      }
    
      if ( keys & HAL_KEY_SW_4 )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
             ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER ) )
        {
          // toggle permit join
          gPermitDuration = gPermitDuration ? 0 : 0xff;
          NLME_PermitJoiningRequest( gPermitDuration );
        }
      }
    
      if ( shift && ( keys & HAL_KEY_SW_5 ) )
      {
        zclSampleTemperatureSensor_BasicResetCB();
      }
      else if ( keys & HAL_KEY_SW_5 )
      {
        if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
        {
          giTemperatureSensorScreenMode = TEMPSENSE_HELPMODE;
        }
        else if ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE )
        {
    #ifdef LCD_SUPPORTED
          HalLcdWriteString( (char *)sClearLine, HAL_LCD_LINE_2 );
    #endif
          giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
        }
      }
    
      // update display
      zclSampleTemperatureSensor_LcdDisplayUpdate();
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayUpdate
     *
     * @brief   Called to update the LCD display.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayUpdate( void )
    {
      // turn on red LED for temperatures >= 24.00C
      if ( zclSampleTemperatureSensor_MeasuredValue >= 2400 )
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_OFF );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
      }
      // turn on green LED for temperatures <= 20.00C
      else if ( zclSampleTemperatureSensor_MeasuredValue <= 2000 )
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_OFF );
      }
      // turn on both red and green LEDs for temperatures between 20.00C and 24.00C
      else
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
      }
    
      if ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE )
      {
        zclSampleTemperatureSensor_LcdDisplayHelpMode();
      }
      else
      {
        zclSampleTemperatureSensor_LcdDisplayMainMode();
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayMainMode
     *
     * @brief   Called to display the main screen on the LCD.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayMainMode( void )
    {
      char sDisplayTemp[16];
    
      if ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD )
      {
        zclHA_LcdStatusLine1( 0 );
      }
      else if ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER )
      {
        zclHA_LcdStatusLine1( 1 );
      }
      else if ( zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE )
      {
        zclHA_LcdStatusLine1( 2 );
      }
    
      // display current temperature
      osal_memcpy(sDisplayTemp, "TEMP: ", 6);
      _ltoa( ( zclSampleTemperatureSensor_MeasuredValue / 100 ), (void *)(&sDisplayTemp[6]), 10 );   // convert temperature to whole number
      osal_memcpy( &sDisplayTemp[8], "C", 2 );
    #ifdef LCD_SUPPORTED
      HalLcdWriteString( (char *)sDisplayTemp, HAL_LCD_LINE_2 );
    #endif
    
    #ifdef LCD_SUPPORTED
      if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
           ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER ) )
      {
        // display help key with permit join status
        if ( gPermitDuration )
        {
          HalLcdWriteString( "SW5: Help      *", HAL_LCD_LINE_3 );
        }
        else
        {
          HalLcdWriteString( "SW5: Help       ", HAL_LCD_LINE_3 );
        }
      }
      else
      {
        // display help key
        HalLcdWriteString( (char *)sSwHelp, HAL_LCD_LINE_3 );
      }
    #endif
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayHelpMode
     *
     * @brief   Called to display the SW options on the LCD.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayHelpMode( void )
    {
    #ifdef LCD_SUPPORTED
      HalLcdWriteString( (char *)sSwTempUp, HAL_LCD_LINE_1 );
      HalLcdWriteString( (char *)sSwEZMode, HAL_LCD_LINE_2 );
      HalLcdWriteString( (char *)sSwTempDown, HAL_LCD_LINE_3 );
    #endif
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_SendTemp
     *
     * @brief   Called to send current temperature information to the thermostat
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_SendTemp( void )
    {
    #ifdef ZCL_REPORT
      zclReportCmd_t *pReportCmd;
      //zclSampleTemperatureSensor_MeasuredValue = getTemperature();
      pReportCmd = osal_mem_alloc( sizeof(zclReportCmd_t) + sizeof(zclReport_t) );
      if ( pReportCmd != NULL )
      {
        pReportCmd->numAttr = 1;
        pReportCmd->attrList[0].attrID = ATTRID_MS_TEMPERATURE_MEASURED_VALUE;
        pReportCmd->attrList[0].dataType = ZCL_DATATYPE_INT16;
        pReportCmd->attrList[0].attrData = (void *)(&zclSampleTemperatureSensor_MeasuredValue);
    
        zcl_SendReportCmd( SAMPLETEMPERATURESENSOR_ENDPOINT, &zclSampleTemperatureSensor_DstAddr,
                           ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT,
                           pReportCmd, ZCL_FRAME_SERVER_CLIENT_DIR, TRUE, zclSampleTemperatureSensorSeqNum++ );
      }
    
      osal_mem_free( pReportCmd );
    #endif  // ZCL_REPORT
    }
    
    /*********************************************************************
     * @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      zclSampleTemperatureSensor_BasicResetCB
     *
     * @brief   Callback from the ZCL General Cluster Library
     *          to set all the Basic Cluster attributes to default values.
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_BasicResetCB( void )
    {
      // Put device back to factory default settings
      zgWriteStartupOptions( ZG_STARTUP_SET, 3 );   // bit set both default configuration and default network
    
      // restart device
      MT_SysCommandProcessing( aProcessCmd );
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_IdentifyCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an Identity Command for this application.
     *
     * @param   srcAddr - source address and endpoint of the response message
     * @param   identifyTime - the number of seconds to identify yourself
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_IdentifyCB( zclIdentify_t *pCmd )
    {
      zclSampleTemperatureSensor_IdentifyTime = pCmd->identifyTime;
      zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_IdentifyQueryRspCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an Identity Query Response Command for this application.
     *
     * @param   srcAddr - requestor's address
     * @param   timeout - number of seconds to identify yourself (valid for query response)
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp )
    {
      (void)pRsp;
    #ifdef ZCL_EZMODE
      {
        zclEZMode_ActionData_t data;
        data.pIdentifyQueryRsp = pRsp;
        zcl_EZModeAction ( EZMODE_ACTION_IDENTIFY_QUERY_RSP, &data );
      }
    #endif
    }
    
    /******************************************************************************
     *
     *  Functions for processing ZCL Foundation incoming Command/Response messages
     *
     *****************************************************************************/
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessIncomingMsg
     *
     * @brief   Process ZCL Foundation incoming message
     *
     * @param   pInMsg - pointer to the received message
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_ProcessIncomingMsg( zclIncomingMsg_t *pInMsg)
    {
      switch ( pInMsg->zclHdr.commandID )
      {
    #ifdef ZCL_READ
        case ZCL_CMD_READ_RSP:
          zclSampleTemperatureSensor_ProcessInReadRspCmd( pInMsg );
          break;
    #endif
    #ifdef ZCL_WRITE
        case ZCL_CMD_WRITE_RSP:
          zclSampleTemperatureSensor_ProcessInWriteRspCmd( pInMsg );
          break;
    #endif
    #ifdef ZCL_REPORT
        // See ZCL Test Applicaiton (zcl_testapp.c) for sample code on Attribute Reporting
        case ZCL_CMD_CONFIG_REPORT:
          //zclSampleTemperatureSensor_ProcessInConfigReportCmd( pInMsg );
          break;
    
        case ZCL_CMD_CONFIG_REPORT_RSP:
          //zclSampleTemperatureSensor_ProcessInConfigReportRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG:
          //zclSampleTemperatureSensor_ProcessInReadReportCfgCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG_RSP:
          //zclSampleTemperatureSensor_ProcessInReadReportCfgRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_REPORT:
          //zclSampleTemperatureSensor_ProcessInReportCmd( pInMsg );
          break;
    #endif
        case ZCL_CMD_DEFAULT_RSP:
          zclSampleTemperatureSensor_ProcessInDefaultRspCmd( pInMsg );
          break;
    #ifdef ZCL_DISCOVER
        case ZCL_CMD_DISCOVER_CMDS_RECEIVED_RSP:
          zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_CMDS_GEN_RSP:
          zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_RSP:
          zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_EXT_RSP:
          zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( pInMsg );
          break;
    #endif
        default:
          break;
      }
    
      if ( pInMsg->attrCmd )
      {
        osal_mem_free( pInMsg->attrCmd );
      }
    }
    
    #ifdef ZCL_READ
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInReadRspCmd
     *
     * @brief   Process the "Profile" Read Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_ProcessInReadRspCmd( zclIncomingMsg_t *pInMsg )
    {
      zclReadRspCmd_t *readRspCmd;
      uint8 i;
    
      readRspCmd = (zclReadRspCmd_t *)pInMsg->attrCmd;
      for ( i = 0; i < readRspCmd->numAttr; i++ )
      {
        // Notify the originator of the results of the original read attributes
        // attempt and, for each successfull request, the value of the requested
        // attribute
      }
    
      return ( TRUE );
    }
    #endif // ZCL_READ
    
    #ifdef ZCL_WRITE
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInWriteRspCmd
     *
     * @brief   Process the "Profile" Write Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_ProcessInWriteRspCmd( zclIncomingMsg_t *pInMsg )
    {
      zclWriteRspCmd_t *writeRspCmd;
      uint8 i;
    
      writeRspCmd = (zclWriteRspCmd_t *)pInMsg->attrCmd;
      for ( i = 0; i < writeRspCmd->numAttr; i++ )
      {
        // Notify the device of the results of the its original write attributes
        // command.
      }
    
      return ( TRUE );
    }
    #endif // ZCL_WRITE
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInDefaultRspCmd
     *
     * @brief   Process the "Profile" Default Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_ProcessInDefaultRspCmd( zclIncomingMsg_t *pInMsg )
    {
      // zclDefaultRspCmd_t *defaultRspCmd = (zclDefaultRspCmd_t *)pInMsg->attrCmd;
    
      // Device is notified of the Default Response command.
      (void)pInMsg;
    
      return ( TRUE );
    }
    
    #ifdef ZCL_DISCOVER
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd
     *
     * @brief   Process the Discover Commands Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( zclIncomingMsg_t *pInMsg )
    {
      zclDiscoverCmdsCmdRsp_t *discoverRspCmd;
      uint8 i;
    
      discoverRspCmd = (zclDiscoverCmdsCmdRsp_t *)pInMsg->attrCmd;
      for ( i = 0; i < discoverRspCmd->numCmd; i++ )
      {
        // Device is notified of the result of its attribute discovery command.
      }
    
      return ( TRUE );
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( zclIncomingMsg_t *pInMsg )
    {
      zclDiscoverAttrsRspCmd_t *discoverRspCmd;
      uint8 i;
    
      discoverRspCmd = (zclDiscoverAttrsRspCmd_t *)pInMsg->attrCmd;
      for ( i = 0; i < discoverRspCmd->numAttr; i++ )
      {
        // Device is notified of the result of its attribute discovery command.
      }
    
      return ( TRUE );
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Extended Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( zclIncomingMsg_t *pInMsg )
    {
      zclDiscoverAttrsExtRsp_t *discoverRspCmd;
      uint8 i;
    
      discoverRspCmd = (zclDiscoverAttrsExtRsp_t *)pInMsg->attrCmd;
      for ( i = 0; i < discoverRspCmd->numAttr; i++ )
      {
        // Device is notified of the result of its attribute discovery command.
      }
    
      return ( TRUE );
    }
    #endif // ZCL_DISCOVER
    
    #ifdef ZCL_EZMODE
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessZDOMsgs
     *
     * @brief   Called when this node receives a ZDO/ZDP response.
     *
     * @param   none
     *
     * @return  status
     */
    static void zclSampleTemperatureSensor_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg )
    {
      zclEZMode_ActionData_t data;
      ZDO_MatchDescRsp_t *pMatchDescRsp;
    
      // Let EZ-Mode know of the Match Descriptor Response
      if ( pMsg->clusterID == Match_Desc_rsp )
      {
        pMatchDescRsp = ZDO_ParseEPListRsp( pMsg );
        data.pMatchDescRsp = pMatchDescRsp;
        zcl_EZModeAction( EZMODE_ACTION_MATCH_DESC_RSP, &data );
        osal_mem_free( pMatchDescRsp );
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_EZModeCB
     *
     * @brief   The Application is informed of events. This can be used to show on the UI what is
    *           going on during EZ-Mode steering/finding/binding.
     *
     * @param   state - an
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_EZModeCB( zlcEZMode_State_t state, zclEZMode_CBData_t *pData )
    {
    #ifdef LCD_SUPPORTED
      char szLine[20];
      char *pStr;
      uint8 err;
    #endif
    
      // time to go into identify mode
      if ( state == EZMODE_STATE_IDENTIFYING )
      {
        zclSampleTemperatureSensor_IdentifyTime = ( EZMODE_TIME / 1000 );  // convert to seconds
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
      }
    
      // autoclosing, show what happened (success, cancelled, etc...)
      if( state == EZMODE_STATE_AUTOCLOSE )
      {
    #ifdef LCD_SUPPORTED
        pStr = NULL;
        err = pData->sAutoClose.err;
        if ( err == EZMODE_ERR_SUCCESS )
        {
          pStr = "EZMode: Success";
        }
        else if ( err == EZMODE_ERR_NOMATCH )
        {
          pStr = "EZMode: NoMatch"; // not a match made in heaven
        }
        if ( pStr )
        {
          if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
          {
            HalLcdWriteString ( pStr, HAL_LCD_LINE_2 );
          }
        }
    #endif
      }
    
      // finished, either show DstAddr/EP, or nothing (depending on success or not)
      if( state == EZMODE_STATE_FINISH )
      {
        // turn off identify mode
        zclSampleTemperatureSensor_IdentifyTime = 0;
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    
    #ifdef LCD_SUPPORTED
        // if successful, inform user which nwkaddr/ep we bound to
        pStr = NULL;
        err = pData->sFinish.err;
        if( err == EZMODE_ERR_SUCCESS )
        {
          // "EZDst:1234 EP:34"
          osal_memcpy( szLine, "EZDst:", 6 );
          zclHA_uint16toa( pData->sFinish.nwkaddr, &szLine[6] );
          osal_memcpy( &szLine[10], " EP:", 4 );
          _ltoa( pData->sFinish.ep, (void *)(&szLine[14]), 16 );  // _ltoa NULL terminates
          pStr = szLine;
        }
        else if ( err == EZMODE_ERR_BAD_PARAMETER )
        {
          pStr = "EZMode: BadParm";
        }
        else if ( err == EZMODE_ERR_CANCELLED )
        {
          pStr = "EZMode: Cancel";
        }
        else
        {
          pStr = "EZMode: TimeOut";
        }
        if ( pStr )
        {
          if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
          {
            HalLcdWriteString ( pStr, HAL_LCD_LINE_2 );
          }
        }
    #endif  // LCD_SUPPORTED
    
        // show main UI screen 3 seconds after joining network
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT, 3000 );
    
        // report current temperature reading 15 seconds after joinging the network
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
      }
    }
    #endif // ZCL_EZMODE
    
    /****************************************************************************
    ****************************************************************************/
    
    
    

    /**************************************************************************************************
      Filename:       OSAL_SampleTemperatureSensor.c
      Revised:        $Date: 2013-04-22 14:49:05 -0700 (Mon, 22 Apr 2013) $
      Revision:       $Revision: 33994 $
    
      Description:    This file contains all the settings and other functions
                      that the user should set and change.
    
    
      Copyright 2013 Texas Instruments Incorporated. All rights reserved.
    
      IMPORTANT: Your use of this Software is limited to those specific rights
      granted under the terms of a software license agreement between the user
      who downloaded the software, his/her employer (which must be your employer)
      and Texas Instruments Incorporated (the "License").  You may not use this
      Software unless you agree to abide by the terms of the License. The License
      limits your use, and you acknowledge, that the Software may not be modified,
      copied or distributed unless embedded on a Texas Instruments microcontroller
      or used solely and exclusively in conjunction with a Texas Instruments radio
      frequency transceiver, which is integrated into your product.  Other than for
      the foregoing purpose, you may not use, reproduce, copy, prepare derivative
      works of, modify, distribute, perform, display or sell this Software and/or
      its documentation for any purpose.
    
      YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
      PROVIDED �AS IS� WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
      INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
      NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
      TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
      NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
      LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
      INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
      OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
      OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
      (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
    
      Should you have any questions regarding your right to use this Software,
      contact Texas Instruments Incorporated at www.TI.com.
    **************************************************************************************************/
    
    /*********************************************************************
     * INCLUDES
     */
    
    #include "ZComDef.h"
    #include "hal_drivers.h"
    #include "OSAL.h"
    #include "OSAL_Tasks.h"
    
    #if defined ( MT_TASK )
      #include "MT.h"
      #include "MT_TASK.h"
    #endif
    
    #include "nwk.h"
    #include "APS.h"
    #include "ZDApp.h"
    #if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
      #include "ZDNwkMgr.h"
    #endif
    #if defined ( ZIGBEE_FRAGMENTATION )
      #include "aps_frag.h"
    #endif
    
    #include "zcl_sampletemperaturesensor.h"
    
    /*********************************************************************
     * GLOBAL VARIABLES
     */
    
    // The order in this table must be identical to the task initialization calls below in osalInitTask.
    const pTaskEventHandlerFn tasksArr[] = {
      macEventLoop,
      nwk_event_loop,
      Hal_ProcessEvent,
    #if defined( MT_TASK )
      MT_ProcessEvent,
    #endif
      APS_event_loop,
    #if defined ( ZIGBEE_FRAGMENTATION )
      APSF_ProcessEvent,
    #endif
      ZDApp_event_loop,
    #if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
      ZDNwkMgr_event_loop,
    #endif
      zcl_event_loop,
      zclSampleTemperatureSensor_event_loop
    };
    
    const uint8 tasksCnt = sizeof( tasksArr ) / sizeof( tasksArr[0] );
    uint16 *tasksEvents;
    
    /*********************************************************************
     * FUNCTIONS
     *********************************************************************/
    
    /*********************************************************************
     * @fn      osalInitTasks
     *
     * @brief   This function invokes the initialization function for each task.
     *
     * @param   void
     *
     * @return  none
     */
    void osalInitTasks( void )
    {
      uint8 taskID = 0;
    
      tasksEvents = (uint16 *)osal_mem_alloc( sizeof( uint16 ) * tasksCnt);
      osal_memset( tasksEvents, 0, (sizeof( uint16 ) * tasksCnt));
    
      macTaskInit( taskID++ );
      nwk_init( taskID++ );
      Hal_Init( taskID++ );
    #if defined( MT_TASK )
      MT_TaskInit( taskID++ );
    #endif
      APS_Init( taskID++ );
    #if defined ( ZIGBEE_FRAGMENTATION )
      APSF_Init( taskID++ );
    #endif
      ZDApp_Init( taskID++ );
    #if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
      ZDNwkMgr_Init( taskID++ );
    #endif
      zcl_Init( taskID++ );
      zclSampleTemperatureSensor_Init( taskID );
    }
    
    /*********************************************************************
    *********************************************************************/
    

    3782.zcl_sampletemperaturesensor.h

    /**************************************************************************************************
      Filename:       zcl_sampletemperaturesensor_data.c
      Revised:        $Date: 2014-04-15 13:37:56 -0700 (Tue, 15 Apr 2014) $
      Revision:       $Revision: 38169 $
    
    
      Description:    Zigbee Cluster Library - sample device application.
    
    
      Copyright 2013-2014 Texas Instruments Incorporated. All rights reserved.
    
      IMPORTANT: Your use of this Software is limited to those specific rights
      granted under the terms of a software license agreement between the user
      who downloaded the software, his/her employer (which must be your employer)
      and Texas Instruments Incorporated (the "License").  You may not use this
      Software unless you agree to abide by the terms of the License. The License
      limits your use, and you acknowledge, that the Software may not be modified,
      copied or distributed unless embedded on a Texas Instruments microcontroller
      or used solely and exclusively in conjunction with a Texas Instruments radio
      frequency transceiver, which is integrated into your product.  Other than for
      the foregoing purpose, you may not use, reproduce, copy, prepare derivative
      works of, modify, distribute, perform, display or sell this Software and/or
      its documentation for any purpose.
    
      YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
      PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
      INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
      NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
      TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
      NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
      LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
      INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
      OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
      OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
      (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
    
      Should you have any questions regarding your right to use this Software,
      contact Texas Instruments Incorporated at www.TI.com.
    **************************************************************************************************/
    
    /*********************************************************************
     * INCLUDES
     */
    #include "ZComDef.h"
    #include "OSAL.h"
    #include "AF.h"
    #include "ZDConfig.h"
    
    #include "zcl.h"
    #include "zcl_general.h"
    #include "zcl_ha.h"
    #include "zcl_ms.h"
    #include "zcl_ezmode.h"
    
    #include "zcl_sampletemperaturesensor.h"
    
    /*********************************************************************
     * CONSTANTS
     */
    
    #define SAMPLETEMPERATURESENSOR_DEVICE_VERSION     0
    #define SAMPLETEMPERATURESENSOR_FLAGS              0
    
    #define SAMPLETEMPERATURESENSOR_HWVERSION          1
    #define SAMPLETEMPERATURESENSOR_ZCLVERSION         1
    
    /*********************************************************************
     * TYPEDEFS
     */
    
    /*********************************************************************
     * MACROS
     */
    
    /*********************************************************************
     * GLOBAL VARIABLES
     */
    
    // Basic Cluster
    const uint8 zclSampleTemperatureSensor_HWRevision = SAMPLETEMPERATURESENSOR_HWVERSION;
    const uint8 zclSampleTemperatureSensor_ZCLVersion = SAMPLETEMPERATURESENSOR_ZCLVERSION;
    const uint8 zclSampleTemperatureSensor_ManufacturerName[] = { 16, 'T','e','x','a','s','I','n','s','t','r','u','m','e','n','t','s' };
    const uint8 zclSampleTemperatureSensor_ModelId[] = { 16, 'T','I','0','0','0','1',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' };
    const uint8 zclSampleTemperatureSensor_DateCode[] = { 16, '2','0','0','6','0','8','3','1',' ',' ',' ',' ',' ',' ',' ',' ' };
    const uint8 zclSampleTemperatureSensor_PowerSource = POWER_SOURCE_MAINS_1_PHASE;
    
    uint8 zclSampleTemperatureSensor_LocationDescription[17] = { 16, ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' };
    uint8 zclSampleTemperatureSensor_PhysicalEnvironment = 0;
    uint8 zclSampleTemperatureSensor_DeviceEnable = DEVICE_ENABLED;
    
    // Identify Cluster
    uint16 zclSampleTemperatureSensor_IdentifyTime = 0;
    
    // On/Off Cluster
    uint8  zclSampleTemperatureSensor_OnOff = LIGHT_OFF;
    
    // Temperature Sensor Cluster
    
    //int16 aadc = 30;
    //int16 zclSampleTemperatureSensor_MeasuredValue = aadc;  // 22.00C
    const int16 zclSampleTemperatureSensor_MinMeasuredValue = 1700;   // 17.00C
    const uint16 zclSampleTemperatureSensor_MaxMeasuredValue = 2700;  // 27.00C
    
    /*********************************************************************
     * ATTRIBUTE DEFINITIONS - Uses REAL cluster IDs
     */
    CONST zclAttrRec_t zclSampleTemperatureSensor_Attrs[SAMPLETEMPERATURESENSOR_MAX_ATTRIBUTES] =
    {
      // *** General Basic Cluster Attributes ***
      {
        ZCL_CLUSTER_ID_GEN_BASIC,             // Cluster IDs - defined in the foundation (ie. zcl.h)
        {  // Attribute record
          ATTRID_BASIC_HW_VERSION,            // Attribute ID - Found in Cluster Library header (ie. zcl_general.h)
          ZCL_DATATYPE_UINT8,                 // Data Type - found in zcl.h
          ACCESS_CONTROL_READ,                // Variable access control - found in zcl.h
          (void *)&zclSampleTemperatureSensor_HWRevision  // Pointer to attribute variable
        }
      },
      {
        ZCL_CLUSTER_ID_GEN_BASIC,
        { // Attribute record
          ATTRID_BASIC_ZCL_VERSION,
          ZCL_DATATYPE_UINT8,
          ACCESS_CONTROL_READ,
          (void *)&zclSampleTemperatureSensor_ZCLVersion
        }
      },
      {
        ZCL_CLUSTER_ID_GEN_BASIC,
        { // Attribute record
          ATTRID_BASIC_MANUFACTURER_NAME,
          ZCL_DATATYPE_CHAR_STR,
          ACCESS_CONTROL_READ,
          (void *)zclSampleTemperatureSensor_ManufacturerName
        }
      },
      {
        ZCL_CLUSTER_ID_GEN_BASIC,
        { // Attribute record
          ATTRID_BASIC_MODEL_ID,
          ZCL_DATATYPE_CHAR_STR,
          ACCESS_CONTROL_READ,
          (void *)zclSampleTemperatureSensor_ModelId
        }
      },
      {
        ZCL_CLUSTER_ID_GEN_BASIC,
        { // Attribute record
          ATTRID_BASIC_DATE_CODE,
          ZCL_DATATYPE_CHAR_STR,
          ACCESS_CONTROL_READ,
          (void *)zclSampleTemperatureSensor_DateCode
        }
      },
      {
        ZCL_CLUSTER_ID_GEN_BASIC,
        { // Attribute record
          ATTRID_BASIC_POWER_SOURCE,
          ZCL_DATATYPE_UINT8,
          ACCESS_CONTROL_READ,
          (void *)&zclSampleTemperatureSensor_PowerSource
        }
      },
      {
        ZCL_CLUSTER_ID_GEN_BASIC,
        { // Attribute record
          ATTRID_BASIC_LOCATION_DESC,
          ZCL_DATATYPE_CHAR_STR,
          (ACCESS_CONTROL_READ | ACCESS_CONTROL_WRITE),
          (void *)zclSampleTemperatureSensor_LocationDescription
        }
      },
      {
        ZCL_CLUSTER_ID_GEN_BASIC,
        { // Attribute record
          ATTRID_BASIC_PHYSICAL_ENV,
          ZCL_DATATYPE_UINT8,
          (ACCESS_CONTROL_READ | ACCESS_CONTROL_WRITE),
          (void *)&zclSampleTemperatureSensor_PhysicalEnvironment
        }
      },
      {
        ZCL_CLUSTER_ID_GEN_BASIC,
        { // Attribute record
          ATTRID_BASIC_DEVICE_ENABLED,
          ZCL_DATATYPE_BOOLEAN,
          (ACCESS_CONTROL_READ | ACCESS_CONTROL_WRITE),
          (void *)&zclSampleTemperatureSensor_DeviceEnable
        }
      },
    
      // *** Identify Cluster Attribute ***
      {
        ZCL_CLUSTER_ID_GEN_IDENTIFY,
        { // Attribute record
          ATTRID_IDENTIFY_TIME,
          ZCL_DATATYPE_UINT16,
          (ACCESS_CONTROL_READ | ACCESS_CONTROL_WRITE),
          (void *)&zclSampleTemperatureSensor_IdentifyTime
        }
      },
    
      // *** On/Off Cluster Attributes ***
      {
        ZCL_CLUSTER_ID_GEN_ON_OFF,
        { // Attribute record
          ATTRID_ON_OFF,
          ZCL_DATATYPE_BOOLEAN,
          ACCESS_CONTROL_READ,
          (void *)&zclSampleTemperatureSensor_OnOff
        }
      },
    
      // *** Temperature Measurement Attriubtes ***
      {
        ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT,
        { // Attribute record
          ATTRID_MS_TEMPERATURE_MEASURED_VALUE,
          ZCL_DATATYPE_INT16,
          ACCESS_CONTROL_READ,
          (void *)&zclSampleTemperatureSensor_MeasuredValue
        }
      },
      {
        ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT,
        { // Attribute record
          ATTRID_MS_TEMPERATURE_MIN_MEASURED_VALUE,
          ZCL_DATATYPE_INT16,
          ACCESS_CONTROL_READ,
          (void *)&zclSampleTemperatureSensor_MinMeasuredValue
        }
      },
      {
        ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT,
        { // Attribute record
          ATTRID_MS_TEMPERATURE_MAX_MEASURED_VALUE,
          ZCL_DATATYPE_UINT16,
          ACCESS_CONTROL_READ,
          (void *)&zclSampleTemperatureSensor_MaxMeasuredValue
        }
      },
    };
    
    /*********************************************************************
     * SIMPLE DESCRIPTOR
     */
    // This is the Cluster ID List and should be filled with Application
    // specific cluster IDs.
    #define ZCLSAMPLETEMPERATURESENSOR_MAX_INCLUSTERS       3
    const cId_t zclSampleTemperatureSensor_InClusterList[ZCLSAMPLETEMPERATURESENSOR_MAX_INCLUSTERS] =
    {
      ZCL_CLUSTER_ID_GEN_BASIC,
      ZCL_CLUSTER_ID_GEN_IDENTIFY,
      ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT
    };
    
    #define ZCLSAMPLETEMPERATURESENSOR_MAX_OUTCLUSTERS       1
    const cId_t zclSampleTemperatureSensor_OutClusterList[ZCLSAMPLETEMPERATURESENSOR_MAX_OUTCLUSTERS] =
    {
      ZCL_CLUSTER_ID_GEN_IDENTIFY
    };
    
    SimpleDescriptionFormat_t zclSampleTemperatureSensor_SimpleDesc =
    {
      SAMPLETEMPERATURESENSOR_ENDPOINT,                  //  int Endpoint;
      ZCL_HA_PROFILE_ID,                     //  uint16 AppProfId[2];
      ZCL_HA_DEVICEID_DIMMABLE_LIGHT,        //  uint16 AppDeviceId[2];
      SAMPLETEMPERATURESENSOR_DEVICE_VERSION,            //  int   AppDevVer:4;
      SAMPLETEMPERATURESENSOR_FLAGS,                     //  int   AppFlags:4;
      ZCLSAMPLETEMPERATURESENSOR_MAX_INCLUSTERS,         //  byte  AppNumInClusters;
      (cId_t *)zclSampleTemperatureSensor_InClusterList, //  byte *pAppInClusterList;
      ZCLSAMPLETEMPERATURESENSOR_MAX_OUTCLUSTERS,        //  byte  AppNumInClusters;
      (cId_t *)zclSampleTemperatureSensor_OutClusterList //  byte *pAppInClusterList;
    };
    
    /*********************************************************************
     * GLOBAL FUNCTIONS
     */
    
    /*********************************************************************
     * LOCAL FUNCTIONS
     */
    
    /****************************************************************************
    ****************************************************************************/
    
    
    

    in this pgm the ZED is displaying a value 42 in its led but it s not transmitting to coordinator node sir ?? any comment on why pl 

  • I have asked you several times.

    Do you do the following steps on your coordinator and end device?

    1. Power  on  the  SampleThermostat,  SampleTemperatureSensor,  and SampleHeatingCoolingUnit  boards.  Once  powered  up,  LCD  will  show  “Texas Instruments”, the MAC address, and the device name. When the coordinator has formed a network, the screen will appear as  shown  in  Figure  19  and the devices will be ready for
    EZ-Mode pairing.
    2. To  pair  the  devices  using  EZ-Mode,  press  SW2  on  the  SampleThermostat  and SampleHeatingCoolingUnit;  after  which,  EZMODE  will  display  on  the  2nd line  of  the LCD  screen.  Repeat  the  pairing  process  for  the  SampleThermostat  and SampleTemperatureSensor.  If  pairing  is  successful,  2nd line  will  display:  “EZMODE: SUCCESS”. If the device is the EZ-Mode “initiator”, the “target” device’s information will be displayed on the initiator’s 2
    nd line. LED1 will blink throughout this process until EZ-Mode  pairing  has  finished.  Refer  to  Figure  20.  EZMODE  device’s  information  or status will disappear after few seconds.
    3. Once all three devices have been successfully paired, current temperature reading will be sent from the SampleTemperatureSensor to the SampleThermostat every 10 seconds.

  • sir i am using only two nodes sir 

  • You don't run coordinator correctly on Thermostat. Please check it.

  • i cleared it sir .. i ran the sample codes in the nodes and they are working properly i mean the value in the ZED is displayed in the coordinator ... (ie if a value is 22 in lcd of ZED it s also shown in coordinator)

    but if i altered the code in ZED just a value 42 is diaplayed it is not varrying with respect to the temperature and it s not displayed in coordinator 

  • after i dumb the modified code the ezmode: which show while pressing sw2 is not at all showing sir 

  • Can you show me your latest zcl_sampletemperaturesensor.c, zcl_sampletemperaturesensor.h and zcl_sampletemperaturesensor_data.c

  • /**************************************************************************************************
      Filename:       OSAL_SampleTemperatureSensor.c
      Revised:        $Date: 2013-04-22 14:49:05 -0700 (Mon, 22 Apr 2013) $
      Revision:       $Revision: 33994 $
    
      Description:    This file contains all the settings and other functions
                      that the user should set and change.
    
    
      Copyright 2013 Texas Instruments Incorporated. All rights reserved.
    
      IMPORTANT: Your use of this Software is limited to those specific rights
      granted under the terms of a software license agreement between the user
      who downloaded the software, his/her employer (which must be your employer)
      and Texas Instruments Incorporated (the "License").  You may not use this
      Software unless you agree to abide by the terms of the License. The License
      limits your use, and you acknowledge, that the Software may not be modified,
      copied or distributed unless embedded on a Texas Instruments microcontroller
      or used solely and exclusively in conjunction with a Texas Instruments radio
      frequency transceiver, which is integrated into your product.  Other than for
      the foregoing purpose, you may not use, reproduce, copy, prepare derivative
      works of, modify, distribute, perform, display or sell this Software and/or
      its documentation for any purpose.
    
      YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
      PROVIDED �AS IS� WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
      INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
      NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
      TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
      NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
      LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
      INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
      OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
      OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
      (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
    
      Should you have any questions regarding your right to use this Software,
      contact Texas Instruments Incorporated at www.TI.com.
    **************************************************************************************************/
    
    /*********************************************************************
     * INCLUDES
     */
    
    #include "ZComDef.h"
    #include "hal_drivers.h"
    #include "OSAL.h"
    #include "OSAL_Tasks.h"
    
    #if defined ( MT_TASK )
      #include "MT.h"
      #include "MT_TASK.h"
    #endif
    
    #include "nwk.h"
    #include "APS.h"
    #include "ZDApp.h"
    #if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
      #include "ZDNwkMgr.h"
    #endif
    #if defined ( ZIGBEE_FRAGMENTATION )
      #include "aps_frag.h"
    #endif
    
    #include "zcl_sampletemperaturesensor.h"
    
    /*********************************************************************
     * GLOBAL VARIABLES
     */
    
    // The order in this table must be identical to the task initialization calls below in osalInitTask.
    const pTaskEventHandlerFn tasksArr[] = {
      macEventLoop,
      nwk_event_loop,
      Hal_ProcessEvent,
    #if defined( MT_TASK )
      MT_ProcessEvent,
    #endif
      APS_event_loop,
    #if defined ( ZIGBEE_FRAGMENTATION )
      APSF_ProcessEvent,
    #endif
      ZDApp_event_loop,
    #if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
      ZDNwkMgr_event_loop,
    #endif
      zcl_event_loop,
      zclSampleTemperatureSensor_event_loop
    };
    
    const uint8 tasksCnt = sizeof( tasksArr ) / sizeof( tasksArr[0] );
    uint16 *tasksEvents;
    
    /*********************************************************************
     * FUNCTIONS
     *********************************************************************/
    
    /*********************************************************************
     * @fn      osalInitTasks
     *
     * @brief   This function invokes the initialization function for each task.
     *
     * @param   void
     *
     * @return  none
     */
    void osalInitTasks( void )
    {
      uint8 taskID = 0;
    
      tasksEvents = (uint16 *)osal_mem_alloc( sizeof( uint16 ) * tasksCnt);
      osal_memset( tasksEvents, 0, (sizeof( uint16 ) * tasksCnt));
    
      macTaskInit( taskID++ );
      nwk_init( taskID++ );
      Hal_Init( taskID++ );
    #if defined( MT_TASK )
      MT_TaskInit( taskID++ );
    #endif
      APS_Init( taskID++ );
    #if defined ( ZIGBEE_FRAGMENTATION )
      APSF_Init( taskID++ );
    #endif
      ZDApp_Init( taskID++ );
    #if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
      ZDNwkMgr_Init( taskID++ );
    #endif
      zcl_Init( taskID++ );
      zclSampleTemperatureSensor_Init( taskID );
    }
    
    /*********************************************************************
    *********************************************************************/
    

    /**************************************************************************************************
      Filename:       zcl_sampletemperaturesensor.c
      Revised:        $Date: 2013-10-18 11:49:27 -0700 (Fri, 18 Oct 2013) $
      Revision:       $Revision: 35718 $
    
      Description:    Zigbee Cluster Library - sample device application.
    
    
      Copyright 2013 Texas Instruments Incorporated. All rights reserved.
    
      IMPORTANT: Your use of this Software is limited to those specific rights
      granted under the terms of a software license agreement between the user
      who downloaded the software, his/her employer (which must be your employer)
      and Texas Instruments Incorporated (the "License").  You may not use this
      Software unless you agree to abide by the terms of the License. The License
      limits your use, and you acknowledge, that the Software may not be modified,
      copied or distributed unless embedded on a Texas Instruments microcontroller
      or used solely and exclusively in conjunction with a Texas Instruments radio
      frequency transceiver, which is integrated into your product.  Other than for
      the foregoing purpose, you may not use, reproduce, copy, prepare derivative
      works of, modify, distribute, perform, display or sell this Software and/or
      its documentation for any purpose.
    
      YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
      PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
      INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
      NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
      TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
      NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
      LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
      INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
      OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
      OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
      (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
    
      Should you have any questions regarding your right to use this Software,
      contact Texas Instruments Incorporated at www.TI.com.
    **************************************************************************************************/
    
    /*********************************************************************
      This device will act as a temperature sensor. It updates the current
      temperature on the thermostat when the user sends the desired
      temperature using SW1.
    
      SCREEN MODES
      ----------------------------------------
      Main:
        - SW1: Send current temperature
        - SW2: Invoke EZMode
        - SW3: Adjust temperature
        - SW5: Go to Help screen
    
      Temperature:
        - SW1: Increase temperature
        - SW3: Decrease temperature
        - SW5: Enter temperature
      ----------------------------------------
    *********************************************************************/
    
    /*********************************************************************
     * INCLUDES
     */
    #include "ZComDef.h"
    #include "OSAL.h"
    #include "AF.h"
    #include "ZDApp.h"
    #include "ZDObject.h"
    #include "ZDProfile.h"
    #include "MT_SYS.h"
    
    #include "zcl.h"
    #include "zcl_general.h"
    #include "zcl_ha.h"
    #include "zcl_ezmode.h"
    #include "zcl_ms.h"
    #include "hal_adc.h"
    
    #include "zcl_sampletemperaturesensor.h"
    
    #include "onboard.h"
    
    /* HAL */
    #include "hal_lcd.h"
    #include "hal_led.h"
    #include "hal_key.h"
    //#include "zcl_sampletemperaturesensor_data.c"
    
    /*********************************************************************
     * MACROS
     */
    
    // how often to report temperature
    #define SAMPLETEMPERATURESENSOR_REPORT_INTERVAL   3000
    
    /*********************************************************************
     * CONSTANTS
     */
    
    /*********************************************************************
     * TYPEDEFS
     */
    /*#ifdef __IAR_SYSTEMS_ICC__
    #define  CODE   __code
    #define  XDATA  __xdata
    */
    
    /*********************************************************************
     * GLOBAL VARIABLES
     */
    byte zclSampleTemperatureSensor_TaskID;
    
    uint8 zclSampleTemperatureSensorSeqNum;
    
    static byte gPermitDuration = 0x00;
    /*void initTempSensor(void){
       DISABLE_ALL_INTERRUPTS();            //??????
       InitClock();                         //????????32M
       *((BYTE XDATA*) 0x624B) = 0x01;    //???????
       *((BYTE XDATA*) 0x61BD) = 0x01;    //???????ADC????
    }*/
    
    /*********************************************************************
     * GLOBAL FUNCTIONS
     */
    
    /*********************************************************************
     * LOCAL VARIABLES
     */
    afAddrType_t zclSampleTemperatureSensor_DstAddr;
    
    #ifdef ZCL_EZMODE
    static void zclSampleTemperatureSensor_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg );
    static void zclSampleTemperatureSensor_EZModeCB( zlcEZMode_State_t state, zclEZMode_CBData_t *pData );
    
    static const zclEZMode_RegisterData_t zclSampleTemperatureSensor_RegisterEZModeData =
    {
      &zclSampleTemperatureSensor_TaskID,
      SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT,
      SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT,
      &zclSampleTemperatureSensorSeqNum,
      zclSampleTemperatureSensor_EZModeCB
    };
    
    // NOT ZCL_EZMODE, Use EndDeviceBind
    #else
    
    static cId_t bindingOutClusters[] =
    {
      ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT
    };
    #define ZCLSAMPLETEMPERATURESENSOR_BINDINGLIST        1
    #endif
    
    devStates_t zclSampleTemperatureSensor_NwkState = DEV_INIT;
    
    uint8 giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;   // display main screen mode first
    
    static uint8 aProcessCmd[] = { 1, 0, 0, 0 }; // used for reset command, { length + cmd0 + cmd1 + data }
    
    // Test Endpoint to allow SYS_APP_MSGs
    static endPointDesc_t sampleTemperatureSensor_TestEp =
    {
      20,                                 // Test endpoint
      &zclSampleTemperatureSensor_TaskID,
      (SimpleDescriptionFormat_t *)NULL,  // No Simple description for this test endpoint
      (afNetworkLatencyReq_t)0            // No Network Latency req
    };
    
    
    /*********************************************************************
     * LOCAL FUNCTIONS
     */
    static void zclSampleTemperatureSensor_HandleKeys( byte shift, byte keys );
    static void zclSampleTemperatureSensor_BasicResetCB( void );
    static void zclSampleTemperatureSensor_IdentifyCB( zclIdentify_t *pCmd );
    static void zclSampleTemperatureSensor_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp );
    static void zclSampleTemperatureSensor_ProcessIdentifyTimeChange( void );
    //void getTemperature(void);
    // app display functions
    void zclSampleTemperatureSensor_LcdDisplayUpdate(void);
    void zclSampleTemperatureSensor_LcdDisplayMainMode(void);
    void zclSampleTemperatureSensor_LcdDisplayTempMode(void);
    void zclSampleTemperatureSensor_LcdDisplayHelpMode(void);
    //int16 getTemperature(void);
    int16 zclSampleTemperatureSensor_MeasuredValue;
    
    static void zclSampleTemperatureSensor_SendTemp(void);
    
    // Functions to process ZCL Foundation incoming Command/Response messages
    static void zclSampleTemperatureSensor_ProcessIncomingMsg( zclIncomingMsg_t *msg );
    #ifdef ZCL_READ
    static uint8 zclSampleTemperatureSensor_ProcessInReadRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    #ifdef ZCL_WRITE
    static uint8 zclSampleTemperatureSensor_ProcessInWriteRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    static uint8 zclSampleTemperatureSensor_ProcessInDefaultRspCmd( zclIncomingMsg_t *pInMsg );
    #ifdef ZCL_DISCOVER
    static uint8 zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( zclIncomingMsg_t *pInMsg );
    #endif // ZCL_DISCOVER
    
    /*********************************************************************
     * STATUS STRINGS
     */
    #ifdef LCD_SUPPORTED
    const char sClearLine[]    = " ";
    const char sDeviceName[]   = "  Temp Sensor";
    const char sSwTempUp[]     = "SW1: Raise Temp";
    const char sSwEZMode[]     = "SW2: EZ-Mode";
    const char sSwTempDown[]   = "SW3: Lower Temp";
    const char sSwHelp[]       = "SW5: Help";
    #endif
    
    /*********************************************************************
     * ZCL General Profile Callback table
     */
    static zclGeneral_AppCallbacks_t zclSampleTemperatureSensor_CmdCallbacks =
    {
      zclSampleTemperatureSensor_BasicResetCB,        // Basic Cluster Reset command
      zclSampleTemperatureSensor_IdentifyCB,          // Identify command
    #ifdef ZCL_EZMODE
      NULL,                                           // Identify EZ-Mode Invoke command
      NULL,                                           // Identify Update Commission State command
    #endif
      NULL,                                           // Identify Trigger Effect command
      zclSampleTemperatureSensor_IdentifyQueryRspCB,  // Identify Query Response command
      NULL,             				                      // On/Off cluster command
      NULL,                                           // On/Off cluster enhanced command Off with Effect
      NULL,                                           // On/Off cluster enhanced command On with Recall Global Scene
      NULL,                                           // On/Off cluster enhanced command On with Timed Off
    #ifdef ZCL_LEVEL_CTRL
      NULL,                                           // Level Control Move to Level command
      NULL,                                           // Level Control Move command
      NULL,                                           // Level Control Step command
      NULL,                                           // Level Control Stop command
    #endif
    #ifdef ZCL_GROUPS
      NULL,                                           // Group Response commands
    #endif
    #ifdef ZCL_SCENES
      NULL,                                           // Scene Store Request command
      NULL,                                           // Scene Recall Request command
      NULL,                                           // Scene Response command
    #endif
    #ifdef ZCL_ALARMS
      NULL,                                           // Alarm (Response) commands
    #endif
    #ifdef SE_UK_EXT
      NULL,                                           // Get Event Log command
      NULL,                                           // Publish Event Log command
    #endif
      NULL,                                           // RSSI Location command
      NULL                                            // RSSI Location Response command
    };
    
    /*********************************************************************
     * @fn          zclSampleTemperatureSensor_Init
     *
     * @brief       Initialization function for the zclGeneral layer.
     *
     * @param       none
     *
     * @return      none
     */
    void zclSampleTemperatureSensor_Init( byte task_id )
    {
      zclSampleTemperatureSensor_TaskID = task_id;
    
      // Set destination address to indirect
      zclSampleTemperatureSensor_DstAddr.addrMode = (afAddrMode_t)AddrNotPresent;
      zclSampleTemperatureSensor_DstAddr.endPoint = 0;
      zclSampleTemperatureSensor_DstAddr.addr.shortAddr = 0;
    
      // This app is part of the Home Automation Profile
      zclHA_Init( &zclSampleTemperatureSensor_SimpleDesc );
    
      // Register the ZCL General Cluster Library callback functions
      zclGeneral_RegisterCmdCallbacks( SAMPLETEMPERATURESENSOR_ENDPOINT, &zclSampleTemperatureSensor_CmdCallbacks );
    
      // Register the application's attribute list
      zcl_registerAttrList( SAMPLETEMPERATURESENSOR_ENDPOINT, SAMPLETEMPERATURESENSOR_MAX_ATTRIBUTES, zclSampleTemperatureSensor_Attrs );
    
      // Register the Application to receive the unprocessed Foundation command/response messages
      zcl_registerForMsg( zclSampleTemperatureSensor_TaskID );
    
    #ifdef ZCL_EZMODE
      // Register EZ-Mode
      zcl_RegisterEZMode( &zclSampleTemperatureSensor_RegisterEZModeData );
    
      // Register with the ZDO to receive Match Descriptor Responses
      ZDO_RegisterForZDOMsg(task_id, Match_Desc_rsp);
    #endif
    
      // Register for all key events - This app will handle all key events
      RegisterForKeys( zclSampleTemperatureSensor_TaskID );
    
      // Register for a test endpoint
      afRegister( &sampleTemperatureSensor_TestEp );
    
    #ifdef LCD_SUPPORTED
      // display the device name
      HalLcdWriteString( (char *)sDeviceName, HAL_LCD_LINE_3 );
    #endif
      osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
    
    }
    
    /*********************************************************************
     * @fn          zclSample_event_loop
     *
     * @brief       Event Loop Processor for zclGeneral.
     *
     * @param       none
     *
     * @return      none
     */
    uint16 zclSampleTemperatureSensor_event_loop( uint8 task_id, uint16 events )
    {
      afIncomingMSGPacket_t *MSGpkt;
    
      (void)task_id;  // Intentionally unreferenced parameter
    
      if ( events & SYS_EVENT_MSG )
      {
        while ( (MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( zclSampleTemperatureSensor_TaskID )) )
        {
          switch ( MSGpkt->hdr.event )
          {
    #ifdef ZCL_EZMODE
            case ZDO_CB_MSG:
              zclSampleTemperatureSensor_ProcessZDOMsgs( (zdoIncomingMsg_t *)MSGpkt );
              break;
    #endif
    
            case ZCL_INCOMING_MSG:
              // Incoming ZCL Foundation command/response messages
              zclSampleTemperatureSensor_ProcessIncomingMsg( (zclIncomingMsg_t *)MSGpkt );
              break;
    
            case KEY_CHANGE:
              zclSampleTemperatureSensor_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
              break;
    
            case ZDO_STATE_CHANGE:
              zclSampleTemperatureSensor_NwkState = (devStates_t)(MSGpkt->hdr.status);
    
    
              // now on the network
              if ( (zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD) ||
                   (zclSampleTemperatureSensor_NwkState == DEV_ROUTER)   ||
                   (zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE) )
              {
    #ifndef HOLD_AUTO_START
                giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
                zclSampleTemperatureSensor_LcdDisplayUpdate();
    #endif
    #ifdef ZCL_EZMODE
                zcl_EZModeAction( EZMODE_ACTION_NETWORK_STARTED, NULL );
    #endif // ZCL_EZMODE
              }
              break;
    
            default:
              break;
          }
    
          // Release the memory
          osal_msg_deallocate( (uint8 *)MSGpkt );
        }
    
        // return unprocessed events
        return (events ^ SYS_EVENT_MSG);
      }
    
      if ( events & SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT )
      {
        if ( zclSampleTemperatureSensor_IdentifyTime > 0 )
          zclSampleTemperatureSensor_IdentifyTime--;
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    
        return ( events ^ SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT );
      }
    
    #ifdef ZCL_EZMODE
      // going on to next state
      if ( events & SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT )
      {
        zcl_EZModeAction ( EZMODE_ACTION_PROCESS, NULL );   // going on to next state
        return ( events ^ SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT );
      }
    
      // the overall EZMode timer expired, so we timed out
      if ( events & SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT )
      {
        zcl_EZModeAction ( EZMODE_ACTION_TIMED_OUT, NULL ); // EZ-Mode timed out
        return ( events ^ SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT );
      }
    #endif // ZLC_EZMODE
    
      if ( events & SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
        zclSampleTemperatureSensor_LcdDisplayUpdate();
    
        return ( events ^ SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT );
      }
    
      if (events & SAMPLETEMPERATURESENSOR_ADC)
      {
       int16 AvgTemp;
      uint8   i;
      uint16  AdcValue;
      uint16  value;
      uint16  value1;
    //char i;
            //char TempValue[10]; 
            //initTempSensor(); 
      AdcValue = 0;
      for( i = 0; i < 4; i++ )
      {
    ADCIF = 0;
        ADCCON3 = (HAL_ADC_REF_125V | HAL_ADC_RESOLUTION_14 | HAL_ADC_CHN_TEMP);    // ??1.25V????,12????,AD??:?????
            while ( !ADCIF );                                              //?????ADC
                     
        value =  ADCL >> 2;                        
        value |= (((UINT16)ADCH) << 6);    
        AdcValue += value;                          
      }
      value = AdcValue >> 2;                        //????4,?????
      value1 =   (((value) >> 4) - 335);
                 //??AD?,????????
     // while(1)
      //      {
             AvgTemp = 0;
             for(i = 0 ; i < 64 ; i++)
              {
                AvgTemp += value1;              
                AvgTemp >>= 1;                              //??????2.
              }
              
        //    }
        zclSampleTemperatureSensor_MeasuredValue = AvgTemp ;
        zclSampleTemperatureSensor_LcdDisplayUpdate();
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
        return ( events ^ SAMPLETEMPERATURESENSOR_ADC );
      }
    
      if ( events & SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT )
      {
        zclSampleTemperatureSensor_SendTemp();
    
        // report current temperature reading every 10 seconds
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
    
        return ( events ^ SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT );
      }
    
      // Discard unknown events
      return 0;
    }
    
    /*int16 getTemperature(void)
    {
      int16 AvgTemp;
      uint8   i;
      uint16  AdcValue;
      uint16  value;
      uint16  value1;
    //char i;
            //char TempValue[10]; 
            //initTempSensor(); 
      AdcValue = 0;
      for( i = 0; i < 4; i++ )
      {
    ADCIF = 0;
        ADCCON3 = (HAL_ADC_REF_125V | HAL_ADC_RESOLUTION_14 | HAL_ADC_CHN_TEMP);    // ??1.25V????,12????,AD??:?????
            while ( !ADCIF );                                              //?????ADC
                     
        value =  ADCL >> 2;                        
        value |= (((UINT16)ADCH) << 6);    
        AdcValue += value;                          
      }
      value = AdcValue >> 2;                        //????4,?????
      value1 =   (((value) >> 4) - 335);
                 //??AD?,????????
      while(1)
            {
              AvgTemp = 0;
              for(i = 0 ; i < 64 ; i++)
              {
                AvgTemp += value1;              
                AvgTemp >>= 1;                              //??????2.
              }
              
            }
      //zclSampleTemperatureSensor_MeasuredValue = AvgTemp ; 
      return AvgTemp;
    }*/
    
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_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_5
     *                 HAL_KEY_SW_4
     *                 HAL_KEY_SW_3
     *                 HAL_KEY_SW_2
     *                 HAL_KEY_SW_1
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_HandleKeys( byte shift, byte keys )
    {
      if ( keys & HAL_KEY_SW_1 )
      {
        // increase temperature
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        if ( zclSampleTemperatureSensor_MeasuredValue < zclSampleTemperatureSensor_MaxMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MeasuredValue + 100;  // considering using whole number value
        }
        else if ( zclSampleTemperatureSensor_MeasuredValue >= zclSampleTemperatureSensor_MaxMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MaxMeasuredValue;
        }
    
        // Send temperature information
        zclSampleTemperatureSensor_SendTemp();
      }
    
      if ( keys & HAL_KEY_SW_2 )
      {
        if ( ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE ) ||
            ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE ) )
        {
          giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
    #ifdef ZCL_EZMODE
          zclEZMode_InvokeData_t ezModeData;
          static uint16 clusterIDs[] = { ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT };   // only bind on the Temperature Measurement cluster
    
          // Invoke EZ-Mode
          ezModeData.endpoint = SAMPLETEMPERATURESENSOR_ENDPOINT; // endpoint on which to invoke EZ-Mode
          if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
               ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER )   ||
               ( zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE ) )
          {
            ezModeData.onNetwork = TRUE;      // node is already on the network
          }
          else
          {
            ezModeData.onNetwork = FALSE;     // node is not yet on the network
          }
          ezModeData.initiator = TRUE;        // Temperature Sensor is an initiator
          ezModeData.numActiveInClusters = 1;
          ezModeData.pActiveInClusterIDs = clusterIDs;
          ezModeData.numActiveOutClusters = 0;   // active output cluster
          ezModeData.pActiveOutClusterIDs = NULL;
          zcl_InvokeEZMode( &ezModeData );
    
    #ifdef LCD_SUPPORTED
          HalLcdWriteString( "EZMode", HAL_LCD_LINE_2 );
    #endif
    
          // NOT ZCL_EZMODE, Use EndDeviceBind
    #else
          {
            zAddrType_t dstAddr;
            dstAddr.addrMode = Addr16Bit;
            dstAddr.addr.shortAddr = 0;   // Coordinator makes the EDB match
    
            // Initiate an End Device Bind Request, this bind request will
            // only use a cluster list that is important to binding.
            HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
            ZDP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(),
                                  SAMPLETEMPERATURESENSOR_ENDPOINT,
                                  ZCL_HA_PROFILE_ID,
                                  0, NULL,
                                  ZCLSAMPLETEMPERATURESENSOR_BINDINGLIST, bindingOutClusters,
                                  FALSE );
          }
    #endif // ZCL_EZMODE
        }
      }
    
      if ( keys & HAL_KEY_SW_3 )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        // decrease the temperature
        if ( zclSampleTemperatureSensor_MeasuredValue > zclSampleTemperatureSensor_MinMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MeasuredValue - 100;  // considering using whole number value
        }
        else if ( zclSampleTemperatureSensor_MeasuredValue >= zclSampleTemperatureSensor_MinMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MinMeasuredValue;
        }
    
        // Send temperature information
        zclSampleTemperatureSensor_SendTemp();
      }
    
      if ( keys & HAL_KEY_SW_4 )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
             ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER ) )
        {
          // toggle permit join
          gPermitDuration = gPermitDuration ? 0 : 0xff;
          NLME_PermitJoiningRequest( gPermitDuration );
        }
      }
    
      if ( shift && ( keys & HAL_KEY_SW_5 ) )
      {
        zclSampleTemperatureSensor_BasicResetCB();
      }
      else if ( keys & HAL_KEY_SW_5 )
      {
        if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
        {
          giTemperatureSensorScreenMode = TEMPSENSE_HELPMODE;
        }
        else if ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE )
        {
    #ifdef LCD_SUPPORTED
          HalLcdWriteString( (char *)sClearLine, HAL_LCD_LINE_2 );
    #endif
          giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
        }
      }
    
      // update display
      zclSampleTemperatureSensor_LcdDisplayUpdate();
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayUpdate
     *
     * @brief   Called to update the LCD display.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayUpdate( void )
    {
      // turn on red LED for temperatures >= 24.00C
      if ( zclSampleTemperatureSensor_MeasuredValue >= 2400 )
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_OFF );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
      }
      // turn on green LED for temperatures <= 20.00C
      else if ( zclSampleTemperatureSensor_MeasuredValue <= 2000 )
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_OFF );
      }
      // turn on both red and green LEDs for temperatures between 20.00C and 24.00C
      else
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
      }
    
      if ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE )
      {
        zclSampleTemperatureSensor_LcdDisplayHelpMode();
      }
      else
      {
        zclSampleTemperatureSensor_LcdDisplayMainMode();
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayMainMode
     *
     * @brief   Called to display the main screen on the LCD.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayMainMode( void )
    {
      char sDisplayTemp[16];
    
      if ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD )
      {
        zclHA_LcdStatusLine1( 0 );
      }
      else if ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER )
      {
        zclHA_LcdStatusLine1( 1 );
      }
      else if ( zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE )
      {
        zclHA_LcdStatusLine1( 2 );
      }
    
      // display current temperature
      osal_memcpy(sDisplayTemp, "TEMP: ", 6);
      _ltoa( ( zclSampleTemperatureSensor_MeasuredValue / 100 ), (void *)(&sDisplayTemp[6]), 10 );   // convert temperature to whole number
      osal_memcpy( &sDisplayTemp[8], "C", 2 );
    #ifdef LCD_SUPPORTED
      HalLcdWriteString( (char *)sDisplayTemp, HAL_LCD_LINE_2 );
    #endif
    
    #ifdef LCD_SUPPORTED
      if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
           ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER ) )
      {
        // display help key with permit join status
        if ( gPermitDuration )
        {
          HalLcdWriteString( "SW5: Help      *", HAL_LCD_LINE_3 );
        }
        else
        {
          HalLcdWriteString( "SW5: Help       ", HAL_LCD_LINE_3 );
        }
      }
      else
      {
        // display help key
        HalLcdWriteString( (char *)sSwHelp, HAL_LCD_LINE_3 );
      }
    #endif
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayHelpMode
     *
     * @brief   Called to display the SW options on the LCD.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayHelpMode( void )
    {
    #ifdef LCD_SUPPORTED
      HalLcdWriteString( (char *)sSwTempUp, HAL_LCD_LINE_1 );
      HalLcdWriteString( (char *)sSwEZMode, HAL_LCD_LINE_2 );
      HalLcdWriteString( (char *)sSwTempDown, HAL_LCD_LINE_3 );
    #endif
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_SendTemp
     *
     * @brief   Called to send current temperature information to the thermostat
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_SendTemp( void )
    {
    #ifdef ZCL_REPORT
      zclReportCmd_t *pReportCmd;
      //zclSampleTemperatureSensor_MeasuredValue = getTemperature();
      pReportCmd = osal_mem_alloc( sizeof(zclReportCmd_t) + sizeof(zclReport_t) );
      if ( pReportCmd != NULL )
      {
        pReportCmd->numAttr = 1;
        pReportCmd->attrList[0].attrID = ATTRID_MS_TEMPERATURE_MEASURED_VALUE;
        pReportCmd->attrList[0].dataType = ZCL_DATATYPE_INT16;
        pReportCmd->attrList[0].attrData = (void *)(&zclSampleTemperatureSensor_MeasuredValue);
    
        zcl_SendReportCmd( SAMPLETEMPERATURESENSOR_ENDPOINT, &zclSampleTemperatureSensor_DstAddr,
                           ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT,
                           pReportCmd, ZCL_FRAME_SERVER_CLIENT_DIR, TRUE, zclSampleTemperatureSensorSeqNum++ );
      }
    
      osal_mem_free( pReportCmd );
    #endif  // ZCL_REPORT
    }
    
    /*********************************************************************
     * @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      zclSampleTemperatureSensor_BasicResetCB
     *
     * @brief   Callback from the ZCL General Cluster Library
     *          to set all the Basic Cluster attributes to default values.
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_BasicResetCB( void )
    {
      // Put device back to factory default settings
      zgWriteStartupOptions( ZG_STARTUP_SET, 3 );   // bit set both default configuration and default network
    
      // restart device
      MT_SysCommandProcessing( aProcessCmd );
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_IdentifyCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an Identity Command for this application.
     *
     * @param   srcAddr - source address and endpoint of the response message
     * @param   identifyTime - the number of seconds to identify yourself
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_IdentifyCB( zclIdentify_t *pCmd )
    {
      zclSampleTemperatureSensor_IdentifyTime = pCmd->identifyTime;
      zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_IdentifyQueryRspCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an Identity Query Response Command for this application.
     *
     * @param   srcAddr - requestor's address
     * @param   timeout - number of seconds to identify yourself (valid for query response)
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp )
    {
      (void)pRsp;
    #ifdef ZCL_EZMODE
      {
        zclEZMode_ActionData_t data;
        data.pIdentifyQueryRsp = pRsp;
        zcl_EZModeAction ( EZMODE_ACTION_IDENTIFY_QUERY_RSP, &data );
      }
    #endif
    }
    
    /******************************************************************************
     *
     *  Functions for processing ZCL Foundation incoming Command/Response messages
     *
     *****************************************************************************/
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessIncomingMsg
     *
     * @brief   Process ZCL Foundation incoming message
     *
     * @param   pInMsg - pointer to the received message
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_ProcessIncomingMsg( zclIncomingMsg_t *pInMsg)
    {
      switch ( pInMsg->zclHdr.commandID )
      {
    #ifdef ZCL_READ
        case ZCL_CMD_READ_RSP:
          zclSampleTemperatureSensor_ProcessInReadRspCmd( pInMsg );
          break;
    #endif
    #ifdef ZCL_WRITE
        case ZCL_CMD_WRITE_RSP:
          zclSampleTemperatureSensor_ProcessInWriteRspCmd( pInMsg );
          break;
    #endif
    #ifdef ZCL_REPORT
        // See ZCL Test Applicaiton (zcl_testapp.c) for sample code on Attribute Reporting
        case ZCL_CMD_CONFIG_REPORT:
          //zclSampleTemperatureSensor_ProcessInConfigReportCmd( pInMsg );
          break;
    
        case ZCL_CMD_CONFIG_REPORT_RSP:
          //zclSampleTemperatureSensor_ProcessInConfigReportRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG:
          //zclSampleTemperatureSensor_ProcessInReadReportCfgCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG_RSP:
          //zclSampleTemperatureSensor_ProcessInReadReportCfgRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_REPORT:
          //zclSampleTemperatureSensor_ProcessInReportCmd( pInMsg );
          break;
    #endif
        case ZCL_CMD_DEFAULT_RSP:
          zclSampleTemperatureSensor_ProcessInDefaultRspCmd( pInMsg );
          break;
    #ifdef ZCL_DISCOVER
        case ZCL_CMD_DISCOVER_CMDS_RECEIVED_RSP:
          zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_CMDS_GEN_RSP:
          zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_RSP:
          zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_EXT_RSP:
          zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( pInMsg );
          break;
    #endif
        default:
          break;
      }
    
      if ( pInMsg->attrCmd )
      {
        osal_mem_free( pInMsg->attrCmd );
      }
    }
    
    #ifdef ZCL_READ
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInReadRspCmd
     *
     * @brief   Process the "Profile" Read Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_ProcessInReadRspCmd( zclIncomingMsg_t *pInMsg )
    {
      zclReadRspCmd_t *readRspCmd;
      uint8 i;
    
      readRspCmd = (zclReadRspCmd_t *)pInMsg->attrCmd;
      for ( i = 0; i < readRspCmd->numAttr; i++ )
      {
        // Notify the originator of the results of the original read attributes
        // attempt and, for each successfull request, the value of the requested
        // attribute
      }
    
      return ( TRUE );
    }
    #endif // ZCL_READ
    
    #ifdef ZCL_WRITE
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInWriteRspCmd
     *
     * @brief   Process the "Profile" Write Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_ProcessInWriteRspCmd( zclIncomingMsg_t *pInMsg )
    {
      zclWriteRspCmd_t *writeRspCmd;
      uint8 i;
    
      writeRspCmd = (zclWriteRspCmd_t *)pInMsg->attrCmd;
      for ( i = 0; i < writeRspCmd->numAttr; i++ )
      {
        // Notify the device of the results of the its original write attributes
        // command.
      }
    
      return ( TRUE );
    }
    #endif // ZCL_WRITE
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInDefaultRspCmd
     *
     * @brief   Process the "Profile" Default Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_ProcessInDefaultRspCmd( zclIncomingMsg_t *pInMsg )
    {
      // zclDefaultRspCmd_t *defaultRspCmd = (zclDefaultRspCmd_t *)pInMsg->attrCmd;
    
      // Device is notified of the Default Response command.
      (void)pInMsg;
    
      return ( TRUE );
    }
    
    #ifdef ZCL_DISCOVER
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd
     *
     * @brief   Process the Discover Commands Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( zclIncomingMsg_t *pInMsg )
    {
      zclDiscoverCmdsCmdRsp_t *discoverRspCmd;
      uint8 i;
    
      discoverRspCmd = (zclDiscoverCmdsCmdRsp_t *)pInMsg->attrCmd;
      for ( i = 0; i < discoverRspCmd->numCmd; i++ )
      {
        // Device is notified of the result of its attribute discovery command.
      }
    
      return ( TRUE );
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( zclIncomingMsg_t *pInMsg )
    {
      zclDiscoverAttrsRspCmd_t *discoverRspCmd;
      uint8 i;
    
      discoverRspCmd = (zclDiscoverAttrsRspCmd_t *)pInMsg->attrCmd;
      for ( i = 0; i < discoverRspCmd->numAttr; i++ )
      {
        // Device is notified of the result of its attribute discovery command.
      }
    
      return ( TRUE );
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Extended Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( zclIncomingMsg_t *pInMsg )
    {
      zclDiscoverAttrsExtRsp_t *discoverRspCmd;
      uint8 i;
    
      discoverRspCmd = (zclDiscoverAttrsExtRsp_t *)pInMsg->attrCmd;
      for ( i = 0; i < discoverRspCmd->numAttr; i++ )
      {
        // Device is notified of the result of its attribute discovery command.
      }
    
      return ( TRUE );
    }
    #endif // ZCL_DISCOVER
    
    #ifdef ZCL_EZMODE
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessZDOMsgs
     *
     * @brief   Called when this node receives a ZDO/ZDP response.
     *
     * @param   none
     *
     * @return  status
     */
    static void zclSampleTemperatureSensor_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg )
    {
      zclEZMode_ActionData_t data;
      ZDO_MatchDescRsp_t *pMatchDescRsp;
    
      // Let EZ-Mode know of the Match Descriptor Response
      if ( pMsg->clusterID == Match_Desc_rsp )
      {
        pMatchDescRsp = ZDO_ParseEPListRsp( pMsg );
        data.pMatchDescRsp = pMatchDescRsp;
        zcl_EZModeAction( EZMODE_ACTION_MATCH_DESC_RSP, &data );
        osal_mem_free( pMatchDescRsp );
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_EZModeCB
     *
     * @brief   The Application is informed of events. This can be used to show on the UI what is
    *           going on during EZ-Mode steering/finding/binding.
     *
     * @param   state - an
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_EZModeCB( zlcEZMode_State_t state, zclEZMode_CBData_t *pData )
    {
    #ifdef LCD_SUPPORTED
      char szLine[20];
      char *pStr;
      uint8 err;
    #endif
    
      // time to go into identify mode
      if ( state == EZMODE_STATE_IDENTIFYING )
      {
        zclSampleTemperatureSensor_IdentifyTime = ( EZMODE_TIME / 1000 );  // convert to seconds
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
      }
    
      // autoclosing, show what happened (success, cancelled, etc...)
      if( state == EZMODE_STATE_AUTOCLOSE )
      {
    #ifdef LCD_SUPPORTED
        pStr = NULL;
        err = pData->sAutoClose.err;
        if ( err == EZMODE_ERR_SUCCESS )
        {
          pStr = "EZMode: Success";
        }
        else if ( err == EZMODE_ERR_NOMATCH )
        {
          pStr = "EZMode: NoMatch"; // not a match made in heaven
        }
        if ( pStr )
        {
          if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
          {
            HalLcdWriteString ( pStr, HAL_LCD_LINE_2 );
          }
        }
    #endif
      }
    
      // finished, either show DstAddr/EP, or nothing (depending on success or not)
      if( state == EZMODE_STATE_FINISH )
      {
        // turn off identify mode
        zclSampleTemperatureSensor_IdentifyTime = 0;
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    
    #ifdef LCD_SUPPORTED
        // if successful, inform user which nwkaddr/ep we bound to
        pStr = NULL;
        err = pData->sFinish.err;
        if( err == EZMODE_ERR_SUCCESS )
        {
          // "EZDst:1234 EP:34"
          osal_memcpy( szLine, "EZDst:", 6 );
          zclHA_uint16toa( pData->sFinish.nwkaddr, &szLine[6] );
          osal_memcpy( &szLine[10], " EP:", 4 );
          _ltoa( pData->sFinish.ep, (void *)(&szLine[14]), 16 );  // _ltoa NULL terminates
          pStr = szLine;
        }
        else if ( err == EZMODE_ERR_BAD_PARAMETER )
        {
          pStr = "EZMode: BadParm";
        }
        else if ( err == EZMODE_ERR_CANCELLED )
        {
          pStr = "EZMode: Cancel";
        }
        else
        {
          pStr = "EZMode: TimeOut";
        }
        if ( pStr )
        {
          if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
          {
            HalLcdWriteString ( pStr, HAL_LCD_LINE_2 );
          }
        }
    #endif  // LCD_SUPPORTED
    
        // show main UI screen 3 seconds after joining network
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT, 3000 );
    
        // report current temperature reading 15 seconds after joinging the network
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
      }
    }
    #endif // ZCL_EZMODE
    
    /****************************************************************************
    ****************************************************************************/
    
    
    

    1106.zcl_sampletemperaturesensor.h

    /**************************************************************************************************
      Filename:       zcl_sampletemperaturesensor_data.c
      Revised:        $Date: 2014-04-15 13:37:56 -0700 (Tue, 15 Apr 2014) $
      Revision:       $Revision: 38169 $
    
    
      Description:    Zigbee Cluster Library - sample device application.
    
    
      Copyright 2013-2014 Texas Instruments Incorporated. All rights reserved.
    
      IMPORTANT: Your use of this Software is limited to those specific rights
      granted under the terms of a software license agreement between the user
      who downloaded the software, his/her employer (which must be your employer)
      and Texas Instruments Incorporated (the "License").  You may not use this
      Software unless you agree to abide by the terms of the License. The License
      limits your use, and you acknowledge, that the Software may not be modified,
      copied or distributed unless embedded on a Texas Instruments microcontroller
      or used solely and exclusively in conjunction with a Texas Instruments radio
      frequency transceiver, which is integrated into your product.  Other than for
      the foregoing purpose, you may not use, reproduce, copy, prepare derivative
      works of, modify, distribute, perform, display or sell this Software and/or
      its documentation for any purpose.
    
      YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
      PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
      INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
      NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
      TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
      NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
      LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
      INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
      OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
      OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
      (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
    
      Should you have any questions regarding your right to use this Software,
      contact Texas Instruments Incorporated at www.TI.com.
    **************************************************************************************************/
    
    /*********************************************************************
     * INCLUDES
     */
    #include "ZComDef.h"
    #include "OSAL.h"
    #include "AF.h"
    #include "ZDConfig.h"
    
    #include "zcl.h"
    #include "zcl_general.h"
    #include "zcl_ha.h"
    #include "zcl_ms.h"
    #include "zcl_ezmode.h"
    
    #include "zcl_sampletemperaturesensor.h"
    
    /*********************************************************************
     * CONSTANTS
     */
    
    #define SAMPLETEMPERATURESENSOR_DEVICE_VERSION     0
    #define SAMPLETEMPERATURESENSOR_FLAGS              0
    
    #define SAMPLETEMPERATURESENSOR_HWVERSION          1
    #define SAMPLETEMPERATURESENSOR_ZCLVERSION         1
    
    /*********************************************************************
     * TYPEDEFS
     */
    
    /*********************************************************************
     * MACROS
     */
    
    /*********************************************************************
     * GLOBAL VARIABLES
     */
    
    // Basic Cluster
    const uint8 zclSampleTemperatureSensor_HWRevision = SAMPLETEMPERATURESENSOR_HWVERSION;
    const uint8 zclSampleTemperatureSensor_ZCLVersion = SAMPLETEMPERATURESENSOR_ZCLVERSION;
    const uint8 zclSampleTemperatureSensor_ManufacturerName[] = { 16, 'T','e','x','a','s','I','n','s','t','r','u','m','e','n','t','s' };
    const uint8 zclSampleTemperatureSensor_ModelId[] = { 16, 'T','I','0','0','0','1',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' };
    const uint8 zclSampleTemperatureSensor_DateCode[] = { 16, '2','0','0','6','0','8','3','1',' ',' ',' ',' ',' ',' ',' ',' ' };
    const uint8 zclSampleTemperatureSensor_PowerSource = POWER_SOURCE_MAINS_1_PHASE;
    
    uint8 zclSampleTemperatureSensor_LocationDescription[17] = { 16, ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' };
    uint8 zclSampleTemperatureSensor_PhysicalEnvironment = 0;
    uint8 zclSampleTemperatureSensor_DeviceEnable = DEVICE_ENABLED;
    
    // Identify Cluster
    uint16 zclSampleTemperatureSensor_IdentifyTime = 0;
    
    // On/Off Cluster
    uint8  zclSampleTemperatureSensor_OnOff = LIGHT_OFF;
    
    // Temperature Sensor Cluster
    
    //int16 aadc = 30;
    //int16 zclSampleTemperatureSensor_MeasuredValue = aadc;  // 22.00C
    const int16 zclSampleTemperatureSensor_MinMeasuredValue = 1700;   // 17.00C
    const uint16 zclSampleTemperatureSensor_MaxMeasuredValue = 2700;  // 27.00C
    
    /*********************************************************************
     * ATTRIBUTE DEFINITIONS - Uses REAL cluster IDs
     */
    CONST zclAttrRec_t zclSampleTemperatureSensor_Attrs[SAMPLETEMPERATURESENSOR_MAX_ATTRIBUTES] =
    {
      // *** General Basic Cluster Attributes ***
      {
        ZCL_CLUSTER_ID_GEN_BASIC,             // Cluster IDs - defined in the foundation (ie. zcl.h)
        {  // Attribute record
          ATTRID_BASIC_HW_VERSION,            // Attribute ID - Found in Cluster Library header (ie. zcl_general.h)
          ZCL_DATATYPE_UINT8,                 // Data Type - found in zcl.h
          ACCESS_CONTROL_READ,                // Variable access control - found in zcl.h
          (void *)&zclSampleTemperatureSensor_HWRevision  // Pointer to attribute variable
        }
      },
      {
        ZCL_CLUSTER_ID_GEN_BASIC,
        { // Attribute record
          ATTRID_BASIC_ZCL_VERSION,
          ZCL_DATATYPE_UINT8,
          ACCESS_CONTROL_READ,
          (void *)&zclSampleTemperatureSensor_ZCLVersion
        }
      },
      {
        ZCL_CLUSTER_ID_GEN_BASIC,
        { // Attribute record
          ATTRID_BASIC_MANUFACTURER_NAME,
          ZCL_DATATYPE_CHAR_STR,
          ACCESS_CONTROL_READ,
          (void *)zclSampleTemperatureSensor_ManufacturerName
        }
      },
      {
        ZCL_CLUSTER_ID_GEN_BASIC,
        { // Attribute record
          ATTRID_BASIC_MODEL_ID,
          ZCL_DATATYPE_CHAR_STR,
          ACCESS_CONTROL_READ,
          (void *)zclSampleTemperatureSensor_ModelId
        }
      },
      {
        ZCL_CLUSTER_ID_GEN_BASIC,
        { // Attribute record
          ATTRID_BASIC_DATE_CODE,
          ZCL_DATATYPE_CHAR_STR,
          ACCESS_CONTROL_READ,
          (void *)zclSampleTemperatureSensor_DateCode
        }
      },
      {
        ZCL_CLUSTER_ID_GEN_BASIC,
        { // Attribute record
          ATTRID_BASIC_POWER_SOURCE,
          ZCL_DATATYPE_UINT8,
          ACCESS_CONTROL_READ,
          (void *)&zclSampleTemperatureSensor_PowerSource
        }
      },
      {
        ZCL_CLUSTER_ID_GEN_BASIC,
        { // Attribute record
          ATTRID_BASIC_LOCATION_DESC,
          ZCL_DATATYPE_CHAR_STR,
          (ACCESS_CONTROL_READ | ACCESS_CONTROL_WRITE),
          (void *)zclSampleTemperatureSensor_LocationDescription
        }
      },
      {
        ZCL_CLUSTER_ID_GEN_BASIC,
        { // Attribute record
          ATTRID_BASIC_PHYSICAL_ENV,
          ZCL_DATATYPE_UINT8,
          (ACCESS_CONTROL_READ | ACCESS_CONTROL_WRITE),
          (void *)&zclSampleTemperatureSensor_PhysicalEnvironment
        }
      },
      {
        ZCL_CLUSTER_ID_GEN_BASIC,
        { // Attribute record
          ATTRID_BASIC_DEVICE_ENABLED,
          ZCL_DATATYPE_BOOLEAN,
          (ACCESS_CONTROL_READ | ACCESS_CONTROL_WRITE),
          (void *)&zclSampleTemperatureSensor_DeviceEnable
        }
      },
    
      // *** Identify Cluster Attribute ***
      {
        ZCL_CLUSTER_ID_GEN_IDENTIFY,
        { // Attribute record
          ATTRID_IDENTIFY_TIME,
          ZCL_DATATYPE_UINT16,
          (ACCESS_CONTROL_READ | ACCESS_CONTROL_WRITE),
          (void *)&zclSampleTemperatureSensor_IdentifyTime
        }
      },
    
      // *** On/Off Cluster Attributes ***
      {
        ZCL_CLUSTER_ID_GEN_ON_OFF,
        { // Attribute record
          ATTRID_ON_OFF,
          ZCL_DATATYPE_BOOLEAN,
          ACCESS_CONTROL_READ,
          (void *)&zclSampleTemperatureSensor_OnOff
        }
      },
    
      // *** Temperature Measurement Attriubtes ***
      {
        ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT,
        { // Attribute record
          ATTRID_MS_TEMPERATURE_MEASURED_VALUE,
          ZCL_DATATYPE_INT16,
          ACCESS_CONTROL_READ,
          (void *)&zclSampleTemperatureSensor_MeasuredValue
        }
      },
      {
        ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT,
        { // Attribute record
          ATTRID_MS_TEMPERATURE_MIN_MEASURED_VALUE,
          ZCL_DATATYPE_INT16,
          ACCESS_CONTROL_READ,
          (void *)&zclSampleTemperatureSensor_MinMeasuredValue
        }
      },
      {
        ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT,
        { // Attribute record
          ATTRID_MS_TEMPERATURE_MAX_MEASURED_VALUE,
          ZCL_DATATYPE_UINT16,
          ACCESS_CONTROL_READ,
          (void *)&zclSampleTemperatureSensor_MaxMeasuredValue
        }
      },
    };
    
    /*********************************************************************
     * SIMPLE DESCRIPTOR
     */
    // This is the Cluster ID List and should be filled with Application
    // specific cluster IDs.
    #define ZCLSAMPLETEMPERATURESENSOR_MAX_INCLUSTERS       3
    const cId_t zclSampleTemperatureSensor_InClusterList[ZCLSAMPLETEMPERATURESENSOR_MAX_INCLUSTERS] =
    {
      ZCL_CLUSTER_ID_GEN_BASIC,
      ZCL_CLUSTER_ID_GEN_IDENTIFY,
      ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT
    };
    
    #define ZCLSAMPLETEMPERATURESENSOR_MAX_OUTCLUSTERS       1
    const cId_t zclSampleTemperatureSensor_OutClusterList[ZCLSAMPLETEMPERATURESENSOR_MAX_OUTCLUSTERS] =
    {
      ZCL_CLUSTER_ID_GEN_IDENTIFY
    };
    
    SimpleDescriptionFormat_t zclSampleTemperatureSensor_SimpleDesc =
    {
      SAMPLETEMPERATURESENSOR_ENDPOINT,                  //  int Endpoint;
      ZCL_HA_PROFILE_ID,                     //  uint16 AppProfId[2];
      ZCL_HA_DEVICEID_DIMMABLE_LIGHT,        //  uint16 AppDeviceId[2];
      SAMPLETEMPERATURESENSOR_DEVICE_VERSION,            //  int   AppDevVer:4;
      SAMPLETEMPERATURESENSOR_FLAGS,                     //  int   AppFlags:4;
      ZCLSAMPLETEMPERATURESENSOR_MAX_INCLUSTERS,         //  byte  AppNumInClusters;
      (cId_t *)zclSampleTemperatureSensor_InClusterList, //  byte *pAppInClusterList;
      ZCLSAMPLETEMPERATURESENSOR_MAX_OUTCLUSTERS,        //  byte  AppNumInClusters;
      (cId_t *)zclSampleTemperatureSensor_OutClusterList //  byte *pAppInClusterList;
    };
    
    /*********************************************************************
     * GLOBAL FUNCTIONS
     */
    
    /*********************************************************************
     * LOCAL FUNCTIONS
     */
    
    /****************************************************************************
    ****************************************************************************/
    
    
    

  • Event is a bit flag. Please don't define #define SAMPLETEMPERATURESENSOR_ADC                          0X0015. You should define it as #define SAMPLETEMPERATURESENSOR_ADC                          0X0020

  • ohh sorry sir i defined the event as 0X0020 only last time inspite of having nothing to do i tried changing its value ... i couldnt get what s wrong with the pgm sir 

    /**************************************************************************************************
      Filename:       OSAL_SampleTemperatureSensor.c
      Revised:        $Date: 2013-04-22 14:49:05 -0700 (Mon, 22 Apr 2013) $
      Revision:       $Revision: 33994 $
    
      Description:    This file contains all the settings and other functions
                      that the user should set and change.
    
    
      Copyright 2013 Texas Instruments Incorporated. All rights reserved.
    
      IMPORTANT: Your use of this Software is limited to those specific rights
      granted under the terms of a software license agreement between the user
      who downloaded the software, his/her employer (which must be your employer)
      and Texas Instruments Incorporated (the "License").  You may not use this
      Software unless you agree to abide by the terms of the License. The License
      limits your use, and you acknowledge, that the Software may not be modified,
      copied or distributed unless embedded on a Texas Instruments microcontroller
      or used solely and exclusively in conjunction with a Texas Instruments radio
      frequency transceiver, which is integrated into your product.  Other than for
      the foregoing purpose, you may not use, reproduce, copy, prepare derivative
      works of, modify, distribute, perform, display or sell this Software and/or
      its documentation for any purpose.
    
      YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
      PROVIDED �AS IS� WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
      INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
      NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
      TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
      NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
      LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
      INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
      OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
      OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
      (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
    
      Should you have any questions regarding your right to use this Software,
      contact Texas Instruments Incorporated at www.TI.com.
    **************************************************************************************************/
    
    /*********************************************************************
     * INCLUDES
     */
    
    #include "ZComDef.h"
    #include "hal_drivers.h"
    #include "OSAL.h"
    #include "OSAL_Tasks.h"
    
    #if defined ( MT_TASK )
      #include "MT.h"
      #include "MT_TASK.h"
    #endif
    
    #include "nwk.h"
    #include "APS.h"
    #include "ZDApp.h"
    #if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
      #include "ZDNwkMgr.h"
    #endif
    #if defined ( ZIGBEE_FRAGMENTATION )
      #include "aps_frag.h"
    #endif
    
    #include "zcl_sampletemperaturesensor.h"
    
    /*********************************************************************
     * GLOBAL VARIABLES
     */
    
    // The order in this table must be identical to the task initialization calls below in osalInitTask.
    const pTaskEventHandlerFn tasksArr[] = {
      macEventLoop,
      nwk_event_loop,
      Hal_ProcessEvent,
    #if defined( MT_TASK )
      MT_ProcessEvent,
    #endif
      APS_event_loop,
    #if defined ( ZIGBEE_FRAGMENTATION )
      APSF_ProcessEvent,
    #endif
      ZDApp_event_loop,
    #if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
      ZDNwkMgr_event_loop,
    #endif
      zcl_event_loop,
      zclSampleTemperatureSensor_event_loop
    };
    
    const uint8 tasksCnt = sizeof( tasksArr ) / sizeof( tasksArr[0] );
    uint16 *tasksEvents;
    
    /*********************************************************************
     * FUNCTIONS
     *********************************************************************/
    
    /*********************************************************************
     * @fn      osalInitTasks
     *
     * @brief   This function invokes the initialization function for each task.
     *
     * @param   void
     *
     * @return  none
     */
    void osalInitTasks( void )
    {
      uint8 taskID = 0;
    
      tasksEvents = (uint16 *)osal_mem_alloc( sizeof( uint16 ) * tasksCnt);
      osal_memset( tasksEvents, 0, (sizeof( uint16 ) * tasksCnt));
    
      macTaskInit( taskID++ );
      nwk_init( taskID++ );
      Hal_Init( taskID++ );
    #if defined( MT_TASK )
      MT_TaskInit( taskID++ );
    #endif
      APS_Init( taskID++ );
    #if defined ( ZIGBEE_FRAGMENTATION )
      APSF_Init( taskID++ );
    #endif
      ZDApp_Init( taskID++ );
    #if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
      ZDNwkMgr_Init( taskID++ );
    #endif
      zcl_Init( taskID++ );
      zclSampleTemperatureSensor_Init( taskID );
    }
    
    /*********************************************************************
    *********************************************************************/
    

    /**************************************************************************************************
      Filename:       zcl_sampletemperaturesensor.c
      Revised:        $Date: 2013-10-18 11:49:27 -0700 (Fri, 18 Oct 2013) $
      Revision:       $Revision: 35718 $
    
      Description:    Zigbee Cluster Library - sample device application.
    
    
      Copyright 2013 Texas Instruments Incorporated. All rights reserved.
    
      IMPORTANT: Your use of this Software is limited to those specific rights
      granted under the terms of a software license agreement between the user
      who downloaded the software, his/her employer (which must be your employer)
      and Texas Instruments Incorporated (the "License").  You may not use this
      Software unless you agree to abide by the terms of the License. The License
      limits your use, and you acknowledge, that the Software may not be modified,
      copied or distributed unless embedded on a Texas Instruments microcontroller
      or used solely and exclusively in conjunction with a Texas Instruments radio
      frequency transceiver, which is integrated into your product.  Other than for
      the foregoing purpose, you may not use, reproduce, copy, prepare derivative
      works of, modify, distribute, perform, display or sell this Software and/or
      its documentation for any purpose.
    
      YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
      PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
      INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
      NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
      TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
      NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
      LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
      INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
      OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
      OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
      (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
    
      Should you have any questions regarding your right to use this Software,
      contact Texas Instruments Incorporated at www.TI.com.
    **************************************************************************************************/
    
    /*********************************************************************
      This device will act as a temperature sensor. It updates the current
      temperature on the thermostat when the user sends the desired
      temperature using SW1.
    
      SCREEN MODES
      ----------------------------------------
      Main:
        - SW1: Send current temperature
        - SW2: Invoke EZMode
        - SW3: Adjust temperature
        - SW5: Go to Help screen
    
      Temperature:
        - SW1: Increase temperature
        - SW3: Decrease temperature
        - SW5: Enter temperature
      ----------------------------------------
    *********************************************************************/
    
    /*********************************************************************
     * INCLUDES
     */
    #include "ZComDef.h"
    #include "OSAL.h"
    #include "AF.h"
    #include "ZDApp.h"
    #include "ZDObject.h"
    #include "ZDProfile.h"
    #include "MT_SYS.h"
    
    #include "zcl.h"
    #include "zcl_general.h"
    #include "zcl_ha.h"
    #include "zcl_ezmode.h"
    #include "zcl_ms.h"
    #include "hal_adc.h"
    
    #include "zcl_sampletemperaturesensor.h"
    
    #include "onboard.h"
    
    /* HAL */
    #include "hal_lcd.h"
    #include "hal_led.h"
    #include "hal_key.h"
    //#include "zcl_sampletemperaturesensor_data.c"
    
    /*********************************************************************
     * MACROS
     */
    
    // how often to report temperature
    #define SAMPLETEMPERATURESENSOR_REPORT_INTERVAL   3000
    
    /*********************************************************************
     * CONSTANTS
     */
    
    /*********************************************************************
     * TYPEDEFS
     */
    /*#ifdef __IAR_SYSTEMS_ICC__
    #define  CODE   __code
    #define  XDATA  __xdata
    */
    
    /*********************************************************************
     * GLOBAL VARIABLES
     */
    byte zclSampleTemperatureSensor_TaskID;
    
    uint8 zclSampleTemperatureSensorSeqNum;
    
    static byte gPermitDuration = 0x00;
    /*void initTempSensor(void){
       DISABLE_ALL_INTERRUPTS();            //??????
       InitClock();                         //????????32M
       *((BYTE XDATA*) 0x624B) = 0x01;    //???????
       *((BYTE XDATA*) 0x61BD) = 0x01;    //???????ADC????
    }*/
    
    /*********************************************************************
     * GLOBAL FUNCTIONS
     */
    
    /*********************************************************************
     * LOCAL VARIABLES
     */
    afAddrType_t zclSampleTemperatureSensor_DstAddr;
    
    #ifdef ZCL_EZMODE
    static void zclSampleTemperatureSensor_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg );
    static void zclSampleTemperatureSensor_EZModeCB( zlcEZMode_State_t state, zclEZMode_CBData_t *pData );
    
    static const zclEZMode_RegisterData_t zclSampleTemperatureSensor_RegisterEZModeData =
    {
      &zclSampleTemperatureSensor_TaskID,
      SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT,
      SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT,
      &zclSampleTemperatureSensorSeqNum,
      zclSampleTemperatureSensor_EZModeCB
    };
    
    // NOT ZCL_EZMODE, Use EndDeviceBind
    #else
    
    static cId_t bindingOutClusters[] =
    {
      ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT
    };
    #define ZCLSAMPLETEMPERATURESENSOR_BINDINGLIST        1
    #endif
    
    devStates_t zclSampleTemperatureSensor_NwkState = DEV_INIT;
    
    uint8 giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;   // display main screen mode first
    
    static uint8 aProcessCmd[] = { 1, 0, 0, 0 }; // used for reset command, { length + cmd0 + cmd1 + data }
    
    // Test Endpoint to allow SYS_APP_MSGs
    static endPointDesc_t sampleTemperatureSensor_TestEp =
    {
      20,                                 // Test endpoint
      &zclSampleTemperatureSensor_TaskID,
      (SimpleDescriptionFormat_t *)NULL,  // No Simple description for this test endpoint
      (afNetworkLatencyReq_t)0            // No Network Latency req
    };
    
    
    /*********************************************************************
     * LOCAL FUNCTIONS
     */
    static void zclSampleTemperatureSensor_HandleKeys( byte shift, byte keys );
    static void zclSampleTemperatureSensor_BasicResetCB( void );
    static void zclSampleTemperatureSensor_IdentifyCB( zclIdentify_t *pCmd );
    static void zclSampleTemperatureSensor_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp );
    static void zclSampleTemperatureSensor_ProcessIdentifyTimeChange( void );
    //void getTemperature(void);
    // app display functions
    void zclSampleTemperatureSensor_LcdDisplayUpdate(void);
    void zclSampleTemperatureSensor_LcdDisplayMainMode(void);
    void zclSampleTemperatureSensor_LcdDisplayTempMode(void);
    void zclSampleTemperatureSensor_LcdDisplayHelpMode(void);
    //int16 getTemperature(void);
    int16 zclSampleTemperatureSensor_MeasuredValue;
    
    static void zclSampleTemperatureSensor_SendTemp(void);
    
    // Functions to process ZCL Foundation incoming Command/Response messages
    static void zclSampleTemperatureSensor_ProcessIncomingMsg( zclIncomingMsg_t *msg );
    #ifdef ZCL_READ
    static uint8 zclSampleTemperatureSensor_ProcessInReadRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    #ifdef ZCL_WRITE
    static uint8 zclSampleTemperatureSensor_ProcessInWriteRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    static uint8 zclSampleTemperatureSensor_ProcessInDefaultRspCmd( zclIncomingMsg_t *pInMsg );
    #ifdef ZCL_DISCOVER
    static uint8 zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( zclIncomingMsg_t *pInMsg );
    #endif // ZCL_DISCOVER
    
    /*********************************************************************
     * STATUS STRINGS
     */
    #ifdef LCD_SUPPORTED
    const char sClearLine[]    = " ";
    const char sDeviceName[]   = "  Temp Sensor";
    const char sSwTempUp[]     = "SW1: Raise Temp";
    const char sSwEZMode[]     = "SW2: EZ-Mode";
    const char sSwTempDown[]   = "SW3: Lower Temp";
    const char sSwHelp[]       = "SW5: Help";
    #endif
    
    /*********************************************************************
     * ZCL General Profile Callback table
     */
    static zclGeneral_AppCallbacks_t zclSampleTemperatureSensor_CmdCallbacks =
    {
      zclSampleTemperatureSensor_BasicResetCB,        // Basic Cluster Reset command
      zclSampleTemperatureSensor_IdentifyCB,          // Identify command
    #ifdef ZCL_EZMODE
      NULL,                                           // Identify EZ-Mode Invoke command
      NULL,                                           // Identify Update Commission State command
    #endif
      NULL,                                           // Identify Trigger Effect command
      zclSampleTemperatureSensor_IdentifyQueryRspCB,  // Identify Query Response command
      NULL,             				                      // On/Off cluster command
      NULL,                                           // On/Off cluster enhanced command Off with Effect
      NULL,                                           // On/Off cluster enhanced command On with Recall Global Scene
      NULL,                                           // On/Off cluster enhanced command On with Timed Off
    #ifdef ZCL_LEVEL_CTRL
      NULL,                                           // Level Control Move to Level command
      NULL,                                           // Level Control Move command
      NULL,                                           // Level Control Step command
      NULL,                                           // Level Control Stop command
    #endif
    #ifdef ZCL_GROUPS
      NULL,                                           // Group Response commands
    #endif
    #ifdef ZCL_SCENES
      NULL,                                           // Scene Store Request command
      NULL,                                           // Scene Recall Request command
      NULL,                                           // Scene Response command
    #endif
    #ifdef ZCL_ALARMS
      NULL,                                           // Alarm (Response) commands
    #endif
    #ifdef SE_UK_EXT
      NULL,                                           // Get Event Log command
      NULL,                                           // Publish Event Log command
    #endif
      NULL,                                           // RSSI Location command
      NULL                                            // RSSI Location Response command
    };
    
    /*********************************************************************
     * @fn          zclSampleTemperatureSensor_Init
     *
     * @brief       Initialization function for the zclGeneral layer.
     *
     * @param       none
     *
     * @return      none
     */
    void zclSampleTemperatureSensor_Init( byte task_id )
    {
      zclSampleTemperatureSensor_TaskID = task_id;
    
      // Set destination address to indirect
      zclSampleTemperatureSensor_DstAddr.addrMode = (afAddrMode_t)AddrNotPresent;
      zclSampleTemperatureSensor_DstAddr.endPoint = 0;
      zclSampleTemperatureSensor_DstAddr.addr.shortAddr = 0;
    
      // This app is part of the Home Automation Profile
      zclHA_Init( &zclSampleTemperatureSensor_SimpleDesc );
    
      // Register the ZCL General Cluster Library callback functions
      zclGeneral_RegisterCmdCallbacks( SAMPLETEMPERATURESENSOR_ENDPOINT, &zclSampleTemperatureSensor_CmdCallbacks );
    
      // Register the application's attribute list
      zcl_registerAttrList( SAMPLETEMPERATURESENSOR_ENDPOINT, SAMPLETEMPERATURESENSOR_MAX_ATTRIBUTES, zclSampleTemperatureSensor_Attrs );
    
      // Register the Application to receive the unprocessed Foundation command/response messages
      zcl_registerForMsg( zclSampleTemperatureSensor_TaskID );
    
    #ifdef ZCL_EZMODE
      // Register EZ-Mode
      zcl_RegisterEZMode( &zclSampleTemperatureSensor_RegisterEZModeData );
    
      // Register with the ZDO to receive Match Descriptor Responses
      ZDO_RegisterForZDOMsg(task_id, Match_Desc_rsp);
    #endif
    
      // Register for all key events - This app will handle all key events
      RegisterForKeys( zclSampleTemperatureSensor_TaskID );
    
      // Register for a test endpoint
      afRegister( &sampleTemperatureSensor_TestEp );
    
    #ifdef LCD_SUPPORTED
      // display the device name
      HalLcdWriteString( (char *)sDeviceName, HAL_LCD_LINE_3 );
    #endif
      osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
    
    }
    
    /*********************************************************************
     * @fn          zclSample_event_loop
     *
     * @brief       Event Loop Processor for zclGeneral.
     *
     * @param       none
     *
     * @return      none
     */
    uint16 zclSampleTemperatureSensor_event_loop( uint8 task_id, uint16 events )
    {
      afIncomingMSGPacket_t *MSGpkt;
    
      (void)task_id;  // Intentionally unreferenced parameter
    
      if ( events & SYS_EVENT_MSG )
      {
        while ( (MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( zclSampleTemperatureSensor_TaskID )) )
        {
          switch ( MSGpkt->hdr.event )
          {
    #ifdef ZCL_EZMODE
            case ZDO_CB_MSG:
              zclSampleTemperatureSensor_ProcessZDOMsgs( (zdoIncomingMsg_t *)MSGpkt );
              break;
    #endif
    
            case ZCL_INCOMING_MSG:
              // Incoming ZCL Foundation command/response messages
              zclSampleTemperatureSensor_ProcessIncomingMsg( (zclIncomingMsg_t *)MSGpkt );
              break;
    
            case KEY_CHANGE:
              zclSampleTemperatureSensor_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
              break;
    
            case ZDO_STATE_CHANGE:
              zclSampleTemperatureSensor_NwkState = (devStates_t)(MSGpkt->hdr.status);
    
    
              // now on the network
              if ( (zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD) ||
                   (zclSampleTemperatureSensor_NwkState == DEV_ROUTER)   ||
                   (zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE) )
              {
    #ifndef HOLD_AUTO_START
                giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
                zclSampleTemperatureSensor_LcdDisplayUpdate();
    #endif
    #ifdef ZCL_EZMODE
                zcl_EZModeAction( EZMODE_ACTION_NETWORK_STARTED, NULL );
    #endif // ZCL_EZMODE
              }
              break;
    
            default:
              break;
          }
    
          // Release the memory
          osal_msg_deallocate( (uint8 *)MSGpkt );
        }
    
        // return unprocessed events
        return (events ^ SYS_EVENT_MSG);
      }
    
      if ( events & SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT )
      {
        if ( zclSampleTemperatureSensor_IdentifyTime > 0 )
          zclSampleTemperatureSensor_IdentifyTime--;
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    
        return ( events ^ SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT );
      }
    
    #ifdef ZCL_EZMODE
      // going on to next state
      if ( events & SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT )
      {
        zcl_EZModeAction ( EZMODE_ACTION_PROCESS, NULL );   // going on to next state
        return ( events ^ SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT );
      }
    
      // the overall EZMode timer expired, so we timed out
      if ( events & SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT )
      {
        zcl_EZModeAction ( EZMODE_ACTION_TIMED_OUT, NULL ); // EZ-Mode timed out
        return ( events ^ SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT );
      }
    #endif // ZLC_EZMODE
    
      if ( events & SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
        zclSampleTemperatureSensor_LcdDisplayUpdate();
    
        return ( events ^ SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT );
      }
    
      if (events & SAMPLETEMPERATURESENSOR_ADC)
      {
       int16 AvgTemp;
      uint8   i;
      uint16  AdcValue;
      uint16  value;
      uint16  value1;
    //char i;
            //char TempValue[10]; 
            //initTempSensor(); 
      AdcValue = 0;
      for( i = 0; i < 4; i++ )
      {
    ADCIF = 0;
        ADCCON3 = (HAL_ADC_REF_125V | HAL_ADC_RESOLUTION_14 | HAL_ADC_CHN_TEMP);    // ??1.25V????,12????,AD??:?????
            while ( !ADCIF );                                              //?????ADC
                     
        value =  ADCL >> 2;                        
        value |= (((UINT16)ADCH) << 6);    
        AdcValue += value;                          
      }
      value = AdcValue >> 2;                        //????4,?????
      value1 =   (((value) >> 4) - 335);
                 //??AD?,????????
     // while(1)
      //      {
             AvgTemp = 0;
             for(i = 0 ; i < 64 ; i++)
              {
                AvgTemp += value1;              
                AvgTemp >>= 1;                              //??????2.
              }
              
        //    }
        zclSampleTemperatureSensor_MeasuredValue = AvgTemp ;
        zclSampleTemperatureSensor_LcdDisplayUpdate();
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
        zclSampleTemperatureSensor_SendTemp();
        return ( events ^ SAMPLETEMPERATURESENSOR_ADC );
      }
    
      if ( events & SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT )
      {
        zclSampleTemperatureSensor_SendTemp();
    
        // report current temperature reading every 10 seconds
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
    
        return ( events ^ SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT );
      }
    
      // Discard unknown events
      return 0;
    }
    
    /*int16 getTemperature(void)
    {
      int16 AvgTemp;
      uint8   i;
      uint16  AdcValue;
      uint16  value;
      uint16  value1;
    //char i;
            //char TempValue[10]; 
            //initTempSensor(); 
      AdcValue = 0;
      for( i = 0; i < 4; i++ )
      {
    ADCIF = 0;
        ADCCON3 = (HAL_ADC_REF_125V | HAL_ADC_RESOLUTION_14 | HAL_ADC_CHN_TEMP);    // ??1.25V????,12????,AD??:?????
            while ( !ADCIF );                                              //?????ADC
                     
        value =  ADCL >> 2;                        
        value |= (((UINT16)ADCH) << 6);    
        AdcValue += value;                          
      }
      value = AdcValue >> 2;                        //????4,?????
      value1 =   (((value) >> 4) - 335);
                 //??AD?,????????
      while(1)
            {
              AvgTemp = 0;
              for(i = 0 ; i < 64 ; i++)
              {
                AvgTemp += value1;              
                AvgTemp >>= 1;                              //??????2.
              }
              
            }
      //zclSampleTemperatureSensor_MeasuredValue = AvgTemp ; 
      return AvgTemp;
    }*/
    
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_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_5
     *                 HAL_KEY_SW_4
     *                 HAL_KEY_SW_3
     *                 HAL_KEY_SW_2
     *                 HAL_KEY_SW_1
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_HandleKeys( byte shift, byte keys )
    {
      if ( keys & HAL_KEY_SW_1 )
      {
        // increase temperature
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        if ( zclSampleTemperatureSensor_MeasuredValue < zclSampleTemperatureSensor_MaxMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MeasuredValue + 100;  // considering using whole number value
        }
        else if ( zclSampleTemperatureSensor_MeasuredValue >= zclSampleTemperatureSensor_MaxMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MaxMeasuredValue;
        }
    
        // Send temperature information
        zclSampleTemperatureSensor_SendTemp();
      }
    
      if ( keys & HAL_KEY_SW_2 )
      {
        if ( ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE ) ||
            ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE ) )
        {
          giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
    #ifdef ZCL_EZMODE
          zclEZMode_InvokeData_t ezModeData;
          static uint16 clusterIDs[] = { ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT };   // only bind on the Temperature Measurement cluster
    
          // Invoke EZ-Mode
          ezModeData.endpoint = SAMPLETEMPERATURESENSOR_ENDPOINT; // endpoint on which to invoke EZ-Mode
          if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
               ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER )   ||
               ( zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE ) )
          {
            ezModeData.onNetwork = TRUE;      // node is already on the network
          }
          else
          {
            ezModeData.onNetwork = FALSE;     // node is not yet on the network
          }
          ezModeData.initiator = TRUE;        // Temperature Sensor is an initiator
          ezModeData.numActiveInClusters = 1;
          ezModeData.pActiveInClusterIDs = clusterIDs;
          ezModeData.numActiveOutClusters = 0;   // active output cluster
          ezModeData.pActiveOutClusterIDs = NULL;
          zcl_InvokeEZMode( &ezModeData );
    
    #ifdef LCD_SUPPORTED
          HalLcdWriteString( "EZMode", HAL_LCD_LINE_2 );
    #endif
    
          // NOT ZCL_EZMODE, Use EndDeviceBind
    #else
          {
            zAddrType_t dstAddr;
            dstAddr.addrMode = Addr16Bit;
            dstAddr.addr.shortAddr = 0;   // Coordinator makes the EDB match
    
            // Initiate an End Device Bind Request, this bind request will
            // only use a cluster list that is important to binding.
            HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
            ZDP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(),
                                  SAMPLETEMPERATURESENSOR_ENDPOINT,
                                  ZCL_HA_PROFILE_ID,
                                  0, NULL,
                                  ZCLSAMPLETEMPERATURESENSOR_BINDINGLIST, bindingOutClusters,
                                  FALSE );
          }
    #endif // ZCL_EZMODE
        }
      }
    
      if ( keys & HAL_KEY_SW_3 )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        // decrease the temperature
        if ( zclSampleTemperatureSensor_MeasuredValue > zclSampleTemperatureSensor_MinMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MeasuredValue - 100;  // considering using whole number value
        }
        else if ( zclSampleTemperatureSensor_MeasuredValue >= zclSampleTemperatureSensor_MinMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MinMeasuredValue;
        }
    
        // Send temperature information
        zclSampleTemperatureSensor_SendTemp();
      }
    
      if ( keys & HAL_KEY_SW_4 )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
             ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER ) )
        {
          // toggle permit join
          gPermitDuration = gPermitDuration ? 0 : 0xff;
          NLME_PermitJoiningRequest( gPermitDuration );
        }
      }
    
      if ( shift && ( keys & HAL_KEY_SW_5 ) )
      {
        zclSampleTemperatureSensor_BasicResetCB();
      }
      else if ( keys & HAL_KEY_SW_5 )
      {
        if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
        {
          giTemperatureSensorScreenMode = TEMPSENSE_HELPMODE;
        }
        else if ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE )
        {
    #ifdef LCD_SUPPORTED
          HalLcdWriteString( (char *)sClearLine, HAL_LCD_LINE_2 );
    #endif
          giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
        }
      }
    
      // update display
      zclSampleTemperatureSensor_LcdDisplayUpdate();
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayUpdate
     *
     * @brief   Called to update the LCD display.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayUpdate( void )
    {
      // turn on red LED for temperatures >= 24.00C
      if ( zclSampleTemperatureSensor_MeasuredValue >= 2400 )
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_OFF );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
      }
      // turn on green LED for temperatures <= 20.00C
      else if ( zclSampleTemperatureSensor_MeasuredValue <= 2000 )
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_OFF );
      }
      // turn on both red and green LEDs for temperatures between 20.00C and 24.00C
      else
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
      }
    
      if ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE )
      {
        zclSampleTemperatureSensor_LcdDisplayHelpMode();
      }
      else
      {
        zclSampleTemperatureSensor_LcdDisplayMainMode();
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayMainMode
     *
     * @brief   Called to display the main screen on the LCD.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayMainMode( void )
    {
      char sDisplayTemp[16];
    
      if ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD )
      {
        zclHA_LcdStatusLine1( 0 );
      }
      else if ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER )
      {
        zclHA_LcdStatusLine1( 1 );
      }
      else if ( zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE )
      {
        zclHA_LcdStatusLine1( 2 );
      }
    
      // display current temperature
      osal_memcpy(sDisplayTemp, "TEMP: ", 6);
      _ltoa( ( zclSampleTemperatureSensor_MeasuredValue / 100 ), (void *)(&sDisplayTemp[6]), 10 );   // convert temperature to whole number
      osal_memcpy( &sDisplayTemp[8], "C", 2 );
    #ifdef LCD_SUPPORTED
      HalLcdWriteString( (char *)sDisplayTemp, HAL_LCD_LINE_2 );
    #endif
    
    #ifdef LCD_SUPPORTED
      if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
           ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER ) )
      {
        // display help key with permit join status
        if ( gPermitDuration )
        {
          HalLcdWriteString( "SW5: Help      *", HAL_LCD_LINE_3 );
        }
        else
        {
          HalLcdWriteString( "SW5: Help       ", HAL_LCD_LINE_3 );
        }
      }
      else
      {
        // display help key
        HalLcdWriteString( (char *)sSwHelp, HAL_LCD_LINE_3 );
      }
    #endif
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayHelpMode
     *
     * @brief   Called to display the SW options on the LCD.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayHelpMode( void )
    {
    #ifdef LCD_SUPPORTED
      HalLcdWriteString( (char *)sSwTempUp, HAL_LCD_LINE_1 );
      HalLcdWriteString( (char *)sSwEZMode, HAL_LCD_LINE_2 );
      HalLcdWriteString( (char *)sSwTempDown, HAL_LCD_LINE_3 );
    #endif
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_SendTemp
     *
     * @brief   Called to send current temperature information to the thermostat
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_SendTemp( void )
    {
    #ifdef ZCL_REPORT
      zclReportCmd_t *pReportCmd;
      //zclSampleTemperatureSensor_MeasuredValue = getTemperature();
      pReportCmd = osal_mem_alloc( sizeof(zclReportCmd_t) + sizeof(zclReport_t) );
      if ( pReportCmd != NULL )
      {
        pReportCmd->numAttr = 1;
        pReportCmd->attrList[0].attrID = ATTRID_MS_TEMPERATURE_MEASURED_VALUE;
        pReportCmd->attrList[0].dataType = ZCL_DATATYPE_INT16;
        pReportCmd->attrList[0].attrData = (void *)(&zclSampleTemperatureSensor_MeasuredValue);
    
        zcl_SendReportCmd( SAMPLETEMPERATURESENSOR_ENDPOINT, &zclSampleTemperatureSensor_DstAddr,
                           ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT,
                           pReportCmd, ZCL_FRAME_SERVER_CLIENT_DIR, TRUE, zclSampleTemperatureSensorSeqNum++ );
      }
    
      osal_mem_free( pReportCmd );
    #endif  // ZCL_REPORT
    }
    
    /*********************************************************************
     * @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      zclSampleTemperatureSensor_BasicResetCB
     *
     * @brief   Callback from the ZCL General Cluster Library
     *          to set all the Basic Cluster attributes to default values.
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_BasicResetCB( void )
    {
      // Put device back to factory default settings
      zgWriteStartupOptions( ZG_STARTUP_SET, 3 );   // bit set both default configuration and default network
    
      // restart device
      MT_SysCommandProcessing( aProcessCmd );
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_IdentifyCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an Identity Command for this application.
     *
     * @param   srcAddr - source address and endpoint of the response message
     * @param   identifyTime - the number of seconds to identify yourself
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_IdentifyCB( zclIdentify_t *pCmd )
    {
      zclSampleTemperatureSensor_IdentifyTime = pCmd->identifyTime;
      zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_IdentifyQueryRspCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an Identity Query Response Command for this application.
     *
     * @param   srcAddr - requestor's address
     * @param   timeout - number of seconds to identify yourself (valid for query response)
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp )
    {
      (void)pRsp;
    #ifdef ZCL_EZMODE
      {
        zclEZMode_ActionData_t data;
        data.pIdentifyQueryRsp = pRsp;
        zcl_EZModeAction ( EZMODE_ACTION_IDENTIFY_QUERY_RSP, &data );
      }
    #endif
    }
    
    /******************************************************************************
     *
     *  Functions for processing ZCL Foundation incoming Command/Response messages
     *
     *****************************************************************************/
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessIncomingMsg
     *
     * @brief   Process ZCL Foundation incoming message
     *
     * @param   pInMsg - pointer to the received message
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_ProcessIncomingMsg( zclIncomingMsg_t *pInMsg)
    {
      switch ( pInMsg->zclHdr.commandID )
      {
    #ifdef ZCL_READ
        case ZCL_CMD_READ_RSP:
          zclSampleTemperatureSensor_ProcessInReadRspCmd( pInMsg );
          break;
    #endif
    #ifdef ZCL_WRITE
        case ZCL_CMD_WRITE_RSP:
          zclSampleTemperatureSensor_ProcessInWriteRspCmd( pInMsg );
          break;
    #endif
    #ifdef ZCL_REPORT
        // See ZCL Test Applicaiton (zcl_testapp.c) for sample code on Attribute Reporting
        case ZCL_CMD_CONFIG_REPORT:
          //zclSampleTemperatureSensor_ProcessInConfigReportCmd( pInMsg );
          break;
    
        case ZCL_CMD_CONFIG_REPORT_RSP:
          //zclSampleTemperatureSensor_ProcessInConfigReportRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG:
          //zclSampleTemperatureSensor_ProcessInReadReportCfgCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG_RSP:
          //zclSampleTemperatureSensor_ProcessInReadReportCfgRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_REPORT:
          //zclSampleTemperatureSensor_ProcessInReportCmd( pInMsg );
          break;
    #endif
        case ZCL_CMD_DEFAULT_RSP:
          zclSampleTemperatureSensor_ProcessInDefaultRspCmd( pInMsg );
          break;
    #ifdef ZCL_DISCOVER
        case ZCL_CMD_DISCOVER_CMDS_RECEIVED_RSP:
          zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_CMDS_GEN_RSP:
          zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_RSP:
          zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_EXT_RSP:
          zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( pInMsg );
          break;
    #endif
        default:
          break;
      }
    
      if ( pInMsg->attrCmd )
      {
        osal_mem_free( pInMsg->attrCmd );
      }
    }
    
    #ifdef ZCL_READ
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInReadRspCmd
     *
     * @brief   Process the "Profile" Read Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_ProcessInReadRspCmd( zclIncomingMsg_t *pInMsg )
    {
      zclReadRspCmd_t *readRspCmd;
      uint8 i;
    
      readRspCmd = (zclReadRspCmd_t *)pInMsg->attrCmd;
      for ( i = 0; i < readRspCmd->numAttr; i++ )
      {
        // Notify the originator of the results of the original read attributes
        // attempt and, for each successfull request, the value of the requested
        // attribute
      }
    
      return ( TRUE );
    }
    #endif // ZCL_READ
    
    #ifdef ZCL_WRITE
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInWriteRspCmd
     *
     * @brief   Process the "Profile" Write Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_ProcessInWriteRspCmd( zclIncomingMsg_t *pInMsg )
    {
      zclWriteRspCmd_t *writeRspCmd;
      uint8 i;
    
      writeRspCmd = (zclWriteRspCmd_t *)pInMsg->attrCmd;
      for ( i = 0; i < writeRspCmd->numAttr; i++ )
      {
        // Notify the device of the results of the its original write attributes
        // command.
      }
    
      return ( TRUE );
    }
    #endif // ZCL_WRITE
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInDefaultRspCmd
     *
     * @brief   Process the "Profile" Default Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_ProcessInDefaultRspCmd( zclIncomingMsg_t *pInMsg )
    {
      // zclDefaultRspCmd_t *defaultRspCmd = (zclDefaultRspCmd_t *)pInMsg->attrCmd;
    
      // Device is notified of the Default Response command.
      (void)pInMsg;
    
      return ( TRUE );
    }
    
    #ifdef ZCL_DISCOVER
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd
     *
     * @brief   Process the Discover Commands Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( zclIncomingMsg_t *pInMsg )
    {
      zclDiscoverCmdsCmdRsp_t *discoverRspCmd;
      uint8 i;
    
      discoverRspCmd = (zclDiscoverCmdsCmdRsp_t *)pInMsg->attrCmd;
      for ( i = 0; i < discoverRspCmd->numCmd; i++ )
      {
        // Device is notified of the result of its attribute discovery command.
      }
    
      return ( TRUE );
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( zclIncomingMsg_t *pInMsg )
    {
      zclDiscoverAttrsRspCmd_t *discoverRspCmd;
      uint8 i;
    
      discoverRspCmd = (zclDiscoverAttrsRspCmd_t *)pInMsg->attrCmd;
      for ( i = 0; i < discoverRspCmd->numAttr; i++ )
      {
        // Device is notified of the result of its attribute discovery command.
      }
    
      return ( TRUE );
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Extended Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( zclIncomingMsg_t *pInMsg )
    {
      zclDiscoverAttrsExtRsp_t *discoverRspCmd;
      uint8 i;
    
      discoverRspCmd = (zclDiscoverAttrsExtRsp_t *)pInMsg->attrCmd;
      for ( i = 0; i < discoverRspCmd->numAttr; i++ )
      {
        // Device is notified of the result of its attribute discovery command.
      }
    
      return ( TRUE );
    }
    #endif // ZCL_DISCOVER
    
    #ifdef ZCL_EZMODE
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessZDOMsgs
     *
     * @brief   Called when this node receives a ZDO/ZDP response.
     *
     * @param   none
     *
     * @return  status
     */
    static void zclSampleTemperatureSensor_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg )
    {
      zclEZMode_ActionData_t data;
      ZDO_MatchDescRsp_t *pMatchDescRsp;
    
      // Let EZ-Mode know of the Match Descriptor Response
      if ( pMsg->clusterID == Match_Desc_rsp )
      {
        pMatchDescRsp = ZDO_ParseEPListRsp( pMsg );
        data.pMatchDescRsp = pMatchDescRsp;
        zcl_EZModeAction( EZMODE_ACTION_MATCH_DESC_RSP, &data );
        osal_mem_free( pMatchDescRsp );
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_EZModeCB
     *
     * @brief   The Application is informed of events. This can be used to show on the UI what is
    *           going on during EZ-Mode steering/finding/binding.
     *
     * @param   state - an
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_EZModeCB( zlcEZMode_State_t state, zclEZMode_CBData_t *pData )
    {
    #ifdef LCD_SUPPORTED
      char szLine[20];
      char *pStr;
      uint8 err;
    #endif
    
      // time to go into identify mode
      if ( state == EZMODE_STATE_IDENTIFYING )
      {
        zclSampleTemperatureSensor_IdentifyTime = ( EZMODE_TIME / 1000 );  // convert to seconds
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
      }
    
      // autoclosing, show what happened (success, cancelled, etc...)
      if( state == EZMODE_STATE_AUTOCLOSE )
      {
    #ifdef LCD_SUPPORTED
        pStr = NULL;
        err = pData->sAutoClose.err;
        if ( err == EZMODE_ERR_SUCCESS )
        {
          pStr = "EZMode: Success";
        }
        else if ( err == EZMODE_ERR_NOMATCH )
        {
          pStr = "EZMode: NoMatch"; // not a match made in heaven
        }
        if ( pStr )
        {
          if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
          {
            HalLcdWriteString ( pStr, HAL_LCD_LINE_2 );
          }
        }
    #endif
      }
    
      // finished, either show DstAddr/EP, or nothing (depending on success or not)
      if( state == EZMODE_STATE_FINISH )
      {
        // turn off identify mode
        zclSampleTemperatureSensor_IdentifyTime = 0;
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    
    #ifdef LCD_SUPPORTED
        // if successful, inform user which nwkaddr/ep we bound to
        pStr = NULL;
        err = pData->sFinish.err;
        if( err == EZMODE_ERR_SUCCESS )
        {
          // "EZDst:1234 EP:34"
          osal_memcpy( szLine, "EZDst:", 6 );
          zclHA_uint16toa( pData->sFinish.nwkaddr, &szLine[6] );
          osal_memcpy( &szLine[10], " EP:", 4 );
          _ltoa( pData->sFinish.ep, (void *)(&szLine[14]), 16 );  // _ltoa NULL terminates
          pStr = szLine;
        }
        else if ( err == EZMODE_ERR_BAD_PARAMETER )
        {
          pStr = "EZMode: BadParm";
        }
        else if ( err == EZMODE_ERR_CANCELLED )
        {
          pStr = "EZMode: Cancel";
        }
        else
        {
          pStr = "EZMode: TimeOut";
        }
        if ( pStr )
        {
          if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
          {
            HalLcdWriteString ( pStr, HAL_LCD_LINE_2 );
          }
        }
    #endif  // LCD_SUPPORTED
    
        // show main UI screen 3 seconds after joining network
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT, 3000 );
    
        // report current temperature reading 15 seconds after joinging the network
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
      }
    }
    #endif // ZCL_EZMODE
    
    /****************************************************************************
    ****************************************************************************/
    
    
    

    5076.zcl_sampletemperaturesensor.h

    /**************************************************************************************************
      Filename:       zcl_sampletemperaturesensor_data.c
      Revised:        $Date: 2014-04-15 13:37:56 -0700 (Tue, 15 Apr 2014) $
      Revision:       $Revision: 38169 $
    
    
      Description:    Zigbee Cluster Library - sample device application.
    
    
      Copyright 2013-2014 Texas Instruments Incorporated. All rights reserved.
    
      IMPORTANT: Your use of this Software is limited to those specific rights
      granted under the terms of a software license agreement between the user
      who downloaded the software, his/her employer (which must be your employer)
      and Texas Instruments Incorporated (the "License").  You may not use this
      Software unless you agree to abide by the terms of the License. The License
      limits your use, and you acknowledge, that the Software may not be modified,
      copied or distributed unless embedded on a Texas Instruments microcontroller
      or used solely and exclusively in conjunction with a Texas Instruments radio
      frequency transceiver, which is integrated into your product.  Other than for
      the foregoing purpose, you may not use, reproduce, copy, prepare derivative
      works of, modify, distribute, perform, display or sell this Software and/or
      its documentation for any purpose.
    
      YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
      PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
      INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
      NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
      TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
      NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
      LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
      INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
      OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
      OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
      (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
    
      Should you have any questions regarding your right to use this Software,
      contact Texas Instruments Incorporated at www.TI.com.
    **************************************************************************************************/
    
    /*********************************************************************
     * INCLUDES
     */
    #include "ZComDef.h"
    #include "OSAL.h"
    #include "AF.h"
    #include "ZDConfig.h"
    
    #include "zcl.h"
    #include "zcl_general.h"
    #include "zcl_ha.h"
    #include "zcl_ms.h"
    #include "zcl_ezmode.h"
    
    #include "zcl_sampletemperaturesensor.h"
    
    /*********************************************************************
     * CONSTANTS
     */
    
    #define SAMPLETEMPERATURESENSOR_DEVICE_VERSION     0
    #define SAMPLETEMPERATURESENSOR_FLAGS              0
    
    #define SAMPLETEMPERATURESENSOR_HWVERSION          1
    #define SAMPLETEMPERATURESENSOR_ZCLVERSION         1
    
    /*********************************************************************
     * TYPEDEFS
     */
    
    /*********************************************************************
     * MACROS
     */
    
    /*********************************************************************
     * GLOBAL VARIABLES
     */
    
    // Basic Cluster
    const uint8 zclSampleTemperatureSensor_HWRevision = SAMPLETEMPERATURESENSOR_HWVERSION;
    const uint8 zclSampleTemperatureSensor_ZCLVersion = SAMPLETEMPERATURESENSOR_ZCLVERSION;
    const uint8 zclSampleTemperatureSensor_ManufacturerName[] = { 16, 'T','e','x','a','s','I','n','s','t','r','u','m','e','n','t','s' };
    const uint8 zclSampleTemperatureSensor_ModelId[] = { 16, 'T','I','0','0','0','1',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' };
    const uint8 zclSampleTemperatureSensor_DateCode[] = { 16, '2','0','0','6','0','8','3','1',' ',' ',' ',' ',' ',' ',' ',' ' };
    const uint8 zclSampleTemperatureSensor_PowerSource = POWER_SOURCE_MAINS_1_PHASE;
    
    uint8 zclSampleTemperatureSensor_LocationDescription[17] = { 16, ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' };
    uint8 zclSampleTemperatureSensor_PhysicalEnvironment = 0;
    uint8 zclSampleTemperatureSensor_DeviceEnable = DEVICE_ENABLED;
    
    // Identify Cluster
    uint16 zclSampleTemperatureSensor_IdentifyTime = 0;
    
    // On/Off Cluster
    uint8  zclSampleTemperatureSensor_OnOff = LIGHT_OFF;
    
    // Temperature Sensor Cluster
    
    //int16 aadc = 30;
    //int16 zclSampleTemperatureSensor_MeasuredValue = aadc;  // 22.00C
    const int16 zclSampleTemperatureSensor_MinMeasuredValue = 1700;   // 17.00C
    const uint16 zclSampleTemperatureSensor_MaxMeasuredValue = 2700;  // 27.00C
    
    /*********************************************************************
     * ATTRIBUTE DEFINITIONS - Uses REAL cluster IDs
     */
    CONST zclAttrRec_t zclSampleTemperatureSensor_Attrs[SAMPLETEMPERATURESENSOR_MAX_ATTRIBUTES] =
    {
      // *** General Basic Cluster Attributes ***
      {
        ZCL_CLUSTER_ID_GEN_BASIC,             // Cluster IDs - defined in the foundation (ie. zcl.h)
        {  // Attribute record
          ATTRID_BASIC_HW_VERSION,            // Attribute ID - Found in Cluster Library header (ie. zcl_general.h)
          ZCL_DATATYPE_UINT8,                 // Data Type - found in zcl.h
          ACCESS_CONTROL_READ,                // Variable access control - found in zcl.h
          (void *)&zclSampleTemperatureSensor_HWRevision  // Pointer to attribute variable
        }
      },
      {
        ZCL_CLUSTER_ID_GEN_BASIC,
        { // Attribute record
          ATTRID_BASIC_ZCL_VERSION,
          ZCL_DATATYPE_UINT8,
          ACCESS_CONTROL_READ,
          (void *)&zclSampleTemperatureSensor_ZCLVersion
        }
      },
      {
        ZCL_CLUSTER_ID_GEN_BASIC,
        { // Attribute record
          ATTRID_BASIC_MANUFACTURER_NAME,
          ZCL_DATATYPE_CHAR_STR,
          ACCESS_CONTROL_READ,
          (void *)zclSampleTemperatureSensor_ManufacturerName
        }
      },
      {
        ZCL_CLUSTER_ID_GEN_BASIC,
        { // Attribute record
          ATTRID_BASIC_MODEL_ID,
          ZCL_DATATYPE_CHAR_STR,
          ACCESS_CONTROL_READ,
          (void *)zclSampleTemperatureSensor_ModelId
        }
      },
      {
        ZCL_CLUSTER_ID_GEN_BASIC,
        { // Attribute record
          ATTRID_BASIC_DATE_CODE,
          ZCL_DATATYPE_CHAR_STR,
          ACCESS_CONTROL_READ,
          (void *)zclSampleTemperatureSensor_DateCode
        }
      },
      {
        ZCL_CLUSTER_ID_GEN_BASIC,
        { // Attribute record
          ATTRID_BASIC_POWER_SOURCE,
          ZCL_DATATYPE_UINT8,
          ACCESS_CONTROL_READ,
          (void *)&zclSampleTemperatureSensor_PowerSource
        }
      },
      {
        ZCL_CLUSTER_ID_GEN_BASIC,
        { // Attribute record
          ATTRID_BASIC_LOCATION_DESC,
          ZCL_DATATYPE_CHAR_STR,
          (ACCESS_CONTROL_READ | ACCESS_CONTROL_WRITE),
          (void *)zclSampleTemperatureSensor_LocationDescription
        }
      },
      {
        ZCL_CLUSTER_ID_GEN_BASIC,
        { // Attribute record
          ATTRID_BASIC_PHYSICAL_ENV,
          ZCL_DATATYPE_UINT8,
          (ACCESS_CONTROL_READ | ACCESS_CONTROL_WRITE),
          (void *)&zclSampleTemperatureSensor_PhysicalEnvironment
        }
      },
      {
        ZCL_CLUSTER_ID_GEN_BASIC,
        { // Attribute record
          ATTRID_BASIC_DEVICE_ENABLED,
          ZCL_DATATYPE_BOOLEAN,
          (ACCESS_CONTROL_READ | ACCESS_CONTROL_WRITE),
          (void *)&zclSampleTemperatureSensor_DeviceEnable
        }
      },
    
      // *** Identify Cluster Attribute ***
      {
        ZCL_CLUSTER_ID_GEN_IDENTIFY,
        { // Attribute record
          ATTRID_IDENTIFY_TIME,
          ZCL_DATATYPE_UINT16,
          (ACCESS_CONTROL_READ | ACCESS_CONTROL_WRITE),
          (void *)&zclSampleTemperatureSensor_IdentifyTime
        }
      },
    
      // *** On/Off Cluster Attributes ***
      {
        ZCL_CLUSTER_ID_GEN_ON_OFF,
        { // Attribute record
          ATTRID_ON_OFF,
          ZCL_DATATYPE_BOOLEAN,
          ACCESS_CONTROL_READ,
          (void *)&zclSampleTemperatureSensor_OnOff
        }
      },
    
      // *** Temperature Measurement Attriubtes ***
      {
        ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT,
        { // Attribute record
          ATTRID_MS_TEMPERATURE_MEASURED_VALUE,
          ZCL_DATATYPE_INT16,
          ACCESS_CONTROL_READ,
          (void *)&zclSampleTemperatureSensor_MeasuredValue
        }
      },
      {
        ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT,
        { // Attribute record
          ATTRID_MS_TEMPERATURE_MIN_MEASURED_VALUE,
          ZCL_DATATYPE_INT16,
          ACCESS_CONTROL_READ,
          (void *)&zclSampleTemperatureSensor_MinMeasuredValue
        }
      },
      {
        ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT,
        { // Attribute record
          ATTRID_MS_TEMPERATURE_MAX_MEASURED_VALUE,
          ZCL_DATATYPE_UINT16,
          ACCESS_CONTROL_READ,
          (void *)&zclSampleTemperatureSensor_MaxMeasuredValue
        }
      },
    };
    
    /*********************************************************************
     * SIMPLE DESCRIPTOR
     */
    // This is the Cluster ID List and should be filled with Application
    // specific cluster IDs.
    #define ZCLSAMPLETEMPERATURESENSOR_MAX_INCLUSTERS       3
    const cId_t zclSampleTemperatureSensor_InClusterList[ZCLSAMPLETEMPERATURESENSOR_MAX_INCLUSTERS] =
    {
      ZCL_CLUSTER_ID_GEN_BASIC,
      ZCL_CLUSTER_ID_GEN_IDENTIFY,
      ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT
    };
    
    #define ZCLSAMPLETEMPERATURESENSOR_MAX_OUTCLUSTERS       1
    const cId_t zclSampleTemperatureSensor_OutClusterList[ZCLSAMPLETEMPERATURESENSOR_MAX_OUTCLUSTERS] =
    {
      ZCL_CLUSTER_ID_GEN_IDENTIFY
    };
    
    SimpleDescriptionFormat_t zclSampleTemperatureSensor_SimpleDesc =
    {
      SAMPLETEMPERATURESENSOR_ENDPOINT,                  //  int Endpoint;
      ZCL_HA_PROFILE_ID,                     //  uint16 AppProfId[2];
      ZCL_HA_DEVICEID_DIMMABLE_LIGHT,        //  uint16 AppDeviceId[2];
      SAMPLETEMPERATURESENSOR_DEVICE_VERSION,            //  int   AppDevVer:4;
      SAMPLETEMPERATURESENSOR_FLAGS,                     //  int   AppFlags:4;
      ZCLSAMPLETEMPERATURESENSOR_MAX_INCLUSTERS,         //  byte  AppNumInClusters;
      (cId_t *)zclSampleTemperatureSensor_InClusterList, //  byte *pAppInClusterList;
      ZCLSAMPLETEMPERATURESENSOR_MAX_OUTCLUSTERS,        //  byte  AppNumInClusters;
      (cId_t *)zclSampleTemperatureSensor_OutClusterList //  byte *pAppInClusterList;
    };
    
    /*********************************************************************
     * GLOBAL FUNCTIONS
     */
    
    /*********************************************************************
     * LOCAL FUNCTIONS
     */
    
    /****************************************************************************
    ****************************************************************************/
    
    
    

  • where s the code going wrong sir ???

  • It seems that your coordinator is not started correctly. Otherwise you should see ZC on LCD of Sample thermostat.

  • no sir there is no pblm in my thermostat it is working properly when i dumb the sample code without changing it and when i change the sampletemperaturesensor code and then dumb it in my ZED it s showing like that sir 

  • If your coordinator (SampleThermostat) is start correctly, you should see something like the attached image. But I don't see it from your previous answer.

  •  it is showing like this when i dump the example pgm for thermostat and temperaturesensor but when i change the code of sample temperature sensor it is not working sir 

  • one more problem is when i use the internal temp sensor of ZED in the lcd of the ZED it is showing a value 42c nothing more or nothing less ... it s not even flutuating when i keep my hand in the chip for heating it up !!

  • How is it possible that you change SampleTemperature sensor code but Sample Thermostat doesn't work? In the screen shot, the right SmartRF05EB+CC2530EM is ZC runs Sample Thermostat, right? You don't have to change anything on it. You only program your revised Sample Temperature ZED to the left SmartRF05EB+CC2530EM.

  • yes sir i didnt change anything in thermostat .... i only chanaged the temperature sensor program 

    and the temp value is cming as 42c it is not at all varying sir !!!! 

    /**************************************************************************************************
      Filename:       zcl_sampletemperaturesensor.c
      Revised:        $Date: 2013-10-18 11:49:27 -0700 (Fri, 18 Oct 2013) $
      Revision:       $Revision: 35718 $
    
      Description:    Zigbee Cluster Library - sample device application.
    
    
      Copyright 2013 Texas Instruments Incorporated. All rights reserved.
    
      IMPORTANT: Your use of this Software is limited to those specific rights
      granted under the terms of a software license agreement between the user
      who downloaded the software, his/her employer (which must be your employer)
      and Texas Instruments Incorporated (the "License").  You may not use this
      Software unless you agree to abide by the terms of the License. The License
      limits your use, and you acknowledge, that the Software may not be modified,
      copied or distributed unless embedded on a Texas Instruments microcontroller
      or used solely and exclusively in conjunction with a Texas Instruments radio
      frequency transceiver, which is integrated into your product.  Other than for
      the foregoing purpose, you may not use, reproduce, copy, prepare derivative
      works of, modify, distribute, perform, display or sell this Software and/or
      its documentation for any purpose.
    
      YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
      PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
      INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
      NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
      TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
      NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
      LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
      INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
      OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
      OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
      (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
    
      Should you have any questions regarding your right to use this Software,
      contact Texas Instruments Incorporated at www.TI.com.
    **************************************************************************************************/
    
    /*********************************************************************
      This device will act as a temperature sensor. It updates the current
      temperature on the thermostat when the user sends the desired
      temperature using SW1.
    
      SCREEN MODES
      ----------------------------------------
      Main:
        - SW1: Send current temperature
        - SW2: Invoke EZMode
        - SW3: Adjust temperature
        - SW5: Go to Help screen
    
      Temperature:
        - SW1: Increase temperature
        - SW3: Decrease temperature
        - SW5: Enter temperature
      ----------------------------------------
    *********************************************************************/
    
    /*********************************************************************
     * INCLUDES
     */
    #include "ZComDef.h"
    #include "OSAL.h"
    #include "AF.h"
    #include "ZDApp.h"
    #include "ZDObject.h"
    #include "ZDProfile.h"
    #include "MT_SYS.h"
    
    #include "zcl.h"
    #include "zcl_general.h"
    #include "zcl_ha.h"
    #include "zcl_ezmode.h"
    #include "zcl_ms.h"
    #include "hal_adc.h"
    
    #include "zcl_sampletemperaturesensor.h"
    
    #include "onboard.h"
    
    /* HAL */
    #include "hal_lcd.h"
    #include "hal_led.h"
    #include "hal_key.h"
    //#include "zcl_sampletemperaturesensor_data.c"
    
    /*********************************************************************
     * MACROS
     */
    
    // how often to report temperature
    #define SAMPLETEMPERATURESENSOR_REPORT_INTERVAL   3000
    
    /*********************************************************************
     * CONSTANTS
     */
    
    /*********************************************************************
     * TYPEDEFS
     */
    /*#ifdef __IAR_SYSTEMS_ICC__
    #define  CODE   __code
    #define  XDATA  __xdata
    */
    
    /*********************************************************************
     * GLOBAL VARIABLES
     */
    byte zclSampleTemperatureSensor_TaskID;
    
    uint8 zclSampleTemperatureSensorSeqNum;
    
    static byte gPermitDuration = 0x00;
    /*void initTempSensor(void){
       DISABLE_ALL_INTERRUPTS();            //??????
       InitClock();                         //????????32M
       *((BYTE XDATA*) 0x624B) = 0x01;    //???????
       *((BYTE XDATA*) 0x61BD) = 0x01;    //???????ADC????
    }*/
    
    /*********************************************************************
     * GLOBAL FUNCTIONS
     */
    
    /*********************************************************************
     * LOCAL VARIABLES
     */
    afAddrType_t zclSampleTemperatureSensor_DstAddr;
    
    #ifdef ZCL_EZMODE
    static void zclSampleTemperatureSensor_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg );
    static void zclSampleTemperatureSensor_EZModeCB( zlcEZMode_State_t state, zclEZMode_CBData_t *pData );
    
    static const zclEZMode_RegisterData_t zclSampleTemperatureSensor_RegisterEZModeData =
    {
      &zclSampleTemperatureSensor_TaskID,
      SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT,
      SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT,
      &zclSampleTemperatureSensorSeqNum,
      zclSampleTemperatureSensor_EZModeCB
    };
    
    // NOT ZCL_EZMODE, Use EndDeviceBind
    #else
    
    static cId_t bindingOutClusters[] =
    {
      ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT
    };
    #define ZCLSAMPLETEMPERATURESENSOR_BINDINGLIST        1
    #endif
    
    devStates_t zclSampleTemperatureSensor_NwkState = DEV_INIT;
    
    uint8 giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;   // display main screen mode first
    
    static uint8 aProcessCmd[] = { 1, 0, 0, 0 }; // used for reset command, { length + cmd0 + cmd1 + data }
    
    // Test Endpoint to allow SYS_APP_MSGs
    static endPointDesc_t sampleTemperatureSensor_TestEp =
    {
      20,                                 // Test endpoint
      &zclSampleTemperatureSensor_TaskID,
      (SimpleDescriptionFormat_t *)NULL,  // No Simple description for this test endpoint
      (afNetworkLatencyReq_t)0            // No Network Latency req
    };
    
    
    /*********************************************************************
     * LOCAL FUNCTIONS
     */
    static void zclSampleTemperatureSensor_HandleKeys( byte shift, byte keys );
    static void zclSampleTemperatureSensor_BasicResetCB( void );
    static void zclSampleTemperatureSensor_IdentifyCB( zclIdentify_t *pCmd );
    static void zclSampleTemperatureSensor_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp );
    static void zclSampleTemperatureSensor_ProcessIdentifyTimeChange( void );
    //void getTemperature(void);
    // app display functions
    void zclSampleTemperatureSensor_LcdDisplayUpdate(void);
    void zclSampleTemperatureSensor_LcdDisplayMainMode(void);
    void zclSampleTemperatureSensor_LcdDisplayTempMode(void);
    void zclSampleTemperatureSensor_LcdDisplayHelpMode(void);
    //int16 getTemperature(void);
    int16 zclSampleTemperatureSensor_MeasuredValue;
    
    static void zclSampleTemperatureSensor_SendTemp(void);
    
    // Functions to process ZCL Foundation incoming Command/Response messages
    static void zclSampleTemperatureSensor_ProcessIncomingMsg( zclIncomingMsg_t *msg );
    #ifdef ZCL_READ
    static uint8 zclSampleTemperatureSensor_ProcessInReadRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    #ifdef ZCL_WRITE
    static uint8 zclSampleTemperatureSensor_ProcessInWriteRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    static uint8 zclSampleTemperatureSensor_ProcessInDefaultRspCmd( zclIncomingMsg_t *pInMsg );
    #ifdef ZCL_DISCOVER
    static uint8 zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( zclIncomingMsg_t *pInMsg );
    #endif // ZCL_DISCOVER
    
    /*********************************************************************
     * STATUS STRINGS
     */
    #ifdef LCD_SUPPORTED
    const char sClearLine[]    = " ";
    const char sDeviceName[]   = "  Temp Sensor";
    const char sSwTempUp[]     = "SW1: Raise Temp";
    const char sSwEZMode[]     = "SW2: EZ-Mode";
    const char sSwTempDown[]   = "SW3: Lower Temp";
    const char sSwHelp[]       = "SW5: Help";
    #endif
    
    /*********************************************************************
     * ZCL General Profile Callback table
     */
    static zclGeneral_AppCallbacks_t zclSampleTemperatureSensor_CmdCallbacks =
    {
      zclSampleTemperatureSensor_BasicResetCB,        // Basic Cluster Reset command
      zclSampleTemperatureSensor_IdentifyCB,          // Identify command
    #ifdef ZCL_EZMODE
      NULL,                                           // Identify EZ-Mode Invoke command
      NULL,                                           // Identify Update Commission State command
    #endif
      NULL,                                           // Identify Trigger Effect command
      zclSampleTemperatureSensor_IdentifyQueryRspCB,  // Identify Query Response command
      NULL,             				                      // On/Off cluster command
      NULL,                                           // On/Off cluster enhanced command Off with Effect
      NULL,                                           // On/Off cluster enhanced command On with Recall Global Scene
      NULL,                                           // On/Off cluster enhanced command On with Timed Off
    #ifdef ZCL_LEVEL_CTRL
      NULL,                                           // Level Control Move to Level command
      NULL,                                           // Level Control Move command
      NULL,                                           // Level Control Step command
      NULL,                                           // Level Control Stop command
    #endif
    #ifdef ZCL_GROUPS
      NULL,                                           // Group Response commands
    #endif
    #ifdef ZCL_SCENES
      NULL,                                           // Scene Store Request command
      NULL,                                           // Scene Recall Request command
      NULL,                                           // Scene Response command
    #endif
    #ifdef ZCL_ALARMS
      NULL,                                           // Alarm (Response) commands
    #endif
    #ifdef SE_UK_EXT
      NULL,                                           // Get Event Log command
      NULL,                                           // Publish Event Log command
    #endif
      NULL,                                           // RSSI Location command
      NULL                                            // RSSI Location Response command
    };
    
    /*********************************************************************
     * @fn          zclSampleTemperatureSensor_Init
     *
     * @brief       Initialization function for the zclGeneral layer.
     *
     * @param       none
     *
     * @return      none
     */
    void zclSampleTemperatureSensor_Init( byte task_id )
    {
      zclSampleTemperatureSensor_TaskID = task_id;
    
      // Set destination address to indirect
      zclSampleTemperatureSensor_DstAddr.addrMode = (afAddrMode_t)AddrNotPresent;
      zclSampleTemperatureSensor_DstAddr.endPoint = 0;
      zclSampleTemperatureSensor_DstAddr.addr.shortAddr = 0;
    
      // This app is part of the Home Automation Profile
      zclHA_Init( &zclSampleTemperatureSensor_SimpleDesc );
    
      // Register the ZCL General Cluster Library callback functions
      zclGeneral_RegisterCmdCallbacks( SAMPLETEMPERATURESENSOR_ENDPOINT, &zclSampleTemperatureSensor_CmdCallbacks );
    
      // Register the application's attribute list
      zcl_registerAttrList( SAMPLETEMPERATURESENSOR_ENDPOINT, SAMPLETEMPERATURESENSOR_MAX_ATTRIBUTES, zclSampleTemperatureSensor_Attrs );
    
      // Register the Application to receive the unprocessed Foundation command/response messages
      zcl_registerForMsg( zclSampleTemperatureSensor_TaskID );
    
    #ifdef ZCL_EZMODE
      // Register EZ-Mode
      zcl_RegisterEZMode( &zclSampleTemperatureSensor_RegisterEZModeData );
    
      // Register with the ZDO to receive Match Descriptor Responses
      ZDO_RegisterForZDOMsg(task_id, Match_Desc_rsp);
    #endif
    
      // Register for all key events - This app will handle all key events
      RegisterForKeys( zclSampleTemperatureSensor_TaskID );
    
      // Register for a test endpoint
      afRegister( &sampleTemperatureSensor_TestEp );
    
    #ifdef LCD_SUPPORTED
      // display the device name
      HalLcdWriteString( (char *)sDeviceName, HAL_LCD_LINE_3 );
    #endif
      osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
    
    }
    
    /*********************************************************************
     * @fn          zclSample_event_loop
     *
     * @brief       Event Loop Processor for zclGeneral.
     *
     * @param       none
     *
     * @return      none
     */
    uint16 zclSampleTemperatureSensor_event_loop( uint8 task_id, uint16 events )
    {
      afIncomingMSGPacket_t *MSGpkt;
    
      (void)task_id;  // Intentionally unreferenced parameter
    
      if ( events & SYS_EVENT_MSG )
      {
        while ( (MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( zclSampleTemperatureSensor_TaskID )) )
        {
          switch ( MSGpkt->hdr.event )
          {
    #ifdef ZCL_EZMODE
            case ZDO_CB_MSG:
              zclSampleTemperatureSensor_ProcessZDOMsgs( (zdoIncomingMsg_t *)MSGpkt );
              break;
    #endif
    
            case ZCL_INCOMING_MSG:
              // Incoming ZCL Foundation command/response messages
              zclSampleTemperatureSensor_ProcessIncomingMsg( (zclIncomingMsg_t *)MSGpkt );
              break;
    
            case KEY_CHANGE:
              zclSampleTemperatureSensor_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
              break;
    
            case ZDO_STATE_CHANGE:
              zclSampleTemperatureSensor_NwkState = (devStates_t)(MSGpkt->hdr.status);
    
    
              // now on the network
              if ( (zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD) ||
                   (zclSampleTemperatureSensor_NwkState == DEV_ROUTER)   ||
                   (zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE) )
              {
    #ifndef HOLD_AUTO_START
                giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
                zclSampleTemperatureSensor_LcdDisplayUpdate();
    #endif
    #ifdef ZCL_EZMODE
                zcl_EZModeAction( EZMODE_ACTION_NETWORK_STARTED, NULL );
    #endif // ZCL_EZMODE
              }
              break;
    
            default:
              break;
          }
    
          // Release the memory
          osal_msg_deallocate( (uint8 *)MSGpkt );
        }
    
        // return unprocessed events
        return (events ^ SYS_EVENT_MSG);
      }
    
      if ( events & SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT )
      {
        if ( zclSampleTemperatureSensor_IdentifyTime > 0 )
          zclSampleTemperatureSensor_IdentifyTime--;
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    
        return ( events ^ SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT );
      }
    
    #ifdef ZCL_EZMODE
      // going on to next state
      if ( events & SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT )
      {
        zcl_EZModeAction ( EZMODE_ACTION_PROCESS, NULL );   // going on to next state
        return ( events ^ SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT );
      }
    
      // the overall EZMode timer expired, so we timed out
      if ( events & SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT )
      {
        zcl_EZModeAction ( EZMODE_ACTION_TIMED_OUT, NULL ); // EZ-Mode timed out
        return ( events ^ SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT );
      }
    #endif // ZLC_EZMODE
    
      if ( events & SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
        zclSampleTemperatureSensor_LcdDisplayUpdate();
    
        return ( events ^ SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT );
      }
    
      if (events & SAMPLETEMPERATURESENSOR_ADC)
      {
       int16 AvgTemp;
      uint8   i;
      uint16  AdcValue;
      uint16  value;
      uint16  value1;
    //char i;
            //char TempValue[10]; 
            //initTempSensor(); 
      AdcValue = 0;
      for( i = 0; i < 4; i++ )
      {
    ADCIF = 0;
        ADCCON3 = (HAL_ADC_REF_125V | HAL_ADC_RESOLUTION_14 | HAL_ADC_CHN_TEMP);    // ??1.25V????,12????,AD??:?????
            while ( !ADCIF );                                              //?????ADC
                     
        value =  ADCL >> 2;                        
        value |= (((UINT16)ADCH) << 6);    
        AdcValue += value;                          
      }
      value = AdcValue >> 2;                        //????4,?????
      value1 =   (((value) >> 4) - 335);
                 //??AD?,????????
     // while(1)
      //      {
             AvgTemp = 0;
             for(i = 0 ; i < 64 ; i++)
              {
                AvgTemp += value1;              
                AvgTemp >>= 1;                              //??????2.
              }
              
        //    }
        zclSampleTemperatureSensor_MeasuredValue = AvgTemp ;
        zclSampleTemperatureSensor_LcdDisplayUpdate();
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
        zclSampleTemperatureSensor_SendTemp();
        return ( events ^ SAMPLETEMPERATURESENSOR_ADC );
      }
    
      if ( events & SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT )
      {
        zclSampleTemperatureSensor_SendTemp();
    
        // report current temperature reading every 10 seconds
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
    
        return ( events ^ SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT );
      }
    
      // Discard unknown events
      return 0;
    }
    
    /*int16 getTemperature(void)
    {
      int16 AvgTemp;
      uint8   i;
      uint16  AdcValue;
      uint16  value;
      uint16  value1;
    //char i;
            //char TempValue[10]; 
            //initTempSensor(); 
      AdcValue = 0;
      for( i = 0; i < 4; i++ )
      {
    ADCIF = 0;
        ADCCON3 = (HAL_ADC_REF_125V | HAL_ADC_RESOLUTION_14 | HAL_ADC_CHN_TEMP);    // ??1.25V????,12????,AD??:?????
            while ( !ADCIF );                                              //?????ADC
                     
        value =  ADCL >> 2;                        
        value |= (((UINT16)ADCH) << 6);    
        AdcValue += value;                          
      }
      value = AdcValue >> 2;                        //????4,?????
      value1 =   (((value) >> 4) - 335);
                 //??AD?,????????
      while(1)
            {
              AvgTemp = 0;
              for(i = 0 ; i < 64 ; i++)
              {
                AvgTemp += value1;              
                AvgTemp >>= 1;                              //??????2.
              }
              
            }
      //zclSampleTemperatureSensor_MeasuredValue = AvgTemp ; 
      return AvgTemp;
    }*/
    
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_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_5
     *                 HAL_KEY_SW_4
     *                 HAL_KEY_SW_3
     *                 HAL_KEY_SW_2
     *                 HAL_KEY_SW_1
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_HandleKeys( byte shift, byte keys )
    {
      if ( keys & HAL_KEY_SW_1 )
      {
        // increase temperature
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        if ( zclSampleTemperatureSensor_MeasuredValue < zclSampleTemperatureSensor_MaxMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MeasuredValue + 100;  // considering using whole number value
        }
        else if ( zclSampleTemperatureSensor_MeasuredValue >= zclSampleTemperatureSensor_MaxMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MaxMeasuredValue;
        }
    
        // Send temperature information
        zclSampleTemperatureSensor_SendTemp();
      }
    
      if ( keys & HAL_KEY_SW_2 )
      {
        if ( ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE ) ||
            ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE ) )
        {
          giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
    #ifdef ZCL_EZMODE
          zclEZMode_InvokeData_t ezModeData;
          static uint16 clusterIDs[] = { ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT };   // only bind on the Temperature Measurement cluster
    
          // Invoke EZ-Mode
          ezModeData.endpoint = SAMPLETEMPERATURESENSOR_ENDPOINT; // endpoint on which to invoke EZ-Mode
          if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
               ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER )   ||
               ( zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE ) )
          {
            ezModeData.onNetwork = TRUE;      // node is already on the network
          }
          else
          {
            ezModeData.onNetwork = FALSE;     // node is not yet on the network
          }
          ezModeData.initiator = TRUE;        // Temperature Sensor is an initiator
          ezModeData.numActiveInClusters = 1;
          ezModeData.pActiveInClusterIDs = clusterIDs;
          ezModeData.numActiveOutClusters = 0;   // active output cluster
          ezModeData.pActiveOutClusterIDs = NULL;
          zcl_InvokeEZMode( &ezModeData );
    
    #ifdef LCD_SUPPORTED
          HalLcdWriteString( "EZMode", HAL_LCD_LINE_2 );
    #endif
    
          // NOT ZCL_EZMODE, Use EndDeviceBind
    #else
          {
            zAddrType_t dstAddr;
            dstAddr.addrMode = Addr16Bit;
            dstAddr.addr.shortAddr = 0;   // Coordinator makes the EDB match
    
            // Initiate an End Device Bind Request, this bind request will
            // only use a cluster list that is important to binding.
            HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
            ZDP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(),
                                  SAMPLETEMPERATURESENSOR_ENDPOINT,
                                  ZCL_HA_PROFILE_ID,
                                  0, NULL,
                                  ZCLSAMPLETEMPERATURESENSOR_BINDINGLIST, bindingOutClusters,
                                  FALSE );
          }
    #endif // ZCL_EZMODE
        }
      }
    
      if ( keys & HAL_KEY_SW_3 )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        // decrease the temperature
        if ( zclSampleTemperatureSensor_MeasuredValue > zclSampleTemperatureSensor_MinMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MeasuredValue - 100;  // considering using whole number value
        }
        else if ( zclSampleTemperatureSensor_MeasuredValue >= zclSampleTemperatureSensor_MinMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MinMeasuredValue;
        }
    
        // Send temperature information
        zclSampleTemperatureSensor_SendTemp();
      }
    
      if ( keys & HAL_KEY_SW_4 )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
             ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER ) )
        {
          // toggle permit join
          gPermitDuration = gPermitDuration ? 0 : 0xff;
          NLME_PermitJoiningRequest( gPermitDuration );
        }
      }
    
      if ( shift && ( keys & HAL_KEY_SW_5 ) )
      {
        zclSampleTemperatureSensor_BasicResetCB();
      }
      else if ( keys & HAL_KEY_SW_5 )
      {
        if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
        {
          giTemperatureSensorScreenMode = TEMPSENSE_HELPMODE;
        }
        else if ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE )
        {
    #ifdef LCD_SUPPORTED
          HalLcdWriteString( (char *)sClearLine, HAL_LCD_LINE_2 );
    #endif
          giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
        }
      }
    
      // update display
      zclSampleTemperatureSensor_LcdDisplayUpdate();
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayUpdate
     *
     * @brief   Called to update the LCD display.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayUpdate( void )
    {
      // turn on red LED for temperatures >= 24.00C
      if ( zclSampleTemperatureSensor_MeasuredValue >= 2400 )
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_OFF );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
      }
      // turn on green LED for temperatures <= 20.00C
      else if ( zclSampleTemperatureSensor_MeasuredValue <= 2000 )
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_OFF );
      }
      // turn on both red and green LEDs for temperatures between 20.00C and 24.00C
      else
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
      }
    
      if ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE )
      {
        zclSampleTemperatureSensor_LcdDisplayHelpMode();
      }
      else
      {
        zclSampleTemperatureSensor_LcdDisplayMainMode();
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayMainMode
     *
     * @brief   Called to display the main screen on the LCD.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayMainMode( void )
    {
      char sDisplayTemp[16];
    
      if ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD )
      {
        zclHA_LcdStatusLine1( 0 );
      }
      else if ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER )
      {
        zclHA_LcdStatusLine1( 1 );
      }
      else if ( zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE )
      {
        zclHA_LcdStatusLine1( 2 );
      }
    
      // display current temperature
      osal_memcpy(sDisplayTemp, "TEMP: ", 6);
      _ltoa( ( zclSampleTemperatureSensor_MeasuredValue / 100 ), (void *)(&sDisplayTemp[6]), 10 );   // convert temperature to whole number
      osal_memcpy( &sDisplayTemp[8], "C", 2 );
    #ifdef LCD_SUPPORTED
      HalLcdWriteString( (char *)sDisplayTemp, HAL_LCD_LINE_2 );
    #endif
    
    #ifdef LCD_SUPPORTED
      if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
           ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER ) )
      {
        // display help key with permit join status
        if ( gPermitDuration )
        {
          HalLcdWriteString( "SW5: Help      *", HAL_LCD_LINE_3 );
        }
        else
        {
          HalLcdWriteString( "SW5: Help       ", HAL_LCD_LINE_3 );
        }
      }
      else
      {
        // display help key
        HalLcdWriteString( (char *)sSwHelp, HAL_LCD_LINE_3 );
      }
    #endif
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayHelpMode
     *
     * @brief   Called to display the SW options on the LCD.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayHelpMode( void )
    {
    #ifdef LCD_SUPPORTED
      HalLcdWriteString( (char *)sSwTempUp, HAL_LCD_LINE_1 );
      HalLcdWriteString( (char *)sSwEZMode, HAL_LCD_LINE_2 );
      HalLcdWriteString( (char *)sSwTempDown, HAL_LCD_LINE_3 );
    #endif
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_SendTemp
     *
     * @brief   Called to send current temperature information to the thermostat
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_SendTemp( void )
    {
    #ifdef ZCL_REPORT
      zclReportCmd_t *pReportCmd;
      //zclSampleTemperatureSensor_MeasuredValue = getTemperature();
      pReportCmd = osal_mem_alloc( sizeof(zclReportCmd_t) + sizeof(zclReport_t) );
      if ( pReportCmd != NULL )
      {
        pReportCmd->numAttr = 1;
        pReportCmd->attrList[0].attrID = ATTRID_MS_TEMPERATURE_MEASURED_VALUE;
        pReportCmd->attrList[0].dataType = ZCL_DATATYPE_INT16;
        pReportCmd->attrList[0].attrData = (void *)(&zclSampleTemperatureSensor_MeasuredValue);
    
        zcl_SendReportCmd( SAMPLETEMPERATURESENSOR_ENDPOINT, &zclSampleTemperatureSensor_DstAddr,
                           ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT,
                           pReportCmd, ZCL_FRAME_SERVER_CLIENT_DIR, TRUE, zclSampleTemperatureSensorSeqNum++ );
      }
    
      osal_mem_free( pReportCmd );
    #endif  // ZCL_REPORT
    }
    
    /*********************************************************************
     * @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      zclSampleTemperatureSensor_BasicResetCB
     *
     * @brief   Callback from the ZCL General Cluster Library
     *          to set all the Basic Cluster attributes to default values.
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_BasicResetCB( void )
    {
      // Put device back to factory default settings
      zgWriteStartupOptions( ZG_STARTUP_SET, 3 );   // bit set both default configuration and default network
    
      // restart device
      MT_SysCommandProcessing( aProcessCmd );
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_IdentifyCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an Identity Command for this application.
     *
     * @param   srcAddr - source address and endpoint of the response message
     * @param   identifyTime - the number of seconds to identify yourself
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_IdentifyCB( zclIdentify_t *pCmd )
    {
      zclSampleTemperatureSensor_IdentifyTime = pCmd->identifyTime;
      zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_IdentifyQueryRspCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an Identity Query Response Command for this application.
     *
     * @param   srcAddr - requestor's address
     * @param   timeout - number of seconds to identify yourself (valid for query response)
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp )
    {
      (void)pRsp;
    #ifdef ZCL_EZMODE
      {
        zclEZMode_ActionData_t data;
        data.pIdentifyQueryRsp = pRsp;
        zcl_EZModeAction ( EZMODE_ACTION_IDENTIFY_QUERY_RSP, &data );
      }
    #endif
    }
    
    /******************************************************************************
     *
     *  Functions for processing ZCL Foundation incoming Command/Response messages
     *
     *****************************************************************************/
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessIncomingMsg
     *
     * @brief   Process ZCL Foundation incoming message
     *
     * @param   pInMsg - pointer to the received message
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_ProcessIncomingMsg( zclIncomingMsg_t *pInMsg)
    {
      switch ( pInMsg->zclHdr.commandID )
      {
    #ifdef ZCL_READ
        case ZCL_CMD_READ_RSP:
          zclSampleTemperatureSensor_ProcessInReadRspCmd( pInMsg );
          break;
    #endif
    #ifdef ZCL_WRITE
        case ZCL_CMD_WRITE_RSP:
          zclSampleTemperatureSensor_ProcessInWriteRspCmd( pInMsg );
          break;
    #endif
    #ifdef ZCL_REPORT
        // See ZCL Test Applicaiton (zcl_testapp.c) for sample code on Attribute Reporting
        case ZCL_CMD_CONFIG_REPORT:
          //zclSampleTemperatureSensor_ProcessInConfigReportCmd( pInMsg );
          break;
    
        case ZCL_CMD_CONFIG_REPORT_RSP:
          //zclSampleTemperatureSensor_ProcessInConfigReportRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG:
          //zclSampleTemperatureSensor_ProcessInReadReportCfgCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG_RSP:
          //zclSampleTemperatureSensor_ProcessInReadReportCfgRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_REPORT:
          //zclSampleTemperatureSensor_ProcessInReportCmd( pInMsg );
          break;
    #endif
        case ZCL_CMD_DEFAULT_RSP:
          zclSampleTemperatureSensor_ProcessInDefaultRspCmd( pInMsg );
          break;
    #ifdef ZCL_DISCOVER
        case ZCL_CMD_DISCOVER_CMDS_RECEIVED_RSP:
          zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_CMDS_GEN_RSP:
          zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_RSP:
          zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_EXT_RSP:
          zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( pInMsg );
          break;
    #endif
        default:
          break;
      }
    
      if ( pInMsg->attrCmd )
      {
        osal_mem_free( pInMsg->attrCmd );
      }
    }
    
    #ifdef ZCL_READ
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInReadRspCmd
     *
     * @brief   Process the "Profile" Read Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_ProcessInReadRspCmd( zclIncomingMsg_t *pInMsg )
    {
      zclReadRspCmd_t *readRspCmd;
      uint8 i;
    
      readRspCmd = (zclReadRspCmd_t *)pInMsg->attrCmd;
      for ( i = 0; i < readRspCmd->numAttr; i++ )
      {
        // Notify the originator of the results of the original read attributes
        // attempt and, for each successfull request, the value of the requested
        // attribute
      }
    
      return ( TRUE );
    }
    #endif // ZCL_READ
    
    #ifdef ZCL_WRITE
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInWriteRspCmd
     *
     * @brief   Process the "Profile" Write Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_ProcessInWriteRspCmd( zclIncomingMsg_t *pInMsg )
    {
      zclWriteRspCmd_t *writeRspCmd;
      uint8 i;
    
      writeRspCmd = (zclWriteRspCmd_t *)pInMsg->attrCmd;
      for ( i = 0; i < writeRspCmd->numAttr; i++ )
      {
        // Notify the device of the results of the its original write attributes
        // command.
      }
    
      return ( TRUE );
    }
    #endif // ZCL_WRITE
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInDefaultRspCmd
     *
     * @brief   Process the "Profile" Default Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_ProcessInDefaultRspCmd( zclIncomingMsg_t *pInMsg )
    {
      // zclDefaultRspCmd_t *defaultRspCmd = (zclDefaultRspCmd_t *)pInMsg->attrCmd;
    
      // Device is notified of the Default Response command.
      (void)pInMsg;
    
      return ( TRUE );
    }
    
    #ifdef ZCL_DISCOVER
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd
     *
     * @brief   Process the Discover Commands Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( zclIncomingMsg_t *pInMsg )
    {
      zclDiscoverCmdsCmdRsp_t *discoverRspCmd;
      uint8 i;
    
      discoverRspCmd = (zclDiscoverCmdsCmdRsp_t *)pInMsg->attrCmd;
      for ( i = 0; i < discoverRspCmd->numCmd; i++ )
      {
        // Device is notified of the result of its attribute discovery command.
      }
    
      return ( TRUE );
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( zclIncomingMsg_t *pInMsg )
    {
      zclDiscoverAttrsRspCmd_t *discoverRspCmd;
      uint8 i;
    
      discoverRspCmd = (zclDiscoverAttrsRspCmd_t *)pInMsg->attrCmd;
      for ( i = 0; i < discoverRspCmd->numAttr; i++ )
      {
        // Device is notified of the result of its attribute discovery command.
      }
    
      return ( TRUE );
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Extended Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( zclIncomingMsg_t *pInMsg )
    {
      zclDiscoverAttrsExtRsp_t *discoverRspCmd;
      uint8 i;
    
      discoverRspCmd = (zclDiscoverAttrsExtRsp_t *)pInMsg->attrCmd;
      for ( i = 0; i < discoverRspCmd->numAttr; i++ )
      {
        // Device is notified of the result of its attribute discovery command.
      }
    
      return ( TRUE );
    }
    #endif // ZCL_DISCOVER
    
    #ifdef ZCL_EZMODE
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessZDOMsgs
     *
     * @brief   Called when this node receives a ZDO/ZDP response.
     *
     * @param   none
     *
     * @return  status
     */
    static void zclSampleTemperatureSensor_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg )
    {
      zclEZMode_ActionData_t data;
      ZDO_MatchDescRsp_t *pMatchDescRsp;
    
      // Let EZ-Mode know of the Match Descriptor Response
      if ( pMsg->clusterID == Match_Desc_rsp )
      {
        pMatchDescRsp = ZDO_ParseEPListRsp( pMsg );
        data.pMatchDescRsp = pMatchDescRsp;
        zcl_EZModeAction( EZMODE_ACTION_MATCH_DESC_RSP, &data );
        osal_mem_free( pMatchDescRsp );
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_EZModeCB
     *
     * @brief   The Application is informed of events. This can be used to show on the UI what is
    *           going on during EZ-Mode steering/finding/binding.
     *
     * @param   state - an
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_EZModeCB( zlcEZMode_State_t state, zclEZMode_CBData_t *pData )
    {
    #ifdef LCD_SUPPORTED
      char szLine[20];
      char *pStr;
      uint8 err;
    #endif
    
      // time to go into identify mode
      if ( state == EZMODE_STATE_IDENTIFYING )
      {
        zclSampleTemperatureSensor_IdentifyTime = ( EZMODE_TIME / 1000 );  // convert to seconds
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
      }
    
      // autoclosing, show what happened (success, cancelled, etc...)
      if( state == EZMODE_STATE_AUTOCLOSE )
      {
    #ifdef LCD_SUPPORTED
        pStr = NULL;
        err = pData->sAutoClose.err;
        if ( err == EZMODE_ERR_SUCCESS )
        {
          pStr = "EZMode: Success";
        }
        else if ( err == EZMODE_ERR_NOMATCH )
        {
          pStr = "EZMode: NoMatch"; // not a match made in heaven
        }
        if ( pStr )
        {
          if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
          {
            HalLcdWriteString ( pStr, HAL_LCD_LINE_2 );
          }
        }
    #endif
      }
    
      // finished, either show DstAddr/EP, or nothing (depending on success or not)
      if( state == EZMODE_STATE_FINISH )
      {
        // turn off identify mode
        zclSampleTemperatureSensor_IdentifyTime = 0;
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    
    #ifdef LCD_SUPPORTED
        // if successful, inform user which nwkaddr/ep we bound to
        pStr = NULL;
        err = pData->sFinish.err;
        if( err == EZMODE_ERR_SUCCESS )
        {
          // "EZDst:1234 EP:34"
          osal_memcpy( szLine, "EZDst:", 6 );
          zclHA_uint16toa( pData->sFinish.nwkaddr, &szLine[6] );
          osal_memcpy( &szLine[10], " EP:", 4 );
          _ltoa( pData->sFinish.ep, (void *)(&szLine[14]), 16 );  // _ltoa NULL terminates
          pStr = szLine;
        }
        else if ( err == EZMODE_ERR_BAD_PARAMETER )
        {
          pStr = "EZMode: BadParm";
        }
        else if ( err == EZMODE_ERR_CANCELLED )
        {
          pStr = "EZMode: Cancel";
        }
        else
        {
          pStr = "EZMode: TimeOut";
        }
        if ( pStr )
        {
          if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
          {
            HalLcdWriteString ( pStr, HAL_LCD_LINE_2 );
          }
        }
    #endif  // LCD_SUPPORTED
    
        // show main UI screen 3 seconds after joining network
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT, 3000 );
    
        // report current temperature reading 15 seconds after joinging the network
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
      }
    }
    #endif // ZCL_EZMODE
    
    /****************************************************************************
    ****************************************************************************/
    
    
    

  • Do you press  SW2  on both Sample Temperature and Thermostat after you flash your revised FW into Sample Temperature?

  • yes sir !!! but the pblm is the sw2 is not at all working in the sample temperaturesensor when i dump the modifies code sir

  • sir i modified the code and made even both the nodes to communicate sir .... the adc value is flutuating between 14 to 17C only . it is not even steady ... i just minimized the adc code which i wrote earlier ... 

    if (events & SAMPLETEMPERATURESENSOR_ADC)
    {
    int16 AvgTemp;
    uint8 i;
    uint16 AdcValue;
    uint16 value;
    uint16 value1;
    //char i;
    //char TempValue[10];
    //initTempSensor();
    AdcValue = 0;
    for( i = 0; i < 4; i++ )
    {
    ADCIF = 0;
    ADCCON3 = (HAL_ADC_REF_125V | HAL_ADC_RESOLUTION_14 | HAL_ADC_CHN_TEMP); // ??1.25V????,12????,AD??:?????
    while ( !ADCIF ); //?????ADC

    value = ADCL >> 2;
    value |= (((UINT16)ADCH) << 6);
    AdcValue += value;
    }
    value = AdcValue >> 2; //????4,?????
    //value1 = (((value) >> 4) - 335);
    //??AD?,????????
    // while(1)
    // {
    //AvgTemp = 0;
    //for(i = 0 ; i < 64 ; i++)
    //{
    //AvgTemp += value1;
    //AvgTemp >>= 1; //??????2.
    //}

    // }
    zclSampleTemperatureSensor_MeasuredValue = value ;
    zclSampleTemperatureSensor_LcdDisplayUpdate();
    osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
    zclSampleTemperatureSensor_SendTemp();
    return ( events ^ SAMPLETEMPERATURESENSOR_ADC );
    }

  • any idea on why that adc value is fluctuating sir ??? 

  • I have told you that you can not have a while(1) in your code and that is why your SW2 doesn't work. You can see that your Sample Temperature and Sample Thermostat can communicate after you comment out the while(1).

  • sir i commmented that while loop whn u then itself ... now every communication and all are working fine sir !!! the problem is the sensor value it is giving a random value from 55 to 75 ...... and it is not continuous it s jst random value .. not even increasing or decreasing in a proper way !!!!  

  • There is an example SensorDemo in Z-Stack Mesh 1.0.0 that show you how to read temperature using ADC.

  • sir i already did the adc coding using the simpleapp example in ZStack-CC2530-2.3.0-1.4.0 ..... 

     s the pgm in Z-Stack Mesh 1.0.0 is differ from the simpleapp example sir ??? 

     

  • Sample codes of Z-Stack Mesh 1.0.0 and  ZStack-CC2530-2.3.0-1.4.0 are similar but your code for temperature reading is not the same with myApp_ReadTemperature() in Z-Stack-CC2530-2.3.0-1.4.0.

  • and in Z-Stack Mesh 1.0.0 sensor demo program is not there sir

  • yes sir .. its kinda different u r suggesting that i can use myApp_ReadTemperature() pgm to read the temperature sensor ?? 

  • If you install Z-Stack Mesh 1.0.0, you should find it in C:\Texas Instruments\Z-Stack Mesh 1.0.0\Projects\zstack\Samples\SensorDemo\Source\DemoSensor.c

  •  i dont have that file sir ... i will try with myapp_gettemperature() pgm in simpleapp 

  • if (events & SAMPLETEMPERATURESENSOR_ADC)
    {
    uint16 value;
    int16 value1;
    /* Clear ADC interrupt flag */
    ADCIF = 0;

    ADCCON3 = (HAL_ADC_REF_125V | HAL_ADC_DEC_512 | HAL_ADC_CHN_TEMP);

    /* Wait for the conversion to finish */
    while ( !ADCIF );

    /* Get the result */
    value = ADCL;
    value |= ((uint16) ADCH) << 8;


    #define VOLTAGE_AT_TEMP_ZERO 5158
    #define TEMP_COEFFICIENT 14


    // limit min temp to 0 C
    if ( value < VOLTAGE_AT_TEMP_ZERO )
    value = VOLTAGE_AT_TEMP_ZERO;

    value = value - VOLTAGE_AT_TEMP_ZERO;

    // limit max temp to 99 C
    if ( value > TEMP_COEFFICIENT * 99 )
    value = TEMP_COEFFICIENT * 99;

    //return ( (uint8)(value/TEMP_COEFFICIENT) );
    value1 = value/TEMP_COEFFICIENT ;

    zclSampleTemperatureSensor_MeasuredValue = value1 ;
    zclSampleTemperatureSensor_LcdDisplayUpdate();
    osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
    zclSampleTemperatureSensor_SendTemp();
    return ( events ^ SAMPLETEMPERATURESENSOR_ADC );
    }

    i got the pgm from simpleapp and while running the same the lcd is displaying only 0 and it can be changed by button SW1 .... any idea y sir ??? 

  • Change zclSampleTemperatureSensor_MeasuredValue = value1 ; to zclSampleTemperatureSensor_MeasuredValue = value1*100 ;

  • it is showing 99c in the lcd and the value changes while pressing the buttons ... it is not atall updating the values itself .. 

    in normal simpleapp example a minute variation in temperature of chip it will update the value but in this pgm the value is at 99c as we speak sir 

  • Try the following function.

    int16 readTemp(void)
    {
      static uint16 voltageAtTemp22;
      static uint8 bCalibrate=TRUE; // Calibrate the first time the temp sensor is read
      uint16 value;
      int8 temp;

      #if defined (HAL_MCU_CC2530)
      ATEST = 0x01;
      TR0  |= 0x01;
     
      /* Clear ADC interrupt flag */
      ADCIF = 0;

      ADCCON3 = (HAL_ADC_REF_125V | 0x30 | HAL_ADC_CHN_TEMP);

      /* Wait for the conversion to finish */
      while ( !ADCIF );

      /* Get the result */
      value = ADCL;
      value |= ((uint16) ADCH) << 8;

      // Use the 12 MSB of adcValue
      value >>= 4;
     
      /*
       * These parameters are typical values and need to be calibrated
       * See the datasheet for the appropriate chip for more details
       * also, the math below may not be very accurate
       */
        /* Assume ADC = 1480 at 25C and ADC = 4/C */
      #define VOLTAGE_AT_TEMP_25        1480
      #define TEMP_COEFFICIENT          4

      // Calibrate for 22C the first time the temp sensor is read.
      // This will assume that the demo is started up in temperature of 22C
      if(bCalibrate) {
        voltageAtTemp22=value;
        bCalibrate=FALSE;
      }
     
      temp = 22 + ( (value - voltageAtTemp22) / TEMP_COEFFICIENT );
     
      // Set 0C as minimum temperature, and 100C as max
      if( temp >= 100)
      {
        return 100*100;
      }
      else if (temp <= 0) {
        return 0;
      }
      else {
        return temp*100;
      }
      // Only CC2530 is supported
      #else
      return 0;
      #endif
    }


  • if (events & SAMPLETEMPERATURESENSOR_ADC)
    {
    static uint16 voltageAtTemp22;
    static uint8 bCalibrate=TRUE; // Calibrate the first time the temp sensor is read
    uint16 value;
    int8 temp;

    // #if defined (HAL_MCU_CC2530)
    ATEST = 0x01;
    TR0 |= 0x01;

    /* Clear ADC interrupt flag */
    ADCIF = 0;

    ADCCON3 = (HAL_ADC_REF_125V | 0x30 | HAL_ADC_CHN_TEMP);

    /* Wait for the conversion to finish */
    while ( !ADCIF );

    /* Get the result */
    value = ADCL;
    value |= ((uint16) ADCH) << 8;

    // Use the 12 MSB of adcValue
    value >>= 4;

    /*
    * These parameters are typical values and need to be calibrated
    * See the datasheet for the appropriate chip for more details
    * also, the math below may not be very accurate
    */
    /* Assume ADC = 1480 at 25C and ADC = 4/C */
    #define VOLTAGE_AT_TEMP_25 1480
    #define TEMP_COEFFICIENT 4

    // Calibrate for 22C the first time the temp sensor is read.
    // This will assume that the demo is started up in temperature of 22C
    if(bCalibrate) {
    voltageAtTemp22=value;
    bCalibrate=FALSE;
    }

    temp = 22 + ( (value - voltageAtTemp22) / TEMP_COEFFICIENT );

    // Set 0C as minimum temperature, and 100C as max
    /* if( temp >= 100)
    {
    return 100*100;
    }
    else if (temp <= 0) {
    return 0;
    }
    else {
    return temp*100;
    }*/
    // Only CC2530 is supported
    //#else
    //return 0;
    //#endif

    zclSampleTemperatureSensor_MeasuredValue = temp ;
    zclSampleTemperatureSensor_LcdDisplayUpdate();
    osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
    zclSampleTemperatureSensor_SendTemp();
    return ( events ^ SAMPLETEMPERATURESENSOR_ADC );
    }

    i used the code like this sir yet showing value 0 and it is not even fluctuating sir ... kindly c to it sir 

  • You can remove SAMPLETEMPERATURESENSOR_ADC event and use SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT event to do this. Add the following red line to SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT event.

      if ( events & SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT )
      {
        zclSampleTemperatureSensor_MeasuredValue = readTemp() ;
        zclSampleTemperatureSensor_LcdDisplayUpdate();
        zclSampleTemperatureSensor_SendTemp();

        // report current temperature reading every 10 seconds
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );

        return ( events ^ SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT );
      }

  • so u r suggesting that i dont even have to create a new event for the ADC to work ... i the code also if i print the "value" alone it is staying at 15c it is not at all changing 

  • did the same as u said sir but the lcd is showing 0 which is increasing on pressing sw1 manually ...

  • Yes, you don't have to create that new event. I have test the code on SampleTemperatureSensor and SampleThermistat and it works fine.

  • yes sir it is working fine sir .... thank u so much !!  it starts from 22 and goes upto 24C

  • is there any way that i could get more accurate values sir ... it is giving 22c and 23c only ... but i wanted to compare the temperatures to create actuator mechanisms sir 

  • thank u so much sir ... i it is working jst as i needed ....