Added a push button combined with a LED

This commit is contained in:
hneemann 2019-09-03 09:33:42 +02:00
parent c3c388edf2
commit fa03810c21
8 changed files with 152 additions and 3 deletions

View File

@ -0,0 +1,44 @@
/*
* Copyright (c) 2019 Helmut Neemann.
* Use of this source code is governed by the GPL v3 license
* that can be found in the LICENSE file.
*/
package de.neemann.digital.core.io;
import de.neemann.digital.core.NodeException;
import de.neemann.digital.core.ObservableValues;
import de.neemann.digital.core.element.ElementAttributes;
import de.neemann.digital.core.element.ElementTypeDescription;
import de.neemann.digital.core.element.Keys;
import static de.neemann.digital.core.element.PinInfo.input;
/**
* The button combined with a LED.
*/
public class ButtonLED extends Button {
/**
* The ButtonLED description
*/
public static final ElementTypeDescription DESCRIPTION = new ElementTypeDescription(ButtonLED.class, input("in"))
.addAttribute(Keys.ROTATE)
.addAttribute(Keys.LABEL)
.addAttribute(Keys.ACTIVE_LOW)
.addAttribute(Keys.MAP_TO_KEY)
.addAttribute(Keys.COLOR);
/**
* Creates a new instance
*
* @param attributes the buttons attributes
*/
public ButtonLED(ElementAttributes attributes) {
super(attributes);
}
@Override
public void setInputs(ObservableValues inputs) throws NodeException {
inputs.get(0).checkBits(1, null);
}
}

View File

