
//MEGA单片机组件
unit MEGA
{
	public const uint16 ID = 0;
	
	#.COM_MCU = RES;
	public RES =
	#include <system/cpu/avr/register_$chip$.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 M1power;
	int8 M2power;
	int8 M3power;
	int8 M4power;
	int8 tick;
	
	
	//[i] linkconst_int32_1 TT;
	public const int32 TT = 1;
	
	int8 T;
	int8 tt;
	
	//---------------------------------------------------
	//[i] function_void OS_init;
	public void OS_init()
	{
		FlashOpen = false;
		FlashForever = false;
		FlashNumber = 0;
		swap = 0;
		WatchDogTick = 0;
		OS_time = 50;
		OS_EventFlag = 0;
		OS_EventFlag.0(bit) = 1;
		
		//开机用于等待外设准备好(带有阻容复位的外设)
		loop( 10 ) {
			delay();
		}
		LED_DDR = 1;
		LED_PORT = 0;
		
		M1power = 100;
		M2power = 100;
		M3power = 100;
		M4power = 100;
		
		tick = 0;
		
		T = (int8)(int16)TT;
		tt = 0;
		
		H595_init();
	}
	//---------------------------------------------------
	//[i] function_void set_t int32;
	public void set_t( int32 t )
	{
		T = (int8)(int16)t;
		tt = 0;
	}
	//---------------------------------------------------
	//[i] function_void OS_thread;
	public void OS_thread()
	{
		OS_EventFlag.1(bit) = 1;
	}
	//---------------------------------------------------
	//[i] function_void OS_run100us;
	public void OS_run100us()
	{
		tt + 1;
		if( tt < T ) {
			return;
		}
		tt = 0;
		
		#include "RUN_$run$.txt"
	}
	//---------------------------------------------------
	//[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 delay()
	{
		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;
	
	
	#include "PWM_$run$.txt"
	
	
	public link unit D {}
	public link unit L {}
	public link unit C {}
	public link unit E {}
	
	//端口缓冲区数据
	uint8 back_data;
	
	//---------------------------------------------
	memory my_bit { type = [ uint8 0, 255 ] uint8; }
	
	public void H595_init()
	{
		E.D0_DIR = 1;
		E.D0_OUT = 0;
		
		D.D0_DIR = 1;
		D.D0_OUT = 0;
		
		L.D0_DIR = 1;
		L.D0_OUT = 0;
		
		C.D0_DIR = 1;
		C.D0_OUT = 0;
		
		PWM1_DDR = 1;
		PWM2_DDR = 1;
		PWM3_DDR = 1;
		PWM4_DDR = 1;
	}
	
	void send_byte( uint8 data )
	{
		loop( 8 ) {
			D.D0_OUT = data.7(bit);
			data << 1;
			C.D0_OUT = 1;
			C.D0_OUT = 0;
		}
		L.D0_OUT = 1;
		L.D0_OUT = 0;
	}
	
	//---------------------------------------------
	void set_uint8( uint8 addr, uint8 b )
	{
		if( b != 0 ) {
			back_data | ( 0b0000_0001 << addr );
		}
		else {
			back_data & ~( 0b0000_0001 << addr );
		}
		send_byte( back_data );
	}
	
	//---------------------------------------------
	uint8 get_uint8( uint8 addr )
	{
		if( back_data & ( 0b0000_0001 << addr ) != 0 ) {
			return 1;
		}
		else {
			return 0;
		}
	}
}

#include "mio_$run$.txt"









