
//MEGA单片机组件
unit MEGA
{
	public const uint16 ID = 0;
	
	#include "$run$.txt"
	
	//---------------------------------------------------
	//[i] var_uint8 OS_time;
	public uint8 OS_time;
	
	//[i] var_uint8 OS_EventFlag;
	uint8 OS_EventFlag;
	//[i] event StartEvent;
	//[i] event IdleEvent;

	uint8 WatchDogTick;
	
	
	int8 valueA;
	int8 valueB;
	bit DTA1;
	bit DTA2;
	bit DTB1;
	bit DTB2;
	
	int8 tick;
	
	
	//[i] function_void OS_thread;
	//[i] interface_reader reader0;
	//[i] linkconst_int16_50 max_length;
	//[i] linkconst_int32_115200 baud;
	//[i] event receive_event;
	//[i] function_void set_baud int32;
	//[i] function_int32 get_data;
	//[i] function_bool is_ready;
	//[i] function_void print_char int32;
	//[i] function_void print_string Astring;
	//[i] function_void print_number int32;
	public link unit PORT {}
	public const int32 baud = 9600;
	#include "uart_v1_$run$.txt"
	
	//---------------------------------------------------
	//[i] function_void OS_init;
	public void OS_init()
	{
		uart_OS_init();
		
		FlashOpen = false;
		FlashForever = false;
		FlashNumber = 0;
		swap = 0;
		WatchDogTick = 0;
		OS_time = 50;
		OS_EventFlag = 0;
		OS_EventFlag.0(bit) = 1;
		
		LED_DDR = 1;
		LED_PORT = 0;
		
		RunInit();
				
		valueA = 100;
		valueB = 100;
		tick = 0;
		
		DTA1 = 0;
		DTA2 = 0;
		DTB1 = 0;
		DTB2 = 0;
		
		//开机闪烁, 用于等待外设准备好(带有阻容复位的外设)
		LED_PORT = 1;
		delayLED();
		LED_PORT = 0;
		delayLED();
		LED_PORT = 1;
		delayLED();
		LED_PORT = 0;
		delayLED();
		LED_PORT = 1;
		delayLED();
		LED_PORT = 0;
		delayLED();
		delayLED();
		delayLED();
		delayLED();
		delayLED();
		delayLED();
	}
	//---------------------------------------------------
	//[i] function_void OS_run100us;
	public void OS_run100us()
	{
		tick % 100;
		bool ResetA = tick == 0;
		bool ResetB = ResetA;
		
		if( tick >= valueA ) {
			RES.PORTC.2(bit) = 0;
			RES.PORTC.3(bit) = 0;
			ResetA = false;
		}
		if( ResetA ) {
			RES.PORTC.2(bit) = DTA1;
			RES.PORTC.3(bit) = DTA2;
		}
		if( tick >= valueB ) {
			RES.PORTC.4(bit) = 0;
			RES.PORTC.5(bit) = 0;
			ResetB = false;
		}
		if( ResetB ) {
			RES.PORTC.4(bit) = DTB1;
			RES.PORTC.5(bit) = DTB2;
		}
		
		tick + 1;
	}
	//---------------------------------------------------
	//[i] function_void OS_run;
	public void OS_run()
	{
		//判断系统事件调度是否卡住, 其中低四位表示时间累加, 高四位用来报警闪烁计时
		if( WatchDogTick >= 15 ) {
			WatchDogTick + 16;
			//LED_PORT = WatchDogTick.7(bit);
			return;
		}
		WatchDogTick + 1;
		
		//闪烁进程
		if( !FlashOpen ) return;
		swap % 10;
		bit OUT;
		if( swap < 1 ) {
			OUT = 1;
		}
		else {
			OUT = 0;
		}
		swap + 1;
		
		if( FlashForever ) {
			LED_PORT = OUT;
			return;
		}
		if( FlashNumber == 0 ) {
			FlashOpen = false;
			return;
		}
		if( LED_PORT == 1 && OUT == 0 ) {
			FlashNumber - 1;
		}
		LED_PORT = OUT;
	}
	//---------------------------------------------------
	//[i] function_void OS_ClearWatchDog;
	public void OS_ClearWatchDog()
	{
		WatchDogTick = 0;
	}
	//---------------------------------------------------
	//[i] function_void LightOpen;
	public void LightOpen()
	{
		FlashOpen = false;
		LED_PORT = 1;
	}
	//---------------------------------------------------
	//[i] function_void LightClose;
	public void LightClose()
	{
		FlashOpen = false;
		LED_PORT = 0;
	}
	//---------------------------------------------------
	//[i] function_void LightSwap;
	public void LightSwap()
	{
		FlashOpen = false;
		LED_PORT = ~LED_PORT;
	}
	//---------------------------------------------------
	//[i] function_void LightFlashTimes int32;
	public void LightFlashTimes( int32 n )
	{
		#asm "cli"
		FlashNumber = n;
		#asm "sei"
		swap = 0;
		FlashForever = false;
		FlashOpen = true;
	}
	//---------------------------------------------------
	//[i] function_void LightFlash;
	public void LightFlash()
	{
		#asm "cli"
		FlashNumber = 0;
		#asm "sei"
		FlashForever = true;
		FlashOpen = true;
	}
	//---------------------------------------------------
	void delayLED()
	{
		loop( 250 ) loop( 250 ) {}
	}
	
	uint8 swap;
	int32 FlashNumber;
	bool FlashOpen;
	bool FlashForever;
	link bit LED_DDR = RES.DDRB.5;
	link bit LED_PORT = RES.PORTB.5;
}

driver.PORT = PORT;

#include "mio_$run$.txt"










