4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 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
.Uart
;
29 import ioio
.lib
.api
.exception
.ConnectionLostException
;
32 * ID = 0, pins TX=3, RX=4
33 * ID = 1, pins TX=5, RX=6
34 * ID = 2, pins TX=10 RX=11
35 * ID = 3, pins TX=12 RX=13
38 final class GlueIOIOPort
extends IOIOPort
implements IOIOConnectionListener
{
39 private static final String TAG
= "XCSoar";
41 private IOIOConnectionHolder holder
;
42 private boolean connected
;
45 * Set to true when the connection is being cycled, e.g. during a
46 * baud rate change. It is used by waitCycled().
48 private boolean cycling
;
50 private final int inPin
;
51 private final int outPin
;
52 private int baudrate
= 0;
55 GlueIOIOPort(IOIOConnectionHolder _holder
, int ID_
, int _baudrate
) {
56 super("IOIO UART " + ID_
);
73 throw new IllegalArgumentException();
77 _holder
.addListener(this);
81 * Wait for a certain amoutn of time until the port has been
82 * reconnected after a baud rate change. Call this to avoid
83 * spurious I/O errors while the baud rate is being changed.
85 private synchronized void waitCycled() {
94 } catch (InterruptedException e
) {
98 @Override public void onIOIOConnect(IOIO ioio
)
99 throws ConnectionLostException
, InterruptedException
{
104 uart
= ioio
.openUart(inPin
, outPin
, baudrate
, Uart
.Parity
.NONE
,
106 } catch (IllegalArgumentException e
) {
107 Log
.w(TAG
, "IOIO.openUart() failed", e
);
119 @Override public void onIOIODisconnect() {
125 @Override public void close() {
126 IOIOConnectionHolder holder
;
128 holder
= this.holder
;
133 holder
.removeListener(this);
136 @Override public int getState() {
142 @Override public int getBaudRate() {
146 @Override public boolean setBaudRate(int _baudrate
) {
147 if (_baudrate
== baudrate
)
150 IOIOConnectionHolder holder
= this.holder
;
152 /* this port was already closed */
155 baudrate
= _baudrate
;
158 holder
.cycleListener(this);
162 @Override public int write(byte[] data
, int length
) {
164 return super.write(data
, length
);