-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircuits.pde
86 lines (80 loc) · 2.03 KB
/
Circuits.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
public class AndGate extends Circuit {
public AndGate(int x, int y) {
super(x, y, 70, 80, 2, 1, "AND");
}
public void calcOutput(int index) {
this.setOutput(index, getInput(0) && getInput(1));
}
}
public class OrGate extends Circuit {
public OrGate(int x, int y) {
super(x, y, 70, 80, 2, 1, "OR");
}
public void calcOutput(int index) {
this.setOutput(index, getInput(0) || getInput(1));
}
}
public class NotGate extends Circuit {
public NotGate(int x, int y) {
super(x, y, 70, 40, 1, 1, "NOT");
}
public void calcOutput(int index) {
this.setOutput(index, !getInput(0));
}
}
public class Clock extends Circuit {
int msDelay;
int localTime;
public Clock(int x, int y, int msDelay) {
super(x, y, 70, 40, 0, 1, "CLOCK");
this.msDelay = msDelay;
if (msDelay<=0) {
println("Error invalid clock time:", msDelay);
println("Clock time set to: 10");
this.msDelay = 10;
}
this.localTime = 0;
}
@Override
public void display() {
super.display();
rect(getX()+10, getY()+20, map(localTime%msDelay, 0, msDelay, 0, 40), 10, 2);
}
@Override
public void update() {
localTime += 1;
if (localTime == 2*msDelay) localTime =0;
calcOutput(0);
}
@Override
public void recUpdate() {
update();
}
public void calcOutput(int index) {
setOutput(index, boolean(localTime/msDelay%2));
}
}
public class Lamp extends Circuit {
public Lamp(int x, int y) {
super(x, y, 30, 30, 1, 0, "LAMP");
}
@Override
public void display() {
stroke(NODE_STROKE_COLOR);
if(getInput(0)) fill(SPLINE_ELECTRICITY_COLOR,200);
else fill(NODE_FILL_COLOR,200);
ellipse(getX(),getY(),30,30);
if( super.splines[0] != null) super.splines[0].display(getX(),getY());
}
@Override
public void highlight(){
noStroke();
fill(NODE_SELECTED_COLOR);
ellipse(getX(),getY(),30,30);
}
@Override
public boolean inNode(int x,int y){
return (x-getX())*(x-getX())+(y-getY())*(y-getY())<= 30;
}
public void calcOutput(int index) {}
}