diff --git a/src/com/lushprojects/circuitjs1/client/AnalogSwitch2Elm.java b/src/com/lushprojects/circuitjs1/client/AnalogSwitch2Elm.java index 0c0a9d15..1757dc97 100644 --- a/src/com/lushprojects/circuitjs1/client/AnalogSwitch2Elm.java +++ b/src/com/lushprojects/circuitjs1/client/AnalogSwitch2Elm.java @@ -84,6 +84,10 @@ void stamp() { sim.stampNonLinear(nodes[0]); sim.stampNonLinear(nodes[1]); sim.stampNonLinear(nodes[2]); + if (needsPulldown()) { + sim.stampResistor(nodes[1], 0, r_off); + sim.stampResistor(nodes[2], 0, r_off); + } } void doStep() { open = (volts[3] < 2.5); @@ -91,18 +95,27 @@ void doStep() { open = !open; if (open) { sim.stampResistor(nodes[0], nodes[2], r_on); - sim.stampResistor(nodes[0], nodes[1], r_off); + if (!needsPulldown()) + sim.stampResistor(nodes[0], nodes[1], r_off); } else { sim.stampResistor(nodes[0], nodes[1], r_on); - sim.stampResistor(nodes[0], nodes[2], r_off); + if (!needsPulldown()) + sim.stampResistor(nodes[0], nodes[2], r_off); } } boolean getConnection(int n1, int n2) { if (n1 == 3 || n2 == 3) return false; + if (needsPulldown()) + return comparePair(n1, n2, 0, open ? 2 : 1); return true; } + + boolean hasGroundConnection(int n) { + return needsPulldown() && n != 3; + } + void getInfo(String arr[]) { arr[0] = "analog switch (SPDT)"; arr[1] = "I = " + getCurrentDText(getCurrent()); diff --git a/src/com/lushprojects/circuitjs1/client/AnalogSwitchElm.java b/src/com/lushprojects/circuitjs1/client/AnalogSwitchElm.java index 44ef66ab..02211cea 100644 --- a/src/com/lushprojects/circuitjs1/client/AnalogSwitchElm.java +++ b/src/com/lushprojects/circuitjs1/client/AnalogSwitchElm.java @@ -21,12 +21,14 @@ class AnalogSwitchElm extends CircuitElm { final int FLAG_INVERT = 1; + final int FLAG_PULLDOWN = 2; double resistance, r_on, r_off; public AnalogSwitchElm(int xx, int yy) { super(xx, yy); r_on = 20; r_off = 1e10; noDiagonal = true; + flags |= FLAG_PULLDOWN; } public AnalogSwitchElm(int xa, int ya, int xb, int yb, int f, StringTokenizer st) { @@ -77,22 +79,37 @@ void draw(Graphics g) { drawPosts(g); } void calculateCurrent() { - current = (volts[0]-volts[1])/resistance; + if (needsPulldown() && open) + current = 0; + else + current = (volts[0]-volts[1])/resistance; } // we need this to be able to change the matrix for each step boolean nonLinear() { return true; } + boolean needsPulldown() { return (flags & FLAG_PULLDOWN) != 0; } + void stamp() { sim.stampNonLinear(nodes[0]); sim.stampNonLinear(nodes[1]); + if (needsPulldown()) { + // pulldown resistor on each side + sim.stampResistor(nodes[0], 0, r_off); + sim.stampResistor(nodes[1], 0, r_off); + } } void doStep() { open = (volts[2] < 2.5); if ((flags & FLAG_INVERT) != 0) open = !open; - resistance = (open) ? r_off : r_on; - sim.stampResistor(nodes[0], nodes[1], resistance); + + // if pulldown flag is set, resistance is r_on. Otherwise, no connection. + // if pulldown flag is unset, resistance is r_on for on, r_off for off. + if (!(needsPulldown() && open)) { + resistance = (open) ? r_off : r_on; + sim.stampResistor(nodes[0], nodes[1], resistance); + } } int getPostCount() { return 3; } Point getPost(int n) { @@ -105,6 +122,7 @@ void getInfo(String arr[]) { arr[3] = "I = " + getCurrentDText(getCurrent()); arr[4] = "Vc = " + getVoltageText(volts[2]); } + // we have to just assume current will flow either way, even though that // might cause singular matrix errors boolean getConnection(int n1, int n2) { @@ -112,6 +130,11 @@ boolean getConnection(int n1, int n2) { return false; return true; } + + boolean hasGroundConnection(int n1) { + return needsPulldown() && (n1 < 2); + } + public EditInfo getEditInfo(int n) { if (n == 0) { EditInfo ei = new EditInfo("", 0, -1, -1); @@ -123,6 +146,9 @@ public EditInfo getEditInfo(int n) { return new EditInfo("On Resistance (ohms)", r_on, 0, 0); if (n == 2) return new EditInfo("Off Resistance (ohms)", r_off, 0, 0); + if (n == 3) + return EditInfo.createCheckbox("Pulldown Resistor", needsPulldown()); + return null; } public void setEditValue(int n, EditInfo ei) { @@ -134,6 +160,8 @@ public void setEditValue(int n, EditInfo ei) { r_on = ei.value; if (n == 2 && ei.value > 0) r_off = ei.value; + if (n == 3) + flags = ei.changeFlag(flags, FLAG_PULLDOWN); } double getCurrentIntoNode(int n) {