-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecg4_mac.txt
279 lines (257 loc) · 8.06 KB
/
ecg4_mac.txt
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
/*
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import processing.serial.*;
import processing.core.PApplet;
javax.swing.JFrame frame;
java.awt.Canvas canvas;
JTextField outputFld;
int _wndW = 1050;
int _wndH = 400;
int _fntSize = 13;
int _btnH = 24;
float x, y, y1;
float inByte = 0;
Serial myPort;
int selectedIndex;
String portName;
boolean connected = false;
boolean saveToDesktop = false;
PApplet sketch = this;
String outputStr = "";
void deviceList(int x, int y, int w, int h) {
String device[] = Serial.list();
JComboBox devices = new JComboBox(device);
devices.setBounds(x, y, w, h);
devices.setFont(new Font("Lucida Grande", Font.PLAIN, _fntSize));
devices.setToolTipText("Devices");
frame.add(devices);
devices.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
JComboBox comboBox = (JComboBox) evt.getSource();
selectedIndex = comboBox.getSelectedIndex();
portName = Serial.list()[selectedIndex];
myPort = new Serial(sketch, portName, 9600);
connected = true;
}
}
);
frame.setVisible(true);
devices.repaint();
}
void disconnectBtn(int x, int y, int w, int h) {
JButton btn = new JButton("Disconnect");
btn.setBounds(x, y, w, h);
btn.setFont(new Font("Lucida Grande", Font.PLAIN, _fntSize));
frame.add(btn);
// **** Action **** //
btn.addActionListener( new ActionListener() {
void actionPerformed(ActionEvent actionEvent) {
if (connected) {
myPort.stop();
connected = false;
}
}
}
);
btn.repaint();
}
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;
}
void scrnShotBtn(int x, int y, int w, int h) {
JButton btn = new JButton("ScrnShot");
btn.setBounds(x, y, w, h);
btn.setFont(new Font("Lucida Grande", Font.PLAIN, _fntSize));
btn.setToolTipText("Screen shot to be saved in existing folder.");
frame.add(btn);
// **** Action **** //
btn.addActionListener( new ActionListener() {
void actionPerformed(ActionEvent actionEvent) {
String dateTimeStr = getDateTime();
String scrnShotStr = dateTimeStr+".png";
if (outputStr.length() > 0) {
save(outputStr + "/" + scrnShotStr);
JOptionPane.showMessageDialog(frame, "Image saved to designated output folder.");
} else if (saveToDesktop == true) {
String desktopPathStr = System.getProperty("user.home") + "/Desktop/ECGs/";
save(desktopPathStr + scrnShotStr);
JOptionPane.showMessageDialog(frame, "Image saved to Desktop/ECGs folder.");
} else {
save(scrnShotStr); // Save to sketch( or Application) folder
JOptionPane.showMessageDialog(frame, "Image saved to sketch (or Application) folder.");
}
}
}
);
btn.repaint();
}
void selectFolderBtn(int x, int y, int w, int h) {
JButton btn = new JButton("Select folder...");
btn.setBounds(x, y, w, h);
btn.setFont(new Font("Lucida Grande", Font.PLAIN, _fntSize));
btn.setToolTipText("Select existing folder for screen shot.");
frame.add(btn);
// **** Action **** //
btn.addActionListener( new ActionListener() {
void actionPerformed(ActionEvent actionEvent) {
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("Select folder for screen shot.");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
File dir = chooser.getCurrentDirectory();
outputStr = dir + "/" + file.getName();
outputFld.setText(outputStr);
} else {
println("Open command canceled");
}
}
}
);
btn.repaint();
}
void outputFld(int x, int y, int w, int h) {
outputFld = new JTextField("");
outputFld.setBounds(x, y, w, h);
outputFld.setFont(new Font("Lucida Grande", Font.PLAIN, _fntSize));
frame.add(outputFld);
// **** Action **** //
outputFld.addActionListener( new ActionListener() {
void actionPerformed(ActionEvent actionEvent) {
outputStr = outputFld.getText();
}
}
);
outputFld.repaint(); // To make it visible after export application.
}
void clearBtn(int x, int y, int w, int h) {
JButton btn = new JButton("X");
btn.setBounds(x, y, w, h);
btn.setToolTipText("Clear output field.");
btn.setFont(new Font("Lucida Grande", Font.PLAIN, _fntSize));
frame.add(btn);
// **** Action **** //
btn.addActionListener( new ActionListener() {
void actionPerformed(ActionEvent actionEvent) {
outputFld.setText("");
outputStr = "";
}
}
);
btn.repaint();
}
void desktopRadioBtn(int x, int y, int w, int h) {
String desktopPath = System.getProperty("user.home") + "/Desktop/ECGs";
String titleStr = "Save to desktop: ";
JRadioButton radioBtn = new JRadioButton(titleStr + desktopPath);
radioBtn.setBounds(x, y, w, h);
radioBtn.setFont(new Font("Lucida Grande", Font.PLAIN, _fntSize));
radioBtn.setToolTipText("Save screen shot to existing desktop folder.");
frame.add(radioBtn);
radioBtn.addItemListener( new ItemListener() {
void itemStateChanged(ItemEvent evnt) {
if (evnt.getStateChange()==1) {
saveToDesktop = true;
} else {
saveToDesktop = false;
}
}
}
);
radioBtn.repaint();
}
void quitBtn(int x, int y, int w, int h) {
JButton btn = new JButton("Quit");
btn.setBounds(x, y, w, h);
btn.setFont(new Font("Lucida Grande", Font.PLAIN, _fntSize));
frame.add(btn);
// **** Action **** //
btn.addActionListener( new ActionListener() {
void actionPerformed(ActionEvent actionEvent) {
exit();
}
}
);
}
void buildWnd() {
deviceList(10, 15, 270, _btnH);
disconnectBtn(280, 15, 100, _btnH);
scrnShotBtn(380, 15, 90, _btnH);
selectFolderBtn(470, 15, 120, _btnH);
outputFld(595, 10, 310, _btnH);
clearBtn(905, 10, 50, _btnH);
desktopRadioBtn(595, 35, 380, _btnH);
quitBtn(_wndW - 80, 15, 60, _btnH);
}
void setup() {
size(_wndW, _wndH);
surface.setTitle("ECG");
frame = (javax.swing.JFrame) ((processing.awt.PSurfaceAWT.SmoothCanvas) surface.getNative()).getFrame();
canvas = (processing.awt.PSurfaceAWT.SmoothCanvas) ((processing.awt.PSurfaceAWT)surface).getNative();
frame.setBounds(600, 300, _wndW, _wndH); // Makes it possible to add swing components
canvas.setBounds(0, 70, _wndW, _wndH - 70);
background(255);
stroke(color(255, 0, 0));
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
buildWnd(); // Builds components on EventDispatchThread
}
}
);
}
void draw () {
if (inByte > 0) {
inByte = map(inByte, 0, 1023, 0, height); // map to screen height
}
y1 = height - inByte;
line(x - 1, y, x, y1);
y = y1;
if (x >= width) {
x = 0; // at edge of screen -> back to beginning
background(255);
} else {
x++; // increment horizontal position:
}
}
void serialEvent( Serial myPort) {
//'\n' is the end delimiter indicating the end of a complete packet
String inStr = myPort.readStringUntil('\n');
//make sure our data isn't empty before continuing
if (inStr != null) {
//trim whitespace and formatting characters (like line feed, carriage return)
inStr = trim(inStr);
inByte = float(inStr);
}
}
// **** Arduino Code **** //
/*
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(analogRead(A5));
//Wait for a bit to keep serial data from saturating
delay(1);
}
*/