
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()
{
	//
}

