-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindow.java
More file actions
284 lines (231 loc) · 7.48 KB
/
Window.java
File metadata and controls
284 lines (231 loc) · 7.48 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
import com.fazecast.jSerialComm.SerialPort;
import com.github.strikerx3.jxinput.XInputDevice14;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Arrays;
public class Window extends JFrame {
private boolean closeRequested = false;
private SerialPort serialPort;
private boolean vibrate = false;
private int waitTime = 5;
private int controller = 0;
private XInputDevice14 device;
private JLabel error, error2, sliderDes, sliderDes2, controllerInfo, vibrationDes, comPortDes, portDes, contentDes, allPorts, baudRateDes;
private JPanel content;
private JSlider slider, slider2;
private JButton toggleVibration, update, switchPort;
private JTextField comPort, baudRate;
public Window() {
super("Controller -> Bluetooth");
this.setSize(400, 420);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
closeRequested = true;
}
});
content = new JPanel(null);
this.setContentPane(content);
error = new JLabel("");
error.setForeground(Color.RED);
addContent(error, 0);
error2 = new JLabel("");
error2.setForeground(Color.RED);
addContent(error2, 1);
sliderDes = new JLabel("Wartezeit zwischen Controller Updates (in ms): " + waitTime);
addContent(sliderDes, 3);
slider = new JSlider(0, 200);
slider.setValue(0);
addContent(slider, 4);
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
waitTime = slider.getValue();
sliderDes.setText("Wartezeit zwischen Controller Updates (in ms): " + waitTime);
}
}
);
sliderDes2 = new JLabel("Controller-ID: " + controller);
addContent(sliderDes2, 5);
slider2 = new JSlider(0, 15);
slider2.setValue(0);
addContent(slider2, 6);
slider2.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e2) {
controller = slider2.getValue();
sliderDes2.setText("Controller-ID: " + controller);
try {
device = XInputDevice14.getDeviceFor(controller);
} catch (Exception e) {
printError(e.getMessage());
}
}
});
controllerInfo = new JLabel("");
addContent(controllerInfo, 7);
vibrationDes = new JLabel("Controller-Vibration: " + (vibrate ? "Ein" : "Aus"));
addContent(vibrationDes, 8);
toggleVibration = new JButton("Controller Vibration umschalten");
addContent(toggleVibration, 9);
toggleVibration.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
vibrate = !vibrate;
vibrationDes.setText("Controller-Vibration: " + (vibrate ? "Ein" : "Aus"));
}
});
comPortDes = new JLabel("Serieller Port:");
addContent(comPortDes, 11);
comPort = new JTextField();
addContent(comPort, 12);
switchPort = new JButton("Port auswählen");
addContent(switchPort, 15);
switchPort.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (serialPort.isOpen()) serialPort.closePort();
serialPort = SerialPort.getCommPort(comPort.getText());
if (!serialPort.isOpen()) serialPort.openPort();
int baud = 9600;
try {
baud = Integer.valueOf(baudRate.getText());
} catch (Exception e2) {
baud = 9600;
}
serialPort.setBaudRate(baud);
}
});
portDes = new JLabel("");
addContent(portDes, 16);
baudRateDes = new JLabel("Baudrate:");
addContent(baudRateDes, 13);
baudRate = new JTextField("9600");
addContent(baudRate, 14);
contentDes = new JLabel("");
addContent(contentDes, 17);
StringBuilder ports = new StringBuilder("Verfügbare Ports: ");
for (SerialPort port: SerialPort.getCommPorts()) {
ports.append(port.getSystemPortName());
ports.append(" ");
}
allPorts = new JLabel(ports.toString());
addContent(allPorts, 19);
update = new JButton("Ports suchen");
addContent(update, 20);
update.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
StringBuilder ports = new StringBuilder("Verfügbare Ports: ");
for (SerialPort port: SerialPort.getCommPorts()) {
ports.append(port.getSystemPortName());
ports.append(" ");
}
allPorts.setText(ports.toString());
}
});
this.setVisible(true);
Insets i = getInsets();
this.setSize(400 + i.left + i.right, 420 + i.top + i.bottom);
if (!XInputDevice14.isAvailable()) {
printError("XInputDevice not available");
}
try {
device = XInputDevice14.getDeviceFor(controller);
} catch (Exception e) {
printError(e.getMessage());
}
serialPort = SerialPort.getCommPort("");
}
private void addContent(JComponent component, int id) {
content.add(component);
component.setBounds(0, 20*id, 400, 20);
}
public void loop() {
tripleVibrate();
while (!closeRequested) {
try {
Thread.sleep(waitTime);
} catch (Exception e) { }
if (device.poll()) {
if (device.getComponents().getButtons().a && vibrate) {
device.setVibration(35665, 35665);
} else {
device.setVibration(0, 0);
}
float dx = device.getComponents().getAxes().lx;
float dy = device.getComponents().getAxes().ly;
double deg = Math.atan2(dx, dy);
if (deg < 0) {
deg += 2*Math.PI;
}
int a = bounds((int) (Math.toDegrees(deg) / 2), 0, 179);
int pow = bounds((int) (Math.min(1, Math.sqrt(dx*dx+dy*dy)) * 255), 0, 254);
int shot = device.getComponents().getButtons().a ? 1 : 2;
byte[] message = new byte[] {(byte) 0xff, (byte)((a+pow+shot) % 254 + 1), (byte)a, (byte)pow, (byte)shot};
controllerInfo.setText(String.format("Winkel: %d, Power: %d, Schuss: %d", a, pow, shot));
error.setText("");
portDes.setText(String.format("Port: %s, Rate: %d, Verfügbar: %b", serialPort.getSystemPortName(), serialPort.getBaudRate(), serialPort.getOutputStream() != null));
if (serialPort.getOutputStream() == null) {
error2.setText("Serieller Port nicht erreichbar");
continue;
}
try {
contentDes.setText(String.format("Gesendete Daten: %d, %d, %d, %d, %d", 0xff & message[0], 0xff & message[1], 0xff & message[2], 0xff & message[3], 0xff & message[4]));
serialPort.getOutputStream().write(message);
serialPort.getOutputStream().flush();
} catch (Exception e) {
error2.setText("Fehler beim senden der Daten");
continue;
}
error2.setText("");
} else {
printError("Kein controller verbunden");
}
}
serialPort.closePort();
}
private int bounds(int val, int min, int max) {
if (val < min) return min;
if (val > max) return max;
return val;
}
private void tripleVibrate() {
device.setVibration(35665, 35665);
try {
Thread.sleep(125);
} catch (Exception e) {}
device.setVibration(0, 0);
try {
Thread.sleep(125);
} catch (Exception e) {}
device.setVibration(35665, 35665);
try {
Thread.sleep(125);
} catch (Exception e) {}
device.setVibration(0, 0);
try {
Thread.sleep(125);
} catch (Exception e) {}
device.setVibration(35665, 35665);
try {
Thread.sleep(125);
} catch (Exception e) {}
device.setVibration(0, 0);
}
private void printError(String message) {
error.setText(message);
}
public static void main(String[] args) {
Window window = new Window();
window.loop();
}
}