package de.mink_ing.automation.blocks;

public class ChangeOnPulse extends DynamicBlock{
	
	public ChangeOnPulse(int Ts) {
		super(Ts);
	}

	//internal states
	private boolean state = false;
	private boolean in_old;
	private boolean state_change = true;
	
	//inputs
	private boolean in;
	private boolean forceOn;
	private boolean forceOff;
	
	//updateStates
	public void oneStep(){
		boolean state_old = state;
		
		if(forceOff) { //Force off has highest prio
			state = false; 
			forceOff = false; //Reset force off (might be set again in next cycle)
		}
		else if(forceOn) { //Force on has prio over toggle
			state = true; 
			forceOn = false; //Reset force off (might be set again in next cycle)
		}
		else if(in & !in_old) { //Toggle has lowest prio
			state = !state;
		}
		in_old = in;
		if(state_old != state) {
			state_change = true;
		}
	}
	
	public void setInput(boolean in){
		this.in = in;
	}
	
	public void forceOn(boolean in){
		this.forceOn = in;
	}
	
	public void forceOff(boolean in){
		this.forceOff = in;
	}
	
	public boolean isStateChanged(){
		boolean sc = state_change;
		state_change = false;
		return(sc);
	}
	
	public boolean getOutput(){
		return( state );
	}
}


@@ -1088,16 +1088,38 @@
 change;%0A
+%09%09if(state_change) %7B%0A%09
 %09%09state_
@@ -1134,16 +1134,20 @@
  false;%0A
+%09%09%7D%0A
 %09%09return

reset state_change only when active (neccessary for concurrent access)