Merge branch 'development' of https://gitlab.lrz.de/lrr-tum/students/eragp-misim...
[Mograsim.git] / net.mograsim.logic.model.am2900 / src / net / mograsim / logic / model / examples / SubmodelComponentTestbench.java
1 package net.mograsim.logic.model.examples;
2
3 import java.io.IOException;
4 import java.io.UncheckedIOException;
5 import java.util.ArrayList;
6 import java.util.List;
7
8 import net.mograsim.logic.model.SimpleLogicUIStandalone;
9 import net.mograsim.logic.model.model.ViewModelModifiable;
10 import net.mograsim.logic.model.model.components.atomic.GUIBitDisplay;
11 import net.mograsim.logic.model.model.components.atomic.GUIManualSwitch;
12 import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
13 import net.mograsim.logic.model.model.wires.GUIWire;
14 import net.mograsim.logic.model.model.wires.Pin;
15 import net.mograsim.logic.model.serializing.SubmodelComponentSerializer;
16
17 public class SubmodelComponentTestbench
18 {
19         public static void main(String[] args)
20         {
21                 SimpleLogicUIStandalone.executeVisualisation(SubmodelComponentTestbench::createTestbench);
22         }
23
24         @SuppressWarnings("unused") // for GUIWires being created
25         public static void createTestbench(ViewModelModifiable model)
26         {
27                 SubmodelComponent comp;
28                 try
29                 {
30                         comp = SubmodelComponentSerializer.deserialize(model, "components/am2901/GUIAm2901.json");
31                 }
32                 catch (IOException e)
33                 {
34                         throw new UncheckedIOException(e);
35                 }
36
37                 // guess which pins are outputs and which are inputs
38                 // TODO this code exists three times... but it seems too "hacky" to put it in a helper class
39                 List<String> inputPinNames = new ArrayList<>();
40                 List<String> outputPinNames = new ArrayList<>();
41                 for (Pin p : comp.getPins().values())
42                         if (p.getRelX() == 0)
43                                 inputPinNames.add(p.name);
44                         else
45                                 outputPinNames.add(p.name);
46
47                 comp.moveTo(100, 0);
48                 for (int i = 0; i < inputPinNames.size(); i++)
49                 {
50                         GUIManualSwitch sw = new GUIManualSwitch(model);
51                         sw.moveTo(0, 20 * i);
52                         new GUIWire(model, comp.getPin(inputPinNames.get(i)), sw.getOutputPin());
53                 }
54                 for (int i = 0; i < outputPinNames.size(); i++)
55                 {
56                         GUIBitDisplay bd = new GUIBitDisplay(model);
57                         bd.moveTo(200, 20 * i);
58                         new GUIWire(model, comp.getPin(outputPinNames.get(i)), bd.getInputPin());
59                 }
60         }
61 }