From d08584eb68bc97440d00ec3f3f5765f7b0a56e18 Mon Sep 17 00:00:00 2001 From: hneemann Date: Fri, 19 May 2017 10:59:05 +0200 Subject: [PATCH] added a light bulb --- src/main/dig/74xx/traficInv.dig | 311 ++++++++++++++++++ .../de/neemann/digital/core/io/LightBulb.java | 50 +++ .../digital/draw/library/ElementLibrary.java | 1 + .../digital/draw/shapes/LightBulbShape.java | 70 ++++ .../digital/draw/shapes/ShapeFactory.java | 1 + src/main/resources/lang/lang_de.xml | 5 + src/main/resources/lang/lang_en.xml | 5 + .../digital/integration/TestExamples.java | 2 +- 8 files changed, 444 insertions(+), 1 deletion(-) create mode 100644 src/main/dig/74xx/traficInv.dig create mode 100644 src/main/java/de/neemann/digital/core/io/LightBulb.java create mode 100644 src/main/java/de/neemann/digital/draw/shapes/LightBulbShape.java diff --git a/src/main/dig/74xx/traficInv.dig b/src/main/dig/74xx/traficInv.dig new file mode 100644 index 000000000..346bb7d85 --- /dev/null +++ b/src/main/dig/74xx/traficInv.dig @@ -0,0 +1,311 @@ + + + 1 + + + Description + simple red-yellow-green traffic light + + + + + 7476.dig + + + + + VDD + + + + + VDD + + + + + Clock + + + runRealTime + true + + + + + + 7400.dig + + + + + Ground + + + + + Ground + + + + + LightBulb + + + Color + + 0 + 204 + 0 + 255 + + + + rotation + + + + + + + LightBulb + + + Color + + 255 + 255 + 0 + 255 + + + + rotation + + + + + + + LightBulb + + + rotation + + + + + + + VDD + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/de/neemann/digital/core/io/LightBulb.java b/src/main/java/de/neemann/digital/core/io/LightBulb.java new file mode 100644 index 000000000..adf24e5c5 --- /dev/null +++ b/src/main/java/de/neemann/digital/core/io/LightBulb.java @@ -0,0 +1,50 @@ +package de.neemann.digital.core.io; + +import de.neemann.digital.core.Model; +import de.neemann.digital.core.NodeException; +import de.neemann.digital.core.ObservableValues; +import de.neemann.digital.core.element.Element; +import de.neemann.digital.core.element.ElementAttributes; +import de.neemann.digital.core.element.ElementTypeDescription; +import de.neemann.digital.core.element.Keys; +import de.neemann.digital.draw.elements.PinException; + +import static de.neemann.digital.core.element.PinInfo.input; + +/** + * A light bulb + * Created by hneemann on 19.05.17. + */ +public class LightBulb implements Element { + + /** + * The LED description + */ + public static final ElementTypeDescription DESCRIPTION + = new ElementTypeDescription(LightBulb.class, input("A"), input("B")) + .addAttribute(Keys.ROTATE) + .addAttribute(Keys.LABEL) + .addAttribute(Keys.COLOR); + + /** + * Creates a new light bulb + * @param attr the attributes + */ + public LightBulb(ElementAttributes attr) { + } + + @Override + public void setInputs(ObservableValues inputs) throws NodeException { + inputs.get(0).checkBits(1, null, 0); + inputs.get(1).checkBits(1, null, 1); + } + + @Override + public ObservableValues getOutputs() throws PinException { + return ObservableValues.EMPTY_LIST; + } + + @Override + public void registerNodes(Model model) { + } +} diff --git a/src/main/java/de/neemann/digital/draw/library/ElementLibrary.java b/src/main/java/de/neemann/digital/draw/library/ElementLibrary.java index c864bddca..ec45cc608 100644 --- a/src/main/java/de/neemann/digital/draw/library/ElementLibrary.java +++ b/src/main/java/de/neemann/digital/draw/library/ElementLibrary.java @@ -78,6 +78,7 @@ public class ElementLibrary implements Iterable .add(DummyElement.TEXTDESCRIPTION) .add(Probe.DESCRIPTION) .add(new LibraryNode(Lang.get("lib_more")) + .add(LightBulb.DESCRIPTION) .add(Out.SEVENDESCRIPTION) .add(Out.SEVENHEXDESCRIPTION) .add(LedMatrix.DESCRIPTION) diff --git a/src/main/java/de/neemann/digital/draw/shapes/LightBulbShape.java b/src/main/java/de/neemann/digital/draw/shapes/LightBulbShape.java new file mode 100644 index 000000000..8362c2da6 --- /dev/null +++ b/src/main/java/de/neemann/digital/draw/shapes/LightBulbShape.java @@ -0,0 +1,70 @@ +package de.neemann.digital.draw.shapes; + +import de.neemann.digital.core.ObservableValue; +import de.neemann.digital.core.Observer; +import de.neemann.digital.core.element.ElementAttributes; +import de.neemann.digital.core.element.Keys; +import de.neemann.digital.core.element.PinDescriptions; +import de.neemann.digital.draw.elements.IOState; +import de.neemann.digital.draw.elements.Pin; +import de.neemann.digital.draw.elements.Pins; +import de.neemann.digital.draw.graphics.Graphic; +import de.neemann.digital.draw.graphics.Style; +import de.neemann.digital.draw.graphics.Vector; + +import java.awt.*; + +import static de.neemann.digital.draw.shapes.GenericShape.SIZE; + +/** + * The light bulb shape + * Created by hneemann on 19.05.17. + */ +public class LightBulbShape implements Shape { + private static final Vector CENTER = new Vector(0, SIZE); + private static final int BORDER = 2; + private static final int RAD = (SIZE - BORDER * 2) * 707 / 1000; + private final PinDescriptions inputs; + private final Style style; + private ObservableValue a; + private ObservableValue b; + + /** + * Creates a new instance + * + * @param attr the attributes + * @param inputs the inputs + * @param outputs the outputs + */ + public LightBulbShape(ElementAttributes attr, PinDescriptions inputs, PinDescriptions outputs) { + this.inputs = inputs; + style = new Style(1, true, attr.get(Keys.COLOR)); + } + + @Override + public Pins getPins() { + return new Pins() + .add(new Pin(new Vector(0, 0), inputs.get(0))) + .add(new Pin(new Vector(0, SIZE * 2), inputs.get(1))); + } + + @Override + public InteractorInterface applyStateMonitor(IOState ioState, Observer guiObserver) { + a = ioState.getInput(0); + b = ioState.getInput(1); + return null; + } + + @Override + public void drawTo(Graphic graphic, boolean highLight) { + if (a != null && b != null) { + boolean on = !a.isHighZ() && !b.isHighZ() && (a.getBool() != b.getBool()); + if (on) + graphic.drawCircle(new Vector(-SIZE + BORDER + 1, BORDER + 1), new Vector(SIZE - BORDER - 1, 2 * SIZE - BORDER - 1), style); + } else { + graphic.drawLine(CENTER.add(-RAD, -RAD), CENTER.add(RAD, RAD), Style.NORMAL); + graphic.drawLine(CENTER.add(-RAD, RAD), CENTER.add(RAD, -RAD), Style.NORMAL); + } + graphic.drawCircle(new Vector(-SIZE + BORDER, BORDER), new Vector(SIZE - BORDER, 2 * SIZE - BORDER), Style.NORMAL); + } +} diff --git a/src/main/java/de/neemann/digital/draw/shapes/ShapeFactory.java b/src/main/java/de/neemann/digital/draw/shapes/ShapeFactory.java index 4502a7c96..81a659f9a 100644 --- a/src/main/java/de/neemann/digital/draw/shapes/ShapeFactory.java +++ b/src/main/java/de/neemann/digital/draw/shapes/ShapeFactory.java @@ -91,6 +91,7 @@ public final class ShapeFactory { map.put(TransGate.DESCRIPTION.getName(), TransGateShape::new); map.put(Out.DESCRIPTION.getName(), OutputShape::new); map.put(Out.LEDDESCRIPTION.getName(), LEDShape::new); + map.put(LightBulb.DESCRIPTION.getName(), LightBulbShape::new); map.put(Button.DESCRIPTION.getName(), ButtonShape::new); map.put(Probe.DESCRIPTION.getName(), ProbeShape::new); map.put(Clock.DESCRIPTION.getName(), ClockShape::new); diff --git a/src/main/resources/lang/lang_de.xml b/src/main/resources/lang/lang_de.xml index 8f0645117..f773f3a39 100644 --- a/src/main/resources/lang/lang_de.xml +++ b/src/main/resources/lang/lang_de.xml @@ -431,6 +431,11 @@ Die gesammte Speichergröße beträgt damit damit dx*dy*2 Speicherworte.Steuereingang Steuereingang, invertiert + Glühlämpchen + Glühlämpchen mit zwei Anschlüssen: Wenn ein Strom fließt, leuchtet das Lämpchen. + Anschluss + Anschluss + Fehler Pin {0} in Element {1} ist werder Eingang noch Ausgang Es muss genau ein Taktelement geben. Alle Flipflops müssen an diesem Takt hängen. diff --git a/src/main/resources/lang/lang_en.xml b/src/main/resources/lang/lang_en.xml index 4e8cfc84a..9c79bdc80 100644 --- a/src/main/resources/lang/lang_en.xml +++ b/src/main/resources/lang/lang_en.xml @@ -421,6 +421,11 @@ control input. inverted control input + Light Bulb + Light bulb with two connections. If a current flows, the bulb lights up! + Connection + Connection + Error Pin {0} in component {1} is not a input or output A single clock component is necessary. All flip-flops must use this clock signal. diff --git a/src/test/java/de/neemann/digital/integration/TestExamples.java b/src/test/java/de/neemann/digital/integration/TestExamples.java index 2d24e41cc..1c4d0c1c6 100644 --- a/src/test/java/de/neemann/digital/integration/TestExamples.java +++ b/src/test/java/de/neemann/digital/integration/TestExamples.java @@ -28,7 +28,7 @@ public class TestExamples extends TestCase { */ public void testDistExamples() throws Exception { File examples = new File(Resources.getRoot().getParentFile().getParentFile(), "/main/dig"); - assertEquals(173, new FileScanner(this::check).scan(examples)); + assertEquals(174, new FileScanner(this::check).scan(examples)); assertEquals(78, testCasesInFiles); }