-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathecg_1.java
More file actions
364 lines (315 loc) · 7.81 KB
/
ecg_1.java
File metadata and controls
364 lines (315 loc) · 7.81 KB
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import processing.core.*;
import processing.data.*;
import processing.event.*;
import processing.opengl.*;
import processing.serial.*;
import controlP5.*;
import java.util.*;
import java.util.HashMap;
import java.util.ArrayList;
import java.io.File;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
public class ecg_1 extends PApplet {
Serial port;
PrintWriter output;
ControlP5 cp5;
Textarea valueFld;
Textlabel logFld;
PFont font;
final int fntSize = 11;
final int menuBarW = 65;
int gray = color(0, 160, 100);
String[] data;
String fileName;
String portName;
int baudRate;
int[] baud = {9600, 14400, 19200, 28800, 38400, 57600, 115200};
int itemSelected;
int index = 0;
int counter = 0;
int count = 0;
int inByte = 0;
float yIn;
float x1, y1, x2, y2;
float x3, y3, x4, y4;
int BLUE = color(12, 16, 255);
boolean connected = false;
boolean showGraph = false;
boolean showText = false;
int[] y = new int[0];
public String getDateTime()
{
int s = second();
int m = minute();
int h = hour();
int day = day();
int mo = month();
int yr = year();
// Avoid slashes which create folders
String date = nf(mo,2)+nf(day,2)+yr+"_";
String time = nf(h,2)+nf(m,2)+nf(s,2);
String dateTime = date+time;
return dateTime;
}
public void setup() {
if (frame != null) {
surface.setResizable(true);
}
background(BLUE);
cp5 = new ControlP5(this);
font = createFont("SourceCodePro-Regular.tif", fntSize);
String[] ports = Serial.list();
List p = Arrays.asList(ports);
cp5.addScrollableList("SerialPorts")
.setPosition(10, 3)
.setSize(230, 90)
.setCaptionLabel("Serial Ports")
.setBarHeight(18)
.setItemHeight(18)
.setFont(font)
.addItems(p);
List b = Arrays.asList("9600","14400","19200","28800","38400","57600","115200");
cp5.addScrollableList("Baud")
.setPosition(250, 3)
.setSize(60,90)
.setBarHeight(18)
.setItemHeight(18)
.setFont(font)
.addItems(b);
cp5.addButton("Connect")
.setPosition(320, 3)
.setFont(font)
.setSize(85,19);
cp5.addButton("Disconnect")
.setPosition(320,23)
.setFont(font)
.setSize(85,19);
cp5.addButton("Save")
.setPosition(415,3)
.setFont(font)
.setSize(70,19)
.setCaptionLabel("Save Data");
cp5.addButton("Open")
.setPosition(415,23)
.setFont(font)
.setSize(70,19)
.setCaptionLabel("Open File");
cp5.addButton("ScreenShot")
.setPosition(495,3)
.setFont(font)
.setSize(80,19);
cp5.addButton("ClrScrn")
.setPosition(495,23)
.setFont(font)
.setSize(80,19)
.setCaptionLabel("Clr Screen");
cp5.addButton("Replay")
.setPosition(585,3)
.setFont(font)
.setSize(90,19)
.setCaptionLabel("Replay Data");
cp5.addButton("RescanPorts")
.setPosition(585,23)
.setFont(font)
.setSize(90,19)
.setCaptionLabel("Rescan Ports");
cp5.addTextlabel("Label")
.setText("Display:")
.setPosition(680,3)
.setColorValue(255)
.setFont(font);
cp5.addRadioButton("Radio")
.setPosition(685,25)
.setFont(font)
.setSize(15,15)
.setItemsPerRow(3)
.setSpacingColumn(34)
.addItem("Graph", 0)
.addItem("Text", 1)
.setColorLabel(color(255))
.activate(0);
showGraph = true;
valueFld = cp5.addTextarea("Value")
.setPosition(780,3)
.setSize(50, menuBarW - 6)
.setColorBackground(0)
.setFont(font)
.setLineHeight(14);
logFld = cp5.addTextlabel("Log")
.setPosition(320,45)
.setSize(360, 18)
.setFont(font)
.setLineHeight(14);
cp5.addButton("Quit")
.setPosition(width-60,3)
.setFont(font)
.setSize(50,19);
}
public void ClrScrn() {
background(BLUE);
}
public void SerialPorts(int n ) {
/* request selected item from Map based on index n */
portName = cp5.get(ScrollableList.class, "SerialPorts").getItem(n).get("name").toString();
logFld.setText("portSelected: "+portName);
background(BLUE);
}
public void Baud(int n ) {
baudRate = baud[n];
logFld.setText("baudRate: "+baudRate);
background(BLUE);
}
public void EmptyArray(){
y = new int[0];
}
public void Connect() {
// **** Zero out data array **** //
EmptyArray();
port = new Serial(this, portName, baudRate);
connected = true;
logFld.setText("Connect btn was hit.");
}
public void Disconnect() {
port.stop();
yIn = 0;
count = 0;
connected = false;
logFld.setText("Disconnect button was hit.");
}
public void Save() {
String dateTimeStr = getDateTime();
String fileToSave = dateTimeStr+".txt";
String[] data = new String[y.length];
for (int i = 0; i < y.length; i++) {
data[i] = y[i]+",";
}
saveStrings(fileToSave, data);
logFld.setText("Data was saved to a file in app folder.");
}
public void ScreenShot() {
String dateTimeStr = getDateTime();
String imageOut = dateTimeStr+".png";
save(imageOut);
logFld.setText("Screenshot was saved to app folder.");
}
public void RescanPorts() {
logFld.setText("Rescan ports.");
cp5.get(ScrollableList.class, "SerialPorts").clear();
String[] ports = Serial.list();
List p = Arrays.asList(ports);
cp5.get(ScrollableList.class, "SerialPorts").addItems(p);
}
public void fileSelected(File selection) {
if (selection != null) {
// reset required for multiple selections
index = 0;
counter = 0;
fileName = selection.getAbsolutePath();
logFld.setText("File selected: " + fileName);
data = loadStrings(selection.getAbsolutePath());
}
}
public void Open(){
selectInput("Select a file to process:", "fileSelected");
}
public void Replay(){
if (fileName == null) {
logFld.setText("There is no file selected.");
// To avoid null pointer exception
return;
} else {
background(BLUE);
data = loadStrings(fileName);
index = 0;
counter = 0;
}
}
public void Radio(int radioID) {
switch(radioID) {
case(0):logFld.setText("Graph selected as output.");
showGraph = true;
showText = false;
break;
case(1):logFld.setText("Text selected as output.");
showGraph = false;
showText = true;
break;
}
}
public void Quit() {
exit();
}
public void draw() {
// **** Menu Bar **** //
fill(128);
rect(0, 0, width-1, menuBarW);
// **** Graph of data from serial connection **** //
if (connected && showGraph){
if (count > width) {
count = 0;
background(BLUE);
}
if (count == 0) {
x1 = count;
y1 = yIn;
}
if (count > 0) {
x2 = count;
y2 = yIn;
stroke(255);
line(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
}
count++;
}else {
// **** Graph of saved file with comma separated values **** //
if (data != null){
if (index < data.length){
String[] pieces = split(data[index], ",");
if (counter > width){
counter = 0;
background(BLUE);
}
if (counter == 0) {
x3 = 0;
y3 = height - PApplet.parseFloat(pieces[0]);
}
if (counter > 0){
// println("["+index+"] "+pieces[0]);
x4 = counter;
y4 = height - PApplet.parseFloat(pieces[0]);
stroke(255);
line(x3, y3, x4, y4);
x3 = x4;
y3 = y4;
}
index++;
counter++;
}
}
}
}
// **** Byte data sent without frame markers **** //
// **** Commas added if data displayed and/or saved to file. **** //
public void serialEvent (Serial port) {
int inByte = port.read();
y = append(y,inByte);
String inStr = str(inByte)+",\n";
yIn = height - inByte;
if(showText == true) {valueFld.append(inStr);}
}
public void settings() { size(900, 450); }
static public void main(String[] passedArgs) {
String[] appletArgs = new String[] { "ecg_1" };
if (passedArgs != null) {
PApplet.main(concat(appletArgs, passedArgs));
} else {
PApplet.main(appletArgs);
}
}
}