2 * Copyright © 2017 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 package org.altusmetrum.altoslib_12;
22 import java.util.concurrent.*;
24 public class AltosAccelCal implements Runnable {
27 AltosAccelCalListener listener;
30 boolean close_on_exit;
36 AltosConfigData config_data;
38 public static final int phase_antenna_up = 0;
39 public static final int phase_antenna_down = 1;
41 void start_link() throws InterruptedException, TimeoutException {
43 link.set_radio_frequency(frequency);
44 link.set_callsign(callsign);
50 boolean stop_link() throws InterruptedException, TimeoutException {
53 return link.reply_abort;
56 public void set_frequency(double in_frequency) {
57 frequency = in_frequency;
61 public void set_callsign(String in_callsign) {
62 callsign = in_callsign;
66 public void abort() throws InterruptedException {
67 while (accel_thread.isAlive()) {
68 accel_thread.interrupt();
75 static private final String press_msg = "press a key...";
77 private Semaphore ui_signal_semaphore;
78 private boolean ui_signal_reply;
80 public void signal(boolean reply) {
81 System.out.printf("Signal cal semaphore %b\n", reply);
82 ui_signal_reply = reply;
83 ui_signal_semaphore.release();
86 private boolean wait_signal() throws InterruptedException {
87 System.out.printf("\twait for cal signal...\n");
88 ui_signal_semaphore.acquire();
89 System.out.printf("\tgot cal signal %b\n", ui_signal_reply);
90 return ui_signal_reply;
93 private boolean wait_press(int timeout) throws InterruptedException {
95 String line = link.get_reply(timeout);
97 System.out.printf("get_reply timeout\n");
100 System.out.printf("got line %s\n", line);
101 if (line.contains(press_msg))
103 if (line.contains("Invalid"))
105 if (line.contains("Syntax"))
107 if (line.contains("Calibrating"))
112 static final int cal_timeout = 20 * 1000;
115 System.out.printf("start accel cal procedure\n");
119 config_data = link.config_data();
121 /* set back to antenna up for calibration */
122 if (config_data.pad_orientation != 0)
123 link.printf("c o 0\n");
125 /* Start calibration */
126 link.set_match(press_msg);
128 link.set_match(press_msg);
129 link.printf("c a 0\n");
130 if (!wait_press(cal_timeout))
131 throw new TimeoutException("timeout");
132 listener.set_phase(this, phase_antenna_up);
134 throw new InterruptedException("aborted");
135 link.set_match(press_msg);
137 if (!wait_press(cal_timeout))
138 throw new TimeoutException("timeout");
139 listener.set_phase(this, phase_antenna_down);
141 throw new InterruptedException("aborted");
142 link.printf("\nv\n");
143 } catch (TimeoutException e) {
145 } catch (InterruptedException e) {
148 link.set_match(null);
150 boolean worked = true;
152 String line = link.get_reply(cal_timeout);
154 throw new TimeoutException();
155 if (line.contains("Invalid"))
157 if (line.contains("software-version"))
161 AltosConfigData new_config = new AltosConfigData(link);
162 listener.cal_done(this, new_config.accel_cal_plus, new_config.accel_cal_minus);
164 throw new InterruptedException("aborted");
166 listener.error(this, "Calibration failed");
168 if (config_data.pad_orientation != AltosLib.MISSING && config_data.pad_orientation != 0)
169 link.printf("c o %d\n", config_data.pad_orientation);
170 if (config_data.accel_cal_plus != AltosLib.MISSING && config_data.accel_cal_minus != AltosLib.MISSING)
171 link.printf("c a %d %d\n",
172 config_data.accel_cal_plus, config_data.accel_cal_minus);
175 } catch (TimeoutException te) {
176 System.out.printf("timeout");
177 listener.error(this, "timeout");
178 } catch (InterruptedException ie) {
179 System.out.printf("interrupted\n");
180 listener.error(this, "interrupted");
184 public void start() {
185 accel_thread = new Thread(this);
186 listener.set_thread(this, accel_thread);
187 accel_thread.start();
190 public AltosAccelCal(AltosLink link, AltosAccelCalListener listener) {
192 this.listener = listener;
193 ui_signal_semaphore = new Semaphore(0);