Added project specific format; Default values in WireArray are now U
[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.Bit;
6 import era.mi.logic.wires.WireArray;
7 import era.mi.logic.wires.WireArray.WireArrayEnd;
8
9 /**
10  * This class models a simple on/off (ONE/ZERO) switch for user interaction.
11  *
12  * @author Christian Femers
13  *
14  */
15 public class ManualSwitch implements Component
16 {
17         private WireArray output;
18         private WireArrayEnd outputI;
19         private boolean isOn;
20
21         public ManualSwitch(WireArray output)
22         {
23                 if (output.length != 1)
24                         throw new IllegalArgumentException("Switch output can be only a single wire");
25                 this.output = output;
26                 this.outputI = output.createInput();
27         }
28
29         public void switchOn()
30         {
31                 setState(true);
32         }
33
34         public void switchOff()
35         {
36                 setState(false);
37         }
38
39         public void toggle()
40         {
41                 setState(!isOn);
42         }
43
44         public void setState(boolean isOn)
45         {
46                 if (this.isOn == isOn)
47                         return;
48                 this.isOn = isOn;
49                 outputI.feedSignals(getValue());
50         }
51
52         public boolean isOn()
53         {
54                 return isOn;
55         }
56
57         public Bit getValue()
58         {
59                 return isOn ? Bit.ONE : Bit.ZERO;
60         }
61
62         @Override
63         public List<WireArray> getAllInputs()
64         {
65                 return List.of();
66         }
67
68         @Override
69         public List<WireArray> getAllOutputs()
70         {
71                 return List.of(output);
72         }
73
74 }