#SYMBOL_NAME "Kincony_Ctrl"
// #HINT ""
#DEFINE_CONSTANT AiMax 64
#DEFINE_CONSTANT AoMax 32
#DEFINE_CONSTANT DiMax 128
#DEFINE_CONSTANT DoMax 128
// #CATEGORY "" 
#CATEGORY "29" // Relay Control
// #PRINT_TO_TRACE
// #DIGITAL_EXPAND 
// #ANALOG_SERIAL_EXPAND 
// #OUTPUT_SHIFT 
// #HELP_PDF_FILE ""
#DEFAULT_VOLATILE
#ENABLE_STACK_CHECKING
#ENABLE_TRACE
// #ENCODING_ASCII
// #ENCODING_UTF16
// #ENCODING_INHERIT_FROM_PARENT
// #ENCODING_INHERIT_FROM_PROGRAM
// #INCLUDEPATH ""

/*
#HELP_BEGIN
   (add additional lines of help lines)
#HELP_END
*/

#USER_SIMPLSHARP_LIBRARY "KinconyHelper"

DIGITAL_INPUT _skip_,_skip_,Do_All_On,Do_All_Off;
DIGITAL_INPUT _skip_,IR_Learn,IR_Send,IR_Delete;
DIGITAL_INPUT _skip_,Do_On[DoMax],Do_Off[DoMax],Do_Toggle[DoMax,DoMax];
ANALOG_INPUT _skip_,IR_ID,IR_Tube;
ANALOG_INPUT _skip_,Ao_Value[AoMax,AoMax];

DIGITAL_OUTPUT _skip_,_skip_,Connected,_skip_;
DIGITAL_OUTPUT _skip_,IR_Learn_OK,IR_Learn_Timeout,IR_Learn_Error;
DIGITAL_OUTPUT _skip_,Di_FB[DiMax],Do_FB[DoMax,DoMax];
ANALOG_OUTPUT _skip_,IR_Mark_FB;
ANALOG_OUTPUT _skip_,Ai_FB[AiMax],Ao_FB[AoMax,AoMax]; 

MainProg kincony;

INTEGER_PARAMETER Port;
STRING_PARAMETER IP[20];

// INTEGER
// LONG_INTEGER
// SIGNED_INTEGER
// SIGNED_LONG_INTEGER
// STRING

Callback Function ConnectionCallFn(integer connection)
{
    Connected = connection;
    if (connection)
    {
    	kincony.ReadAllDo();
    	delay ( 200 );
    	kincony.ReadAllDi();
    }
}

Callback Function DiAllCallFn(UshortArrayArg ret)
{
	integer length,i,j;
	length = ret.Count;
	if ((length*8) > DiMax)
		length = DiMax/8;
	for ( i = 0 to length-1 )
	{
		for ( j = 0 to 7 )
		{
			Di_FB[(i*8)+j+1] = ret.Data[i] & (1<<j);
		}
	}
}

Callback Function DoAllCallFn(UshortArrayArg ret)
{
	integer length,i,j;
	length = ret.Count;
	if ((length*8) > DoMax)
		length = DoMax/8;
	for ( i = 0 to length-1 )
	{
		for ( j = 0 to 7 )
		{
			Do_FB[(i*8)+j+1] = ret.Data[i] & (1<<j);
		}
	}
}

Callback Function AiAllCallFn(UshortArrayArg ret)
{
	integer length,i;
	length = ret.Count;
	if ( length > AiMax )
		length = AiMax;
	for ( i = 1 to length )
		Ai_FB[i] = ret.Data[i-1];
}

Callback Function SingleLoopCallFn(SingleLoopArg data)
{
	integer loop,value;
	string type[10];
	type = data.PacketType;
	loop = data.Loop;
	value = data.Value;
	if ( type = "DI" )
	{
		if ( loop <= DiMax )
			Di_FB[loop] = value;
	}
	else if ( type = "DO" )
	{
		if ( loop <= DoMax )
			Do_FB[loop] = value;
	}
	else if ( type = "AO" )
	{
		if ( loop <= DoMax )
			Ao_FB[loop] = value;
	}
	else if ( type = "IRLEARN" )
	{
		IR_Mark_FB = loop;
		switch ( value )
		{
			case (0):
				pulse (100 , IR_Learn_OK);
			case (1):
				pulse (100 , IR_Learn_Timeout);
			case (2):
				pulse (100 , IR_Learn_Error);
		}
	}
}

PUSH Do_All_On
{
	kincony.SetDoAll(1);
}

PUSH Do_All_Off
{
	kincony.SetDoAll(0);
}

PUSH Do_On
{
	integer mark;
	mark = getlastmodifiedarrayindex();
	kincony.SetDo(mark,1);
}

PUSH Do_Off
{
	integer mark;
	mark = getlastmodifiedarrayindex();
	kincony.SetDo(mark,0);
}

PUSH Do_Toggle
{
	integer mark;
	mark = getlastmodifiedarrayindex();
	kincony.SetDoToggle(mark);
	Do_FB[mark] = !Do_FB[mark];
}

CHANGE Ao_Value
{
	integer mark;
	mark = getlastmodifiedarrayindex();
	kincony.SetAo(mark,Ao_Value[mark]);
}

PUSH IR_Learn
{
	kincony.IrLearn(IR_ID,IR_Tube);
}

PUSH IR_Send
{
	kincony.IrSend(IR_ID);
}

PUSH IR_Delete
{
	kincony.IrDelete(IR_ID);
}

Function Main()
{
	if ( WaitForInitializationComplete() < 0 )
	{
		return;
	}
	
	RegisterDelegate ( kincony , ConnectionCallBack , ConnectionCallFn );
	RegisterDelegate ( kincony , DiAllCallBack , DiAllCallFn );
	RegisterDelegate ( kincony , DoAllCallBack , DoAllCallFn );
	RegisterDelegate ( kincony , AiAllCallBack , AiAllCallFn );
	RegisterDelegate ( kincony , SingleLoopCallBack , SingleLoopCallFn );	

    kincony.Initialize(IP,Port);
}
