-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Example.java
178 lines (170 loc) · 7.21 KB
/
Example.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
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Oleg Kurbatov ([email protected])
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
package org.firmata4j;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import org.firmata4j.firmata.FirmataDevice;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import jssc.SerialNativeInterface;
import jssc.SerialPortList;
import org.firmata4j.ui.JPinboard;
/**
* Example of usage {@link JPinboard}.
*
* @author Oleg Kurbatov <[email protected]>
*/
public class Example {
private static final JFrame INITIALIZATION_FRAME = new JFrame();
public static void main(String[] args) throws IOException {
try { // set look and feel
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
Logger.getLogger(Example.class.getName()).log(Level.SEVERE, "Cannot load system look and feel.", ex);
}
// requesting a user to define the port name
String port = requestPort();
final IODevice device = new FirmataDevice(port);
showInitializationMessage();
device.start();
try {
device.ensureInitializationIsDone();
} catch (InterruptedException e) {
JOptionPane.showMessageDialog(INITIALIZATION_FRAME, e.getMessage(), "Connection error", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
hideInitializationWindow();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame mainFrame = new JFrame("Pinboard Example");
GridBagLayout layout = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
mainFrame.setLayout(layout);
constraints.gridy = 0;
constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = 1;
constraints.weighty = 1;
JPinboard pinboard = new JPinboard(device);
layout.setConstraints(pinboard, constraints);
mainFrame.add(pinboard);
mainFrame.pack();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
try {
device.stop();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
super.windowClosing(e);
}
});
mainFrame.setVisible(true);
}
});
}
@SuppressWarnings("unchecked")
private static String requestPort() {
JComboBox<String> portNameSelector = new JComboBox<>();
portNameSelector.setModel(new DefaultComboBoxModel<String>());
String[] portNames;
if (SerialNativeInterface.getOsType() == SerialNativeInterface.OS_MAC_OS_X) {
// for MAC OS default pattern of jssc library is too restrictive
portNames = SerialPortList.getPortNames("/dev/", Pattern.compile("tty\\..*"));
} else {
portNames = SerialPortList.getPortNames();
}
for (String portName : portNames) {
portNameSelector.addItem(portName);
}
if (portNameSelector.getItemCount() == 0) {
JOptionPane.showMessageDialog(null, "Cannot find any serial port", "Error", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
panel.add(new JLabel("Port "));
panel.add(portNameSelector);
if (JOptionPane.showConfirmDialog(null, panel, "Select the port", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
return portNameSelector.getSelectedItem().toString();
} else {
System.exit(0);
}
return "";
}
private static void showInitializationMessage() {
try {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
JFrame frame = INITIALIZATION_FRAME;
frame.setUndecorated(true);
JLabel label = new JLabel("Connecting to device");
label.setHorizontalAlignment(JLabel.CENTER);
frame.add(label);
frame.pack();
frame.setSize(frame.getWidth() + 40, frame.getHeight() + 40);
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int) ((dimension.getWidth() - frame.getWidth()) / 2);
int y = (int) ((dimension.getHeight() - frame.getHeight()) / 2);
frame.setLocation(x, y);
frame.setVisible(true);
}
});
} catch (InterruptedException | InvocationTargetException ex) {
throw new RuntimeException(ex);
}
}
private static void hideInitializationWindow() {
try {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
INITIALIZATION_FRAME.setVisible(false);
INITIALIZATION_FRAME.dispose();
}
});
} catch (InterruptedException | InvocationTargetException ex) {
throw new RuntimeException(ex);
}
}
}