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

unit button
{
	public const uint16 ID = 0;

	public link unit OUT1 {}
	public link unit OUT2 {}
	public link unit OUT3 {}
	public link unit OUT4 {}
	
	#include "$run$.txt"

	//------------------------------------------------------------
	//[i] var_uint8 OS_EventFlag;
	public uint16 OS_EventFlag;
	
	//[i] event key0_press_event;
	//[i] event key0_up_event;
	
	//[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] var_bool key0_press;
	public bool key0_press;
	//---------------------
	//[i] var_bool key1_press;
	public bool key1_press;
	//---------------------
	//[i] var_bool key2_press;
	public bool key2_press;
	//---------------------
	//[i] var_bool key3_press;
	public bool key3_press;

	//---------------------
	//[i] var_bool OS_time;
	public uint8 OS_time;

	bit last_key0_press;
	
	bit last_key1_press;
	
	bit last_key2_press;
	
	bit last_key3_press;
	
	//------------------------------------------------------------
	//[i] function_void OS_init;
	public void OS_init()
	{
		OS_time = 50;
		OS_EventFlag = 0;
		
		KEY0_DIR = 0;
		KEY0_OUT = 1;
		KEY1_DIR = 0;
		KEY1_OUT = 1;
		KEY2_DIR = 0;
		KEY2_OUT = 1;
		KEY3_DIR = 0;
		KEY3_OUT = 1;
		
		last_key0_press = 1;
		last_key1_press = 1;
		last_key2_press = 1;
		last_key3_press = 1;

		key0_press = false;
		key1_press = false;
		key2_press = false;
		key3_press = false;
	}
	//------------------------------------------------------------
	//[i] function_void OS_run;
	public void OS_run()
	{
		//按键0处理
		//一定要先读取到一个临时变量中!!!!
		//2015.9.14 22:41
		bit KEY0 = KEY0_IN;
		
		bit key0_down = ~KEY0 & last_key0_press;
		bit key0_up = KEY0 & ~last_key0_press;
		if( key0_down == 1 ) OS_EventFlag.0(bit) = 1;
		if( key0_up == 1 ) OS_EventFlag.1(bit) = 1;
		last_key0_press = KEY0;
		
		if( KEY0 == 0 ) {
			key0_press = true;
		} else {
			key0_press = false;
		}
		//按键1处理
		//一定要先读取到一个临时变量中!!!!
		//2015.9.14 22:41
		bit KEY1 = KEY1_IN;
		
		bit key1_down = ~KEY1 & last_key1_press;
		bit key1_up = KEY1 & ~last_key1_press;
		if( key1_down == 1 ) OS_EventFlag.2(bit) = 1;
		if( key1_up == 1 ) OS_EventFlag.3(bit) = 1;
		last_key1_press = KEY1;
		
		if( KEY1 == 0 ) {
			key1_press = true;
		} else {
			key1_press = false;
		}
		//按键2处理
		//一定要先读取到一个临时变量中!!!!
		//2015.9.14 22:41
		bit KEY2 = KEY2_IN;
		
		bit key2_down = ~KEY2 & last_key2_press;
		bit key2_up = KEY2 & ~last_key2_press;
		if( key2_down == 1 ) OS_EventFlag.4(bit) = 1;
		if( key2_up == 1 ) OS_EventFlag.5(bit) = 1;
		last_key2_press = KEY2;
		
		if( KEY2 == 0 ) {
			key2_press = true;
		} else {
			key2_press = false;
		}
		//按键3处理
		//一定要先读取到一个临时变量中!!!!
		//2015.9.14 22:41
		bit KEY3 = KEY3_IN;
		
		bit key3_down = ~KEY3 & last_key3_press;
		bit key3_up = KEY3 & ~last_key3_press;
		if( key3_down == 1 ) OS_EventFlag.6(bit) = 1;
		if( key3_up == 1 ) OS_EventFlag.7(bit) = 1;
		last_key3_press = KEY3;
		
		if( KEY3 == 0 ) {
			key3_press = true;
		} else {
			key3_press = false;
		}
	}
}





