android/GlueIOIOPort: fix spurious errors after IOIO baud rate change
[xcsoar.git] / android / src / GlueIOIOPort.java
blob35c981ca7a13590ee535e044152fac05cba5e4de
1 /*
2 Copyright_License {
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.
24 package org.xcsoar;
26 import android.util.Log;
27 import ioio.lib.api.IOIO;
28 import ioio.lib.api.Uart;
29 import ioio.lib.api.exception.ConnectionLostException;
31 /**
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;
44 /**
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;
53 private int ID;
55 GlueIOIOPort(IOIOConnectionHolder _holder, int ID_, int _baudrate) {
56 super("IOIO UART " + ID_);
58 ID = ID_;
59 baudrate = _baudrate;
61 switch (ID) {
62 case 0:
63 case 1:
64 inPin = (ID * 2) + 4;
65 outPin = inPin - 1;
66 break;
67 case 2:
68 case 3:
69 inPin = (ID * 2) + 7;
70 outPin = inPin - 1;
71 break;
72 default:
73 throw new IllegalArgumentException();
76 holder = _holder;
77 _holder.addListener(this);
80 /**
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() {
86 if (!cycling)
87 return;
89 /* wait only once */
90 cycling = false;
92 try {
93 wait(200);
94 } catch (InterruptedException e) {
98 @Override public void onIOIOConnect(IOIO ioio)
99 throws ConnectionLostException, InterruptedException {
100 connected = true;
102 Uart uart;
103 try {
104 uart = ioio.openUart(inPin, outPin, baudrate, Uart.Parity.NONE,
105 Uart.StopBits.ONE);
106 } catch (IllegalArgumentException e) {
107 Log.w(TAG, "IOIO.openUart() failed", e);
108 return;
111 set(uart);
113 synchronized(this) {
114 cycling = false;
115 notifyAll();
119 @Override public void onIOIODisconnect() {
120 connected = false;
122 super.close();
125 @Override public void close() {
126 IOIOConnectionHolder holder;
127 synchronized(this) {
128 holder = this.holder;
129 this.holder = null;
132 if (holder != null)
133 holder.removeListener(this);
136 @Override public int getState() {
137 return connected
138 ? super.getState()
139 : STATE_LIMBO;
142 @Override public int getBaudRate() {
143 return baudrate;
146 @Override public boolean setBaudRate(int _baudrate) {
147 if (_baudrate == baudrate)
148 return true;
150 IOIOConnectionHolder holder = this.holder;
151 if (holder == null)
152 /* this port was already closed */
153 return false;
155 baudrate = _baudrate;
156 if (connected)
157 cycling = true;
158 holder.cycleListener(this);
159 return true;
162 @Override public int write(byte[] data, int length) {
163 waitCycled();
164 return super.write(data, length);