[go: up one dir, main page]

File: AltosLaunchUI.java

package info (click to toggle)
altos 1.9.16-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 78,396 kB
  • sloc: ansic: 110,166; java: 42,103; makefile: 7,904; sh: 4,375; xml: 2,155; pascal: 2,008
file content (513 lines) | stat: -rw-r--r-- 12,239 bytes parent folder | download | duplicates (4)
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*
 * Copyright © 2010 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

package altosui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.text.*;
import java.util.concurrent.*;
import org.altusmetrum.altosuilib_14.*;

class FireButton extends JButton {
	protected void processMouseEvent(MouseEvent e) {
		super.processMouseEvent(e);
		switch (e.getID()) {
		case MouseEvent.MOUSE_PRESSED:
			if (actionListener != null)
				actionListener.actionPerformed(new ActionEvent(this, e.getID(), "fire_down"));
			break;
		case MouseEvent.MOUSE_RELEASED:
			if (actionListener != null)
				actionListener.actionPerformed(new ActionEvent(this, e.getID(), "fire_up"));
			break;
		}
	}

	public FireButton(String s) {
		super(s);
	}
}

public class AltosLaunchUI
	extends AltosUIDialog
	implements ActionListener
{
	AltosDevice	device;
	JFrame		owner;
	JLabel		label;

	int		radio_channel;
	JLabel		radio_channel_label;
	JTextField	radio_channel_text;

	int		launcher_serial;
	JLabel		launcher_serial_label;
	JTextField	launcher_serial_text;

	int		launcher_channel;
	JLabel		launcher_channel_label;
	JTextField	launcher_channel_text;

	JLabel		armed_label;
	JLabel		armed_status_label;
	JLabel		igniter;
	JLabel		igniter_status_label;
	JToggleButton	arm;
	FireButton	fire;
	javax.swing.Timer	arm_timer;
	javax.swing.Timer	fire_timer;

	boolean		firing;
	boolean		armed;
	int		armed_status;
	int		igniter_status;
	int		rssi;

	final static int	arm_timeout = 1 * 1000;
	final static int	fire_timeout = 250;

	int		armed_count;

	LinkedBlockingQueue<String>	command_queue;

	class LaunchHandler implements Runnable {
		AltosLaunch	launch;
		JFrame		owner;

		void send_exception(Exception e) {
			final Exception	f_e = e;
			Runnable r = new Runnable() {
					public void run() {
						launch_exception(f_e);
					}
				};
			SwingUtilities.invokeLater(r);
		}

		public void run () {
			try {
				launch = new AltosLaunch(device);
			} catch (Exception e) {
				send_exception(e);
				return;
			}
			launch.set_frame(owner);
			launch.set_remote(launcher_serial, launcher_channel);

			for (;;) {
				Runnable	r;

				try {
					String		command = command_queue.take();
					String		reply = null;

					if (command.equals("get_status")) {
						launch.status();
						reply = "status";
						armed_status = launch.armed;
						igniter_status = launch.igniter;
						rssi = launch.rssi;
					} else if (command.equals("set_remote")) {
						launch.set_remote(launcher_serial, launcher_channel);
						reply = "remote set";
					} else if (command.equals("arm")) {
						launch.arm();
						reply = "armed";
					} else if (command.equals("fire")) {
						launch.fire();
						reply = "fired";
					} else if (command.equals("quit")) {
						launch.close();
						break;
					} else {
						throw new ParseException(String.format("invalid command %s", command), 0);
					}
					final String f_reply = reply;
					r = new Runnable() {
							public void run() {
								launch_reply(f_reply);
							}
						};
					SwingUtilities.invokeLater(r);
				} catch (Exception e) {
					send_exception(e);
				}
			}
		}

		public LaunchHandler(JFrame in_owner) {
			owner = in_owner;
		}
	}

	void launch_exception(Exception e) {
		if (e instanceof FileNotFoundException) {
			JOptionPane.showMessageDialog(owner,
						      ((FileNotFoundException) e).getMessage(),
						      "Cannot open target device",
						      JOptionPane.ERROR_MESSAGE);
		} else if (e instanceof AltosSerialInUseException) {
			JOptionPane.showMessageDialog(owner,
						      String.format("Device \"%s\" already in use",
								    device.toShortString()),
						      "Device in use",
						      JOptionPane.ERROR_MESSAGE);
		} else if (e instanceof IOException) {
			IOException ee = (IOException) e;
			JOptionPane.showMessageDialog(owner,
						      device.toShortString(),
						      ee.getLocalizedMessage(),
						      JOptionPane.ERROR_MESSAGE);
		} else {
			JOptionPane.showMessageDialog(owner,
						      String.format("Connection to \"%s\" failed",
								    device.toShortString()),
						      "Connection Failed",
						      JOptionPane.ERROR_MESSAGE);
		}
		close();
	}

	void launch_reply(String reply) {
		if (reply == null)
			return;
		if (reply.equals("remote set"))
			poll_launch_status();
		if (reply.equals("status")) {
			set_launch_status();
		}
	}

	void set_arm_text() {
		if (arm.isSelected())
			arm.setText(String.format("%d", armed_count));
		else
			arm.setText("Arm");
	}

	void start_arm_timer() {
		armed_count = 30;
		set_arm_text();
	}

	void stop_arm_timer() {
		armed_count = 0;
		armed = false;
		arm.setSelected(false);
		fire.setEnabled(false);
		set_arm_text();
	}

	void cancel () {
		fire.setEnabled(false);
		firing = false;
		stop_arm_timer();
	}

	void send_command(String command) {
		try {
			command_queue.put(command);
		} catch (Exception ex) {
			launch_exception(ex);
		}
	}

	boolean	getting_status = false;

	void set_launch_status() {
		getting_status = false;
		armed_status_label.setText(String.format("\"%s\"", AltosLaunch.status_string(armed_status)));
		igniter_status_label.setText(String.format("\"%s\"", AltosLaunch.status_string(igniter_status)));
	}

	void poll_launch_status() {
		if (!getting_status && !firing && !armed) {
			getting_status = true;
			send_command("get_status");
		}
	}

	void fired() {
		firing = false;
		cancel();
	}

	void close() {
		send_command("quit");
		arm_timer.stop();
		setVisible(false);
		dispose();
	}

	void tick_arm_timer() {
		if (armed_count > 0) {
			--armed_count;
			if (armed_count <= 0) {
				armed_count = 0;
				cancel();
			} else {
				if (!firing) {
					send_command("arm");
					set_arm_text();
				}
			}
		}
		poll_launch_status();
	}

	void arm() {
		if (arm.isSelected()) {
			fire.setEnabled(true);
			start_arm_timer();
			if (!firing)
				send_command("arm");
			armed = true;
		} else
			cancel();
	}

	void fire_more() {
		if (firing)
			send_command("fire");
	}

	void fire_down() {
		if (arm.isEnabled() && arm.isSelected() && armed_count > 0) {
			firing = true;
			fire_more();
			fire_timer.restart();
		}
	}

	void fire_up() {
		firing = false;
		fire_timer.stop();
	}

	void set_radio() {
		try {
			radio_channel = Integer.parseInt(radio_channel_text.getText());
		} catch (NumberFormatException ne) {
			radio_channel_text.setText(String.format("%d", radio_channel));
		}
	}

	void set_serial() {
		try {
			launcher_serial = Integer.parseInt(launcher_serial_text.getText());
			AltosUIPreferences.set_launcher_serial(launcher_serial);
			send_command("set_remote");
		} catch (NumberFormatException ne) {
			launcher_serial_text.setText(String.format("%d", launcher_serial));
		}
	}

	void set_channel() {
		try {
			launcher_channel = Integer.parseInt(launcher_channel_text.getText());
			AltosUIPreferences.set_launcher_serial(launcher_channel);
			send_command("set_remote");
		} catch (NumberFormatException ne) {
			launcher_channel_text.setText(String.format("%d", launcher_channel));
		}
	}

	public void actionPerformed(ActionEvent e) {
		String cmd = e.getActionCommand();
		if (cmd.equals("armed") || cmd.equals("igniter")) {
			stop_arm_timer();
		}

		if (cmd.equals("arm"))
			arm();
		if (cmd.equals("tick_arm"))
			tick_arm_timer();
		if (cmd.equals("close"))
			close();
		if (cmd.equals("fire_down"))
			fire_down();
		if (cmd.equals("fire_up"))
			fire_up();
		if (cmd.equals("tick_fire"))
			fire_more();
		if (cmd.equals("new_serial"))
			set_serial();
		if (cmd.equals("new_channel"))
			set_channel();
	}

	/* A window listener to catch closing events and tell the config code */
	class ConfigListener extends WindowAdapter {
		AltosLaunchUI	ui;

		public ConfigListener(AltosLaunchUI this_ui) {
			ui = this_ui;
		}

		public void windowClosing(WindowEvent e) {
			ui.actionPerformed(new ActionEvent(e.getSource(),
							   ActionEvent.ACTION_PERFORMED,
							   "close"));
		}
	}

	private boolean open() {
		command_queue = new LinkedBlockingQueue<String>();

		device = AltosDeviceUIDialog.show(owner, Altos.product_any);
		if (device != null) {
				LaunchHandler	handler = new LaunchHandler(owner);
				Thread		t = new Thread(handler);
				t.start();
				return true;
		}
		return false;
	}

	public AltosLaunchUI(JFrame in_owner) {

		launcher_channel = AltosUIPreferences.launcher_channel();
		launcher_serial = AltosUIPreferences.launcher_serial();
		owner = in_owner;
		armed_status = AltosLaunch.Unknown;
		igniter_status = AltosLaunch.Unknown;

		if (!open())
			return;

		Container		pane = getScrollablePane();
		GridBagConstraints	c = new GridBagConstraints();
		Insets			i = new Insets(4,4,4,4);

		arm_timer = new javax.swing.Timer(arm_timeout, this);
		arm_timer.setActionCommand("tick_arm");
		arm_timer.restart();

		fire_timer = new javax.swing.Timer(fire_timeout, this);
		fire_timer.setActionCommand("tick_fire");

		owner = in_owner;

		pane.setLayout(new GridBagLayout());

		c.fill = GridBagConstraints.NONE;
		c.anchor = GridBagConstraints.CENTER;
		c.insets = i;
		c.weightx = 1;
		c.weighty = 1;

		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 2;
		c.anchor = GridBagConstraints.CENTER;
		label = new JLabel ("Launch Controller");
		pane.add(label, c);

		c.gridx = 0;
		c.gridy = 1;
		c.gridwidth = 1;
		c.anchor = GridBagConstraints.WEST;
		launcher_serial_label = new JLabel("Launcher Serial");
		pane.add(launcher_serial_label, c);

		c.gridx = 1;
		c.gridy = 1;
		c.gridwidth = 1;
		c.anchor = GridBagConstraints.WEST;
		launcher_serial_text = new JTextField(7);
		launcher_serial_text.setText(String.format("%d", launcher_serial));
		launcher_serial_text.setActionCommand("new_serial");
		launcher_serial_text.addActionListener(this);
		pane.add(launcher_serial_text, c);

		c.gridx = 0;
		c.gridy = 2;
		c.gridwidth = 1;
		c.anchor = GridBagConstraints.WEST;
		launcher_channel_label = new JLabel("Launcher Channel");
		pane.add(launcher_channel_label, c);

		c.gridx = 1;
		c.gridy = 2;
		c.gridwidth = 1;
		c.anchor = GridBagConstraints.WEST;
		launcher_channel_text = new JTextField(7);
		launcher_channel_text.setText(String.format("%d", launcher_channel));
		launcher_channel_text.setActionCommand("new_channel");
		launcher_channel_text.addActionListener(this);
		pane.add(launcher_channel_text, c);

		c.gridx = 0;
		c.gridy = 3;
		c.gridwidth = 1;
		c.anchor = GridBagConstraints.WEST;
		armed_label = new JLabel ("Armed");
		pane.add(armed_label, c);

		c.gridx = 1;
		c.gridy = 3;
		c.gridwidth = 1;
		c.anchor = GridBagConstraints.WEST;
		armed_status_label = new JLabel();
		pane.add(armed_status_label, c);

		c.gridx = 0;
		c.gridy = 4;
		c.gridwidth = 1;
		c.anchor = GridBagConstraints.WEST;
		igniter = new JLabel ("Igniter");
		pane.add(igniter, c);

		c.gridx = 1;
		c.gridy = 4;
		c.gridwidth = 1;
		c.anchor = GridBagConstraints.WEST;
		igniter_status_label = new JLabel();
		pane.add(igniter_status_label, c);

		c.gridx = 0;
		c.gridy = 5;
		c.gridwidth = 1;
		c.anchor = GridBagConstraints.CENTER;
		arm = new JToggleButton ("Arm");
		pane.add(arm, c);
		arm.addActionListener(this);
		arm.setActionCommand("arm");
		arm.setEnabled(true);

		c.gridx = 1;
		c.gridy = 5;
		c.gridwidth = 1;
		c.anchor = GridBagConstraints.CENTER;
		fire = new FireButton ("Fire");
		fire.setEnabled(false);
		pane.add(fire, c);
		fire.addActionListener(this);
		fire.setActionCommand("fire");

		pack();
		setLocationRelativeTo(owner);

		addWindowListener(new ConfigListener(this));

		setVisible(true);
	}
}