Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[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_13;
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                         System.out.printf("adxl parse line %s\n", line);
35                         String[] items = line.split("\\s+");
36                         if (axis == AltosLib.MISSING)
37                                 throw new NumberFormatException("No ADXL375 axis specified");
38                         if (items.length >= 3) {
39                                 accel = Integer.parseInt(items[2 + axis]);
40                                 System.out.printf("accel %d\n", accel);
41                                 return true;
42                         }
43                 }
44                 return false;
45         }
46
47         public AltosAdxl375 clone() {
48                 AltosAdxl375    n = new AltosAdxl375(axis);
49
50                 n.accel = accel;
51                 return n;
52         }
53
54         static public void provide_data(AltosDataListener listener, AltosLink link) throws InterruptedException, AltosUnknownProduct {
55                 try {
56                         AltosCalData    cal_data = listener.cal_data();
57                         AltosAdxl375    adxl375 = new AltosAdxl375(link, cal_data.adxl375_axis);
58
59                         if (adxl375 != null) {
60                                 int accel = adxl375.accel;
61                                 if (cal_data.adxl375_inverted)
62                                         accel = -accel;
63                                 if (cal_data.pad_orientation == 1)
64                                         accel = -accel;
65                                 listener.set_acceleration(cal_data.acceleration(accel));
66                         }
67                 } catch (TimeoutException te) {
68                 } catch (NumberFormatException ne) {
69                 }
70         }
71
72         public AltosAdxl375() {
73                 accel = AltosLib.MISSING;
74                 axis = AltosLib.MISSING;
75         }
76
77         public AltosAdxl375(int axis) {
78                 this();
79                 this.axis = axis;
80         }
81
82         public AltosAdxl375(AltosLink link, int axis) throws InterruptedException, TimeoutException, NumberFormatException {
83                 this(axis);
84                 link.printf("A\n");
85                 for (;;) {
86                         String line = link.get_reply_no_dialog(5000);
87                         if (line == null)
88                                 throw new TimeoutException();
89                         if (parse_line(line))
90                                 break;
91                 }
92         }
93 }