3a728ac4d3f6092af73c3301c8316f7340d315df
[fw/altos] / altoslib / AltosMma655x.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 AltosMma655x implements Cloneable {
24
25         private int     accel;
26
27         public boolean parse_line(String line) throws NumberFormatException {
28                 if (line.startsWith("MMA655X value")) {
29                         String[] items = line.split("\\s+");
30                         if (items.length >= 3) {
31                                 accel = Integer.parseInt(items[2]);
32                                 return true;
33                         }
34                 }
35                 return false;
36         }
37
38         public AltosMma655x() {
39                 accel = AltosLib.MISSING;
40         }
41
42         public AltosMma655x clone() {
43                 AltosMma655x    n = new AltosMma655x();
44
45                 n.accel = accel;
46                 return n;
47         }
48
49         static public void provide_data(AltosDataListener listener, AltosLink link) throws InterruptedException, AltosUnknownProduct {
50                 try {
51                         AltosMma655x    mma655x = new AltosMma655x(link);
52                         AltosCalData    cal_data = listener.cal_data();
53
54                         if (mma655x != null) {
55                                 int accel = mma655x.accel;
56                                 if (cal_data.mma655x_inverted)
57                                         accel = 4095 - accel;
58                                 if (cal_data.pad_orientation == 1)
59                                         accel = 4095 - accel;
60                                 listener.set_acceleration(cal_data.acceleration(accel));
61                         }
62                 } catch (TimeoutException te) {
63                 } catch (NumberFormatException ne) {
64                 }
65         }
66
67         public AltosMma655x(AltosLink link) throws InterruptedException, TimeoutException, NumberFormatException {
68                 this();
69                 link.printf("A\n");
70                 for (;;) {
71                         String line = link.get_reply_no_dialog(5000);
72                         if (line == null)
73                                 throw new TimeoutException();
74                         if (parse_line(line))
75                                 break;
76                 }
77         }
78 }