refactoring of FET shapes

This commit is contained in:
hneemann 2017-02-25 17:54:26 +01:00
parent 4905eeb50a
commit c7b550a010
5 changed files with 125 additions and 116 deletions

View File

@ -3,25 +3,22 @@ package de.neemann.digital.draw.shapes;
import de.neemann.digital.core.Observer; import de.neemann.digital.core.Observer;
import de.neemann.digital.core.element.ElementAttributes; import de.neemann.digital.core.element.ElementAttributes;
import de.neemann.digital.core.element.PinDescriptions; import de.neemann.digital.core.element.PinDescriptions;
import de.neemann.digital.core.wiring.NFET;
import de.neemann.digital.core.wiring.Relay; import de.neemann.digital.core.wiring.Relay;
import de.neemann.digital.draw.elements.IOState; 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.*; import de.neemann.digital.draw.graphics.*;
import static de.neemann.digital.draw.shapes.GenericShape.SIZE; import static de.neemann.digital.draw.shapes.GenericShape.SIZE;
import static de.neemann.digital.draw.shapes.GenericShape.SIZE2; import static de.neemann.digital.draw.shapes.GenericShape.SIZE2;
/** /**
* The FETS shape * FET shape.
* Created by hneemann on 25.02.17.
*/ */
public class NFETShape implements Shape { public abstract class FETShape implements Shape {
private final PinDescriptions inputs; private final PinDescriptions inputs;
private final PinDescriptions outputs; private final PinDescriptions outputs;
private final String label; private final String label;
private NFET fet; private Relay fet;
/** /**
* Creates a new instance * Creates a new instance
@ -30,25 +27,16 @@ public class NFETShape implements Shape {
* @param inputs the inputs * @param inputs the inputs
* @param outputs the outputs * @param outputs the outputs
*/ */
public NFETShape(ElementAttributes attributes, PinDescriptions inputs, PinDescriptions outputs) { protected FETShape(ElementAttributes attributes, PinDescriptions inputs, PinDescriptions outputs) {
this.inputs = inputs; this.inputs = inputs;
this.outputs = outputs; this.outputs = outputs;
label = attributes.getCleanLabel(); label = attributes.getCleanLabel();
} }
@Override
public Pins getPins() {
return new Pins()
.add(new Pin(new Vector(0, SIZE * 2), inputs.get(0)))
.add(new Pin(new Vector(SIZE, 0), outputs.get(0)))
.add(new Pin(new Vector(SIZE, SIZE * 2), outputs.get(1)));
}
@Override @Override
public InteractorInterface applyStateMonitor(IOState ioState, Observer guiObserver) { public InteractorInterface applyStateMonitor(IOState ioState, Observer guiObserver) {
fet = (NFET) ioState.getElement(); fet = (Relay) ioState.getElement();
ioState.getInput(0).addObserverToValue(guiObserver); ioState.getInput(0).addObserverToValue(guiObserver);
ioState.getInput(2).addObserverToValue(guiObserver);
return null; return null;
} }
@ -66,13 +54,6 @@ public class NFETShape implements Shape {
.add(x1, SIZE * 2) .add(x1, SIZE * 2)
.add(x1, SIZE * 2 - SIZE2 + g), Style.NORMAL); .add(x1, SIZE * 2 - SIZE2 + g), Style.NORMAL);
graphic.drawLine(new Vector(SIZE2 + 3, SIZE), new Vector(SIZE, SIZE), Style.THIN);
graphic.drawPolygon(new Polygon(true)
.add(x1 + 4, SIZE)
.add(SIZE - SIZE2 / 3, SIZE - SIZE2 / 4)
.add(SIZE - SIZE2 / 3, SIZE + SIZE2 / 4), Style.THIN_FILLED);
graphic.drawLine(new Vector(x1, SIZE2 + g), new Vector(x1, SIZE + SIZE2 - g), Style.NORMAL); graphic.drawLine(new Vector(x1, SIZE2 + g), new Vector(x1, SIZE + SIZE2 - g), Style.NORMAL);
graphic.drawLine(new Vector(1, 0), new Vector(1, SIZE * 2), Style.NORMAL); graphic.drawLine(new Vector(1, 0), new Vector(1, SIZE * 2), Style.NORMAL);
@ -81,16 +62,15 @@ public class NFETShape implements Shape {
graphic.drawText(new Vector(SIZE + SIZE2, SIZE * 2), new Vector(SIZE * 2, SIZE * 2), label, Orientation.LEFTBOTTOM, Style.SHAPE_PIN); graphic.drawText(new Vector(SIZE + SIZE2, SIZE * 2), new Vector(SIZE * 2, SIZE * 2), label, Orientation.LEFTBOTTOM, Style.SHAPE_PIN);
if (fet != null) if (fet != null)
drawSwitch(graphic, fet); drawSwitch(graphic);
} }
/** /**
* Draws the small switch beside the fet * Draws the small switch beside the fet
* *
* @param graphic the instance to draw to * @param graphic the instance to draw to
* @param fet the fet
*/ */
public static void drawSwitch(Graphic graphic, Relay fet) { private void drawSwitch(Graphic graphic) {
boolean closed = fet.isClosed(); boolean closed = fet.isClosed();
if (closed) { if (closed) {
graphic.drawLine(new Vector(SIZE + SIZE2, 0), new Vector(SIZE + SIZE2, SIZE), Style.SHAPE_PIN); graphic.drawLine(new Vector(SIZE + SIZE2, 0), new Vector(SIZE + SIZE2, SIZE), Style.SHAPE_PIN);
@ -103,4 +83,17 @@ public class NFETShape implements Shape {
} }
} }
/**
* @return the inputs (gate)
*/
public PinDescriptions getInputs() {
return inputs;
}
/**
* @return the outputs (source and drain)
*/
public PinDescriptions getOutputs() {
return outputs;
}
} }

