mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-13 14:56:29 -04:00
added seven segment displays
This commit is contained in:
parent
db5dfdc7c0
commit
336d1fdb07
@ -3,10 +3,7 @@ package de.neemann.digital.core.io;
|
||||
import de.neemann.digital.core.Model;
|
||||
import de.neemann.digital.core.NodeException;
|
||||
import de.neemann.digital.core.ObservableValue;
|
||||
import de.neemann.digital.core.element.AttributeKey;
|
||||
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.*;
|
||||
|
||||
/**
|
||||
* @author hneemann
|
||||
@ -25,6 +22,19 @@ public class Out implements Element {
|
||||
.addAttribute(AttributeKey.Label)
|
||||
.addAttribute(AttributeKey.Color);
|
||||
|
||||
public static final ElementTypeDescription SEVENDESCRIPTION = new ElementTypeDescription("Seven-Seg", Out.class, "a", "b", "c", "d", "e", "f", "g", "dp")
|
||||
.addAttribute(AttributeKey.Label)
|
||||
.addAttribute(AttributeKey.Color);
|
||||
|
||||
public static final ElementTypeDescription SEVENHEXDESCRIPTION = new ElementTypeDescription("Seven-Seg-Hex", new ElementFactory() {
|
||||
@Override
|
||||
public Element create(ElementAttributes attributes) {
|
||||
return new Out(4);
|
||||
}
|
||||
}, "d")
|
||||
.addAttribute(AttributeKey.Label)
|
||||
.addAttribute(AttributeKey.Color);
|
||||
|
||||
private final int bits;
|
||||
private ObservableValue value;
|
||||
|
||||
@ -32,6 +42,10 @@ public class Out implements Element {
|
||||
bits = attributes.getBits();
|
||||
}
|
||||
|
||||
public Out(int bits) {
|
||||
this.bits = bits;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setInputs(ObservableValue... inputs) throws NodeException {
|
||||
value = inputs[0].checkBits(bits, null);
|
||||
|
@ -47,6 +47,8 @@ public class ElementLibrary implements Iterable<ElementLibrary.ElementContainer>
|
||||
add(Out.LEDDESCRIPTION, menu);
|
||||
add(Out.PROBEDESCRIPTION, menu);
|
||||
add(Clock.DESCRIPTION, menu);
|
||||
add(Out.SEVENDESCRIPTION, menu);
|
||||
add(Out.SEVENHEXDESCRIPTION, menu);
|
||||
|
||||
menu = Lang.get("lib_mux");
|
||||
add(Multiplexer.DESCRIPTION, menu);
|
||||
|
@ -0,0 +1,54 @@
|
||||
package de.neemann.digital.draw.shapes;
|
||||
|
||||
import de.neemann.digital.core.ObservableValue;
|
||||
import de.neemann.digital.core.Observer;
|
||||
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.Style;
|
||||
import de.neemann.digital.draw.graphics.Vector;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
import static de.neemann.digital.draw.shapes.GenericShape.SIZE;
|
||||
|
||||
/**
|
||||
* @author hneemann
|
||||
*/
|
||||
public class SevenSegHexShape extends SevenShape {
|
||||
private static final int[] table = new int[]{0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71};
|
||||
private Pins pins;
|
||||
private ObservableValue input;
|
||||
|
||||
public SevenSegHexShape(String label, Color color) {
|
||||
super(label, color);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Style getStyle(int i) {
|
||||
if (input == null)
|
||||
return onStyle;
|
||||
|
||||
int v = (int) input.getValueIgnoreBurn() & 0xf;
|
||||
v = table[v];
|
||||
if ((v & (1 << i)) != 0)
|
||||
return onStyle;
|
||||
else
|
||||
return offStyle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pins getPins() {
|
||||
if (pins == null) {
|
||||
pins = new Pins();
|
||||
pins.add(new Pin(new Vector(SIZE * 3, SIZE * HEIGHT), "d", Pin.Direction.input));
|
||||
}
|
||||
return pins;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Interactor applyStateMonitor(IOState ioState, Observer guiObserver) {
|
||||
input = ioState.getInput(0);
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
package de.neemann.digital.draw.shapes;
|
||||
|
||||
import de.neemann.digital.core.ObservableValue;
|
||||
import de.neemann.digital.core.Observer;
|
||||
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.Style;
|
||||
import de.neemann.digital.draw.graphics.Vector;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
import static de.neemann.digital.draw.shapes.GenericShape.SIZE;
|
||||
|
||||
/**
|
||||
* @author hneemann
|
||||
*/
|
||||
public class SevenSegShape extends SevenShape {
|
||||
private ObservableValue[] inputs;
|
||||
private Pins pins;
|
||||
|
||||
public SevenSegShape(String label, Color color) {
|
||||
super(label, color);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pins getPins() {
|
||||
if (pins == null) {
|
||||
pins = new Pins();
|
||||
pins.add(new Pin(new Vector(0, 0), "a", Pin.Direction.input));
|
||||
pins.add(new Pin(new Vector(SIZE, 0), "b", Pin.Direction.input));
|
||||
pins.add(new Pin(new Vector(SIZE * 2, 0), "c", Pin.Direction.input));
|
||||
pins.add(new Pin(new Vector(SIZE * 3, 0), "d", Pin.Direction.input));
|
||||
pins.add(new Pin(new Vector(0, SIZE * HEIGHT), "e", Pin.Direction.input));
|
||||
pins.add(new Pin(new Vector(SIZE, SIZE * HEIGHT), "f", Pin.Direction.input));
|
||||
pins.add(new Pin(new Vector(SIZE * 2, SIZE * HEIGHT), "g", Pin.Direction.input));
|
||||
pins.add(new Pin(new Vector(SIZE * 3, SIZE * HEIGHT), "dp", Pin.Direction.input));
|
||||
}
|
||||
return pins;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Interactor applyStateMonitor(IOState ioState, Observer guiObserver) {
|
||||
inputs = ioState.getInputs();
|
||||
for (ObservableValue o : inputs)
|
||||
o.addObserver(guiObserver);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
protected Style getStyle(int i) {
|
||||
if (inputs == null)
|
||||
return onStyle;
|
||||
else if (inputs[i].getValueIgnoreBurn() > 0)
|
||||
return onStyle;
|
||||
else
|
||||
return offStyle;
|
||||
}
|
||||
|
||||
}
|
54
src/main/java/de/neemann/digital/draw/shapes/SevenShape.java
Normal file
54
src/main/java/de/neemann/digital/draw/shapes/SevenShape.java
Normal file
@ -0,0 +1,54 @@
|
||||
package de.neemann.digital.draw.shapes;
|
||||
|
||||
import de.neemann.digital.draw.graphics.Graphic;
|
||||
import de.neemann.digital.draw.graphics.Polygon;
|
||||
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;
|
||||
import static de.neemann.digital.draw.shapes.GenericShape.SIZE2;
|
||||
|
||||
/**
|
||||
* @author hneemann
|
||||
*/
|
||||
public abstract class SevenShape implements Shape {
|
||||
protected static final int HEIGHT = 7;
|
||||
private static final Vector ofs = new Vector(2, 8);
|
||||
private static final int LEN = 26;
|
||||
|
||||
|
||||
private final String label;
|
||||
protected final Style onStyle;
|
||||
protected final Style offStyle;
|
||||
|
||||
public SevenShape(String label, Color color) {
|
||||
this.label = label;
|
||||
onStyle = new Style(4, true, color);
|
||||
offStyle = new Style(4, true, new Color(230, 230, 230));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawTo(Graphic graphic) {
|
||||
graphic.drawPolygon(new Polygon(true)
|
||||
.add(-SIZE2, 0)
|
||||
.add(SIZE * 3 + SIZE2, 0)
|
||||
.add(SIZE * 3 + SIZE2, HEIGHT * SIZE)
|
||||
.add(-SIZE2, HEIGHT * SIZE), Style.NORMAL);
|
||||
|
||||
int th = onStyle.getThickness();
|
||||
int slant = 2;
|
||||
graphic.drawLine(new Vector(th + slant, 0).add(ofs), new Vector(LEN - th + slant, 0).add(ofs), getStyle(0));
|
||||
graphic.drawLine(new Vector(LEN + slant, th).add(ofs), new Vector(LEN, LEN - th).add(ofs), getStyle(1));
|
||||
graphic.drawLine(new Vector(LEN, LEN + th).add(ofs), new Vector(LEN - slant, 2 * LEN - th).add(ofs), getStyle(2));
|
||||
graphic.drawLine(new Vector(th - slant, 2 * LEN).add(ofs), new Vector(LEN - th - slant, 2 * LEN).add(ofs), getStyle(3));
|
||||
graphic.drawLine(new Vector(0, LEN + th).add(ofs), new Vector(-slant, 2 * LEN - th).add(ofs), getStyle(4));
|
||||
graphic.drawLine(new Vector(slant, th).add(ofs), new Vector(0, LEN - th).add(ofs), getStyle(5));
|
||||
graphic.drawLine(new Vector(th, LEN).add(ofs), new Vector(LEN - th, LEN).add(ofs), getStyle(6));
|
||||
graphic.drawCircle(new Vector(LEN, LEN * 2 - slant).add(ofs), new Vector(LEN + slant * 2, LEN * 2 + slant).add(ofs), getStyle(7));
|
||||
}
|
||||
|
||||
protected abstract Style getStyle(int i);
|
||||
|
||||
}
|
@ -60,6 +60,10 @@ public final class ShapeFactory {
|
||||
map.put(Out.LEDDESCRIPTION.getName(), attr -> new LEDShape(attr.get(AttributeKey.Label), attr.get(AttributeKey.Color)));
|
||||
map.put(Out.PROBEDESCRIPTION.getName(), attr -> new ProbeShape(attr.get(AttributeKey.Label)));
|
||||
map.put(Clock.DESCRIPTION.getName(), attr -> new ClockShape(attr.get(AttributeKey.Label)));
|
||||
map.put(Out.SEVENDESCRIPTION.getName(), attr -> new SevenSegShape(attr.get(AttributeKey.Label), attr.get(AttributeKey.Color)));
|
||||
map.put(Out.SEVENHEXDESCRIPTION.getName(), attr -> new SevenSegHexShape(attr.get(AttributeKey.Label), attr.get(AttributeKey.Color)));
|
||||
|
||||
|
||||
map.put(Multiplexer.DESCRIPTION.getName(), attr -> new MuxerShape(attr.get(AttributeKey.SelectorBits), attr.get(AttributeKey.FlipSelPositon)));
|
||||
map.put(Demultiplexer.DESCRIPTION.getName(), attr -> new DemuxerShape(attr.get(AttributeKey.SelectorBits), true, attr.get(AttributeKey.FlipSelPositon)));
|
||||
map.put(Decoder.DESCRIPTION.getName(), attr -> new DemuxerShape(attr.get(AttributeKey.SelectorBits), false, attr.get(AttributeKey.FlipSelPositon)));
|
||||
|
Loading…
x
Reference in New Issue
Block a user