Renamed project folders to match the respective project name
[Mograsim.git] / net.mograsim.logic.core / src / net / mograsim / logic / core / components / ManualSwitch.java
1 package net.mograsim.logic.core.components;
2
3 import java.util.List;
4
5 import net.mograsim.logic.core.timeline.Timeline;
6 import net.mograsim.logic.core.types.Bit;
7 import net.mograsim.logic.core.wires.Wire.ReadEnd;
8 import net.mograsim.logic.core.wires.Wire.ReadWriteEnd;
9
10 /**
11  * This class models a simple on/off (ONE/ZERO) switch for user interaction.
12  *
13  * @author Christian Femers
14  *
15  */
16 public class ManualSwitch extends Component
17 {
18         private ReadWriteEnd output;
19         private boolean isOn;
20
21         public ManualSwitch(Timeline timeline, ReadWriteEnd output)
22         {
23                 super(timeline);
24                 if (output.length() != 1)
25                         throw new IllegalArgumentException("Switch output can be only a single wire");
26                 this.output = output;
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                 output.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<ReadEnd> getAllInputs()
64         {
65                 return List.of();
66         }
67
68         @Override
69         public List<ReadWriteEnd> getAllOutputs()
70         {
71                 return List.of(output);
72         }
73
74 }