View File

@ -0,0 +1,50 @@
package de.neemann.digital.draw.shapes;
import de.neemann.digital.core.element.ElementAttributes;
import de.neemann.digital.core.element.PinDescriptions;
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.Polygon;
import de.neemann.digital.draw.graphics.Style;
import de.neemann.digital.draw.graphics.Vector;
import static de.neemann.digital.draw.shapes.GenericShape.SIZE;
import static de.neemann.digital.draw.shapes.GenericShape.SIZE2;
/**
* The n-chan FET shape
*/
public class FETShapeN extends FETShape {
/**
* Creates a new instance
*
* @param attributes the attributes
* @param inputs the inputs
* @param outputs the outputs
*/
public FETShapeN(ElementAttributes attributes, PinDescriptions inputs, PinDescriptions outputs) {
super(attributes, inputs, outputs);
}
@Override
public Pins getPins() {
return new Pins()
.add(new Pin(new Vector(0, SIZE * 2), getInputs().get(0)))
.add(new Pin(new Vector(SIZE, 0), getOutputs().get(0)))
.add(new Pin(new Vector(SIZE, SIZE * 2), getOutputs().get(1)));
}
@Override
public void drawTo(Graphic graphic, boolean highLight) {
super.drawTo(graphic, highLight);
// the arrow
graphic.drawLine(new Vector(SIZE2 + 5, SIZE), new Vector(SIZE, SIZE), Style.THIN);
graphic.drawPolygon(new Polygon(true)
.add(SIZE2 + 2, SIZE)
.add(SIZE - SIZE2 / 3, SIZE - SIZE2 / 4)
.add(SIZE - SIZE2 / 3, SIZE + SIZE2 / 4), Style.THIN_FILLED);
}
}

View File

@ -0,0 +1,52 @@
package de.neemann.digital.draw.shapes;
import de.neemann.digital.core.element.ElementAttributes;
import de.neemann.digital.core.element.PinDescriptions;
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.Polygon;
import de.neemann.digital.draw.graphics.Style;
import de.neemann.digital.draw.graphics.Vector;
import static de.neemann.digital.draw.shapes.GenericShape.SIZE;
import static de.neemann.digital.draw.shapes.GenericShape.SIZE2;
/**
* The p-chan FET shape
*/
public class FETShapeP extends FETShape {
/**
* Creates a new instance
*
* @param attributes the attributes
* @param inputs the inputs
* @param outputs the outputs
*/
public FETShapeP(ElementAttributes attributes, PinDescriptions inputs, PinDescriptions outputs) {
super(attributes, inputs, outputs);
}
@Override
public Pins getPins() {
return new Pins()
.add(new Pin(new Vector(0, 0), getInputs().get(0)))
.add(new Pin(new Vector(SIZE, 0), getOutputs().get(0)))
.add(new Pin(new Vector(SIZE, SIZE * 2), getOutputs().get(1)));
}
@Override
public void drawTo(Graphic graphic, boolean highLight) {
super.drawTo(graphic, highLight);
// the arrow
graphic.drawLine(new Vector(SIZE2 - 2, SIZE), new Vector(SIZE2 + 4, SIZE), Style.THIN);
graphic.drawPolygon(new Polygon(true)
.add(SIZE - SIZE2 / 3 + 2, SIZE)
.add(SIZE2 + 4, SIZE - SIZE2 / 4)
.add(SIZE2 + 4, SIZE + SIZE2 / 4), Style.THIN_FILLED);
}
}

