/**************************************************************************************************
Filename: SampleApp.c
Revised: $Date: 2009-03-18 15:56:27 -0700 (Wed, 18 Mar 2009) $
Revision: $Revision: 19453 $
Description: Combined Application with Interrupt Handling and Environmental Sensing
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 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极地 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 <stdio.h>
#include <string.h>
#include "OSAL.h"
#include "ZGlobals.h"
#include "AF.h"
#include "aps_groups.h"
#include "ZDApp.h"
#include "MT_UART.h"
#include "SampleApp.h" // 包含头文件
#include "SampleAppHw.h"
#include "OnBoard.h"
#include "sht11.h" // 温湿度传感器头文件
/* HAL */
#include "hal_lcd.h"
#include "hal_led.h"
#include "hal_key.h"
// 确保所有簇ID被定义
#ifndef SAMPLEAPP_SENDINT_CLUSTERID
#define SAMPLEAPP_SENDINT_CLUSTERID 0x0003
#endif
#ifndef SAMPLEAPP_ALARM_CLUSTERID
#define SAMPLEAPP_ALARM_CLUSTERID 0x0004 // 报警簇ID
#endif
/*********************************************************************
* CONSTANTS
*/
// 定义中断事件
#define SAMPLEAPP_SEND_SENSOR_INT_EVT 0x0004
// 学生信息
#define STUDENT_ID_NAME "42213361ljl"
#define SENSOR2_NAME "HW01" // 传感器2名称
// 温湿度阈值
#define TEMP_THRESHOLD 25.0
#define HUMI_THRESHOLD 60.0
// 中断计数阈值
#define INT_COUNT_THRESHOLD 5
/*********************************************************************
* GLOBAL VARIABLES
*/
const cId_t SampleApp_ClusterList[SAMPLEAPP_MAX_CLUSTERS] =
{
SAMPLEAPP_PERIODIC_CLUSTERID, // 温湿度数据
SAMPLEAPP_FLASH_CLUSTERID, // Flash命令
SAMPLEAPP_SENDINT_CLUSTERID, // 中断消息
SAMPLEAPP_ALARM_CLUSTERID // 报警信息
};
const SimpleDescriptionFormat_t SampleApp_SimpleDesc =
{
SAMPLEAPP_ENDPOINT, // int Endpoint;
SAMPLEAPP_PROFID, // uint16 AppProfId;
SAMPLEAPP_DEVICEID, // uint16 AppDeviceId;
SAMPLEAPP_DEVICE_VERSION, // int AppDevVer:4;
SAMPLEAPP_FLAGS, // int AppFlags:4;
SAMPLEAPP_MAX_CLUSTERS, // uint8 AppNumInClusters;
(cId_t *)SampleApp_ClusterList, // uint8 *pAppInClusterList;
SAMPLEAPP_MAX_CLUSTERS, // uint8 AppNumOutClusters;
(cId_t *)SampleApp_ClusterList // uint8 *pAppOutClusterList;
};
endPointDesc_t SampleApp_epDesc;
/*********************************************************************
* LOCAL VARIABLES
*/
uint8 SampleApp_TaskID;
devStates_t SampleApp_NwkState;
uint8 SampleApp_TransID;
uint8 gIntFlag = 0; // 中断标志
// 新增状态变量
uint8 gTempHumiAlarm = 0; // 温湿度异常标志
uint8 gIntAlarm = 0; // 中断异常标志
uint8 gIntCount = 0; // 中断计数
uint8 gLED1State = 0; // LED1状态(温湿度模块)
uint8 gLED2State = 0; // LED2状态(中断模块)
afAddrType_t SampleApp_Periodic_DstAddr;
afAddrType_t SampleApp_Flash_DstAddr;
afAddrType_t SampleApp_Alarm_DstAddr; // 报警目标地址
aps_Group_t SampleApp_Group;
uint8 SampleAppPeriodicCounter = 0;
uint8 SampleAppFlashCounter = 0;
/*********************************************************************
* FUNCTION PROTOTYPES
*/
void SampleApp_HandleKeys(uint8 shift, uint8 keys);
void SampleApp_MessageMSGCB(afIncomingMSGPacket_t *pckt);
void SampleApp_SendPeriodicMessage(void);
void SampleApp_SendFlashMessage(uint16 flashTime);
void SensorIO_Init(void);
void SampleApp_Process_SensorInt(void);
void SampleApp_ProcessIntMessage(afIncomingMSGPacket_t *pkt);
void SampleApp_SendAlarmMessage(const char *alarmMsg);
void SampleApp_CheckTempHumiThreshold(float temp, float humi);
void SampleApp_CheckIntCount(void);
void SampleApp_ProcessControlCommand(char *cmd);
HAL_ISR_FUNCTION(halP12isr, P1INT_VECTOR);
void Delay(int n);
void BuildSensorDataString(char *buf, uint8 bufSize, float temp, float humi);
void BuildInterruptString(char *buf, uint8 bufSize);
void BuildAlarmString(char *buf, uint8 bufSize, const char *alarmType, uint8 isReset);
void BuildForcedAlarmString(char *buf, uint8 bufSize, const char *alarmType);
void RemoveSpaces(char *str); // 移除字符串中的空格
/*********************************************************************
* @fn SampleApp_Init
*/
void SampleApp_Init(uint8 task_id)
{
SampleApp_TaskID = task_id;
SampleApp_NwkState = DEV_INIT;
SampleApp_TransID = 0;
#if defined(BUILD_ALL_DEVICES)
if (readCoordinatorJumper())
zgDeviceLogicalType = ZG_DEVICETYPE_COORDINATOR;
else
zgDeviceLogicalType = ZG_DEVICETYPE_ROUTER;
#endif
#if defined(HOLD_AUTO_START)
ZDOInitDevice(0);
#endif
// 广播地址用于发送温湿度数据
SampleApp_Periodic_DstAddr.addrMode = (afAddrMode_t)AddrBroadcast;
SampleApp_Periodic_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
SampleApp_Periodic_DstAddr.addr.shortAddr = 0xFFFF;
// 组播地址用于Flash命令
SampleApp_Flash_DstAddr.addrMode = (afAddrMode_t)afAddrGroup;
SampleApp_Flash_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
SampleApp_Flash_DstAddr.addr.shortAddr = SAMPLEAPP_FLASH_GROUP;
// 报警目标地址(协调器)
SampleApp_Alarm_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;
SampleApp_Alarm_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
SampleApp_Alarm_DstAddr.addr.shortAddr = 0x0000; // 协调器地址
// 端点描述符
SampleApp_epDesc.endPoint = SAMPLEAPP_ENDPOINT;
SampleApp_epDesc.task_id = &SampleApp_TaskID;
SampleApp_epDesc.simpleDesc = (SimpleDescriptionFormat_t *)&SampleApp_SimpleDesc;
SampleApp_epDesc.latencyReq = noLatencyReqs;
afRegister(&SampleApp_epDesc);
RegisterForKeys(SampleApp_TaskID);
MT_UartRegisterTaskID(SampleApp_TaskID);
// 默认组设置
SampleApp_Group.ID = SAMPLEAPP_FLASH_GROUP;
osal_memcpy(SampleApp_Group.name, "Group 1", 7);
aps_AddGroup(SAMPLEAPP_ENDPOINT, &SampleApp_Group);
// 初始化中断IO
SensorIO_Init();
// 初始化LED状态
HalLedSet(HAL_LED_1, HAL_LED_MODE_OFF);
HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF);
gLED1State = 0;
gLED2State = 0;
#if defined(LCD_SUPPORTED)
HalLcdWriteString("Int&Env Sensor", HAL_LCD_LINE_1);
HalLcdWriteString(STUDENT_ID_NAME, HAL_LCD_LINE_2);
#endif
}
/*********************************************************************
* @fn SampleApp_ProcessEvent
*/
uint16 SampleApp_ProcessEvent(uint8 task_id, uint16 events)
{
afIncomingMSGPacket_t *MSGpkt;
(void)task_id; // Unused parameter
if (events & SYS_EVENT_MSG) {
MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive(SampleApp_TaskID);
while (MSGpkt) {
switch (MSGpkt->hdr.event) {
case KEY_CHANGE:
SampleApp_HandleKeys(((keyChange_t *)MSGpkt)->state,
((keyChange_t *)MSGpkt)->keys);
break;
case AF_INCOMING_MSG_CMD:
SampleApp_MessageMSGCB(MSGpkt);
break;
case ZDO_STATE_CHANGE:
SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status);
if ((SampleApp_NwkState == DEV_ZB_COORD) ||
(SampleApp_NwkState == DEV_ROUTER) ||
(SampleApp_NwkState == DEV_END_DEVICE))
{
HalLedSet(HAL_LED_1, HAL_LED_MODE_ON);
osal_start_timerEx(SampleApp_TaskID,
SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT);
}
break;
default:
break;
}
osal_msg_deallocate((uint8 *)MSGpkt);
MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive(SampleApp_TaskID);
}
return (events ^ SYS_EVENT_MSG);
}
if (events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT) {
// 发送温湿度数据
char buffer[64];
float humi, temp;
if (GetHumiAndTemp(&humi, &temp) == 0) {
// 构建数据字符串:"42213361ljl huim:26 temp:45"
BuildSensorDataString(buffer, sizeof(buffer), temp, humi);
// 广播发送
AF_DataRequest(&SampleApp_Periodic_DstAddr,
&SampleApp_epDesc,
SAMPLEAPP_PERIODIC_CLUSTERID,
strlen(buffer),
(uint8 *)buffer,
&SampleApp_TransID,
AF_DISCV_ROUTE,
AF_DEFAULT_RADIUS);
// 检查温湿度阈值
SampleApp_CheckTempHumiThreshold(temp, humi);
}
// 重新设置定时器
osal_start_timerEx(SampleApp_TaskID,
SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT);
return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT);
}
if (events & SAMPLEAPP_SEND_SENSOR_INT_EVT) {
SampleApp_Process_SensorInt();
// 检查中断计数
SampleApp_CheckIntCount();
return (events ^ SAMPLEAPP_SEND_SENSOR_INT_EVT);
}
return 0;
}
/*********************************************************************
* @fn SampleApp_HandleKeys
*/
void SampleApp_HandleKeys(uint8 shift, uint8 keys)
{
(void)shift; // Unused parameter
if (keys & HAL_KEY_SW_6) {
SampleApp_SendFlashMessage(SAMPLEAPP_FLASH_DURATION);
}
if (keys & HAL_KEY_SW_2) {
aps_Group_t *grp = aps_FindGroup(SAMPLEAPP_ENDPOINT,
SAMPLEAPP_FLASH_GROUP);
if (grp) {
aps_RemoveGroup(SAMPLEAPP_ENDPOINT, SAMPLEAPP_FLASH_GROUP);
} else {
aps_AddGroup(SAMPLEAPP_ENDPOINT, &SampleApp_Group);
}
}
}
/*********************************************************************
* @fn SensorIO_Init
*/
void SensorIO_Init(void)
{
// 配置P1.2为中断输入
P1DIR &= ~0x04; // P1.2输入模式
P1INP &= ~0x04; // 上拉电阻
P1IEN |= 0x04; // 使能中断
PICTL &= ~0x02; // 上升沿触发
// 使能中断
IEN2 |= 0x10; // P1中断使能
IEN0 |= 0x80; // 全局中断使能
P1IFG &= ~0x04; // 清除中断标志
}
/*********************************************************************
* @fn halP12isr
*/
HAL_ISR_FUNCTION(halP12isr, P1INT_VECTOR)
{
if (P1IFG & 0x04) { // 检查P1.2中断
gIntFlag = 0x01; // 设置中断标志
gIntCount++; // 增加中断计数
osal_set_event(SampleApp_TaskID, SAMPLEAPP_SEND_SENSOR_INT_EVT);
P1IFG &= ~0x04; // 清除中断标志
}
P1IF = 0; // 清除端口中断标志
}
/*********************************************************************
* @fn SampleApp_Process_SensorInt
*/
void SampleApp_Process_SensorInt(void)
{
char intBuffer[64];
// 构建中断消息字符串
BuildInterruptString(intBuffer, sizeof(intBuffer));
// 发送中断通知到协调器(短地址0x0000)
afAddrType_t intDstAddr = {
.addrMode = (afAddrMode_t)Addr16Bit,
.endPoint = SAMPLEAPP_ENDPOINT,
.addr.shortAddr = 0x0000
};
AF_DataRequest(&intDstAddr,
&SampleApp_epDesc,
SAMPLEAPP_SENDINT_CLUSTERID,
strlen(intBuffer),
(uint8 *)intBuffer,
&SampleApp_TransID,
AF_DISCV_ROUTE,
AF_DEFAULT_RADIUS);
// 本地指示
HalLedBlink(HAL_LED_1, 4, 50, 250);
HalUARTWrite(0, "\r\nINT Triggered!", 15);
gIntFlag = 0x00; // 重置中断标志
}
/*********************************************************************
* @fn SampleApp_MessageMSGCB
*/
void SampleApp_MessageMSGCB(afIncomingMSGPacket_t *pkt)
{
switch (pkt->clusterId) {
case SAMPLEAPP_PERIODIC_CLUSTERID: // 温湿度数据
// 在协调器上显示温湿度数据
HalUARTWrite(0, (uint8 *)"\r\n", 2);
HalUARTWrite(0, pkt->cmd.Data, pkt->cmd.DataLength);
break;
case SAMPLEAPP_SENDINT_CLUSTERID: // 中断消息
SampleApp_ProcessIntMessage(pkt);
break;
case SAMPLEAPP_FLASH_CLUSTERID: { // Flash命令/控制命令
// 将接收到的数据视为字符串命令
char cmd[32];
uint8 len = pkt->cmd.DataLength;
if (len > sizeof(cmd) - 1) len = sizeof(cmd) - 1;
memcpy(cmd, pkt->cmd.Data, len);
cmd[len] = '\0'; // 确保字符串结束
// 处理控制命令
SampleApp_ProcessControlCommand(cmd);
break;
}
case SAMPLEAPP_ALARM_CLUSTERID: // 报警信息
// 在协调器上显示报警信息
HalUARTWrite(0, (uint8 *)"\r\n", 2);
HalUARTWrite(0, pkt->cmd.Data, pkt->cmd.DataLength);
break;
}
}
/*********************************************************************
* @fn SampleApp_ProcessIntMessage
*/
void SampleApp_ProcessIntMessage(afIncomingMSGPacket_t *pkt)
{
HalLedBlink(HAL_LED_1, 8, 100, 500); // 长闪烁指示
HalUARTWrite(0, "\r\nInterrupt Received: ", 23);
HalUARTWrite(0, pkt->cmd.Data, pkt->cmd.DataLength);
}
/*********************************************************************
* @fn SampleApp_SendFlashMessage
*/
void SampleApp_SendFlashMessage(uint16 flashTime)
{
uint8 buffer[3];
buffer[0] = (uint8)(SampleAppFlashCounter++);
buffer[1] = LO_UINT16(flashTime);
buffer[2] = HI_UINT16(flashTime);
if (AF_DataRequest(&SampleApp_Flash_DstAddr,
&SampleApp_epDesc,
SAMPLEAPP_FLASH_CLUSTERID,
3,
buffer,
&SampleApp_TransID,
AF_DISCV_ROUTE,
AF_DEFAULT_RADIUS) != afStatus_SUCCESS)
{
// 错误处理
}
}
/*********************************************************************
* @fn SampleApp_SendAlarmMessage
*/
void SampleApp_SendAlarmMessage(const char *alarmMsg)
{
uint8 len = strlen(alarmMsg);
AF_DataRequest(&SampleApp_Alarm_DstAddr,
&SampleApp_epDesc,
SAMPLEAPP_ALARM_CLUSTERID,
len,
(uint8 *)alarmMsg,
&SampleApp_TransID,
AF_DISCV_ROUTE,
AF_DEFAULT_RADIUS);
}
/*********************************************************************
* @fn SampleApp_CheckTempHumiThreshold
*/
void SampleApp_CheckTempHumiThreshold(float temp, float humi)
{
char buffer[64];
// 检查是否超过阈值:任一条件超过就触发报警
if (temp > TEMP_THRESHOLD || humi > HUMI_THRESHOLD) {
if (!gTempHumiAlarm) {
gTempHumiAlarm = 1;
// LED1持续闪烁(温湿度模块)
HalLedBlink(HAL_LED_1, 0, 50, 500);
BuildAlarmString(buffer, sizeof(buffer), "温湿度异常", 0);
SampleApp_SendAlarmMessage(buffer);
}
} else {
// 当两个都恢复到正常范围,才解除报警
if (gTempHumiAlarm && temp <= TEMP_THRESHOLD && humi <= HUMI_THRESHOLD) {
gTempHumiAlarm = 0;
// 恢复LED1到控制命令设置的状态
if (gLED1State) {
HalLedSet(HAL_LED_1, HAL_LED_MODE_ON);
} else {
HalLedSet(HAL_LED_1, HAL_LED_MODE_OFF);
}
BuildAlarmString(buffer, sizeof(buffer), "温湿度异常", 1);
SampleApp_SendAlarmMessage(buffer);
}
}
}
/*********************************************************************
* @fn SampleApp_CheckIntCount
*/
void SampleApp_CheckIntCount(void)
{
char buffer[64];
// 检查中断计数是否超过阈值
if (gIntCount >= INT_COUNT_THRESHOLD) {
if (!gIntAlarm) {
gIntAlarm = 1;
HalLedBlink(HAL_LED_2, 0, 50, 500); // LED2持续闪烁(中断模块)
BuildAlarmString(buffer, sizeof(buffer), "传感状态异常", 0);
SampleApp_SendAlarmMessage(buffer);
}
} else {
if (gIntAlarm) {
gIntAlarm = 0;
// 恢复LED2到控制命令设置的状态
if (gLED2State) {
HalLedSet(HAL_LED_2, HAL_LED_MODE_ON);
} else {
HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF);
}
BuildAlarmString(buffer, sizeof(buffer), "传感状态异常", 1);
SampleApp_SendAlarmMessage(buffer);
}
}
}
/*********************************************************************
* @fn RemoveSpaces
* @brief 移除字符串中的所有空格
*/
void RemoveSpaces(char *str)
{
char *p1 = str, *p2 = str;
while (*p1) {
if (*p1 != ' ') {
*p2++ = *p1;
}
p1++;
}
*p2 = '\0';
}
/*********************************************************************
* @fn SampleApp_ProcessControlCommand
*/
void SampleApp_ProcessControlCommand(char *cmd)
{
char buffer[64];
// 调试:输出接收到的命令
HalUARTWrite(0, "\r\nRecv Cmd: ", 11);
HalUARTWrite(0, (uint8*)cmd, strlen(cmd));
// 复制命令并移除所有空格
char cmdCopy[64];
strncpy(cmdCopy, cmd, sizeof(cmdCopy)-1);
cmdCopy[sizeof(cmdCopy)-1] = '\0';
RemoveSpaces(cmdCopy);
// 调试:输出处理后的命令
HalUARTWrite(0, "\r\nProcessed: ", 12);
HalUARTWrite(0, (uint8*)cmdCopy, strlen(cmdCopy));
// 检查命令前缀
if (strncmp(cmdCopy, STUDENT_ID_NAME, strlen(STUDENT_ID_NAME)) != 0) {
HalUARTWrite(0, "\r\nInvalid Prefix", 15);
return; // 命令前缀不匹配
}
char *command = cmdCopy + strlen(STUDENT_ID_NAME);
// 处理LED控制命令 - Num1控制温湿度模块LED1
if (strstr(command, "Num1Led")) {
if (strstr(command, "on")) {
HalLedSet(HAL_LED_1, HAL_LED_MODE_ON);
gLED1State = 1;
HalUARTWrite(0, "\r\nTEMP LED1 ON", 13);
}
else if (strstr(command, "off")) {
HalLedSet(HAL_LED_1, HAL_LED_MODE_OFF);
gLED1State = 0;
HalUARTWrite(0, "\r\nTEMP LED1 OFF", 14);
}
else if (strstr(command, "relie")) {
// 强制解除温湿度异常
if (gTempHumiAlarm) {
gTempHumiAlarm = 0;
HalLedSet(HAL_LED_1, HAL_LED_MODE_OFF);
gLED1State = 0;
BuildForcedAlarmString(buffer, sizeof(buffer), "温湿度");
SampleApp_SendAlarmMessage(buffer);
HalUARTWrite(0, "\r\n强制解除温湿度异常", 20);
}
else {
HalUARTWrite(0, "\r\n无温湿度异常", 12);
}
}
}
// 处理LED控制命令 - Num2控制中断模块LED2
else if (strstr(command, "Num2Led")) {
if (strstr(command, "on")) {
HalLedSet(HAL_LED_2, HAL_LED_MODE_ON);
gLED2State = 1;
HalUARTWrite(0, "\r\nINT LED2 ON", 12);
}
else if (strstr(command, "off")) {
HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF);
gLED2State = 0;
HalUARTWrite(0, "\r\nINT LED2 OFF", 13);
}
else if (strstr(command, "relie")) {
// 强制解除中断异常
if (gIntAlarm) {
gIntAlarm = 0;
gIntCount = 0;
HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF);
gLED2State = 0;
BuildForcedAlarmString(buffer, sizeof(buffer), "传感");
SampleApp_SendAlarmMessage(buffer);
HalUARTWrite(0, "\r\n强制解除传感异常", 18);
}
else {
HalUARTWrite(0, "\r\n无传感异常", 10);
}
}
}
// 处理全局解除命令
else if (strstr(command, "LedRile")) {
// 解除所有报警并熄灭所有LED
uint8 cleared = 0;
if (gTempHumiAlarm) {
gTempHumiAlarm = 0;
HalLedSet(HAL_LED_1, HAL_LED_MODE_OFF);
gLED1State = 0;
BuildForcedAlarmString(buffer, sizeof(buffer), "温湿度");
SampleApp_SendAlarmMessage(buffer);
cleared = 1;
}
if (gIntAlarm) {
gIntAlarm = 0;
gIntCount = 0;
HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF);
gLED2State = 0;
BuildForcedAlarmString(buffer, sizeof(buffer), "传感");
SampleApp_SendAlarmMessage(buffer);
cleared = 1;
}
if (cleared) {
HalUARTWrite(0, "\r\n强制解除所有异常", 18);
} else {
HalUARTWrite(0, "\r\n无任何异常", 10);
}
}
}
/*********************************************************************
* @fn BuildSensorDataString
* @brief 构建传感器数据字符串:"42213361ljl huim:26 temp:45"
*/
void BuildSensorDataString(char *buf, uint8 bufSize, float temp, float humi)
{
if (bufSize < 50) {
buf[0] = '\0';
return;
}
// 新格式:"42213361ljl huim:26 temp:45"
sprintf(buf, "%s huim:%.0f temp:%.0f", STUDENT_ID_NAME, humi, temp);
}
/*********************************************************************
* @fn BuildInterruptString
* @brief 构建中断消息字符串
*/
void BuildInterruptString(char *buf, uint8 bufSize)
{
if (bufSize < 50) {
buf[0] = '\0';
return;
}
// 明确的中断消息
sprintf(buf, "%s %s interrupt triggered!", STUDENT_ID_NAME, SENSOR2_NAME);
}
/*********************************************************************
* @fn BuildAlarmString
* @brief 构建报警信息字符串
*/
void BuildAlarmString(char *buf, uint8 bufSize, const char *alarmType, uint8 isReset)
{
if (bufSize < 50) {
buf[0] = '\0';
return;
}
if (isReset) {
sprintf(buf, "%s %s alarm cleared", STUDENT_ID_NAME, alarmType);
} else {
sprintf(buf, "%s %s alarm triggered!", STUDENT_ID_NAME, alarmType);
}
}
/*********************************************************************
* @fn BuildForcedAlarmString
* @brief 构建强制解除报警信息字符串
*/
void BuildForcedAlarmString(char *buf, uint8 bufSize, const char *alarmType)
{
if (bufSize < 50) {
buf[0] = '\0';
return;
}
sprintf(buf, "%s %s alarm force-cleared!", STUDENT_ID_NAME, alarmType);
}
/*********************************************************************
* @fn Delay
*/
void Delay(int n)
{
volatile int i, t;
for(i = 0; i < 5; i++)
for(t = 0; t < n; t++);
}
/*********************************************************************
*********************************************************************/
检查代码,为什么灯光控制和报警功能,均没有实现,给出解决方法,给出修改后的完整代码