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_14;
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"))
108 listener.message(this, line);
112 static final int cal_timeout = 20 * 1000;
115 System.out.printf("start accel cal procedure\n");
117 AltosConfigData new_config = null;
121 config_data = link.config_data();
123 /* set back to antenna up for calibration */
124 if (config_data.pad_orientation != 0)
125 link.printf("c o 0\n");
127 /* Start calibration */
129 System.out.printf("*** start cal\n");
130 link.set_match(press_msg);
131 link.printf("c a 0\n");
132 System.out.printf("*** wait press\n");
133 if (!wait_press(cal_timeout))
134 throw new TimeoutException("timeout");
135 System.out.printf("*** set_phase antenna_up\n");
136 listener.set_phase(this, phase_antenna_up);
137 System.out.printf("*** wait_signal\n");
139 throw new InterruptedException("aborted");
140 link.set_match(press_msg);
141 System.out.printf("*** send newline\n");
143 System.out.printf("*** wait press\n");
144 if (!wait_press(cal_timeout))
145 throw new TimeoutException("timeout");
146 System.out.printf("***set_phase antenna_down\n");
147 listener.set_phase(this, phase_antenna_down);
148 System.out.printf("*** wait_signal\n");
150 throw new InterruptedException("aborted");
151 System.out.printf("*** send newline and version command\n");
152 link.printf("\nv\n");
153 } catch (TimeoutException e) {
155 } catch (InterruptedException e) {
158 link.set_match(null);
160 boolean worked = true;
162 String line = link.get_reply(cal_timeout);
164 throw new TimeoutException();
165 System.out.printf("*** waiting for finish: %s\n", line);
166 if (line.contains("Invalid"))
168 if (line.contains("software-version"))
170 if (line.contains("Calibrating"))
171 listener.message(this, line);
173 System.out.printf("*** worked: %b\n", worked);
175 new_config = new AltosConfigData(link);
177 int plus = config_data.accel_cal_plus(config_data.pad_orientation);
178 int minus = config_data.accel_cal_minus(config_data.pad_orientation);
179 System.out.printf("Restore orientation %d +g %d -g %d\n",
180 config_data.pad_orientation,
182 if (config_data.pad_orientation != AltosLib.MISSING)
183 link.printf("c o %d\n", config_data.pad_orientation);
184 if (plus != AltosLib.MISSING && minus != AltosLib.MISSING && plus != 0) {
185 if (config_data.accel_zero_along != AltosLib.MISSING)
186 link.printf("c a %d %d %d %d %d\n",
188 config_data.accel_zero_along,
189 config_data.accel_zero_across,
190 config_data.accel_zero_through);
192 link.printf("c a %d %d\n", plus, minus);
197 if (new_config != null) {
198 int plus = new_config.accel_cal_plus(AltosLib.AO_PAD_ORIENTATION_ANTENNA_UP);
199 int minus = new_config.accel_cal_minus(AltosLib.AO_PAD_ORIENTATION_ANTENNA_UP);
200 System.out.printf("*** +1g %d -1g %d\n", plus, minus);
201 listener.cal_done(this, plus, minus);
203 throw new InterruptedException("aborted");
205 listener.error(this, "Calibration failed");
206 } catch (TimeoutException te) {
207 System.out.printf("timeout");
208 listener.error(this, "timeout");
209 } catch (InterruptedException ie) {
210 System.out.printf("interrupted\n");
211 listener.error(this, "interrupted");
215 public void start() {
216 accel_thread = new Thread(this);
217 listener.set_thread(this, accel_thread);
218 accel_thread.start();
221 public AltosAccelCal(AltosLink link, AltosAccelCalListener listener) {
223 this.listener = listener;
224 ui_signal_semaphore = new Semaphore(0);