View File

@ -1,86 +0,0 @@
package de.neemann.digital.draw.shapes;
import de.neemann.digital.core.Observer;
import de.neemann.digital.core.element.ElementAttributes;
import de.neemann.digital.core.element.PinDescriptions;
import de.neemann.digital.core.wiring.PFET;
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.*;
import static de.neemann.digital.draw.shapes.GenericShape.SIZE;
import static de.neemann.digital.draw.shapes.GenericShape.SIZE2;
/**
* The FETS shape
*/
public class PFETShape implements Shape {
private final PinDescriptions inputs;
private final PinDescriptions outputs;
private final String label;
private PFET fet;
/**
* Creates a new instance
*
* @param attributes the attributes
* @param inputs the inputs
* @param outputs the outputs
*/
public PFETShape(ElementAttributes attributes, PinDescriptions inputs, PinDescriptions outputs) {
this.inputs = inputs;
this.outputs = outputs;
label = attributes.getCleanLabel();
}
@Override
public Pins getPins() {
return new Pins()
.add(new Pin(new Vector(0, 0), inputs.get(0)))
.add(new Pin(new Vector(SIZE, 0), outputs.get(0)))
.add(new Pin(new Vector(SIZE, SIZE * 2), outputs.get(1)));
}
@Override
public InteractorInterface applyStateMonitor(IOState ioState, Observer guiObserver) {
fet = (PFET) ioState.getElement();
ioState.getInput(0).addObserverToValue(guiObserver);
ioState.getInput(1).addObserverToValue(guiObserver);
return null;
}
@Override
public void drawTo(Graphic graphic, boolean highLight) {
final int x1 = SIZE2 - 2;
final int g = SIZE2 / 2;
graphic.drawPolygon(new Polygon(false)
.add(SIZE, 0)
.add(x1, 0)
.add(x1, SIZE2 - g), Style.NORMAL);
graphic.drawPolygon(new Polygon(false)
.add(SIZE, SIZE * 2)
.add(x1, SIZE * 2)
.add(x1, SIZE * 2 - SIZE2 + g), Style.NORMAL);
graphic.drawLine(new Vector(x1, SIZE), new Vector(SIZE, SIZE), Style.THIN);
graphic.drawPolygon(new Polygon(true)
.add(SIZE - SIZE2 / 3, SIZE)
.add(x1 + 4, SIZE - SIZE2 / 4)
.add(x1 + 4, SIZE + SIZE2 / 4), Style.THIN_FILLED);
graphic.drawLine(new Vector(x1, SIZE2 + g), new Vector(x1, SIZE + SIZE2 - g), Style.NORMAL);
graphic.drawLine(new Vector(1, 0), new Vector(1, SIZE * 2), Style.NORMAL);
if (label != null && label.length() > 0)
graphic.drawText(new Vector(SIZE + SIZE2, SIZE * 2), new Vector(SIZE * 2, SIZE * 2), label, Orientation.LEFTBOTTOM, Style.SHAPE_PIN);
if (fet != null)
NFETShape.drawSwitch(graphic, fet);
}
}

View File

@ -81,8 +81,8 @@ public final class ShapeFactory {
map.put(VDD.DESCRIPTION.getName(), VDDShape::new); map.put(VDD.DESCRIPTION.getName(), VDDShape::new);
map.put(Switch.DESCRIPTION.getName(), SwitchShape::new); map.put(Switch.DESCRIPTION.getName(), SwitchShape::new);
map.put(Relay.DESCRIPTION.getName(), RelayShape::new); map.put(Relay.DESCRIPTION.getName(), RelayShape::new);
map.put(NFET.DESCRIPTION.getName(), NFETShape::new); map.put(NFET.DESCRIPTION.getName(), FETShapeN::new);
map.put(PFET.DESCRIPTION.getName(), PFETShape::new); map.put(PFET.DESCRIPTION.getName(), FETShapeP::new);
map.put(Out.DESCRIPTION.getName(), OutputShape::new); map.put(Out.DESCRIPTION.getName(), OutputShape::new);
map.put(Out.LEDDESCRIPTION.getName(), LEDShape::new); map.put(Out.LEDDESCRIPTION.getName(), LEDShape::new);
map.put(Button.DESCRIPTION.getName(), ButtonShape::new); map.put(Button.DESCRIPTION.getName(), ButtonShape::new);