added a break element

This commit is contained in:
hneemann 2016-03-29 17:16:49 +02:00
parent c5c071f223
commit ff32e2e25b
8 changed files with 117 additions and 45 deletions

View File

@ -110,6 +110,12 @@
<pos x="240" y="190"/>
<rotate>0</rotate>
</visualElement>
<visualElement>
<elementName>Break</elementName>
<elementAttributes/>
<pos x="190" y="110"/>
<rotate>0</rotate>
</visualElement>
</visualElements>
<wires>
<wire>

View File

@ -5,6 +5,7 @@ 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;
/**
@ -20,6 +21,9 @@ public class Break implements Element {
private ObservableValue input;
public Break(ElementAttributes attributes) {
}
@Override
public void setInputs(ObservableValue... inputs) throws NodeException {
input = inputs[0].checkBits(1, null);

View File

@ -52,49 +52,4 @@ public class Clock implements Element {
return frequency;
}
// private static class MyModelStateObserver implements ModelStateObserver {
// private final Model model;
// private final Clock clock;
// private final int frequency;
// private final ObservableValue output;
// private Timer timer;
//
// public MyModelStateObserver(Model model, Clock clock, int frequency, ObservableValue output) {
// this.model = model;
// this.clock = clock;
// this.frequency = frequency;
// this.output = output;
// }
//
// @Override
// public void handleEvent(ModelEvent event) {
// switch (event.getType()) {
// case STARTED:
// int delay = 1000 / frequency;
// if (delay < 100) delay = 100;
// timer = new Timer(delay, e -> {
// output.setValue(1 - output.getValue());
// try {
// model.doStep();
// } catch (NodeException e1) {
// SwingUtilities.invokeLater(new ErrorMessage("ClockError").addCause(e1));
// timer.stop();
// }
// });
// timer.start();
// break;
// case STOPPED:
// if (timer != null)
// timer.stop();
// break;
// case FETCHCLOCK:
// event.registerClock(clock);
// break;
// }
// }
//
// public void remove() {
// model.removeObserver(this);
// }
// }
}

View File

@ -47,6 +47,7 @@ public class ElementLibrary implements Iterable<ElementLibrary.ElementContainer>
add(Out.LEDDESCRIPTION, menu);
add(Out.PROBEDESCRIPTION, menu);
add(Clock.DESCRIPTION, menu);
add(Break.DESCRIPTION, menu);
add(Out.SEVENDESCRIPTION, menu);
add(Out.SEVENHEXDESCRIPTION, menu);

View File

@ -0,0 +1,50 @@
package de.neemann.digital.draw.model;
import de.neemann.digital.core.*;
import de.neemann.digital.core.wiring.Clock;
import de.neemann.gui.ErrorMessage;
import javax.swing.*;
/**
* @author hneemann
*/
public class RealTimeClock implements ModelStateObserver {
private final Model model;
private final int frequency;
private final ObservableValue output;
private Timer timer;
public RealTimeClock(Model model, Clock clock) {
this.model = model;
int f = clock.getFrequency();
if (f < 1) f = 1;
this.frequency = f;
this.output = clock.getClockOutput();
}
@Override
public void handleEvent(ModelEvent event) {
switch (event.getType()) {
case STARTED:
int delay = 1000 / frequency;
if (delay < 100) delay = 100;
timer = new Timer(delay, e -> {
output.setValue(1 - output.getValue());
try {
model.doStep();
} catch (NodeException e1) {
SwingUtilities.invokeLater(new ErrorMessage("ClockError").addCause(e1));
timer.stop();
}
});
timer.start();
break;
case STOPPED:
if (timer != null)
timer.stop();
break;
}
}
}

View File

@ -0,0 +1,46 @@
package de.neemann.digital.draw.shapes;
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.Graphic;
import de.neemann.digital.draw.graphics.Orientation;
import de.neemann.digital.draw.graphics.Style;
import de.neemann.digital.draw.graphics.Vector;
/**
* @author hneemann
*/
public class BreakShape implements Shape {
private static final int SIZE = 8;
private static final int SIZEQ = 5;
private static final Vector RAD = new Vector(SIZE, SIZE);
private static final Vector D1 = new Vector(SIZEQ, -SIZEQ);
private static final Vector D2 = new Vector(SIZEQ, SIZEQ);
private final String label;
public BreakShape(String label) {
this.label = label;
}
@Override
public Pins getPins() {
return new Pins().add(new Pin(new Vector(0, 0), "brk", Pin.Direction.input));
}
@Override
public Interactor applyStateMonitor(IOState ioState, Observer guiObserver) {
return null;
}
@Override
public void drawTo(Graphic graphic) {
Vector center = new Vector(2 + SIZE, 0);
graphic.drawCircle(center.sub(RAD), center.add(RAD), Style.NORMAL);
graphic.drawLine(center.sub(D1), center.add(D1), Style.NORMAL);
graphic.drawLine(center.sub(D2), center.add(D2), Style.NORMAL);
Vector textPos = new Vector(SIZE * 3, 0);
graphic.drawText(textPos, textPos.add(1, 0), label, Orientation.LEFTCENTER, Style.NORMAL);
}
}

View File

@ -50,6 +50,7 @@ public final class ShapeFactory {
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(Break.DESCRIPTION.getName(), attributes -> new BreakShape(attributes.get(AttributeKey.Label)));
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)));

View File

@ -4,11 +4,13 @@ import de.neemann.digital.core.Model;
import de.neemann.digital.core.NodeException;
import de.neemann.digital.core.Observer;
import de.neemann.digital.core.SpeedTest;
import de.neemann.digital.core.wiring.Clock;
import de.neemann.digital.draw.elements.Circuit;
import de.neemann.digital.draw.elements.PinException;
import de.neemann.digital.draw.elements.PinOrder;
import de.neemann.digital.draw.library.ElementLibrary;
import de.neemann.digital.draw.model.ModelDescription;
import de.neemann.digital.draw.model.RealTimeClock;
import de.neemann.digital.draw.shapes.ShapeFactory;
import de.neemann.digital.gui.components.CircuitComponent;
import de.neemann.digital.gui.components.ElementOrderer;
@ -277,6 +279,7 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave {
toolBar.add(runModel.createJButtonNoText());
toolBar.add(runModelMicro.createJButtonNoText());
toolBar.add(doStep.createJButtonNoText());
toolBar.addSeparator();
toolBar.add(runFast.createJButtonNoText());
toolBar.addSeparator();
@ -306,9 +309,15 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave {
circuitComponent.setModeAndReset(CircuitComponent.Mode.running);
modelDescription = new ModelDescription(circuitComponent.getCircuit(), library);
model = modelDescription.createModel();
modelDescription.connectToGui(circuitComponent);
if (runClock)
for (Clock c : model.getClocks())
model.addObserver(new RealTimeClock(model, c));
model.init();
} catch (NodeException e) {
if (modelDescription != null) {
if (e.getNodes() != null)