Added GUITest, ManualSwitch and one method to Timeline
[Mograsim.git] / era.mi / src / era / mi / logic / components / ManualSwitch.java
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 (file)
index 0000000..5e6a00c
--- /dev/null
@@ -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<WireArray> getAllInputs() {
+               return List.of();
+       }
+
+       @Override
+       public List<WireArray> getAllOutputs() {
+               return List.of(output);
+       }
+
+}