@ -131,6 +131,7 @@ public class ElementLibrary implements Iterable<ElementLibrary.ElementContainer>
.add(new LibraryNode(Lang.get("lib_more"))
.add(RGBLED.DESCRIPTION)
.add(Out.POLARITYAWARELEDDESCRIPTION)
.add(ButtonLED.DESCRIPTION)
.add(Out.SEVENDESCRIPTION)
.add(Out.SEVENHEXDESCRIPTION)
.add(Out.SIXTEENDESCRIPTION)

View File

@ -0,0 +1,84 @@
/*
* Copyright (c) 2019 Helmut Neemann.
* Use of this source code is governed by the GPL v3 license
* that can be found in the LICENSE file.
*/
package de.neemann.digital.draw.shapes;
import de.neemann.digital.core.ObservableValue;
import de.neemann.digital.core.Observer;
import de.neemann.digital.core.Value;
import de.neemann.digital.core.element.ElementAttributes;
import de.neemann.digital.core.element.Keys;
import de.neemann.digital.core.element.PinDescription;
import de.neemann.digital.core.element.PinDescriptions;
import de.neemann.digital.core.io.ButtonLED;
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 static de.neemann.digital.draw.shapes.GenericShape.SIZE2;
import static de.neemann.digital.draw.shapes.OutputShape.OUT_SIZE;
/**
* The shape used for the button combined with a LED.
*/
public class ButtonLEDShape extends ButtonShape {
private final PinDescription input;
private final PinDescription output;
private final Style color;
private ObservableValue inputValue;
private Value ledValue;
private ButtonLED button;
/**
* Creates a new instance
*
* @param attr the attributes
* @param inputs the inputs
* @param outputs the outputs
*/
public ButtonLEDShape(ElementAttributes attr, PinDescriptions inputs, PinDescriptions outputs) {
super(attr, inputs, outputs);
input = inputs.get(0);
output = outputs.get(0);
color = Style.NORMAL.deriveStyle(0, true, attr.get(Keys.COLOR));
}
@Override
public Pins getPins() {
return new Pins()
.add(new Pin(new Vector(0, 0), output))
.add(new Pin(new Vector(0, 20), input));
}
@Override
public InteractorInterface applyStateMonitor(IOState ioState, Observer guiObserver) {
inputValue = ioState.getInput(0);
button = (ButtonLED) ioState.getElement();
return super.applyStateMonitor(ioState, guiObserver);
}
@Override
public void readObservableValues() {
if (inputValue != null)
ledValue = inputValue.getCopy();
}
@Override
public void drawTo(Graphic graphic, Style heighLight) {
super.drawTo(graphic, heighLight);
if (ledValue == null || ledValue.getBool()) {
Vector center;
if (button != null && button.isPressed()) {
center = new Vector(-OUT_SIZE - 1, 0);
} else
center = new Vector(-OUT_SIZE - 1 - ButtonShape.HEIGHT, -ButtonShape.HEIGHT);
graphic.drawCircle(center.add(-SIZE2, -SIZE2), center.add(SIZE2, SIZE2), color);
}
}
}

View File

@ -27,7 +27,7 @@ import static de.neemann.digital.draw.shapes.OutputShape.OUT_SIZE;
*/
public class ButtonShape implements Shape {
private static final int HEIGHT = OUT_SIZE / 2;
protected static final int HEIGHT = OUT_SIZE / 2;
private final String label;
private final PinDescriptions outputs;

View File

@ -110,6 +110,7 @@ public final class ShapeFactory {
map.put(RGBLED.DESCRIPTION.getName(), RGBLEDShape::new);
map.put(Out.POLARITYAWARELEDDESCRIPTION.getName(), PolarityAwareLEDShape::new);
map.put(Button.DESCRIPTION.getName(), ButtonShape::new);
map.put(ButtonLED.DESCRIPTION.getName(), ButtonLEDShape::new);
map.put(Probe.DESCRIPTION.getName(), ProbeShape::new);
map.put(Clock.DESCRIPTION.getName(), ClockShape::new);
map.put(Out.SEVENDESCRIPTION.getName(), SevenSegShape::new);

View File

@ -165,6 +165,11 @@
<string name="elem_Button">Taster</string>
<string name="elem_Button_tt">Ein einfacher Taster, welcher in seinen Ausgangszustand zurückkehrt, wenn er nicht gehalten wird.</string>
<string name="elem_Button_pin_out">Das Ausgangssignal des Tasters.</string>
<string name="elem_ButtonLED">Taster mit LED</string>
<string name="elem_ButtonLED_tt">Ein einfacher Taster, welcher in seinen Ausgangszustand zurückkehrt, wenn er nicht gehalten wird.
Der Taster verfügt über eine LED, die über ein Eingangssignal geschaltet werden kann.</string>
<string name="elem_ButtonLED_pin_out">Das Ausgangssignal des Tasters.</string>
<string name="elem_ButtonLED_pin_in">Eingang zur Steuerung der LED.</string>
<string name="elem_Text">Text</string>
<string name="elem_Text_tt">Zeigt einen einfachen Text in der Schaltung an.
Hat keine weitere Funktion für die Simulation.

View File

@ -178,6 +178,11 @@
<string name="elem_Button">Button</string>
<string name="elem_Button_tt">A simple push button which goes back to its original state when it is released.</string>
<string name="elem_Button_pin_out">The output signal of the button.</string>
<string name="elem_ButtonLED">Button</string>
<string name="elem_ButtonLED_tt">A simple push button which goes back to its original state when it is released.
The push button has an LED which can be switched via an input signal.</string>
<string name="elem_ButtonLED_pin_out">The output signal of the button.</string>
<string name="elem_ButtonLED_pin_in">Input for controlling the LED.</string>
<string name="elem_Text">Text</string>
<string name="elem_Text_tt">Shows a text in the circuit.
Does not affect the simulation.

View File

@ -604,6 +604,11 @@
<elementAttributes/>
<pos x="300" y="940"/>
</visualElement>
<visualElement>
<elementName>ButtonLED</elementName>
<elementAttributes/>
<pos x="180" y="280"/>
</visualElement>
</visualElements>
<wires>
<wire>
@ -1059,8 +1064,8 @@
<p2 x="420" y="300"/>
</wire>
<wire>
<p1 x="220" y="300"/>
<p2 x="260" y="300"/>
<p1 x="180" y="300"/>
<p2 x="220" y="300"/>
</wire>
<wire>
<p1 x="940" y="300"/>
@ -1074,6 +1079,10 @@
<p1 x="660" y="300"/>
<p2 x="680" y="300"/>
</wire>
<wire>
<p1 x="220" y="300"/>
<p2 x="260" y="300"/>
</wire>
<wire>
<p1 x="1080" y="880"/>
<p2 x="1100" y="880"/>