1 /*
2  * Copyright (c) 2017 Helmut Neemann
3  * Use of this source code is governed by the GPL v3 license
4  * that can be found in the LICENSE file.
5  */
6 package de.neemann.digital.draw.shapes;
7 
8 import de.neemann.digital.core.element.ElementAttributes;
9 import de.neemann.digital.core.element.PinDescriptions;
10 import de.neemann.digital.core.switching.NFET;
11 import de.neemann.digital.draw.elements.IOState;
12 import de.neemann.digital.draw.graphics.*;
13 
14 import static de.neemann.digital.draw.shapes.GenericShape.SIZE;
15 import static de.neemann.digital.draw.shapes.GenericShape.SIZE2;
16 
17 /**
18  * FET shape.
19  */
20 public abstract class FETShape implements Shape {
21     private final PinDescriptions inputs;
22     private final PinDescriptions outputs;
23     private final String label;
24     private int xOffs = SIZE2 - 2;
25     private NFET fet;
26     private boolean isClosed;
27 
28     /**
29      * Creates a new instance
30      *
31      * @param attributes the attributes
32      * @param inputs     the inputs
33      * @param outputs    the outputs
34      */
FETShape(ElementAttributes attributes, PinDescriptions inputs, PinDescriptions outputs)35     FETShape(ElementAttributes attributes, PinDescriptions inputs, PinDescriptions outputs) {
36         this.inputs = inputs;
37         this.outputs = outputs;
38         label = attributes.getLabel();
39     }
40 
41     /**
42      * Sets the gap width
43      *
44      * @param xOffs the gap width
45      */
setXOffs(int xOffs)46     void setXOffs(int xOffs) {
47         this.xOffs = xOffs;
48     }
49 
50     @Override
applyStateMonitor(IOState ioState)51     public InteractorInterface applyStateMonitor(IOState ioState) {
52         fet = (NFET) ioState.getElement();
53         return null;
54     }
55 
56     @Override
drawTo(Graphic graphic, Style highLight)57     public void drawTo(Graphic graphic, Style highLight) {
58         final int g = SIZE2 / 2;
59         graphic.drawPolygon(new Polygon(false)
60                 .add(SIZE, 0)
61                 .add(xOffs, 0)
62                 .add(xOffs, SIZE2 - g), Style.NORMAL);
63 
64         graphic.drawPolygon(new Polygon(false)
65                 .add(SIZE, SIZE * 2)
66                 .add(xOffs, SIZE * 2)
67                 .add(xOffs, SIZE * 2 - SIZE2 + g), Style.NORMAL);
68 
69         graphic.drawLine(new Vector(xOffs, SIZE2 + g), new Vector(xOffs, SIZE + SIZE2 - g), Style.NORMAL);
70 
71         graphic.drawLine(new Vector(1, 0), new Vector(1, SIZE * 2), Style.NORMAL);
72 
73         if (label != null && label.length() > 0)
74             graphic.drawText(new Vector(SIZE + SIZE2, SIZE * 2), label, Orientation.LEFTBOTTOM, Style.SHAPE_PIN);
75 
76         if (fet != null)
77             drawSwitch(graphic);
78     }
79 
80     @Override
readObservableValues()81     public void readObservableValues() {
82         if (fet!=null)
83             isClosed=fet.isClosed();
84     }
85 
86     /**
87      * Draws the small switch beside the fet
88      *
89      * @param graphic the instance to draw to
90      */
drawSwitch(Graphic graphic)91     private void drawSwitch(Graphic graphic) {
92         drawSwitch(graphic, isClosed);
93     }
94 
95     /**
96      * draws the switch
97      *
98      * @param graphic the graphics instance to draw to
99      * @param closed  state of the switch
100      */
drawSwitch(Graphic graphic, boolean closed)101     public static void drawSwitch(Graphic graphic, boolean closed) {
102         if (closed) {
103             graphic.drawLine(new Vector(SIZE + SIZE2, 0), new Vector(SIZE + SIZE2, SIZE), Style.SHAPE_PIN);
104         } else {
105             graphic.drawLine(new Vector(SIZE + SIZE2, 0), new Vector(SIZE + SIZE2, SIZE2 / 2), Style.SHAPE_PIN);
106             graphic.drawPolygon(new Polygon(false)
107                     .add(SIZE + SIZE2 / 2, SIZE2 / 2 + 2)
108                     .add(SIZE + SIZE2, SIZE - SIZE2 / 2)
109                     .add(SIZE + SIZE2, SIZE), Style.SHAPE_PIN);
110         }
111     }
112 
113     /**
114      * @return the inputs (gate)
115      */
getInputs()116     public PinDescriptions getInputs() {
117         return inputs;
118     }
119 
120     /**
121      * @return the outputs (source and drain)
122      */
getOutputs()123     public PinDescriptions getOutputs() {
124         return outputs;
125     }
126 }
127