Merged logic into master
[Mograsim.git] / era.mi / src / era / mi / logic / components / ManualSwitch.java
1 package era.mi.logic.components;
2
3 import java.util.List;
4
5 import era.mi.logic.types.Bit;
6 import era.mi.logic.wires.Wire.WireEnd;
7
8 /**
9  * This class models a simple on/off (ONE/ZERO) switch for user interaction.
10  *
11  * @author Christian Femers
12  *
13  */
14 public class ManualSwitch implements Component
15 {
16         private WireEnd output;
17         private boolean isOn;
18
19         public ManualSwitch(WireEnd output)
20         {
21                 if (output.length() != 1)
22                         throw new IllegalArgumentException("Switch output can be only a single wire");
23                 this.output = output;
24         }
25
26         public void switchOn()
27         {
28                 setState(true);
29         }
30
31         public void switchOff()
32         {
33                 setState(false);
34         }
35
36         public void toggle()
37         {
38                 setState(!isOn);
39         }
40
41         public void setState(boolean isOn)
42         {
43                 if (this.isOn == isOn)
44                         return;
45                 this.isOn = isOn;
46                 output.feedSignals(getValue());
47         }
48
49         public boolean isOn()
50         {
51                 return isOn;
52         }
53
54         public Bit getValue()
55         {
56                 return isOn ? Bit.ONE : Bit.ZERO;
57         }
58
59         @Override
60         public List<WireEnd> getAllInputs()
61         {
62                 return List.of();
63         }
64
65         @Override
66         public List<WireEnd> getAllOutputs()
67         {
68                 return List.of(output);
69         }
70
71 }