-
Notifications
You must be signed in to change notification settings - Fork 7
/
Initiator.pde
154 lines (135 loc) · 4.02 KB
/
Initiator.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
public class Initiator extends Cell implements TimerSubscriber {
private static final int RHYTHMICITY = 1;
private static final int BURSTINESS = 2;
private static final int FREQUENCY = 3;
private float fRhythmicity, fFreq;
private int fBurstiness, fType;
private CircularSlider fRhythmicitySlider, fFreqSlider, fBurstinessSlider;
private Timer fTimer;
private int[] fFiringQueue;
public Initiator(float x, float y, float size, color cc) {
this(x, y, size, cc, DEFAULT_RHYTHMICITY,
DEFAULT_BURSTINESS, DEFAULT_FREQUENCY);
}
public Initiator(float x, float y, float size, color cc, float rhythmicity, int burstiness, float frequency) {
super(x, y, size, cc);
fRhythmicity = rhythmicity;
fBurstiness = burstiness;
fFreq = frequency;
fType = EXCITATORY;
fFiringQueue = new int[0]; // Using append() and subset() is probably inefficient
// Add controls
float controlSize = fSize + 3 * SLIDER_BAR_WIDTH;
fRhythmicitySlider = new CircularSlider(
fLoc.x, fLoc.y, controlSize,
0, TWO_PI/3,
fRhythmicity, 0, MAX_RHYTHMICITY,
RHYTHMICITY, this
);
fControls.add(fRhythmicitySlider);
fBurstinessSlider = new DiscreteCircularSlider(
fLoc.x, fLoc.y, controlSize,
TWO_PI/3, 2 * TWO_PI/3,
fBurstiness, 1, MAX_BURSTINESS,
BURSTINESS, this
);
fControls.add(fBurstinessSlider);
fFreqSlider = new CircularSlider(
fLoc.x, fLoc.y, controlSize,
2 * TWO_PI/3, TWO_PI,
fFreq, DEFAULT_FREQUENCY, MAX_FREQUENCY,
FREQUENCY, this
);
fControls.add(fFreqSlider);
fTimer = new Timer(this, round(1000/fFreq), 0.5);
fTimer.reset();
}
public int getType() {
return INITIATOR;
}
protected boolean fireSignals() {
for (Path p : fAxons)
p.addSignal(new ActionPotential(
SIGNAL_DEFAULT_SPEED,
SIGNAL_DEFAULT_LENGTH,
SIGNAL_DEFAULT_DECAY,
SIGNAL_DEFAULT_STRENGTH,
p));
return true;
}
public void onTimerFiring(int id, int time) {
if (!fTimer.throttled() && random(1.0) <= fRhythmicity) {
for (int i = 0; i < fBurstiness; i+=1) {
// resulting in fBurstiness - 1 bursts
fFiringQueue = append(fFiringQueue, time + i * BURST_DELAY);
}
}
}
public void update() {
fTimer.update();
int time = millis();
if (fTimer.ended()) fTimer.reset();
if (fFiringQueue.length > 0 && fFiringQueue[0] <= time) {
fire();
fFiringQueue = subset(fFiringQueue, 1);
}
}
public void drawBackground() {
pushStyle();
drawControls();
float s = fSize - SOMA_RING_WIDTH;
fill(fColor);
noStroke();
ellipse(fLoc.x, fLoc.y, s, s);
color c = SHADOW_COLOR;
ring(s, fLoc.x + SHADOW_OFFSETX, fLoc.y + SHADOW_OFFSETY, SOMA_RING_WIDTH, c);
popStyle();
}
public void drawForeground() {
pushStyle();
float s = fSize - SOMA_RING_WIDTH;
color c;
noStroke();
if (!fTimer.ended()) {
c = lerpColor(fHighlightColor, HIGHLIGHT_COLOR,
1.0 - 2*abs(fTimer.getProgress() - 0.5));
}
else {
c = fHighlightColor;
}
ring(s, fLoc.x, fLoc.y, SOMA_RING_WIDTH, c);
popStyle();
}
public void flipColor() {
super.flipColor();
fType ^= 1; // Flip between EXCITATORY and INHIBITORY
}
public void copyAttributes(Cell c) {
Initiator s = (Initiator)c;
fRhythmicity = s.fRhythmicity;
fRhythmicitySlider.setValue(fRhythmicity);
fBurstiness = s.fBurstiness;
fBurstinessSlider.setValue(fBurstiness);
fFreq = s.fFreq;
fFreqSlider.setValue(fFreq);
}
public void onEvent(int controlID, float value) {
switch (controlID) {
case RHYTHMICITY:
fRhythmicity = value;
break;
case BURSTINESS:
fBurstiness = (int)value;
break;
case FREQUENCY:
fFreq = value;
fTimer.setLength(round(1000/fFreq));
break;
default:
break;
}
}
public void onSignal(Signal s) {
println("Something's wrong! Initiator is getting a signal!");
}
}