⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sensor.c

📁 是zmac的协议的全部完整的解析.代码例子很全
💻 C
📖 第 1 页 / 共 2 页
字号:
	static	int8u		ucCount = 4;
   	int16u				time;
	BOOL				bDiscover_Meter;
	int16u				uiTemp;

	#ifdef USE_BOOTLOADER_LIB
		bootloadUtilTick();
	#endif

	time = halCommonGetInt16uMillisecondTick();

	if ( time - lastBlinkTime > 120 )
		GPIO_SETL = BIT(15);

	if ( time - lastBlinkTime > 250 )
	{
		GPIO_CLRL = BIT(15);            //turn on Led1  HeartBeat
		lastBlinkTime = time;

		ucCount --;
		if ( ucCount == 0 )				// 1 秒时间到, 调整实时时间
		{
			Adjust_ucCurrent_Time( &sCurrent_Time );
			ucCount = 4;
			if ( ucSensor_State == HL_SENSOR_STATE_RESET )
				if ( sCurrent_Time.ucSecond == 0 )
					halReboot();

			Read_OD( HL_AMR_METER_ADDR_LOW_INDEX, 2, (int8u*)&bDiscover_Meter );
			if ( bDiscover_Meter )				// 判断是否需要发现电表
			{
				Read_OD( HL_AMR_PARA_CONFIG_INDEX, 8, (int8u*)&bDiscover_Meter );
				if ( bDiscover_Meter == HL_CONN_TO_METER_RS485 )	// 判断与电表的连接方式
				{
					if ( ( ucSensor_State == HL_SENSOR_STATE_NOMAL ) ||
						 ( ucSensor_State == HL_SENSOR_STATE_DATA_FREEZE ) )
					{
						ucSensor_State = HL_SENSOR_STATE_MTR_DISCOVER;
						uiMeter_Disc_Sequence = 99;
						MEMSET( aucSwam, 0, HL_SWAM_SIZE );
						aucSwam[1]	= TRUE;
						Write_OD( HL_AMR_METER_ADDR_LOW_INDEX, 0, aucSwam,	// 清除现有数据
								  sizeof( HL_AMR_Meter_Addr_Low ), TRUE );
					}
				}
				else
				{
					bDiscover_Meter = FALSE;			// 将 bDiscover_Meter 恢复为 FALSE
					Write_OD( HL_AMR_METER_ADDR_LOW_INDEX, 2, &bDiscover_Meter, 1, FALSE );
				}
			}

			Read_OD( HL_AMR_PARA_CONFIG_INDEX, 0, aucSwam );
			if ( ( aucSwam[4] == sCurrent_Time.ucMonth ) &&
				 ( aucSwam[5] == sCurrent_Time.ucDate ) &&
				 ( aucSwam[6] == sCurrent_Time.ucHour ) &&
				 ( aucSwam[7] == sCurrent_Time.ucMinute ) )
			{
				if ( ucSensor_State == HL_SENSOR_STATE_NOMAL )		// 判断是否需要冻结数据
				{
					ucSensor_State = HL_SENSOR_STATE_DATA_FREEZE;
					ucFreeze_Meter_Sequence	= 0;
					ucFreeze_ID_Sequence	= 0;
					MEMSET( aucFrozen_Data, 0, HL_MAX_FROZEN_DATA_LENGTH );
				}

				uiTemp = Get_BIT16_Value( aucSwam+2 );		// uiRead_Cycle
				Sensor_Change_Next_Freeze_Time( uiTemp );	// 修改下一次数据冻结的时间
			}

			uiUart_Resp_Time_Out = Get_BIT16_Value( aucSwam+10 );	// uiUART_Resp_Timeout
//			#ifdef	HL_OUTPUT_DEBUG_INFO_VIA_SERIAL
//				emberSerialPrintf( APP_SERIAL, "\r\nuiUart_Resp_Time_Out=%2x.\r\n", uiUart_Resp_Time_Out );
//			#endif
		}

		if ( ( ucSensor_State == HL_SENSOR_STATE_MTR_DISCOVER ) &&
			 ( bWaiting_UART_Respond == FALSE ) )
			Sensor_Discover_Meter();

		if ( ( ucSensor_State == HL_SENSOR_STATE_DATA_FREEZE ) &&
			 ( bWaiting_UART_Respond == FALSE ) )
			Sensor_Freeze_Data();

		if ( ucReset_Token > 0 )
		{
			switch ( ucReset_Token )		// 判断是否需要将 Token 恢复为缺省值
			{
				case 6:
					halCommonSetToken( TOKEN_HL_NETWORK_PARA,		( int8u* )&sHL_Network_Para );
					break;
				case 5:
					halCommonSetToken( TOKEN_HL_AMR_PARA_CONFIG,	( int8u *)&sHL_AMR_Para_Config );
					break;
				case 4:
					halCommonSetToken( TOKEN_HL_AMR_DATA_IDS,		 ( int8u *)&sHL_AMR_Data_IDs );
					break;
				case 3:
					halCommonSetToken( TOKEN_HL_AMR_METER_ADDR_LOW,	 ( int8u *)&sHL_AMR_Meter_Addr_Low );
					break;
				case 2:
					halCommonSetToken( TOKEN_HL_AMR_METER_ADDR_HIGH, ( int8u *)&sHL_AMR_Meter_Addr_High );
					break;
				case 1:
					halCommonSetToken( TOKEN_HL_AMR_UNRESPOND_METERS,( int8u *)&sHL_AMR_Unrespond_Meters );
					break;
				default:
					break;
			}

			#ifdef	HL_OUTPUT_DEBUG_INFO_VIA_SERIAL
				emberSerialPrintf( APP_SERIAL, "\r\nReset Token %x to default value.\r\n", ucReset_Token );
			#endif
			ucReset_Token --;
		}
	}

	time = halCommonGetInt16uMillisecondTick();	// 重新取一次时间, 否则可能会出现 time < uiUART_Last_Output_Time 的情况

	if ( bWaiting_UART_Respond )		// 判断是否等待 UART 口应答
		if ( time - uiUART_Last_Output_Time > uiUart_Resp_Time_Out )
		{
			bWaiting_UART_Respond = FALSE;
//			#ifdef	HL_OUTPUT_DEBUG_INFO_VIA_SERIAL
//				emberSerialPrintf( APP_SERIAL, "\r\ntime=%2x, uiUART_Last_Output_Time=%2x, uiUart_Resp_Time_Out=%2x.\r\n", time, uiUART_Last_Output_Time, uiUart_Resp_Time_Out );
//				emberSerialWaitSend( APP_SERIAL );
//			#endif
		}

	if ( ucSensor_Back_Off_State == HL_SENSOR_STATE_BACKOFF_START )	// 判断是否等待随机延时
		if ( time - uiBack_Off_Start_Time > uiBack_Off_Time )
			ucSensor_Back_Off_State = HL_SENSOR_STATE_BACKOFF_FINISH;
}



