Bump java lib versions in preparation for 1.9.2
[fw/altos] / altoslib / AltosAdxl375.java
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
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 2 of the License, or
7  * (at your option) any later version.
8  *
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.
13  *
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  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altoslib_14;
20
21 import java.util.concurrent.*;
22
23 public class AltosAdxl375 implements Cloneable {
24
25         private int     accel;
26         private int     axis;
27
28         public static final int X_AXIS = 0;
29         public static final int Y_AXIS = 1;
30         public static final int Z_AXIS = 2;
31
32         public boolean parse_line(String line) throws NumberFormatException {
33                 if (line.startsWith("ADXL375 value")) {
34                         String[] items = line.split("\\s+");
35                         if (axis == AltosLib.MISSING)
36                                 throw new NumberFormatException("No ADXL375 axis specified");
37                         if (items.length >= 3) {
38                                 accel = Integer.parseInt(items[2 + axis]);
39                                 return true;
40                         }
41                 }
42                 return false;
43         }
44
45         public AltosAdxl375 clone() {
46                 AltosAdxl375    n = new AltosAdxl375(axis);
47
48                 n.accel = accel;
49                 return n;
50         }
51
52         static public void provide_data(AltosDataListener listener, AltosLink link) throws InterruptedException, AltosUnknownProduct {
53                 try {
54                         AltosCalData    cal_data = listener.cal_data();
55                         AltosAdxl375    adxl375 = new AltosAdxl375(link, cal_data.adxl375_axis);
56
57                         if (adxl375 != null) {
58                                 int accel = adxl375.accel;
59                                 if (cal_data.adxl375_inverted)
60                                         accel = -accel;
61                                 if (cal_data.pad_orientation == 1)
62                                         accel = -accel;
63                                 listener.set_acceleration(cal_data.acceleration(accel));
64                         }
65                 } catch (TimeoutException te) {
66                 } catch (NumberFormatException ne) {
67                 }
68         }
69
70         public AltosAdxl375() {
71                 accel = AltosLib.MISSING;
72                 axis = AltosLib.MISSING;
73         }
74
75         public AltosAdxl375(int axis) {
76                 this();
77                 this.axis = axis;
78         }
79
80         public AltosAdxl375(AltosLink link, int axis) throws InterruptedException, TimeoutException, NumberFormatException {
81                 this(axis);
82                 link.printf("A\n");
83                 for (;;) {
84                         String line = link.get_reply_no_dialog(5000);
85                         if (line == null)
86                                 throw new TimeoutException();
87                         if (parse_line(line))
88                                 break;
89                 }
90         }
91 }