altos/lisp: Fix uninitialized values in ao_lisp_make_const
[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_11;
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 update_state(AltosState state, AltosLink link, AltosConfigData config_data) throws InterruptedException, AltosUnknownProduct {
50                 try {
51                         AltosMma655x    mma655x = new AltosMma655x(link);
52
53                         if (mma655x != null) {
54                                 int accel = mma655x.accel;
55                                 if (config_data.mma655x_inverted())
56                                         accel = 4095 - accel;
57                                 if (config_data.pad_orientation == 1)
58                                         accel = 4095 - accel;
59                                 state.set_accel(accel);
60                         }
61                 } catch (TimeoutException te) {
62                 } catch (NumberFormatException ne) {
63                 }
64         }
65
66         public AltosMma655x(AltosLink link) throws InterruptedException, TimeoutException, NumberFormatException {
67                 this();
68                 link.printf("A\n");
69                 for (;;) {
70                         String line = link.get_reply_no_dialog(5000);
71                         if (line == null)
72                                 throw new TimeoutException();
73                         if (parse_line(line))
74                                 break;
75                 }
76         }
77 }