void	checkButtonEvents(void)
{
	EmberNetworkParameters	networkParams;
	EmberStatus				status;

    if (buttonZeroPress)
	{
		buttonZeroPress = FALSE;

		switch ( emberNetworkState() ) 	// if not joined with a network, join
		{
			case EMBER_NO_NETWORK:

				#ifdef USE_HARDCODED_NETWORK_SETTINGS
					Read_OD( HL_NETWORK_PARAMETER_INDEX, 0, aucSwam );
					networkParams.panId			= Get_BIT16_Value( aucSwam + 20 );
					networkParams.radioTxPower	= aucSwam[3];
					networkParams.radioChannel	= aucSwam[0];
				#else
					networkParams.radioTxPower = +3;
					scanUtilFindActiveNetwork ( &( networkParams.radioChannel ),
												&( networkParams.panId ) );
				#endif

				status = emberJoinNetwork(	EMBER_ROUTER,
											&networkParams,
											USE_KEY_WHEN_JOINING );

				#ifdef	HL_OUTPUT_DEBUG_INFO_VIA_SERIAL
					emberSerialPrintf ( APP_SERIAL,
										"\r\njoining, channel 0x%x, panid 0x%2x\r\n",
										networkParams.radioChannel, networkParams.panId );
					emberSerialWaitSend( APP_SERIAL );

					if (status != EMBER_SUCCESS)
					{
						emberSerialPrintf ( APP_SERIAL,
											"\r\nerror returned from emberJoin: 0x%x\r\n", status );
					}
					else
					{
						emberSerialPrintf(APP_SERIAL, "\r\nwaiting for stack up...\r\n");
					}
				#endif
				break;

			case EMBER_JOINING_NETWORK:		// if in the middle of joining, do nothing
				#ifdef	HL_OUTPUT_DEBUG_INFO_VIA_SERIAL
//					emberSerialPrintf( APP_SERIAL, "\r\nApp already trying to join\r\n" );
				#endif
				break;

			case EMBER_JOINED_NETWORK:		// if already joined, turn allow joining on
				emberPermitJoining(0xff);			// turn allow join on
				#ifdef	HL_OUTPUT_DEBUG_INFO_VIA_SERIAL
					emberSerialPrintf(APP_SERIAL, "\r\nTurn permit join ON.\r\n");
				#endif
				break;

			case EMBER_LEAVING_NETWORK:		// if leaving, do nothing
				#ifdef	HL_OUTPUT_DEBUG_INFO_VIA_SERIAL
					emberSerialPrintf(APP_SERIAL, "\r\nApp leaving, no action\r\n");
				#endif
				break;
		}
    }
}





