-
Notifications
You must be signed in to change notification settings - Fork 0
/
GUI.java
305 lines (276 loc) · 8.13 KB
/
GUI.java
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
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Collections;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
//Set up GUI
public class GUI {
/** Member Variables **/
final static boolean shouldFill = true;
private static JButton getData, run;
private static JTextArea logs,logs2;
private static Vector<String> availableChannels=new Vector<String>();
private static JComboBox triggerChannel;
static PrintWriter pw;
static JFileChooser path = new JFileChooser();
/** Set up the GUI **/
public static void addComponentsToPane(final Container pane) {
pane.setPreferredSize(new Dimension(800,800));
pane.setLayout(new GridBagLayout());
pane.setBackground(Color.black);
GridBagConstraints c = new GridBagConstraints();
if (shouldFill) {
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
}
JLabel ComPort = new JLabel("COM Port: ");
JLabel COM = new JLabel("COM1");
JLabel Connected = new JLabel("Connected: ");
JLabel isConnected = new JLabel("False");
JLabel channels = new JLabel("Select Channels:");
final JCheckBox ch1 = new JCheckBox("Ch1");
final JCheckBox ch2 = new JCheckBox("Ch2");
final JCheckBox ch3 = new JCheckBox("Ch3");
final JCheckBox ch4 = new JCheckBox("Ch4");
run = new JButton("Run");
getData = new JButton("Get Data");
JButton choosePath = new JButton("Save Location");
logs = new JTextArea();
logs2 = new JTextArea();
c.insets= new Insets(5,35,5,35);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(ComPort, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 0;
pane.add(COM, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;
pane.add(Connected, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 1;
pane.add(isConnected, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 5;
c.gridwidth = 2;
pane.add(run, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 5;
c.gridwidth = 2;
pane.add(choosePath, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 6;
c.gridwidth = 2;
pane.add(getData, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 0;
pane.add(channels, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 1;
c.gridwidth = 1;
pane.add(ch1, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 2;
c.gridwidth = 1;
pane.add(ch2, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 3;
c.gridwidth = 1;
pane.add(ch3, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 4;
c.gridwidth = 1;
pane.add(ch4, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 8;
c.gridwidth = 4;
pane.add(logs, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 9;
c.gridwidth = 4;
pane.add(logs2, c);
for(Component comp : pane.getComponents()){
comp.setBackground(Color.black);
comp.setForeground(Color.white);
}
run.setForeground(Color.blue);
run.setBackground(Color.white);
getData.setForeground(Color.blue);
getData.setBackground(Color.white);
getData.setEnabled(false);
//Initialize the connection, and set to 2 byte data stream
run.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
try {
run.setEnabled(false);
new brains(availableChannels);
} catch (FileNotFoundException e) {
printException(e);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
Thread.sleep(250);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
choosePath.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
path.setDialogTitle("Specify a file to save");
int userSelection = path.showSaveDialog(pane);
if (userSelection == JFileChooser.APPROVE_OPTION) {
File fileToSave = path.getSelectedFile();
//System.out.println("Save as file: " + fileToSave.getAbsolutePath());
convert.savePath = fileToSave.getAbsolutePath();
}
}
});
//Get the data from the O-Scope from the specific channels
getData.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
try {
pw = new PrintWriter(new FileWriter("Data.txt", true));
brains.getData();
Thread.sleep(1000);
pw.close();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
/** Add the channels to the list when they're being checked. **/
ch1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
if(ch1.isSelected()){
availableChannels.add("ch1");
}
else
availableChannels.remove("ch1");
Collections.sort(availableChannels);
}
});
ch2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
if(ch2.isSelected()){
availableChannels.add("ch2");
}
else
availableChannels.remove("ch2");
Collections.sort(availableChannels);
}
});
ch3.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
if(ch3.isSelected()){
availableChannels.add("ch3");
}
else
availableChannels.remove("ch3");
Collections.sort(availableChannels);
}
});
ch4.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
if(ch4.isSelected()){
availableChannels.add("ch4");
}
else
availableChannels.remove("ch4");
Collections.sort(availableChannels);
}
});
}
//Setters and getters for the main program
/** Print errors to the on-screen logs **/
public static void printException(Exception e){
Writer writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
e.printStackTrace(printWriter);
String s = writer.toString();
logs.append(s + "\n");
}
/** Print the data to the on-screen logs **/
public static void print(String s){
if(!s.isEmpty())
logs.append(s + "\n");
}
/** Print the data to the file **/
public static void printRx(String s){
if(!s.isEmpty()){
pw.flush();
pw.append(s);
}
}
//Enable the getData button
public static void enableGetData(){getData.setEnabled(true);}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
public static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Automated Oscilloscope Program");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
addComponentsToPane(frame.getContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
}
/** Program start **/
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
try {
( new SerialStuff() ).connect();
} catch( Exception e ) {
printException(e);
}
}
});
}
}