
//2015.5.27
//开始设计串口协议解析器

unit Protocol
{
	public const uint16 ID = 0;

	//[i] linkinterface_reader reader0;
	link unit reader0 {}
	
	//[i] var_uint8 OS_EventFlag;
	public uint8 OS_EventFlag;
	//[i] event trig_event;
	//[i] event error_event;
	
	->[uint32*?] pro;
	
	int16 index;
	
	bool ProtocolPause;
	
	//[i] var_int32 Angle;
	public int32 Angle;
	
	[#.code uint32*?] v_Protocol0 = [3,0x0000FFAA,0x00000000,0x00000000,];
	
	//---------------------------------------------------
	//[i] function_void OS_init;
	public void OS_init()
	{
		OS_EventFlag = 0;
		
		index = 0;
		
		ProtocolPause = false;
		
		pro -> v_Protocol0;
	}
	//---------------------------------------------------
	//[i] function_void OS_thread;
	public void OS_thread()
	{
		if( ProtocolPause && OS_EventFlag.0(bit) == 0 ) {
			index = 0;
			ProtocolPause = false;
		}
		if( ProtocolPause ) {
			return;
		}
		//判断数据是否就绪
		if( !reader0.IsReady() ){
			return;
		}
		if( index == 0 ) {
			reader0.SetStart();
		}
		uint8 data = (uint8)(uint16)(uint)reader0.GetData();
		
		uint32 PData = pro[index + 1];
		uint8 Type = PData.24(uint8);
		uint8 Mask = PData.8(uint8);
		uint8 Value = PData.0(uint8);
		
		if( Type == 0 ) {
			if( Value & Mask != data & Mask  ) {
				OS_EventFlag.1(bit) = 1;
				index = 0;
				return;
			}
			else {
				index + 1;
			}
		}
		if( Type == 1 ) {
			if( data == Value ) {
				index + 1;
			}
		}
		
		if( (uint)index < pro[0] ) {
			return;
		}
		ProtocolPause = true;
		
		uint16 d = (uint16)(uint)reader0.ReadBuffer( 1 );
		d.8(uint8) = (uint8)(uint16)(uint)reader0.ReadBuffer( 2 );
		Angle = (int)d;
		Angle -= 320;
		
		OS_EventFlag.0(bit) = 1;
	}
}



