FUNCTION_BLOCK "FB_PhasenTest"
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
   VAR_INPUT 
      xInSignal : Bool;
   END_VAR

   VAR RETAIN
      nCount : Int;
      arEventFlagPos : Array[1..10] of Bool;
      arEventFlagNeg : Array[1..10] of Bool;
   END_VAR
   VAR 
      rTrig {InstructionName := 'R_TRIG'; LibVersion := '1.0'} : R_TRIG;
      fTrig {InstructionName := 'F_TRIG'; LibVersion := '1.0'} : F_TRIG;
   END_VAR
   VAR RETAIN
      xActive : Bool;
   END_VAR
   VAR 
      xReset : Bool;
   END_VAR

   VAR_TEMP 
      nIdx : Int;
   END_VAR


BEGIN
	    #rTrig(CLK := #xInSignal);
	    #fTrig(CLK := #xInSignal);
	    //Bei positiver oder negativer Flanke im entspr. Array immer das nächste freie Bit setzen
	    IF #xActive THEN
	        
	        IF #rTrig.Q THEN
	            FOR #nIdx := 1 TO 10 DO
	                IF NOT #arEventFlagPos[#nIdx] THEN
	                    #arEventFlagPos[#nIdx] := TRUE;
	                    EXIT;
	                END_IF;
	            END_FOR;
	        END_IF;
	    
	        IF #fTrig.Q THEN
	            FOR #nIdx := 1 TO 10 DO
	                IF NOT #arEventFlagNeg[#nIdx] THEN
	                    #arEventFlagNeg[#nIdx] := TRUE;
	                    EXIT;
	                END_IF;
	            END_FOR;
	        END_IF;
	        
	    END_IF;
	    
	    //Arrays löschen
	    IF #xReset THEN
	        FOR #nIdx := 1 TO 10 DO
	            #arEventFlagPos[#nIdx] := FALSE;
	            #arEventFlagNeg[#nIdx] := FALSE;
	        END_FOR;
	    END_IF;
END_FUNCTION_BLOCK

