X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=era.mi%2Fsrc%2Fera%2Fmi%2Flogic%2Fcomponents%2FManualSwitch.java;fp=era.mi%2Fsrc%2Fera%2Fmi%2Flogic%2Fcomponents%2FManualSwitch.java;h=5e6a00c133eda7fa7eee1963b2fda448ddef2fb2;hb=74206ee98e42a6228aed448f45c49cbdab98253a;hp=0000000000000000000000000000000000000000;hpb=a07a799d9d93669126a544b88856a05e11323b79;p=Mograsim.git diff --git a/era.mi/src/era/mi/logic/components/ManualSwitch.java b/era.mi/src/era/mi/logic/components/ManualSwitch.java new file mode 100644 index 00000000..5e6a00c1 --- /dev/null +++ b/era.mi/src/era/mi/logic/components/ManualSwitch.java @@ -0,0 +1,72 @@ +package era.mi.logic.components; + +import java.util.List; + +import era.mi.logic.Bit; +import era.mi.logic.wires.WireArray; +import era.mi.logic.wires.WireArray.WireArrayInput; + +/** + * This class models a simple on/off (ONE/ZERO) switch for user interaction. + * + * @author Christian Femers + * + */ +public final class ManualSwitch implements Component +{ + private WireArray output; + private WireArrayInput outputI; + private boolean isOn; + + public ManualSwitch(WireArray output) + { + if(output.length != 1) + throw new IllegalArgumentException("Switch output can be only a single wire"); + this.output = output; + this.outputI = output.createInput(); + } + + public void switchOn() + { + setState(true); + } + + public void switchOff() + { + setState(false); + } + + public void toggle() + { + setState(!isOn); + } + + public void setState(boolean isOn) + { + if(this.isOn == isOn) + return; + this.isOn = isOn; + outputI.feedSignals(getValue()); + } + + public boolean isOn() + { + return isOn; + } + + public Bit getValue() + { + return isOn ? Bit.ONE : Bit.ZERO; + } + + @Override + public List getAllInputs() { + return List.of(); + } + + @Override + public List getAllOutputs() { + return List.of(output); + } + +}