
//AVR系列单片机的按键驱动程序

unit button
{
	public const uint16 ID = 0;

	public link unit OUT1 {}
	public link unit OUT2 {}
	public link unit OUT3 {}
	public link unit OUT4 {}
	
	public link unit IN1 {}
	public link unit IN2 {}
	public link unit IN3 {}
	public link unit IN4 {}
	
	//------------------------------------------------------------
	//[i] var_uint16 OS_time;
	public uint8 OS_time;
	
	//------------------------------------------------------------
	//[i] var_uint16 OS_EventFlag;
	public uint32 OS_EventFlag;
	
	//------------------------------------------------------------
	//[i] var_uint16 OS_EventFlag1;
	public uint8 OS_EventFlag1;

	//[i] event key1_press_event;
	//[i] event key1_up_event;
	//[i] event key2_press_event;
	//[i] event key2_up_event;
	//[i] event key3_press_event;
	//[i] event key3_up_event;
	//[i] event key4_press_event;
	//[i] event key4_up_event;
	//[i] event key5_press_event;
	//[i] event key5_up_event;
	//[i] event key6_press_event;
	//[i] event key6_up_event;
	//[i] event key7_press_event;
	//[i] event key7_up_event;
	//[i] event key8_press_event;
	//[i] event key8_up_event;
	//[i] event key9_press_event;
	//[i] event key9_up_event;
	//[i] event key10_press_event;
	//[i] event key10_up_event;
	//[i] event key11_press_event;
	//[i] event key11_up_event;
	//[i] event key12_press_event;
	//[i] event key12_up_event;
	//[i] event key13_press_event;
	//[i] event key13_up_event;
	//[i] event key14_press_event;
	//[i] event key14_up_event;
	//[i] event key15_press_event;
	//[i] event key15_up_event;
	//[i] event key16_press_event;
	//[i] event key16_up_event;
	
	//[i] event keyAny_press_event;
	//[i] event keyAny_up_event;

	//------------------------------------------------------------
	//[i] var_int32 Value;
	public int32 Value;

	uint16 KeyList;
	uint16 LastKeyList;
	
	[bool*16] KeyPress;
	
	//------------------------------------------------------------
	//[i] function_void OS_init;
	public void OS_init()
	{
		KeyList = 0xFFFF;
		LastKeyList = KeyList;
		
		OS_time = 50;
		OS_EventFlag = 0;
		
		OUT1.D0_DIR = 0;
		OUT1.D0_OUT = 1;
		OUT2.D0_DIR = 0;
		OUT2.D0_OUT = 1;
		OUT3.D0_DIR = 0;
		OUT3.D0_OUT = 1;
		OUT4.D0_DIR = 0;
		OUT4.D0_OUT = 1;
		
		IN1.D0_DIR = 1;
		IN1.D0_OUT = 1;
		IN2.D0_DIR = 1;
		IN2.D0_OUT = 1;
		IN3.D0_DIR = 1;
		IN3.D0_OUT = 1;
		IN4.D0_DIR = 1;
		IN4.D0_OUT = 1;
		
		uint8 i = 0;
		loop( 16 ) {
			KeyPress[i] = false;
			i + 1;
		}
	}
	//------------------------------------------------------------
	//[i] function_void OS_run;
	public void OS_run()
	{
		#include "$run$.txt"
		
		if( KeyList != LastKeyList ) {
			
			int8 i = 0;
			uint32 EventSet = 0x0001;
			uint16 ck = KeyList;
			uint16 lk = LastKeyList;
			loop( 16 ) {
				if( ck.0(bit) == 0 && lk.0(bit) == 1 ) {
					OS_EventFlag | EventSet;
					KeyPress[i] = true;
				}
				EventSet << 1;
				if( ck.0(bit) == 1 && lk.0(bit) == 0 ) {
					OS_EventFlag | EventSet;
					KeyPress[i] = false;
				}
				EventSet << 1;
				
				ck >> 1;
				lk >> 1;
			}
		}
		LastKeyList = KeyList;
	}
	//------------------------------------------------------------
	//[i] function_bool is_press int32;
	public bool is_press( int32 i )
	{
		return KeyPress[i];
	}
}