void	halButtonIsr(int8u button, int8u state)
{
	if (button == BUTTON0 && state == BUTTON_PRESSED) 	// button 0 was pushed down
		buttonZeroPress = TRUE;
}





// Called when a message has completed transmission --
// status indicates whether the message was successfully
// transmitted or not.
void	emberMessageSent (	int8u bindingTableIndex,
							int8u clusterId,
							EmberMessageBuffer message,
							EmberStatus status)
{

}

// Called when a unicast has completed transmission --
// status indicates whether the message was successfully
// transmitted or not.
void	emberUnicastSent (	EmberNodeId destination,
							EmberApsFrame *apsFrame,
							EmberMessageBuffer message,
							EmberStatus status )
{
	// nothing to do here since this application doesn't use APS unicasts,
	// it uses transport datagrams
}

// called when a remote device sends a message to add a binding
EmberStatus	emberRemoteSetBindingHandler(EmberBindingTableEntry *entry)
{
	// this application doesn't allow external provisioning
	return EMBER_INVALID_BINDING_INDEX;
}

// called when a remote device sends a message to delete a binding
EmberStatus	emberRemoteDeleteBindingHandler(int8u index)
{
	// this application doesn't allow external provisioning
	return EMBER_INVALID_BINDING_INDEX;
}

// for compile
void printDataMode(void)
{
}

#ifdef USE_BOOTLOADER_LIB
// When a device sends out a bootloader query, the bootloader
// query response messages are parsed by the bootloader-util
// libray and and handled to this function. This application
// simply prints out the EUI of the device sending the query
// response.
void	bootloadUtilQueryResponseHandler (  boolean bootloaderActive,
											int16u manufacturerId,
											int8u *hardwareTag,
											EmberEUI64 targetEui,
											int8u bootloaderCapabilities,
											int8u platform,
											int8u micro,
											int8u phy,
											int16u ulversion )
{
		emberSerialPrintf(APP_SERIAL,"RX [BL QUERY RESP] eui: ");
		printEUI64(APP_SERIAL, (EmberEUI64*)targetEui);
		emberSerialPrintf(APP_SERIAL," running %p\r\n",
						bootloaderActive ? "bootloader":"stack");
		emberSerialWaitSend(APP_SERIAL);
}

// This function is called by the bootloader-util library
// to ask the application if it is ok to start the bootloader.
// This happens when the device is meant to be the target of
// a bootload. The application could compare the manufacturerId
// and/or hardwareTag arguments to known values to enure that
// the correct image will be bootloaded to this device.
BOOL	bootloadUtilLaunchRequestHandler (  int16u manufacturerId,
											int8u *hardwareTag,
											EmberEUI64 sourceEui )
{
	// TODO: Compare arguments to known values.

	// TODO: Check for minimum required radio signal strength (RSSI).

	// TODO: Do not agree to launch the bootloader if any of the above conditions
	// are not met.  For now, always agree to launch the bootloader.
	return TRUE;
}

#endif // USE_BOOTLOADER_LIB

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -