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.
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
{
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
{
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
);
64 if (h_di
!= null && !h_di
.read()) {
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; }
72 if (h_di
!= null) h_di
.close();
74 } catch (IllegalStateException e
) {
75 } catch (InterruptedException e
) {
79 h_led
= ioio
.openDigitalOutput(IOIO
.LED_PIN
);
82 sleeptime
= 60000 / sample_rate
;
103 } catch (InterruptedException e
) {
107 @Override public void run() {
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
);
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
) {
124 listener
.onVoltageError();