//This file was generated by the LASAL2 CodeGenerator  -- 
//Please, do not edit this file (it might be overwritten by the next generator run)
//{{LSL_DECLARATION

(*!
<Class
	Name               = "Drive_AsyncParaDS402"
	Revision           = "1.1"
	GUID               = "{7C845974-8991-46F9-B7C8-EBBEF6C56FB9}"
	RealtimeTask       = "false"
	CyclicTask         = "false"
	BackgroundTask     = "false"
	Sigmatek           = "true"
	OSInterface        = "false"
	HighPriority       = "false"
	Automatic          = "false"
	UpdateMode         = "Prescan"
	IconPath           = ".\Class\MDD2000_Mng\MDD2000SafetyDrive.ico"
	SharedCommandTable = "true"
	Objectsize         = "(330,480)"
	Comment            = "Class for asynchron CanOpen parameter handling">
	<Channels>
		<Server Name="ParaID" GUID="{660FD1BC-0759-466F-B4FF-58D4204EFCC4}" Visualized="true" Initialize="true" WriteProtected="false" Retentive="false" Comment="Parameter Index, which should be accessed (2 Byte)."/>
		<Server Name="ParaNr" GUID="{A0294B93-DBDE-4407-B8C7-2E6E01608DEA}" Visualized="true" Initialize="true" DefValue="-1" WriteProtected="false" Retentive="false" Comment="Sigmatek Parameter Number which should be accessed. Values from 0 to 255.&#13;&#10;&#13;&#10;For the Execution of the SDO-Command &#10;ParaID and ParaSubID will be used to identify the SDO, if ParaNr is &lt; 0.&#10; &#13;&#10;If ParaNr is &gt;= 0 this parameter number will be converted to the corrosponding ParaID and ParaSubID, and then used."/>
		<Server Name="ParaRdWr" GUID="{4FA7DAAB-82F1-40C9-88F3-20F87530F83C}" Visualized="true" Initialize="true" WriteProtected="false" Retentive="false" Comment="0 .. read the value of the parameter number&#13;&#10;1 .. write a new value to the parameter number&#13;&#10;&#13;&#10;Calling the Write-Methode starts the Execution of the SDO-Command&#13;&#10;&#13;&#10;For the Execution of the SDO-Command &#10;ParaID and ParaSubID will be used to identify the SDO, if ParaNr is &lt; 0.&#10; &#13;&#10;If ParaNr is &gt;= 0 this parameter number will be converted to the corrosponding ParaID and ParaSubID, and then used."/>
		<Server Name="ParaSize" GUID="{372A1A30-4E45-47B0-AE54-319B393CA3E6}" Visualized="true" Initialize="true" WriteProtected="false" Retentive="false" Comment="Size of the Parameter:&#13;&#10;0...4  Byte&#13;&#10;1...8  Byte&#13;&#10;2...12 Byte&#13;&#10;"/>
		<Server Name="ParaSubID" GUID="{21B6EEF5-168E-4602-9E34-8C6A8FA82B01}" Visualized="true" Initialize="true" WriteProtected="false" Retentive="false" Comment="Parameter SubIndex, which should be accessed (1 Byte)"/>
		<Server Name="ParaValue" GUID="{200FB752-15F0-436D-AD6D-6F1FF5D25F94}" Visualized="true" Initialize="true" WriteProtected="false" Retentive="false" Comment="Read Command = actual value of the parameter&#13;&#10;Write Command = the new value which has to written to the parameter"/>
	</Channels>
	<Separators>
		<Servers>
			<SepChn Position="5"/>
		</Servers>
	</Separators>
	<RevDoku>
		<Owner Company="Sigmatek" Author="LanSte"/>
		<Dokumentation Revision="1.1" Date="13.12.2024" Author="LanSte" Company="Sigmatek" Description="Added Server &quot;ParaNr&quot; to provide compatibility for old sigmatek drive Parameter format."/>
		<Dokumentation Revision="1.0" Date="28.08.2023" Author="LanSte" Company="Sigmatek" Description="First library version."/>
	</RevDoku>
	<Network Name="Drive_AsyncParaDS402">
		<!-- List of Components in this network -->
		<Components>
			<Object
				Name       = "_base"
				GUID       = "{E8FF8369-EDDF-4E6A-8DB7-72A4A25B7C23}"
				Class      = "_DriveSafetyIFParaBase"
				Position   = "(218,120)"
				Visualized = "true"
				Remotely   = "true">
				<Channels>
					<Server Name="ErrorQuit"/>
					<Server Name="State"/>
					<Client Name="DriveIF"/>
					<Client Name="ErrorCode"/>
					<Client Name="UpdateCyTime" Value="0"/>
				</Channels>
			</Object>
		</Components>
		<Comments>
		</Comments>
		<!-- List of Connections in this network -->
		<Connections>
			<Connection Source="this.State" Destination="_base.State" Vertices="(740,210),(568,210),"/>
			<Connection Source="this.ErrorQuit" Destination="_base.ErrorQuit" Vertices="(740,270),(568,270),"/>
			<Connection Source="_base.DriveIF" Destination="this.DriveIF" Vertices="(218,210),(38,210),"/>
			<Connection Source="_base.ErrorCode" Destination="this.ErrorCode" Vertices="(218,270),(38,270),"/>
		</Connections>
		<!-- Headerfiles -->
		<Options>
		</Options>
	</Network>
</Class>
*)
#pragma using _DriveSafetyIFParaBase

Drive_AsyncParaDS402 : CLASS
: _DriveSafetyIFParaBase
  //Servers:
	ParaID 	: SvrCh_DINT;
	ParaSubID 	: SvrCh_DINT;
	ParaRdWr 	: SvrCh_DINT;
	ParaSize 	: SvrCh_DINT;
	ParaValue 	: SvrCh_DINT;
	ParaNr 	: SvrCh_DINT;
  //Clients:
  //Variables:
		aWriteBuffer : ARRAY [0..11] OF USINT;

		aReadBuffer : ARRAY [0..11] OF USINT;

		bExternValue 	: BOOL;
  //Functions:
	
	FUNCTION VIRTUAL GLOBAL Init;
	
	FUNCTION VIRTUAL GLOBAL RefreshPara
		VAR_INPUT
			hParaIndex 	: HINT;
			hsParaSubIndex 	: HSINT;
			bRW 	: DINT;
			bsDataInfo 	: BSINT;
			pValue 	: ^DINT;
		END_VAR;
				//! <Function Comment="with this methode a value can be get." Name="GetValue"/>
	FUNCTION GLOBAL GetValue
		VAR_INPUT
			pValue 	: ^DINT;			//! <Variable Comment="Pointer to which the read data will be copied." Name="GetValue.pValue"/>
		END_VAR
		VAR_OUTPUT
			result 	: DINT;			//! <Variable Comment="0 .. Parameter data was copied to passed poiner&#13;&#10;&#10;-1 .. Parameter data could not be copied&#13;&#10;-x .. An error occured during Parameter access. See Client ErrorCode for details&#13;&#10;&gt;0 State: &#10;     &#13;&#10;                  BUSY ... Last transmission not yet completed&#10;     &#13;&#10;                  ERROR ... An error occurred during the last transmission&#10; occurred which must be acknowledged first." Name="GetValue.result"/>
		END_VAR;
				//! <Function Comment="with this methode a value can be set.&#13;&#10;" Name="SetValue"/>
	FUNCTION GLOBAL SetValue
		VAR_INPUT
			pValue 	: ^DINT;			//! <Variable Comment="Pointer to the data to be written&#13;&#10;(Not used when data is read)" Name="SetValue.pValue"/>
		END_VAR
		VAR_OUTPUT
			result 	: DINT;			//! <Variable Comment="0 .. Parameter Transmission was started.&#13;&#10;&#10;-1 .. Parameter transmission could not be started.&#10;&#13;&#10;16#80000010 ACCESS_DENIED Drive is not ready for parameter transmission.&#13;&#10;&gt;0 State: &#10;     &#13;&#10;                  BUSY ... Last transmission not yet completed&#10;     &#13;&#10;                  ERROR ... An error occurred during the last transmission&#10; occurred which must be acknowledged first." Name="SetValue.result"/>
		END_VAR;
	
	FUNCTION ConvertOldParaFormatToDS402Format
		VAR_INPUT
			ParaNo 	: DINT;
			phParaIndex 	: ^DINT;
			phsParaSubIndex 	: ^DINT;
		END_VAR;
	
	FUNCTION VIRTUAL GLOBAL ParaRdWr::Write
		VAR_INPUT
			input (EAX) 	: DINT;
		END_VAR
		VAR_OUTPUT
			result (EAX) 	: DINT;
		END_VAR;
  //Tables:
	FUNCTION @STD
		VAR_OUTPUT
			ret_code	: CONFSTATES;
		END_VAR;
	FUNCTION GLOBAL TAB @CT_;
END_CLASS;

//}}LSL_DECLARATION


FUNCTION GLOBAL TAB Drive_AsyncParaDS402::@CT_
0$UINT,
2#0100000000000000$UINT, //TY_DRIVE_ASYNCPARADS402
1$UINT, 1$UINT, (SIZEOF(::Drive_AsyncParaDS402))$UINT, 
6$UINT, 0$UINT, 0$UINT, 
TO_UDINT(3760282393), "Drive_AsyncParaDS402", //Class
TO_UDINT(1914213293), "_DriveSafetyIFParaBase", 1$UINT, 0$UINT, //Baseclass
//Servers:
(::Drive_AsyncParaDS402.ParaID.pMeth)$UINT, _CH_SVR$UINT, 2#0000000000000000$UINT, TO_UDINT(2559150518), "ParaID", 
(::Drive_AsyncParaDS402.ParaSubID.pMeth)$UINT, _CH_SVR$UINT, 2#0000000000000000$UINT, TO_UDINT(2706822172), "ParaSubID", 
(::Drive_AsyncParaDS402.ParaRdWr.pMeth)$UINT, _CH_SVR$UINT, 2#0000000000000000$UINT, TO_UDINT(2426770201), "ParaRdWr", 
(::Drive_AsyncParaDS402.ParaSize.pMeth)$UINT, _CH_SVR$UINT, 2#0000000000000000$UINT, TO_UDINT(381459621), "ParaSize", 
(::Drive_AsyncParaDS402.ParaValue.pMeth)$UINT, _CH_SVR$UINT, 2#0000000000000000$UINT, TO_UDINT(757324645), "ParaValue", 
(::Drive_AsyncParaDS402.ParaNr.pMeth)$UINT, _CH_SVR$UINT, 2#0000000000000000$UINT, TO_UDINT(589081120), "ParaNr", 
//Clients:
END_FUNCTION


#define USER_CNT_Drive_AsyncParaDS402 19

TYPE
	_LSL_STD_VMETH	: STRUCT
			CmdTable	: CMDMETH;
			UserFcts	: ARRAY[0..USER_CNT_Drive_AsyncParaDS402] OF ^Void;
	END_STRUCT;
END_TYPE

FUNCTION Drive_AsyncParaDS402::@STD
	VAR_OUTPUT
		ret_code	: CONFSTATES;
	END_VAR
	VAR
		vmt	: _LSL_STD_VMETH;
		nCmdSize	: UINT;
	END_VAR

	ret_code	:= _DriveSafetyIFParaBase::@STD();
	IF ret_code <> C_OK THEN
		RETURN;
	END_IF;
	nCmdSize		:= _DriveSafetyIFParaBase::State.pMeth^.nCmds$UINT*SIZEOF(pVoid) + CMDMETH.Init;

	_memcpy((#vmt.CmdTable)$^USINT, _DriveSafetyIFParaBase::State.pMeth, nCmdSize);
	vmt.CmdTable.nCmds		:= nSTDCMD + USER_CNT_Drive_AsyncParaDS402;
	vmt.CmdTable.Init		:= #Init();
#pragma warning (disable : 74)
	vmt.UserFcts[2]		:= #RefreshPara();

#pragma warning (default : 74)
	_DriveSafetyIFParaBase::State.pMeth		:= StoreCmd (pCmd := #vmt.CmdTable, SHARED);

	IF _DriveSafetyIFParaBase::State.pMeth THEN
		ret_code	:= C_OK;
	ELSE
		ret_code	:= C_OUTOF_NEAR;
		RETURN;
	END_IF;
	ParaID.pMeth			:= StoreMethod( #M_RD_DIRECT(), #M_WR_DIRECT() );
	IF ParaID.pMeth THEN
		ret_code	:= C_OK;
	ELSE
		ret_code	:= C_OUTOF_NEAR;
		RETURN;
	END_IF;
	ParaSubID.pMeth			:= StoreMethod( #M_RD_DIRECT(), #M_WR_DIRECT() );
	IF ParaSubID.pMeth THEN
		ret_code	:= C_OK;
	ELSE
		ret_code	:= C_OUTOF_NEAR;
		RETURN;
	END_IF;
	ParaRdWr.pMeth			:= StoreMethod( #M_RD_DIRECT(), #ParaRdWr::Write() );
	IF ParaRdWr.pMeth THEN
		ret_code	:= C_OK;
	ELSE
		ret_code	:= C_OUTOF_NEAR;
		RETURN;
	END_IF;
	ParaSize.pMeth			:= StoreMethod( #M_RD_DIRECT(), #M_WR_DIRECT() );
	IF ParaSize.pMeth THEN
		ret_code	:= C_OK;
	ELSE
		ret_code	:= C_OUTOF_NEAR;
		RETURN;
	END_IF;
	ParaValue.pMeth			:= StoreMethod( #M_RD_DIRECT(), #M_WR_DIRECT() );
	IF ParaValue.pMeth THEN
		ret_code	:= C_OK;
	ELSE
		ret_code	:= C_OUTOF_NEAR;
		RETURN;
	END_IF;
	ParaNr.pMeth			:= StoreMethod( #M_RD_DIRECT(), #M_WR_DIRECT() );
	IF ParaNr.pMeth THEN
		ret_code	:= C_OK;
	ELSE
		ret_code	:= C_OUTOF_NEAR;
		RETURN;
	END_IF;

END_FUNCTION

//{{LSL_IMPLEMENTATION
FUNCTION VIRTUAL GLOBAL Drive_AsyncParaDS402::Init

  _DriveSafetyIFParaBase::Init();
  
END_FUNCTION


FUNCTION VIRTUAL GLOBAL Drive_AsyncParaDS402::ParaRdWr::Write
	VAR_INPUT
		input (EAX) 	: DINT;
	END_VAR
	VAR_OUTPUT
		result (EAX) 	: DINT;
	END_VAR  
  
  if input then
    ParaRdWr := 1;
  else
    ParaRdWr := 0;
  end_if;
  
  result := ACCESS_DENIED;
  
  if State = READY then
  
    if ParaNr >= 0 then 
      
      if (ParaNr$HDINT > 0xFF) then
        State     := ERROR;
        ErrorCode := 7; // Object not available, ParaNr Out of range
        return;
      end_if;
      
      ConvertOldParaFormatToDS402Format(ParaNo:=ParaNr, phParaIndex:=#ParaID.dData, phsParaSubIndex:=#ParaSubID.dData);
    
    end_if;
    
    if (ParaID$HDINT > 0xFFFF) | (ParaSubID$HDINT > 0xFF)  then
      State     := ERROR;
      ErrorCode := 7; // Object not available, ParaID, ParaSubID Out of range
      return;
    end_if;
  
    if bExternValue <> TRUE then
      aWriteBuffer[0]$DINT  := ParaValue;
    end_if;  
    _memset(dest:=#aReadBuffer[0], usByte:=0, cntr:=sizeof(aReadBuffer));
    
    result := AddAsyncEntryDS402( ParaID      :=  ParaID$HINT
                                , ParaSubID   :=  ParaSubID$HSINT
                                , ParaRdWr    :=  ParaRdWr$USINT
                                , ParaSize    :=  ParaSize
                                , pParaValue  :=  #aWriteBuffer[0]
                                , pThis       :=  this
                                , usEntry     :=  0
                                , udTimeout   :=  0
                                , dCallID     :=  0);
      
    bExternValue := FALSE;

  end_if;

END_FUNCTION


FUNCTION GLOBAL Drive_AsyncParaDS402::GetValue
	VAR_INPUT
		pValue 	: ^DINT;
	END_VAR
	VAR_OUTPUT
		result 	: DINT;
	END_VAR
  
  result := 0; 
  if (State = Ready) then 
    if (ParaRdWr = 0) & (pValue <> NIL) then
      _memcpy(ptr1:=pValue, ptr2:=#aReadBuffer[0], cntr:=4*(ParaSize$USINT+1));
    else
      result$DINT := -1;
    end_if;
  elsif ErrorCode <> 0 then
    result$DINT := ErrorCode;
  else
    result := State$DINT;
  end_if;

END_FUNCTION


FUNCTION GLOBAL Drive_AsyncParaDS402::SetValue
	VAR_INPUT
		pValue 	: ^DINT;
	END_VAR
	VAR_OUTPUT
		result 	: DINT;
	END_VAR

  if State = Ready then
    _memcpy(ptr1:=#aWriteBuffer[0], ptr2:=pValue, cntr:=4*(1+ParaSize$USINT));
    bExternValue := TRUE;
    result := ParaRdWr.Write(input:=ParaRdWr);
  else
    result := State$DINT;
  end_if;
  
END_FUNCTION


FUNCTION VIRTUAL GLOBAL Drive_AsyncParaDS402::RefreshPara
	VAR_INPUT
		hParaIndex 	: HINT;
		hsParaSubIndex 	: HSINT;
		bRW 	: DINT;
		bsDataInfo 	: BSINT;
		pValue 	: ^DINT;
	END_VAR
  
  if hParaIndex = ParaID & hsParaSubIndex = ParaSubID then
    ParaValue := pValue^;
    _memcpy(ptr1:=#aReadBuffer[0], ptr2:=pValue, cntr:=4*(ParaSize$USINT+1));
  end_if;

END_FUNCTION

FUNCTION Drive_AsyncParaDS402::ConvertOldParaFormatToDS402Format
	VAR_INPUT
		ParaNo 	: DINT;
		phParaIndex 	: ^DINT;
		phsParaSubIndex 	: ^DINT;
	END_VAR
  
  if ParaNo >=DRIVESAFETYIF_PID_SIGMATEKPARA_INDEX_SET_MAX_NR  then
    phParaIndex^      := DRIVESAFETYIF_PID_SIGMATEKPARA_INDEX_INFO;
    phsParaSubIndex^  := (ParaNo - (DRIVESAFETYIF_PID_SIGMATEKPARA_INDEX_SET_MAX_NR - 1));
  else
    phParaIndex^      := DRIVESAFETYIF_PID_SIGMATEKPARA_INDEX_SET;
    phsParaSubIndex^  := ParaNo;
  end_if;

END_FUNCTION