
uint8 OS_EventFlag;

const uint16 max_length = 20;
[uint8*max_length] buffer;
uint8 length;
uint8 ErrNumber;
uint8 DataLength;
bool DataIsOK;

bool isBusy;
public [->uint16 * 100] FlagList;


//虚拟类型--电脑端内存模型
public memory linka
{
	type = [uint32] uint32;
}
void set_uint32( uint32 addr, uint32 data )
{
	SetValue( addr.8(uint8), addr.0(uint8), data );
}
uint32 get_uint32( uint32 addr )
{
	return GetValue( addr.8(uint8), addr.0(uint8) );
}


interrupt [0x10]
void Uart( uint8 x )
{
	buffer[length] = x;
	length + 1;
	
	//+++++++++++++++++++++++
	//通信协议解析 0x55 N * * * ... * * * SUM
	uint8 L = length;
	uint8 Head = buffer[0];
	
	//判断协议头部
	if( L == 1 && Head != 0x55 ) {
		length = 0;
		return;
	}
	if( L == 2 ) {
		DataLength = buffer[1];
		return;
	}
	if( L < DataLength + 3 ) return;
	uint8 CMD = buffer[2];
	uint8 SUM = buffer[L - 1];
	
	length = 0;
	
	//通信协议解析完成
	//+++++++++++++++++++++++
	
	//建立通信标志
	if( DataLength == 0 ) {
//		StartWrite();
		
//		if( Connected ) {
//			//WriteByte( 0x55 );
//		}
//		else {
//			//WriteByte( 0xAA );
//		}
//		EndWrite();
		
//		//如果已经连接, 启动看门狗并等待触发复位
//		if( Connected ) {
//			#asm "CLI"
//			OPEN_WATCHDOG();
//			forever {}
//		}
//		Connected = true;
		return;
	}
	//解析GUI协议, 分为事件和通信两种
	if( CMD == 0 ) {
		DataIsOK = true;
		return;
	}
	if( CMD == 1 ) {
		uint8 id = buffer[3];
		uint8 EI = buffer[4];
		FlagList[id] = FlagList[id] | EI;
		return;
	}
}

public void OS_init()
{
	length = 0;
	OS_EventFlag = 0;
	OS_EventFlag.3(bit) = 1;
	ErrNumber = 0;
	DataLength = 0;
	DataIsOK = false;
	isBusy = false;
	#.OS.REMO_SetInterrupt( 0x10, true );
}
//建立连接
public void Connect()
{
}
public void OS_ClearWatchDog()
{
}
//---------------------------------------------------
//发送一个字符串
public void SetString( uint8 id, uint8 Command, [#.code uint8*?] string )
{
	isBusy = true;
	
	StartWrite();
	#.OS.REMO_WriteByte( 0xAA );
	#.OS.REMO_WriteByte( 0 );
	#.OS.REMO_WriteByte( id ); //发给指定ID的控件
	#.OS.REMO_WriteByte( Command ); //命令类型
	
	int16 i = 0;
	do {
		uint8 char = string[i];
		#.OS.REMO_WriteByte( char );
		i + 1;
	}
	while( char != 0 );
	
	EndWrite();
	
	isBusy = false;
}
//---------------------------------------------------
//发送一个参数
public void SetValue( uint8 id, uint8 Command, uint32 data )
{
	isBusy = true;
	
	StartWrite();
	#.OS.REMO_WriteByte( 0x55 );
	#.OS.REMO_WriteByte( 6 );
	#.OS.REMO_WriteByte( id ); //发给指定ID的控件
	#.OS.REMO_WriteByte( Command ); //命令类型
	#.OS.REMO_WriteByte( data.0(uint8) );
	#.OS.REMO_WriteByte( data.8(uint8) );
	#.OS.REMO_WriteByte( data.16(uint8) );
	#.OS.REMO_WriteByte( data.24(uint8) );
	#.OS.REMO_WriteByte( 0 );
	EndWrite();
	
	isBusy = false;
}
//---------------------------------------------------
//读取一个参数
//2015.8.2
//注意: 等待时间溢出常数需要根据波特率调整
//115200: 1000 (好像有时候丢失数据)
//9600: 5000 (很稳定, 几乎没有需要重新发送的情况)
//最新情况: 5000 还是不行, 设置为 50000 没再出现过问题

public uint32 GetValue( uint8 vID, uint8 Command )
{
	isBusy = true;
	
	#.OS.REMO_SetInterrupt( 0x0A, true );
	
	//reconnect:
	StartWrite();
	#.OS.REMO_WriteByte( 0x55 );
	#.OS.REMO_WriteByte( 2 );
	#.OS.REMO_WriteByte( vID ); //发给指定数字框
	#.OS.REMO_WriteByte( 0x80 + Command ); //读取参数
	#.OS.REMO_WriteByte( 0 );
	EndWrite();
	
	uint16 tick = 0;
	while( !DataIsOK ) {
		//tick + 1;
		//if( tick == 50000 ) {
			//#.控制器.指示灯反转();
		//	goto reconnect;
		//}
	}
	DataIsOK = false;
	uint32 D = 0;
	
	D.0(uint8) = buffer[4];
	D.8(uint8) = buffer[5];
	D.16(uint8) = buffer[6];
	D.24(uint8) = buffer[7];
	
	#.OS.REMO_SetInterrupt( 0x0A, false );
	
	isBusy = false;
	return D;
}
void StartWrite()
{
	//
}
void EndWrite()
{
	//
}

