📄 simplecollector.c
字号:
/**************************************************************************************************
Filename: SimpleApp.c
Revised: $Date: 2007-12-04 11:46:18 -0800 (Tue, 04 Dec 2007) $
Revision: $Revision: 16007 $
Description: Sample application utilizing the Simple API.
Copyright 2007 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 揂S 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 "sapi.h"
#include "hal_key.h"
#include "hal_led.h"
#include "DebugTrace.h"
#include "SimpleApp.h"
#if defined( MT_TASK )
#include "osal_nv.h"
#endif
/*********************************************************************
* CONSTANTS
*/
// Application States
#define APP_INIT 0
#define APP_START 1
// Application osal event identifiers
#define MY_START_EVT 0x0001
// Same definitions as in SimpleSensor.c
#define TEMP_REPORT 0x01
#define BATTERY_REPORT 0x02
/*********************************************************************
* TYPEDEFS
*/
/*********************************************************************
* LOCAL VARIABLES
*/
static uint8 myAppState = APP_INIT;
static uint8 myStartRetryDelay = 10;
#if defined( MT_TASK )
extern uint8 aExtendedAddress[8];
#endif
/*********************************************************************
* GLOBAL VARIABLES
*/
// Inputs and Outputs for Switch device
#define NUM_OUT_CMD_COLLECTOR 0
#define NUM_IN_CMD_COLLECTOR 1
// List of output and input commands for Switch device
const cId_t zb_InCmdList[NUM_IN_CMD_COLLECTOR] =
{
SENSOR_REPORT_CMD_ID
};
// Define SimpleDescriptor for Switch device
const SimpleDescriptionFormat_t zb_SimpleDesc =
{
MY_ENDPOINT_ID, // Endpoint
MY_PROFILE_ID, // Profile ID
DEV_ID_COLLECTOR, // Device ID
DEVICE_VERSION_COLLECTOR, // Device Version
0, // Reserved
NUM_IN_CMD_COLLECTOR, // Number of Input Commands
(cId_t *) zb_InCmdList, // Input Command List
NUM_OUT_CMD_COLLECTOR, // Number of Output Commands
(cId_t *) NULL // Output Command List
};
/******************************************************************************
* @fn zb_HandleOsalEvent
*
* @brief The zb_HandleOsalEvent function is called by the operating
* system when a task event is set
*
* @param event - Bitmask containing the events that have been set
*
* @return none
*/
void zb_HandleOsalEvent( uint16 event )
{
}
/*********************************************************************
* @fn zb_HandleKeys
*
* @brief Handles all key events for this device.
*
* @param shift - true if in shift/alt.
* @param keys - bit field for key events. Valid entries:
* EVAL_SW4
* EVAL_SW3
* EVAL_SW2
* EVAL_SW1
*
* @return none
*/
void zb_HandleKeys( uint8 shift, uint8 keys )
{
uint8 startOptions;
uint8 logicalType;
// Shift is used to make each button/switch dual purpose.
if ( shift )
{
if ( keys & HAL_KEY_SW_1 )
{
}
if ( keys & HAL_KEY_SW_2 )
{
}
if ( keys & HAL_KEY_SW_3 )
{
}
if ( keys & HAL_KEY_SW_4 )
{
}
}
else
{
#if defined( MT_TASK )
if ( (myAppState == APP_INIT)
&& ((keys & HAL_KEY_SW_1) || (keys & HAL_KEY_SW_2)) )
{
// Write temporary 64-bit address to NV
osal_nv_write( ZCD_NV_EXTADDR, 0, Z_EXTADDR_LEN, &aExtendedAddress );
}
#endif
if ( keys & HAL_KEY_SW_1 )
{
if ( myAppState == APP_INIT )
{
// In the init state, keys are used to indicate the logical mode.
// Key 1 starts device as a coordinator
zb_ReadConfiguration( ZCD_NV_LOGICAL_TYPE, sizeof(uint8), &logicalType );
if ( logicalType != ZG_DEVICETYPE_ENDDEVICE )
{
logicalType = ZG_DEVICETYPE_COORDINATOR;
zb_WriteConfiguration(ZCD_NV_LOGICAL_TYPE, sizeof(uint8), &logicalType);
}
// Do more configuration if necessary and then restart device with auto-start bit set
// write endpoint to simple desc...dont pass it in start req..then reset
zb_ReadConfiguration( ZCD_NV_STARTUP_OPTION, sizeof(uint8), &startOptions );
startOptions = ZCD_STARTOPT_AUTO_START;
zb_WriteConfiguration( ZCD_NV_STARTUP_OPTION, sizeof(uint8), &startOptions );
zb_SystemReset();
}
else
{
// Turn ON Allow Bind mode indefinitely
zb_AllowBind( 0xFF );
HalLedSet( HAL_LED_1, HAL_LED_MODE_ON );
}
}
if ( keys & HAL_KEY_SW_2 )
{
if ( myAppState == APP_INIT )
{
// In the init state, keys are used to indicate the logical mode.
// Key 2 starts device as a router
zb_ReadConfiguration( ZCD_NV_LOGICAL_TYPE, sizeof(uint8), &logicalType );
if ( logicalType != ZG_DEVICETYPE_ENDDEVICE )
{
logicalType = ZG_DEVICETYPE_ROUTER;
zb_WriteConfiguration(ZCD_NV_LOGICAL_TYPE, sizeof(uint8), &logicalType);
}
zb_ReadConfiguration( ZCD_NV_STARTUP_OPTION, sizeof(uint8), &startOptions );
startOptions = ZCD_STARTOPT_AUTO_START;
zb_WriteConfiguration( ZCD_NV_STARTUP_OPTION, sizeof(uint8), &startOptions );
zb_SystemReset();
}
else
{
// Turn OFF Allow Bind mode indefinitely
zb_AllowBind( 0x00 );
HalLedSet( HAL_LED_1, HAL_LED_MODE_OFF );
}
}
if ( keys & HAL_KEY_SW_3 )
{
}
if ( keys & HAL_KEY_SW_4 )
{
}
}
}
/******************************************************************************
* @fn zb_StartConfirm
*
* @brief The zb_StartConfirm callback is called by the ZigBee stack
* after a start request operation completes
*
* @param status - The status of the start operation. Status of
* ZB_SUCCESS indicates the start operation completed
* successfully. Else the status is an error code.
*
* @return none
*/
void zb_StartConfirm( uint8 status )
{
// If the device sucessfully started, change state to running
if ( status == ZB_SUCCESS )
{
myAppState = APP_START;
}
else
{
// Try again later with a delay
osal_start_timerEx( sapi_TaskID, MY_START_EVT, myStartRetryDelay );
}
}
/******************************************************************************
* @fn zb_SendDataConfirm
*
* @brief The zb_SendDataConfirm callback function is called by the
* ZigBee after a send data operation completes
*
* @param handle - The handle identifying the data transmission.
* status - The status of the operation.
*
* @return none
*/
void zb_SendDataConfirm( uint8 handle, uint8 status )
{
}
/******************************************************************************
* @fn zb_BindConfirm
*
* @brief The zb_BindConfirm callback is called by the ZigBee stack
* after a bind operation completes.
*
* @param commandId - The command ID of the binding being confirmed.
* status - The status of the bind operation.
*
* @return none
*/
void zb_BindConfirm( uint16 commandId, uint8 status )
{
}
/******************************************************************************
* @fn zb_AllowBindConfirm
*
* @brief Indicates when another device attempted to bind to this device
*
* @param
*
* @return none
*/
void zb_AllowBindConfirm( uint16 source )
{
}
/******************************************************************************
* @fn zb_FindDeviceConfirm
*
* @brief The zb_FindDeviceConfirm callback function is called by the
* ZigBee stack when a find device operation completes.
*
* @param searchType - The type of search that was performed.
* searchKey - Value that the search was executed on.
* result - The result of the search.
*
* @return none
*/
void zb_FindDeviceConfirm( uint8 searchType, uint8 *searchKey, uint8 *result )
{
}
/******************************************************************************
* @fn zb_ReceiveDataIndication
*
* @brief The zb_ReceiveDataIndication callback function is called
* asynchronously by the ZigBee stack to notify the application
* when data is received from a peer device.
*
* @param source - The short address of the peer device that sent the data
* command - The commandId associated with the data
* len - The number of bytes in the pData parameter
* pData - The data sent by the peer device
*
* @return none
*/
CONST uint8 strDevice[] = "Device:0x";
CONST uint8 strTemp[] = "Temp: ";
CONST uint8 strBattery[] = "Battery: ";
void zb_ReceiveDataIndication( uint16 source, uint16 command, uint16 len, uint8 *pData )
{
uint8 buf[32];
uint8 *pBuf;
uint8 tmpLen;
uint8 sensorReading;
if (command == SENSOR_REPORT_CMD_ID)
{
// Received report from a sensor
sensorReading = pData[1];
// If tool available, write to serial port
tmpLen = (uint8)osal_strlen( (char*)strDevice );
pBuf = osal_memcpy( buf, strDevice, tmpLen);
_ltoa( source, pBuf, 16 );
pBuf += 4;
*pBuf++ = ' ';
if ( pData[0] == BATTERY_REPORT )
{
tmpLen = (uint8)osal_strlen( (char*)strBattery );
pBuf = osal_memcpy( pBuf, strBattery, tmpLen );
*pBuf++ = (sensorReading / 10 ) + '0'; // convent msb to ascii
*pBuf++ = '.'; // decimal point ( battery reading is in units of 0.1 V
*pBuf++ = (sensorReading % 10 ) + '0'; // convert lsb to ascii
*pBuf++ = ' ';
*pBuf++ = 'V';
}
else
{
tmpLen = (uint8)osal_strlen( (char*)strTemp );
pBuf = osal_memcpy( pBuf, strTemp, tmpLen );
*pBuf++ = (sensorReading / 10 ) + '0'; // convent msb to ascii
*pBuf++ = (sensorReading % 10 ) + '0'; // convert lsb to ascii
*pBuf++ = ' ';
*pBuf++ = 'C';
}
*pBuf++ = '\r';
*pBuf++ = '\n';
*pBuf = '\0';
#if defined( MT_TASK )
debug_str( (uint8 *)buf );
#endif
// can also write directly to uart
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -