FUNCTION_BLOCK "FB_ButtonAck"
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
   VAR_INPUT 
      tInPeriodicTime : Time := T#500ms;
      sInDeviceName : String;
      xInButtonNIU : Bool;
      xInAckReqLocal : Bool;
      xInEstopOkLocal : Bool;
      xInAckReqGlobal : Bool;
      xInSafetyNotOkGlobal : Bool;
      xInGlobFunctionActive : Bool;
   END_VAR

   VAR_OUTPUT 
      xOutLed : Bool;
      xOutSafetyNotOk : Bool;
      xOutAckReq : Bool;
   END_VAR

   VAR_IN_OUT 
      stInOutControlUnitCabinet : "UDT_MainState";
   END_VAR

   VAR 
      FB_Blinker { S7_SetPoint := 'False'} : "FB_Blinker";
      fbMonitoringEStop : "FB_Monitoring";
   END_VAR


BEGIN
	REGION Blinker 
	    #FB_Blinker(tInPeriodicTime := #tInPeriodicTime,
	                xInActivate:=TRUE);
	END_REGION
	
	REGION Logic
	    
	    IF NOT #xInGlobFunctionActive THEN
	        
	        IF NOT #xInEstopOkLocal AND #xInAckReqLocal THEN
	            #xOutLed := #FB_Blinker.xOutput;
	            #xOutSafetyNotOk := FALSE;
	            #xOutAckReq := TRUE;
	        ELSIF NOT #xInEstopOkLocal AND NOT #xInAckReqLocal THEN
	            #xOutLed := TRUE;
	            #xOutSafetyNotOk := TRUE;
	            #xOutAckReq := FALSE;
	        ELSIF #xInEstopOkLocal AND NOT #xInAckReqLocal THEN
	            #xOutLed := FALSE;
	            #xOutSafetyNotOk := FALSE;
	            #xOutAckReq := FALSE;
	        ELSE
	            #xOutLed := TRUE;
	            #xOutSafetyNotOk := TRUE;
	            #xOutAckReq := FALSE;
	        END_IF;
	        
	    ELSE
	        
	        IF ((NOT #xInEstopOkLocal AND #xInAckReqLocal AND NOT #xInAckReqGlobal) OR
	            (#xInEstopOkLocal AND NOT #xInAckReqLocal AND #xInAckReqGlobal) OR
	            (NOT #xInEstopOkLocal AND #xInAckReqLocal AND #xInAckReqGlobal))
	            AND NOT #xInSafetyNotOkGlobal THEN
	            #xOutLed := #FB_Blinker.xOutput;
	        ELSIF (NOT #xInEstopOkLocal AND NOT #xInAckReqLocal) OR #xInSafetyNotOkGlobal THEN
	            #xOutLed := true;
	        ELSIF #xInEstopOkLocal AND NOT #xInAckReqLocal THEN
	            #xOutLed := false;
	        ELSE
	            #xOutLed := true;
	        END_IF;
	        
	    END_IF;
	END_REGION
	
	REGION Monitoring
	    
	    #fbMonitoringEStop.stSettings.nErrorType := 2;
	    #fbMonitoringEStop.stSettings.xErrLedIfMachineIsOff := TRUE;
	    #fbMonitoringEStop.stSettings.xSelfQuit := TRUE;
	    #fbMonitoringEStop(sInFctnName := 'E-Stop',
	                       xInQuitError := #stInOutControlUnitCabinet.xQuitError,
	                       xInSignal := #xInEstopOkLocal,
	                       sInAlarmMessage := 'Not-Halt gedrueckt',
	                       InOutMachineState:=#stInOutControlUnitCabinet);
	    
	END_REGION
END_FUNCTION_BLOCK

