
//串口元件

unit uart
{
	public const uint16 ID = 0;

	public link unit TXD {}
	public link unit RXD {}
	
	//[i] linkconst_int32_115200 baud;
	public const int32 baud = 115200;
	
	//---------------------
	//[i] var_uint8 OS_time;
	public uint8 OS_time;
	
	//[i] var_uint8 OS_EventFlag;
	public uint8 OS_EventFlag;
	
	//[i] event receive_event;
	
	//[i] var_int32 Data;
	public int32 Data;
	
	public const uint8 max_length = 40;
	public [uint8*max_length] buffer;
	public uint8 length;
	
	public const int32 XTAL = $freq$;
	
	#include "$run$.txt"
	
	//----------------------------------------------------
	//[i] function_void OS_init;
	public void OS_init()
	{
		OS_EventFlag = 0;
		
		RXD.D0_DIR = 1;
		RXD.D0_OUT = 1;
		
		TXD.D0_DIR = 0;
		TXD.D0_OUT = 1;
		
		//设置默认波特率
		set_baud( baud );
		
		length = 0;
		
		OS_time = 100;
		
		loop( 50 ) {
			delay();
		}
	}
	
	//---------------------------------------------------
	//[i] function_void reset;
	public void reset()
	{
		//重启
		print_string( "AT+RST\r\n" );
		delay();
	}
	
	//---------------------------------------------------
	//[i] function_void set_ap;
	public void set_ap()
	{
		//设置为AP模式
		print_string( "AT+CWMODE=2\r\n" );
		delay();
	}
	//---------------------------------------------------
	//[i] function_void set_route Cstring Cstring;
	public void set_route( [int8*?] name, [int8*?] password )
	{
		//print_string( "AT+CWSAP=\"linkboy8266\",\"123456\",11,0\r\n" );
		
		//设置路由器参数
		print_string( "AT+CWSAP=\"" );
		print_string( name );
		print_string( "\",\"" );
		print_string( password );
		print_string( "\",11,0\r\n" );
		delay();
	}
	//---------------------------------------------------
	//[i] function_void set_server int32;
	public void set_server( int32 port )
	{
		//设置为多连接
		print_string( "AT+CIPMUX=1\r\n" );
		delay();
		
		//开启服务器模式
		print_string( "AT+CIPSERVER=1," );
		print_number( port );
		print_string( "\r\n" );
		delay();
	}
	
	//.... 分隔
	//[m] split;
	
	//---------------------------------------------------
	//[i] function_void set_sta;
	public void set_sta()
	{
		//设置为STA模式
		print_string( "AT+CWMODE=1\r\n" );
		delay();
	}
	
	//---------------------------------------------------
	//[i] function_void send_message Cstring;
	public void send_message( [int8*?] mes )
	{
		//计算长度
		int16 i = 0;
		while( mes[i] != 0 ) {
			i + 1;
		}
		print_string( "AT+CIPSEND=0," );
		print_number( i );
		print_string( "\r\n" );
		delay();
		
		print_string( mes );
		delay();
	}
	
	
	
	//---------------------------------------------------
	//[i] function_void print_string Cstring;
	//void print_string( [int8*?] s )
	
	//---------------------------------------------------
	//[i] function_void OS_run;
	//public void OS_run()
	
	void delay()
	{
		loop( 250 ) loop( 250 ) {}
	}
}













