
unit Light
{
	public const uint16 ID = 0;

	//[i] linkinterface_motor motor;
	link unit motor {}
	
	//[i] interface_analog analog;
	unit analog
	{
		public link void SetValue( int32 v ) {}
		public link void Stop() {}
	}
	analog.SetValue = set_value;
	analog.Stop = Stop;
	
	//---------------------------------------------------
	//[i] function_void set_value int32;
	void set_value( int32 v )
	{
		set_uint32( (uint)v );
	}
	void Stop()
	{
		set_value( 0 );
	}
	
	//[i] var_int32_w speed;
	public myVT int32 speed;
	memory myVT {}

	void set_uint32( uint32 vv )
	{
		int32 v = (int)vv;
		if( v < -100 ) {
			value = -100;
		}
		else if( v > 100 ) {
			value = 100;
		}
		else {
			value = (int8)(int16)v;
		}
		if( value < 0 ) {
			value = -value;
			Dir = -1;
		}
		else {
			Dir = 1;
		}
	}
	uint32 get_uint32()
	{
		int32 v = value;
		if( Dir == -1 ) {
			return (uint)-v;
		}
		else {
			return (uint)v;
		}
	}
	
	
	int8 value;
	int8 tick;
	int8 Dir;
	bool Open;
	bool Changed;
	
	//---------------------------------------------------
	//[i] function_void OS_init;
	public void OS_init()
	{
		value = 0;
		tick = 0;
		Dir = 0;
		Open = false;
		Changed = false;
	}
	//---------------------------------------------------
	//[i] function_void OS_run100us;
	public void OS_run100us()
	{
		Changed = false;
		
		tick + 1;
		if( tick == 100 ) {
			tick = 0;
			Open = true;
			Changed = true;
		}
		if( tick == value ) {
			Open = false;
			Changed = true;
		}
		if( !Changed ) {
			return;
		}
		if( Open ) {
			if( Dir == 1 ) {
				motor.Left();
			}
			else if( Dir == -1 ) {
				motor.Right();
			}
		}
		else {
			motor.Stop();
		}
	}
}



