2 * Copyright © 2012 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 2 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 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.altoslib_14;
21 import java.util.concurrent.*;
23 public class AltosAdxl375 implements Cloneable {
25 private int[] accels = new int[3];
28 public static final int X_AXIS = 0;
29 public static final int Y_AXIS = 1;
30 public static final int Z_AXIS = 2;
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 for (int i = 0; i < 3; i++)
39 accels[i] = Integer.parseInt(items[2+i]);
46 public AltosAdxl375 clone() {
47 AltosAdxl375 n = new AltosAdxl375(axis);
49 for (int i = 0; i < 3; i++)
50 n.accels[i] = accels[i];
54 private int accel_along() {
58 private int accel_across() {
60 return accels[Y_AXIS];
62 return accels[X_AXIS];
65 private int accel_through() {
66 return accels[Z_AXIS];
69 static public void provide_data(AltosDataListener listener, AltosLink link, boolean three_axis, int imu_type) throws InterruptedException, AltosUnknownProduct {
71 AltosCalData cal_data = listener.cal_data();
72 AltosAdxl375 adxl375 = new AltosAdxl375(link, cal_data.adxl375_axis);
74 if (adxl375 != null) {
75 int accel = adxl375.accel_along();
76 if (!cal_data.adxl375_inverted)
78 if (cal_data.pad_orientation == 1)
80 listener.set_acceleration(cal_data.acceleration(accel));
82 cal_data.set_imu_type(imu_type);
83 double accel_along = cal_data.accel_along(-accel);
84 double accel_across = cal_data.accel_across(adxl375.accel_across());
85 double accel_through = cal_data.accel_through(adxl375.accel_through());
86 listener.set_accel_ground(accel_along,
89 listener.set_accel(accel_along,
94 } catch (TimeoutException te) {
95 } catch (NumberFormatException ne) {
99 public AltosAdxl375() {
100 for (int i = 0; i < 3; i++)
101 accels[i] = AltosLib.MISSING;
102 axis = AltosLib.MISSING;
105 public AltosAdxl375(int axis) {
110 public AltosAdxl375(AltosLink link, int axis) throws InterruptedException, TimeoutException, NumberFormatException {
114 String line = link.get_reply_no_dialog(5000);
116 throw new TimeoutException();
117 if (parse_line(line))