-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathW_MarkerMode.pde
287 lines (222 loc) · 8.21 KB
/
W_MarkerMode.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
////////////////////////////////////////////////////
//
// W_MarkerMode is used to put the board into marker mode
// by Gerrie van Zyl
// Basd on W_Analogread by AJ Keller
//
//
///////////////////////////////////////////////////,
class W_MarkerMode extends Widget {
//to see all core variables/methods of the Widget class, refer to Widget.pde
//put your custom variables here...
// color boxBG;
color graphStroke = #d2d2d2;
color graphBG = #f5f5f5;
color textColor = #000000;
color strokeColor;
// Accelerometer Stuff
int MarkerBuffSize = 500; //points registered in accelerometer buff
int padding = 30;
// bottom xyz graph
int MarkerWindowWidth;
int MarkerWindowHeight;
int MarkerWindowX;
int MarkerWindowY;
color eggshell;
color Xcolor;
float yMaxMin;
float currentXvalue;
int[] X;
int lastMarker=0;
int localValidLastMarker;
float dummyX;
// for the synthetic markers
float synthTime;
int synthCount;
boolean OBCI_inited= true;
Button markerModeButton;
W_MarkerMode(PApplet _parent){
super(_parent); //calls the parent CONSTRUCTOR method of Widget (DON'T REMOVE)
// boxBG = bgColor;
strokeColor = color(138, 146, 153);
// Marker Sensor Stuff
eggshell = color(255, 253, 248);
Xcolor = color(224, 56, 45);
setGraphDimensions();
// The range of markers
yMaxMin = 256;
// XYZ buffer for bottom graph
X = new int[MarkerBuffSize];
// for synthesizing values
synthTime = 0.0;
markerModeButton = new Button((int)(x + 3), (int)(y + 3 - navHeight), 120, navHeight - 6, "Turn MarkerMode On", 12);
markerModeButton.setCornerRoundess((int)(navHeight-6));
markerModeButton.setFont(p6,10);
markerModeButton.setColorNotPressed(color(57,128,204));
markerModeButton.textColorNotActive = color(255);
markerModeButton.hasStroke(false);
markerModeButton.setHelpText("Click this button to activate/deactivate the MarkerMode of your Cyton board!");
}
public void initPlayground(Cyton _OBCI) {
OBCI_inited = true;
}
void update(){
super.update(); //calls the parent update() method of Widget (DON'T REMOVE)
localValidLastMarker = hub.validLastMarker; // make a local copy so it can be manipulated in SYNTHETIC mode
hub.validLastMarker = 0;
if (eegDataSource == DATASOURCE_SYNTHETIC) {
localValidLastMarker = synthesizeMarkerData();
}
if (eegDataSource == DATASOURCE_CYTON || eegDataSource == DATASOURCE_SYNTHETIC) {
if (isRunning && cyton.getBoardMode() == BOARD_MODE_MARKER) {
if (localValidLastMarker > 0){
lastMarker = localValidLastMarker; // this holds the last marker for the display
}
X[X.length-1] =
int(map(logScaleMarker(localValidLastMarker), 0, yMaxMin, float(MarkerWindowY+MarkerWindowHeight), float(MarkerWindowY)));
X[X.length-1] = constrain(X[X.length-1], MarkerWindowY, MarkerWindowY+MarkerWindowHeight);
shiftWave();
}
} else { // playback data
currentXvalue = accelerometerBuff[0][accelerometerBuff[0].length-1];
}
}
void draw(){
super.draw(); //calls the parent draw() method of Widget (DON'T REMOVE)
pushStyle();
//put your code here...
//remember to refer to x,y,w,h which are the positioning variables of the Widget class
if (true) {
fill(50);
textFont(p4, 14);
textAlign(CENTER,CENTER);
fill(graphBG);
stroke(graphStroke);
rect(MarkerWindowX, MarkerWindowY, MarkerWindowWidth, MarkerWindowHeight);
line(MarkerWindowX, MarkerWindowY + MarkerWindowHeight/2, MarkerWindowX+MarkerWindowWidth, MarkerWindowY + MarkerWindowHeight/2); //midline
fill(50);
textFont(p5, 12);
textAlign(CENTER,CENTER);
text((int)yMaxMin, MarkerWindowX+MarkerWindowWidth + 12, MarkerWindowY);
text((int)16, MarkerWindowX+MarkerWindowWidth + 12, MarkerWindowY + MarkerWindowHeight/2);
text("0", MarkerWindowX+MarkerWindowWidth + 12, MarkerWindowY + MarkerWindowHeight);
fill(graphBG); // pulse window background
stroke(graphStroke);
stroke(180);
fill(50);
textFont(p3, 16);
if (eegDataSource == DATASOURCE_CYTON || eegDataSource == DATASOURCE_SYNTHETIC) { // LIVE
markerModeButton.draw();
drawMarkerValues();
drawMarkerWave();
}
else { // PLAYBACK
drawMarkerValues();
drawMarkerWave2();
}
}
popStyle();
}
void setGraphDimensions(){
MarkerWindowWidth = w - padding*2;
MarkerWindowHeight = int((float(h) - float(padding*3)));
MarkerWindowX = x + padding;
MarkerWindowY = y + h - MarkerWindowHeight - padding;
}
void screenResized(){
int prevX = x;
int prevY = y;
int prevW = w;
int prevH = h;
super.screenResized(); //calls the parent screenResized() method of Widget (DON'T REMOVE)
int dy = y - prevY;
println("dy = " + dy);
//put your code here...
println("Acc Widget -- Screen Resized.");
setGraphDimensions();
//empty arrays to start redrawing from scratch
for (int i=0; i<X.length; i++) { // initialize the accelerometer data
X[i] = MarkerWindowY + MarkerWindowHeight; // X at 1/4
}
markerModeButton.setPos((int)(x + 3), (int)(y + 3 - navHeight));
}
void mousePressed(){
super.mousePressed(); //calls the parent mousePressed() method of Widget (DON'T REMOVE)
if (markerModeButton.isMouseHere()) {
markerModeButton.setIsActive(true);
}
}
void mouseReleased(){
super.mouseReleased(); //calls the parent mouseReleased() method of Widget (DON'T REMOVE)
//put your code here...
if(markerModeButton.isActive && markerModeButton.isMouseHere()){
// println("markerModeButton...");
if((cyton.isPortOpen() && eegDataSource == DATASOURCE_CYTON) || eegDataSource == DATASOURCE_SYNTHETIC) {
if (cyton.getBoardMode() != BOARD_MODE_MARKER) {
cyton.setBoardMode(BOARD_MODE_MARKER);
output("Starting to read markers");
markerModeButton.setString("Turn Marker Off");
} else {
cyton.setBoardMode(BOARD_MODE_DEFAULT);
output("Starting to read accelerometer");
markerModeButton.setString("Turn Marker On");
}
}
}
markerModeButton.setIsActive(false);
}
//add custom classes functions here
void drawMarkerValues() {
textAlign(LEFT,CENTER);
textFont(h1,20);
fill(Xcolor);
text("Last Marker = " + lastMarker, x+padding , y + (h/12)*1.5);
}
void shiftWave() {
for (int i = 0; i < X.length-1; i++) { // move the pulse waveform by
X[i] = X[i+1];
}
}
void drawMarkerWave() {
noFill();
strokeWeight(2);
beginShape(); // using beginShape() renders fast
stroke(Xcolor);
for (int i = 0; i < X.length; i++) {
// int xi = int(map(i, 0, X.length-1, 0, MarkerWindowWidth-1));
// vertex(MarkerWindowX+xi, X[i]); //draw a line connecting the data points
int xi = int(map(i, 0, X.length-1, 0, MarkerWindowWidth-1));
// int yi = int(map(X[i], yMaxMin, -yMaxMin, 0.0, MarkerWindowHeight-1));
// int yi = 2;
vertex(MarkerWindowX+xi, X[i]); //draw a line connecting the data points
}
endShape();
}
void drawMarkerWave2() {
noFill();
strokeWeight(1);
beginShape(); // using beginShape() renders fast
stroke(Xcolor);
for (int i = 0; i < accelerometerBuff[0].length; i++) {
int x = int(map(accelerometerBuff[0][i], -yMaxMin, yMaxMin, float(MarkerWindowY+MarkerWindowHeight), float(MarkerWindowY))); // ss
x = constrain(x, MarkerWindowY, MarkerWindowY+MarkerWindowHeight);
vertex(MarkerWindowX+i, x); //draw a line connecting the data points
}
endShape();
}
int synthesizeMarkerData() {
synthTime += 0.02;
int valueMarker;
if (synthCount++ > 10){
valueMarker = int((sin(synthTime) +1.0)*127.);
synthCount = 0;
} else {
valueMarker = 0;
}
return valueMarker;
}
int logScaleMarker( float value ) {
// this returns log value between 0 and yMaxMin for a value between 0. and 255.
return int(log(int(value)+1.0)*yMaxMin/log(yMaxMin+1));
}
};