
unit delayer
{
	public const uint16 ID = 0;
	
	public link unit io {}
	
	//---------------------
	//[i] var_uint8 OS_time;
	public uint8 OS_time;
	
	link unit OS {} = #.OS;
	const int16 T_NUMBER = OS.TASK_NUMBER;
	[int32*T_NUMBER] time_list;
	[bool*T_NUMBER] enable_list;
	
	//------------------------------------------
	//[i] function_void OS_init;
	public void OS_init()
	{
		OS_time = 1;
		EnableSchedule = true;
		Stop();
	}
	//------------------------------------------
	//[i] function_void OS_run;
	public void OS_run()
	{
		for( int16 i = 0; i < T_NUMBER; i + 1 ) {
			if( enable_list[i] ) {
				time_list[i] - 1;
				if( time_list[i] == 0 ) enable_list[i] = false;
				
				#include "$run$.txt"
			}
		}
	}
	//---------------------------------------------------
	//[i] function_void DelayMillisecond int32;
	public void DelayMillisecond( int32 t )
	{
		int32 tt = t / $tick$;
		if( tt != 0 ) {
			wait( tt);
		}
	}
	//---------------------------------------------------
	//[i] function_void DelaySecond float;
	public void DelaySecond( int32 t )
	{
		uint32 ftt = (uint)t;
		if( ftt.30(bit) == 1 ) {
			ftt.30(bit) = 0;
			t = (int)ftt;
		}
		else {
			t * 1000;
		}
		DelayMillisecond( t );
	}
	//---------------------------------------------------
	//[i] function_void DelayMinute int32;
	public void DelayMinute( int32 t )
	{
		DelayMillisecond( t * 60000 );
	}
	//---------------------------------------------------
	//[i] function_void DelayHour int32;
	public void DelayHour( int32 t )
	{
		DelayMillisecond( t * 3600000 );
	}
	//---------------------------------------------------
	//[i] function_void DelayDay int32;
	public void DelayDay( int32 t )
	{
		DelayMillisecond( t * 86400000 );
	}
	//---------------------------------------------------
	//[i] function_void Stop;
	public void Stop()
	{
		for( int16 i = 0; i < T_NUMBER; i + 1 ) {
			time_list[i] = 0;
			enable_list[i] = false;
		}
	}
	//---------------------------------------------------
	void wait( int32 t )
	{
		time_list[OS.CurrentTaskIndex] = t;
		enable_list[OS.CurrentTaskIndex] = true;
		while( enable_list[OS.CurrentTaskIndex] ) {
			if( EnableSchedule ) OS.Schedule();
		}
	}
	//---------------------
	//[i] var_bool_w EnableSchedule;
	bool EnableSchedule;
}












