android/GlueIOIOPort: fix spurious errors after IOIO baud rate change
[xcsoar.git] / android / src / Voltage.java
blobb60a386e30dd18b0691929b554a2565bc16ad61c
1 /*
2 Copyright_License {
4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2012 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 package org.xcsoar;
26 import android.util.Log;
27 import ioio.lib.api.IOIO;
28 import ioio.lib.api.AnalogInput;
29 import ioio.lib.api.DigitalOutput;
30 import ioio.lib.api.DigitalInput;
31 import ioio.lib.api.exception.ConnectionLostException;
34 * A driver for voltage measurement on the IOIO board.
36 final class Voltage extends Thread {
37 interface Listener {
38 /**
39 * @param raw values from ad converter.
41 void onVoltageValues(int temp_adc, int voltage_index, int volt_adc);
42 void onVoltageError();
45 private static final String TAG = "XCSoar";
47 private AnalogInput h_volt;
48 private AnalogInput h_temp;
49 private DigitalOutput h_led;
50 private final int sleeptime;
52 private final Listener listener;
54 public Voltage(IOIO ioio, int sample_rate, Listener _listener)
55 throws ConnectionLostException {
56 super("Voltage");
58 // when you want to use other pins: you got the source ....
59 h_volt = ioio.openAnalogInput(40);
60 // When pin 39 cannot be pulled-up when used as digital input and is between 0.3 and 0.6 Volt
61 // when using analog input then assume there is a kty83 temperature sensor there.
62 DigitalInput h_di = ioio.openDigitalInput(39, DigitalInput.Spec.Mode.PULL_UP);
63 try {
64 if (h_di != null && !h_di.read()) {
65 h_di.close();
66 h_temp = ioio.openAnalogInput(39);
67 for (int i=0; h_temp != null && i<100; i++) {
68 float v = h_temp.getVoltage();
69 if (v < 0.3 || v > 0.6) { h_temp.close(); h_temp = null; break; }
71 } else {
72 if (h_di != null) h_di.close();
74 } catch (IllegalStateException e) {
75 } catch (InterruptedException e) {
76 } finally {
79 h_led = ioio.openDigitalOutput(IOIO.LED_PIN);
81 if (sample_rate != 0)
82 sleeptime = 60000 / sample_rate;
83 else
84 sleeptime = 1000;
86 listener = _listener;
88 start();
91 public void close() {
92 if (h_volt != null)
93 h_volt.close();
94 if (h_temp != null)
95 h_temp.close();
96 if (h_led != null)
97 h_led.close();
99 interrupt();
101 try {
102 join();
103 } catch (InterruptedException e) {
107 @Override public void run() {
108 try {
109 boolean led = false;
110 while (true) {
111 int v = -1; int t = -1;
112 if (h_volt != null) v = (int)(h_volt.read() * 1024);
113 if (h_temp != null) t = (int)(h_temp.read() * 1024);
114 if (h_led != null) { h_led.write(led); led = !led; }
115 listener.onVoltageValues(t, 0, v);
116 sleep(sleeptime);
118 } catch (ConnectionLostException e) {
119 Log.d(TAG, "Voltage.run() failed", e);
120 } catch (IllegalStateException e) {
121 Log.d(TAG, "Voltage.run() failed", e);
122 } catch (InterruptedException e) {
123 } finally {
124 listener.onVoltageError();