Skip to content

Commit

Permalink
add pulldown resistors to analog switches (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
pfalstad committed Nov 11, 2024
1 parent f07a6c8 commit 1d86868
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
17 changes: 15 additions & 2 deletions src/com/lushprojects/circuitjs1/client/AnalogSwitch2Elm.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,25 +84,38 @@ 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);
if ((flags & FLAG_INVERT) != 0)
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());
Expand Down
34 changes: 31 additions & 3 deletions src/com/lushprojects/circuitjs1/client/AnalogSwitchElm.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -105,13 +122,19 @@ 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) {
if (n1 == 2 || n2 == 2)
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);
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit 1d86868

Please sign in to comment.