diff --git a/IOIOLib/bin/classes/com/TwentyCodes/android/ioio/IOIOManager.class b/IOIOLib/bin/classes/com/TwentyCodes/android/ioio/IOIOManager.class index 9b214e8..cb7b843 100644 Binary files a/IOIOLib/bin/classes/com/TwentyCodes/android/ioio/IOIOManager.class and b/IOIOLib/bin/classes/com/TwentyCodes/android/ioio/IOIOManager.class differ diff --git a/IOIOLib/bin/classes/com/TwentyCodes/android/ioio/IOIOThread.class b/IOIOLib/bin/classes/com/TwentyCodes/android/ioio/IOIOThread.class index b74834e..55f7630 100644 Binary files a/IOIOLib/bin/classes/com/TwentyCodes/android/ioio/IOIOThread.class and b/IOIOLib/bin/classes/com/TwentyCodes/android/ioio/IOIOThread.class differ diff --git a/IOIOLib/bin/classes/ioio/lib/api/AnalogInput.class b/IOIOLib/bin/classes/ioio/lib/api/AnalogInput.class index 319adff..584c764 100644 Binary files a/IOIOLib/bin/classes/ioio/lib/api/AnalogInput.class and b/IOIOLib/bin/classes/ioio/lib/api/AnalogInput.class differ diff --git a/IOIOLib/bin/classes/ioio/lib/api/IOIO.class b/IOIOLib/bin/classes/ioio/lib/api/IOIO.class index 3fea9fd..c9a6c47 100644 Binary files a/IOIOLib/bin/classes/ioio/lib/api/IOIO.class and b/IOIOLib/bin/classes/ioio/lib/api/IOIO.class differ diff --git a/IOIOLib/bin/classes/ioio/lib/api/IOIOFactory.class b/IOIOLib/bin/classes/ioio/lib/api/IOIOFactory.class index 179048c..f6d3f37 100644 Binary files a/IOIOLib/bin/classes/ioio/lib/api/IOIOFactory.class and b/IOIOLib/bin/classes/ioio/lib/api/IOIOFactory.class differ diff --git a/IOIOLib/bin/classes/ioio/lib/api/SpiMaster$Config.class b/IOIOLib/bin/classes/ioio/lib/api/SpiMaster$Config.class index e8378f5..cebccbc 100644 Binary files a/IOIOLib/bin/classes/ioio/lib/api/SpiMaster$Config.class and b/IOIOLib/bin/classes/ioio/lib/api/SpiMaster$Config.class differ diff --git a/IOIOLib/bin/classes/ioio/lib/api/SpiMaster$Rate.class b/IOIOLib/bin/classes/ioio/lib/api/SpiMaster$Rate.class index 5ed0359..78849bb 100644 Binary files a/IOIOLib/bin/classes/ioio/lib/api/SpiMaster$Rate.class and b/IOIOLib/bin/classes/ioio/lib/api/SpiMaster$Rate.class differ diff --git a/IOIOLib/bin/classes/ioio/lib/api/Uart$Parity.class b/IOIOLib/bin/classes/ioio/lib/api/Uart$Parity.class index 2ccb948..50617a7 100644 Binary files a/IOIOLib/bin/classes/ioio/lib/api/Uart$Parity.class and b/IOIOLib/bin/classes/ioio/lib/api/Uart$Parity.class differ diff --git a/IOIOLib/bin/classes/ioio/lib/api/Uart$StopBits.class b/IOIOLib/bin/classes/ioio/lib/api/Uart$StopBits.class index 9571d8c..87fe6aa 100644 Binary files a/IOIOLib/bin/classes/ioio/lib/api/Uart$StopBits.class and b/IOIOLib/bin/classes/ioio/lib/api/Uart$StopBits.class differ diff --git a/IOIOLib/bin/classes/ioio/lib/bluetooth/BluetoothIOIOConnection.class b/IOIOLib/bin/classes/ioio/lib/bluetooth/BluetoothIOIOConnection.class index c1d4b23..d78a3d2 100644 Binary files a/IOIOLib/bin/classes/ioio/lib/bluetooth/BluetoothIOIOConnection.class and b/IOIOLib/bin/classes/ioio/lib/bluetooth/BluetoothIOIOConnection.class differ diff --git a/IOIOLib/bin/classes/ioio/lib/bluetooth/BluetoothIOIOConnectionDiscovery.class b/IOIOLib/bin/classes/ioio/lib/bluetooth/BluetoothIOIOConnectionDiscovery.class deleted file mode 100644 index 68d4ebc..0000000 Binary files a/IOIOLib/bin/classes/ioio/lib/bluetooth/BluetoothIOIOConnectionDiscovery.class and /dev/null differ diff --git a/IOIOLib/bin/classes/ioio/lib/impl/AbstractPin.class b/IOIOLib/bin/classes/ioio/lib/impl/AbstractPin.class index 4d7f207..a9e5dce 100644 Binary files a/IOIOLib/bin/classes/ioio/lib/impl/AbstractPin.class and b/IOIOLib/bin/classes/ioio/lib/impl/AbstractPin.class differ diff --git a/IOIOLib/bin/classes/ioio/lib/impl/AbstractResource.class b/IOIOLib/bin/classes/ioio/lib/impl/AbstractResource.class index 8f74d33..2ad47bf 100644 Binary files a/IOIOLib/bin/classes/ioio/lib/impl/AbstractResource.class and b/IOIOLib/bin/classes/ioio/lib/impl/AbstractResource.class differ diff --git a/IOIOLib/bin/classes/ioio/lib/impl/AnalogInputImpl.class b/IOIOLib/bin/classes/ioio/lib/impl/AnalogInputImpl.class index 54e69eb..3f63933 100644 Binary files a/IOIOLib/bin/classes/ioio/lib/impl/AnalogInputImpl.class and b/IOIOLib/bin/classes/ioio/lib/impl/AnalogInputImpl.class differ diff --git a/IOIOLib/bin/classes/ioio/lib/impl/DigitalInputImpl.class b/IOIOLib/bin/classes/ioio/lib/impl/DigitalInputImpl.class index c0951ec..ca776c9 100644 Binary files a/IOIOLib/bin/classes/ioio/lib/impl/DigitalInputImpl.class and b/IOIOLib/bin/classes/ioio/lib/impl/DigitalInputImpl.class differ diff --git a/IOIOLib/bin/classes/ioio/lib/impl/DigitalOutputImpl.class b/IOIOLib/bin/classes/ioio/lib/impl/DigitalOutputImpl.class index 0e7c67a..5d8c712 100644 Binary files a/IOIOLib/bin/classes/ioio/lib/impl/DigitalOutputImpl.class and b/IOIOLib/bin/classes/ioio/lib/impl/DigitalOutputImpl.class differ diff --git a/IOIOLib/bin/classes/ioio/lib/impl/FlowControlledOutputStream$FlushThread.class b/IOIOLib/bin/classes/ioio/lib/impl/FlowControlledOutputStream$FlushThread.class index 040180b..2c32629 100644 Binary files a/IOIOLib/bin/classes/ioio/lib/impl/FlowControlledOutputStream$FlushThread.class and b/IOIOLib/bin/classes/ioio/lib/impl/FlowControlledOutputStream$FlushThread.class differ diff --git a/IOIOLib/bin/classes/ioio/lib/impl/FlowControlledOutputStream.class b/IOIOLib/bin/classes/ioio/lib/impl/FlowControlledOutputStream.class index c1157f0..c732183 100644 Binary files a/IOIOLib/bin/classes/ioio/lib/impl/FlowControlledOutputStream.class and b/IOIOLib/bin/classes/ioio/lib/impl/FlowControlledOutputStream.class differ diff --git a/IOIOLib/bin/classes/ioio/lib/impl/FlowControlledPacketSender.class b/IOIOLib/bin/classes/ioio/lib/impl/FlowControlledPacketSender.class index eb76ced..e72b01e 100644 Binary files a/IOIOLib/bin/classes/ioio/lib/impl/FlowControlledPacketSender.class and b/IOIOLib/bin/classes/ioio/lib/impl/FlowControlledPacketSender.class differ diff --git a/IOIOLib/bin/classes/ioio/lib/impl/IOIOImpl$State.class b/IOIOLib/bin/classes/ioio/lib/impl/IOIOImpl$State.class deleted file mode 100644 index 5785c2e..0000000 Binary files a/IOIOLib/bin/classes/ioio/lib/impl/IOIOImpl$State.class and /dev/null differ diff --git a/IOIOLib/bin/classes/ioio/lib/impl/IOIOImpl.class b/IOIOLib/bin/classes/ioio/lib/impl/IOIOImpl.class index 153fa9b..7388142 100644 Binary files a/IOIOLib/bin/classes/ioio/lib/impl/IOIOImpl.class and b/IOIOLib/bin/classes/ioio/lib/impl/IOIOImpl.class differ diff --git a/IOIOLib/bin/classes/ioio/lib/impl/IOIOProtocol$IncomingThread.class b/IOIOLib/bin/classes/ioio/lib/impl/IOIOProtocol$IncomingThread.class index 1fc9970..3233d4f 100644 Binary files a/IOIOLib/bin/classes/ioio/lib/impl/IOIOProtocol$IncomingThread.class and b/IOIOLib/bin/classes/ioio/lib/impl/IOIOProtocol$IncomingThread.class differ diff --git a/IOIOLib/bin/classes/ioio/lib/impl/IOIOProtocol$PwmScale.class b/IOIOLib/bin/classes/ioio/lib/impl/IOIOProtocol$PwmScale.class index 73a9d44..5a0471b 100644 Binary files a/IOIOLib/bin/classes/ioio/lib/impl/IOIOProtocol$PwmScale.class and b/IOIOLib/bin/classes/ioio/lib/impl/IOIOProtocol$PwmScale.class differ diff --git a/IOIOLib/bin/classes/ioio/lib/impl/IOIOProtocol.class b/IOIOLib/bin/classes/ioio/lib/impl/IOIOProtocol.class index ddf6967..418da08 100644 Binary files a/IOIOLib/bin/classes/ioio/lib/impl/IOIOProtocol.class and b/IOIOLib/bin/classes/ioio/lib/impl/IOIOProtocol.class differ diff --git a/IOIOLib/bin/classes/ioio/lib/impl/IcspMasterImpl.class b/IOIOLib/bin/classes/ioio/lib/impl/IcspMasterImpl.class index d60563f..5434699 100644 Binary files a/IOIOLib/bin/classes/ioio/lib/impl/IcspMasterImpl.class and b/IOIOLib/bin/classes/ioio/lib/impl/IcspMasterImpl.class differ diff --git a/IOIOLib/bin/classes/ioio/lib/impl/IncapImpl.class b/IOIOLib/bin/classes/ioio/lib/impl/IncapImpl.class index 6ef1255..ad7ed89 100644 Binary files a/IOIOLib/bin/classes/ioio/lib/impl/IncapImpl.class and b/IOIOLib/bin/classes/ioio/lib/impl/IncapImpl.class differ diff --git a/IOIOLib/bin/classes/ioio/lib/impl/IncomingState.class b/IOIOLib/bin/classes/ioio/lib/impl/IncomingState.class index 98e9961..cf806a1 100644 Binary files a/IOIOLib/bin/classes/ioio/lib/impl/IncomingState.class and b/IOIOLib/bin/classes/ioio/lib/impl/IncomingState.class differ diff --git a/IOIOLib/bin/classes/ioio/lib/impl/ModuleAllocator.class b/IOIOLib/bin/classes/ioio/lib/impl/ModuleAllocator.class index 8d06f1f..e140bfd 100644 Binary files a/IOIOLib/bin/classes/ioio/lib/impl/ModuleAllocator.class and b/IOIOLib/bin/classes/ioio/lib/impl/ModuleAllocator.class differ diff --git a/IOIOLib/bin/classes/ioio/lib/impl/PinFunctionMap.class b/IOIOLib/bin/classes/ioio/lib/impl/PinFunctionMap.class index 04464a0..f848507 100644 Binary files a/IOIOLib/bin/classes/ioio/lib/impl/PinFunctionMap.class and b/IOIOLib/bin/classes/ioio/lib/impl/PinFunctionMap.class differ diff --git a/IOIOLib/bin/classes/ioio/lib/impl/PwmImpl.class b/IOIOLib/bin/classes/ioio/lib/impl/PwmImpl.class index e77877a..604ccd3 100644 Binary files a/IOIOLib/bin/classes/ioio/lib/impl/PwmImpl.class and b/IOIOLib/bin/classes/ioio/lib/impl/PwmImpl.class differ diff --git a/IOIOLib/bin/classes/ioio/lib/impl/QueueInputStream.class b/IOIOLib/bin/classes/ioio/lib/impl/QueueInputStream.class index 104aa9f..9eadecf 100644 Binary files a/IOIOLib/bin/classes/ioio/lib/impl/QueueInputStream.class and b/IOIOLib/bin/classes/ioio/lib/impl/QueueInputStream.class differ diff --git a/IOIOLib/bin/classes/ioio/lib/impl/SocketIOIOConnection.class b/IOIOLib/bin/classes/ioio/lib/impl/SocketIOIOConnection.class index c12e05c..0ff0680 100644 Binary files a/IOIOLib/bin/classes/ioio/lib/impl/SocketIOIOConnection.class and b/IOIOLib/bin/classes/ioio/lib/impl/SocketIOIOConnection.class differ diff --git a/IOIOLib/bin/classes/ioio/lib/impl/SpiMasterImpl.class b/IOIOLib/bin/classes/ioio/lib/impl/SpiMasterImpl.class index 4b5af06..73d8977 100644 Binary files a/IOIOLib/bin/classes/ioio/lib/impl/SpiMasterImpl.class and b/IOIOLib/bin/classes/ioio/lib/impl/SpiMasterImpl.class differ diff --git a/IOIOLib/bin/classes/ioio/lib/impl/TwiMasterImpl.class b/IOIOLib/bin/classes/ioio/lib/impl/TwiMasterImpl.class index 74ec643..47c7a4a 100644 Binary files a/IOIOLib/bin/classes/ioio/lib/impl/TwiMasterImpl.class and b/IOIOLib/bin/classes/ioio/lib/impl/TwiMasterImpl.class differ diff --git a/IOIOLib/bin/classes/ioio/lib/impl/UartImpl.class b/IOIOLib/bin/classes/ioio/lib/impl/UartImpl.class index 84e23c1..b210d3c 100644 Binary files a/IOIOLib/bin/classes/ioio/lib/impl/UartImpl.class and b/IOIOLib/bin/classes/ioio/lib/impl/UartImpl.class differ diff --git a/IOIOLib/bin/classes/ioio/lib/util/AbstractIOIOActivity$IOIOThread.class b/IOIOLib/bin/classes/ioio/lib/util/AbstractIOIOActivity$IOIOThread.class index 837784e..8c1116b 100644 Binary files a/IOIOLib/bin/classes/ioio/lib/util/AbstractIOIOActivity$IOIOThread.class and b/IOIOLib/bin/classes/ioio/lib/util/AbstractIOIOActivity$IOIOThread.class differ diff --git a/IOIOLib/bin/classes/ioio/lib/util/AbstractIOIOActivity.class b/IOIOLib/bin/classes/ioio/lib/util/AbstractIOIOActivity.class index bac453d..5c9ac82 100644 Binary files a/IOIOLib/bin/classes/ioio/lib/util/AbstractIOIOActivity.class and b/IOIOLib/bin/classes/ioio/lib/util/AbstractIOIOActivity.class differ diff --git a/IOIOLib/bin/classes/ioio/lib/util/IOIOConnectionDiscovery$IOIOConnectionSpec.class b/IOIOLib/bin/classes/ioio/lib/util/IOIOConnectionDiscovery$IOIOConnectionSpec.class deleted file mode 100644 index 041743c..0000000 Binary files a/IOIOLib/bin/classes/ioio/lib/util/IOIOConnectionDiscovery$IOIOConnectionSpec.class and /dev/null differ diff --git a/IOIOLib/bin/classes/ioio/lib/util/IOIOConnectionDiscovery.class b/IOIOLib/bin/classes/ioio/lib/util/IOIOConnectionDiscovery.class deleted file mode 100644 index 1cc95f6..0000000 Binary files a/IOIOLib/bin/classes/ioio/lib/util/IOIOConnectionDiscovery.class and /dev/null differ diff --git a/IOIOLib/bin/classes/ioio/lib/util/SocketIOIOConnectionDiscovery.class b/IOIOLib/bin/classes/ioio/lib/util/SocketIOIOConnectionDiscovery.class deleted file mode 100644 index 884afb9..0000000 Binary files a/IOIOLib/bin/classes/ioio/lib/util/SocketIOIOConnectionDiscovery.class and /dev/null differ diff --git a/IOIOLib/bin/ioiolib.jar b/IOIOLib/bin/ioiolib.jar deleted file mode 100644 index 691991d..0000000 Binary files a/IOIOLib/bin/ioiolib.jar and /dev/null differ diff --git a/IOIOLib/src/ioio/lib/api/AnalogInput.java b/IOIOLib/src/ioio/lib/api/AnalogInput.java index dab37f8..1178a54 100644 --- a/IOIOLib/src/ioio/lib/api/AnalogInput.java +++ b/IOIOLib/src/ioio/lib/api/AnalogInput.java @@ -52,10 +52,42 @@ import ioio.lib.api.exception.ConnectionLostException; * Typical usage: * *
+ * {@code
* AnalogInput potentiometer = ioio.openAnalogInput(40);
* float value = potentiometer.read();
* ...
* potentiometer.close(); // pin 40 can now be used for something else.
+ * }
+ *
+ * + * An alternate usage allows reading periodically sampled data without missing + * samples. The {@link #setBuffer(int)} method must first be called, for setting + * up an internal buffer for queuing samples. Then, samples can be obtained by + * calling {@link #readBuffered()} or {@link #getVoltageBuffered()}. These + * methods will block until a sample is available. If this is undesirable, the + * {@link #available()} method can be called first to check how many samples are + * ready in the buffer. In case the buffer overflows, as result of the client + * not reading fast enough, old samples will be dropped, and the client can + * check {@link #getOverflowCount()} to determine how many samples have been + * lost. The sample rate used for capturing samples can be obtained by calling + * {@link #getSampleRate()}. + *
+ * The non-buffered versions of the read methods will still behave normally when + * buffering is enabled. The {@link #read()} and {@link #getVoltage()} methods + * will always return the most recent value, regardless of the buffer state. + *
+ * Typical usage: + * + *
+ * AnalogInput potentiometer = ioio.openAnalogInput(40);
+ * potentiometer.setBuffer(256);
+ * for (int i = 0; i < 1024; ++i) {
+ * // next line will block until at least one sample is available
+ * float sample = potentiometer.readBuffered();
+ * ...
+ * }
+ * ...
+ * potentiometer.close(); // pin 40 can now be used for something else.
*
*
* @see IOIO#openAnalogInput(int)
@@ -69,11 +101,13 @@ public interface AnalogInput extends Closeable {
* may block shortly. If this is a problem, the calling thread can be
* interrupted.
* - * If a scaled value is desired, consider using {@link #read()}. + * If a scaled value is desired, consider using {@link #read()}. * * @return The voltage, in Volt units. - * @throws InterruptedException The calling thread has been interrupted. - * @throws ConnectionLostException The connection with the IOIO is lost. + * @throws InterruptedException + * The calling thread has been interrupted. + * @throws ConnectionLostException + * The connection with the IOIO is lost. * @see #read() */ public float getVoltage() throws InterruptedException, @@ -81,6 +115,7 @@ public interface AnalogInput extends Closeable { /** * Gets the maximum value against which {@link #read()} values are scaled. + * * @return The voltage, in Volts. */ public float getReference(); @@ -96,9 +131,95 @@ public interface AnalogInput extends Closeable { * If an absolute value is desired, consider using {@link #getVoltage()}. * * @return The voltage, in scaled units. - * @throws InterruptedException The calling thread has been interrupted. - * @throws ConnectionLostException The connection with the IOIO is lost. + * @throws InterruptedException + * The calling thread has been interrupted. + * @throws ConnectionLostException + * The connection with the IOIO is lost. * @see #getVoltage() */ public float read() throws InterruptedException, ConnectionLostException; + + /** + * Initializes or destroys an internal buffer, used for queuing sampled + * data. When called with a positive argument, an internal buffer will be + * created, and start storing sampled data. The client can then call + * {@link #readBuffered()} or {@link #getVoltageBuffered()} for obtaining + * buffered samples. + *
+ * When called with argument of 0, the internal buffer is destroyed. + * + * @param capacity + * The maximum number of unread samples that can be buffered + * before overflow occurs. + * @throws ConnectionLostException + * The connection with the IOIO is lost. + */ + public void setBuffer(int capacity) throws ConnectionLostException; + + /** + * Gets the number of samples that have been dropped as result of overflow, + * since {@link #setBuffer(int)} has been called. + * + * @return The number of dropped samples. + * @throws ConnectionLostException + * The connection with the IOIO is lost. + */ + public int getOverflowCount() throws ConnectionLostException; + + /** + * Gets the number of samples currently in the buffer. Reading that many + * samples is guaranteed not to block. + * + * @return The number of samples available in the buffer. + * @throws ConnectionLostException + * The connection with the IOIO is lost. + */ + public int available() throws ConnectionLostException; + + /** + * Read a sample from the internal buffer. This method will block until at + * least one sample is available, the instance is closed (via + * {@link #close()}), the thread is interrupted (via + * {@link Thread#interrupt()} or connection is lost. {@link #setBuffer(int)} + * must be called prior to this method for setting up an internal buffer for + * storing samples. + * + * @see #getVoltageBuffered() + * @return The earliest (oldest) sample available in the buffer, scaled to + * the range [0,1]. + * @throws InterruptedException + * The calling thread has been interrupted. + * @throws ConnectionLostException + * The connection with the IOIO is lost. + */ + public float readBuffered() throws InterruptedException, + ConnectionLostException; + + /** + * Read a sample from the internal buffer. This method will block until at + * least one sample is available, the instance is closed (via + * {@link #close()}), the thread is interrupted (via + * {@link Thread#interrupt()} or connection is lost. {@link #setBuffer(int)} + * must be called prior to this method for setting up an internal buffer for + * storing samples. + * + * @see #readBuffered() + * @return The earliest (oldest) sample available in the buffer, in Volt + * units. + * @throws InterruptedException + * The calling thread has been interrupted. + * @throws ConnectionLostException + * The connection with the IOIO is lost. + */ + public float getVoltageBuffered() throws InterruptedException, + ConnectionLostException; + + /** + * Gets the sample rate used for obtaining buffered samples. + * + * @return The sample rate, in Hz units. + * @throws ConnectionLostException + * The connection with the IOIO is lost. + */ + public float getSampleRate() throws ConnectionLostException; } diff --git a/IOIOLib/src/ioio/lib/api/DigitalInput.java b/IOIOLib/src/ioio/lib/api/DigitalInput.java index 63567e2..f773d88 100644 --- a/IOIOLib/src/ioio/lib/api/DigitalInput.java +++ b/IOIOLib/src/ioio/lib/api/DigitalInput.java @@ -1,140 +1,140 @@ -/* - * Copyright 2011 Ytai Ben-Tsvi. All rights reserved. - * - * - * Redistribution and use in source and binary forms, with or without modification, are - * permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND - * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ARSHAN POURSOHI OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * The views and conclusions contained in the software and documentation are those of the - * authors and should not be interpreted as representing official policies, either expressed - * or implied. - */ -package ioio.lib.api; - -import ioio.lib.api.exception.ConnectionLostException; - -/** - * A pin used for digital input. - *
- * A digital input pin can be used to read logic-level signals. DigitalInput - * instances are obtained by calling {@link IOIO#openDigitalInput(Spec)}. - *
- * The value of the pin is obtained by calling {@link #read()}. It is also - * possible for the client to block until a certain level is sensed, by using - * {@link #waitForValue(boolean)}. - *
- * The instance is alive since its creation. The first {@link #read()} call - * block for a few milliseconds until the initial value is updated. If the - * connection with the IOIO drops at any point, the instance transitions to a - * disconnected state, in which every attempt to use the pin (except - * {@link #close()}) will throw a {@link ConnectionLostException}. Whenever - * {@link #close()} is invoked the instance may no longer be used. Any resources - * associated with it are freed and can be reused. - *
- * Typical usage: - * - *
- * DigitalInput button = ioio.openDigitalInput(10); // used an external pull-up - * button.waitForValue(false); // wait for press - * ... - * button.close(); // pin 10 can now be used for something else. - *- */ -public interface DigitalInput extends Closeable { - /** - * A digital input pin specification, used when opening digital inputs. - */ - static public class Spec { - /** Input pin mode. */ - public enum Mode { - /** - * Pin is floating. When the pin is left disconnected the value - * sensed is undefined. Use this mode when an external pull-up or - * pull-down resistor is used or when interfacing push-pull type - * logic circuits. - */ - FLOATING, - /** - * Internal pull-up resistor is used. When the pin is left - * disconnected, a logical "HIGH" (true) will be sensed. This is - * useful for interfacing with open drain circuits or for - * interacting with a switch connected between the pin and ground. - */ - PULL_UP, - /** - * Internal pull-down resistor is used. When the pin is left - * disconnected, a logical "LOW" (false) will be sensed. This is - * useful for interacting with a switch connected between the pin - * and Vdd. - */ - PULL_DOWN - } - - /** The pin number, as labeled on the board. */ - public int pin; - /** The pin mode. */ - public Mode mode; - - /** - * Constructor. - * - * @param pin - * Pin number, as labeled on the board. - * @param mode - * Pin mode. - */ - public Spec(int pin, Mode mode) { - this.pin = pin; - this.mode = mode; - } - - /** Shorthand for Spec(pin, Mode.FLOATING). */ - public Spec(int pin) { - this(pin, Mode.FLOATING); - } - } - - /** - * Read the value sensed on the pin. May block for a few milliseconds if - * called right after creation of the instance. If this is a problem, the - * calling thread may be interrupted. - * - * @return True for logical "HIGH", false for logical "LOW". - * @throws InterruptedException - * The calling thread has been interrupted. - * @throws ConnectionLostException - * The connection with the IOIO has been lost. - */ - public boolean read() throws InterruptedException, ConnectionLostException; - - /** - * Block until a desired logical level is sensed. The calling thread can be - * interrupted for aborting this operation. - * - * @param value - * The desired logical level. true for "HIGH", false for "LOW". - * @throws InterruptedException - * The calling thread has been interrupted. - * @throws ConnectionLostException - * The connection with the IOIO has been lost. - */ - public void waitForValue(boolean value) throws InterruptedException, - ConnectionLostException; -} +/* + * Copyright 2011 Ytai Ben-Tsvi. All rights reserved. + * + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ARSHAN POURSOHI OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied. + */ +package ioio.lib.api; + +import ioio.lib.api.exception.ConnectionLostException; + +/** + * A pin used for digital input. + *
+ * A digital input pin can be used to read logic-level signals. DigitalInput + * instances are obtained by calling {@link IOIO#openDigitalInput(DigitalInput.Spec)}. + *
+ * The value of the pin is obtained by calling {@link #read()}. It is also + * possible for the client to block until a certain level is sensed, by using + * {@link #waitForValue(boolean)}. + *
+ * The instance is alive since its creation. The first {@link #read()} call + * block for a few milliseconds until the initial value is updated. If the + * connection with the IOIO drops at any point, the instance transitions to a + * disconnected state, in which every attempt to use the pin (except + * {@link #close()}) will throw a {@link ConnectionLostException}. Whenever + * {@link #close()} is invoked the instance may no longer be used. Any resources + * associated with it are freed and can be reused. + *
+ * Typical usage: + * + *
+ * DigitalInput button = ioio.openDigitalInput(10); // used an external pull-up + * button.waitForValue(false); // wait for press + * ... + * button.close(); // pin 10 can now be used for something else. + *+ */ +public interface DigitalInput extends Closeable { + /** + * A digital input pin specification, used when opening digital inputs. + */ + static public class Spec { + /** Input pin mode. */ + public enum Mode { + /** + * Pin is floating. When the pin is left disconnected the value + * sensed is undefined. Use this mode when an external pull-up or + * pull-down resistor is used or when interfacing push-pull type + * logic circuits. + */ + FLOATING, + /** + * Internal pull-up resistor is used. When the pin is left + * disconnected, a logical "HIGH" (true) will be sensed. This is + * useful for interfacing with open drain circuits or for + * interacting with a switch connected between the pin and ground. + */ + PULL_UP, + /** + * Internal pull-down resistor is used. When the pin is left + * disconnected, a logical "LOW" (false) will be sensed. This is + * useful for interacting with a switch connected between the pin + * and Vdd. + */ + PULL_DOWN + } + + /** The pin number, as labeled on the board. */ + public int pin; + /** The pin mode. */ + public Mode mode; + + /** + * Constructor. + * + * @param pin + * Pin number, as labeled on the board. + * @param mode + * Pin mode. + */ + public Spec(int pin, Mode mode) { + this.pin = pin; + this.mode = mode; + } + + /** Shorthand for Spec(pin, Mode.FLOATING). */ + public Spec(int pin) { + this(pin, Mode.FLOATING); + } + } + + /** + * Read the value sensed on the pin. May block for a few milliseconds if + * called right after creation of the instance. If this is a problem, the + * calling thread may be interrupted. + * + * @return True for logical "HIGH", false for logical "LOW". + * @throws InterruptedException + * The calling thread has been interrupted. + * @throws ConnectionLostException + * The connection with the IOIO has been lost. + */ + public boolean read() throws InterruptedException, ConnectionLostException; + + /** + * Block until a desired logical level is sensed. The calling thread can be + * interrupted for aborting this operation. + * + * @param value + * The desired logical level. true for "HIGH", false for "LOW". + * @throws InterruptedException + * The calling thread has been interrupted. + * @throws ConnectionLostException + * The connection with the IOIO has been lost. + */ + public void waitForValue(boolean value) throws InterruptedException, + ConnectionLostException; +} diff --git a/IOIOLib/src/ioio/lib/api/DigitalOutput.java b/IOIOLib/src/ioio/lib/api/DigitalOutput.java index fe8a09d..6d6a2a5 100644 --- a/IOIOLib/src/ioio/lib/api/DigitalOutput.java +++ b/IOIOLib/src/ioio/lib/api/DigitalOutput.java @@ -1,120 +1,120 @@ -/* - * Copyright 2011 Ytai Ben-Tsvi. All rights reserved. - * - * - * Redistribution and use in source and binary forms, with or without modification, are - * permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND - * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ARSHAN POURSOHI OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * The views and conclusions contained in the software and documentation are those of the - * authors and should not be interpreted as representing official policies, either expressed - * or implied. - */ -package ioio.lib.api; - -import ioio.lib.api.exception.ConnectionLostException; - -/** - * A pin used for digital output. - *
- * A digital input pin can be used to generate logic-level signals. - * DigitalOutput instances are obtained by calling - * {@link IOIO#openDigitalOutput(Spec, boolean)}. - *
- * The value of the pin is set by calling {@link #write(boolean)}. - *
- * The instance is alive since its creation. If the connection with the IOIO - * drops at any point, the instance transitions to a disconnected state, in - * which every attempt to use the pin (except {@link #close()}) will throw a - * {@link ConnectionLostException}. Whenever {@link #close()} is invoked the - * instance may no longer be used. Any resources associated with it are freed - * and can be reused. - *
- * Typical usage: - * - *
- * DigitalOutput led = ioio.openDigitalInput(2); // LED anode on pin 2. - * led.write(true); // turn LED on. - * ... - * led.close(); // pin 2 can now be used for something else. - *- */ -public interface DigitalOutput extends Closeable { - /** - * A digital output pin specification, used when opening digital outputs. - */ - public static class Spec { - /** Output pin mode. */ - public enum Mode { - /** - * Pin operates in push-pull mode, i.e. a logical "HIGH" is - * represented by a voltage of Vdd on the pin and a logical "LOW" by - * a voltage of 0 (ground). - */ - NORMAL, - /** - * Pin operates in open-drain mode, i.e. a logical "HIGH" is - * represented by a high impedance on the pin (as if it is - * disconnected) and a logical "LOW" by a voltage of 0 (ground). - * This mode is most commonly used for generating 5V logical signal - * on a 3.3V pin: 5V tolerant pins must be used; a pull-up resistor - * is connected between the pin and 5V, and the pin is used in open- - * drain mode. - */ - OPEN_DRAIN, - } - - /** The pin number, as labeled on the board. */ - public int pin; - /** The pin mode. */ - public Mode mode; - - /** - * Constructor. - * - * @param pin - * Pin number, as labeled on the board. - * @param mode - * Pin mode. - */ - public Spec(int pin, Mode mode) { - this.pin = pin; - this.mode = mode; - } - - /** - * Shorthand for Spec(pin, Mode.NORMAL). - * - * @see #Spec(int, Mode) - */ - public Spec(int pin) { - this(pin, Mode.NORMAL); - } - } - - /** - * Set the output of the pin. - * - * @param val - * The output. true is logical "HIGH", false is logical "LOW". - * @throws ConnectionLostException - * The connection with the IOIO has been lost. - */ - public void write(boolean val) throws ConnectionLostException; -} +/* + * Copyright 2011 Ytai Ben-Tsvi. All rights reserved. + * + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ARSHAN POURSOHI OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied. + */ +package ioio.lib.api; + +import ioio.lib.api.exception.ConnectionLostException; + +/** + * A pin used for digital output. + *
+ * A digital input pin can be used to generate logic-level signals. + * DigitalOutput instances are obtained by calling + * {@link IOIO#openDigitalOutput(DigitalOutput.Spec, boolean)}. + *
+ * The value of the pin is set by calling {@link #write(boolean)}. + *
+ * The instance is alive since its creation. If the connection with the IOIO + * drops at any point, the instance transitions to a disconnected state, in + * which every attempt to use the pin (except {@link #close()}) will throw a + * {@link ConnectionLostException}. Whenever {@link #close()} is invoked the + * instance may no longer be used. Any resources associated with it are freed + * and can be reused. + *
+ * Typical usage: + * + *
+ * DigitalOutput led = ioio.openDigitalInput(2); // LED anode on pin 2. + * led.write(true); // turn LED on. + * ... + * led.close(); // pin 2 can now be used for something else. + *+ */ +public interface DigitalOutput extends Closeable { + /** + * A digital output pin specification, used when opening digital outputs. + */ + public static class Spec { + /** Output pin mode. */ + public enum Mode { + /** + * Pin operates in push-pull mode, i.e. a logical "HIGH" is + * represented by a voltage of Vdd on the pin and a logical "LOW" by + * a voltage of 0 (ground). + */ + NORMAL, + /** + * Pin operates in open-drain mode, i.e. a logical "HIGH" is + * represented by a high impedance on the pin (as if it is + * disconnected) and a logical "LOW" by a voltage of 0 (ground). + * This mode is most commonly used for generating 5V logical signal + * on a 3.3V pin: 5V tolerant pins must be used; a pull-up resistor + * is connected between the pin and 5V, and the pin is used in open- + * drain mode. + */ + OPEN_DRAIN, + } + + /** The pin number, as labeled on the board. */ + public int pin; + /** The pin mode. */ + public Mode mode; + + /** + * Constructor. + * + * @param pin + * Pin number, as labeled on the board. + * @param mode + * Pin mode. + */ + public Spec(int pin, Mode mode) { + this.pin = pin; + this.mode = mode; + } + + /** + * Shorthand for Spec(pin, Mode.NORMAL). + * + * @see DigitalOutput.Spec#Spec(int, DigitalOutput.Spec.Mode) + */ + public Spec(int pin) { + this(pin, Mode.NORMAL); + } + } + + /** + * Set the output of the pin. + * + * @param val + * The output. true is logical "HIGH", false is logical "LOW". + * @throws ConnectionLostException + * The connection with the IOIO has been lost. + */ + public void write(boolean val) throws ConnectionLostException; +} diff --git a/IOIOLib/src/ioio/lib/api/IOIO.java b/IOIOLib/src/ioio/lib/api/IOIO.java index 4851f5c..627e2e7 100644 --- a/IOIOLib/src/ioio/lib/api/IOIO.java +++ b/IOIOLib/src/ioio/lib/api/IOIO.java @@ -1,638 +1,696 @@ -/* - * Copyright 2011 Ytai Ben-Tsvi. All rights reserved. - * - * - * Redistribution and use in source and binary forms, with or without modification, are - * permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND - * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ARSHAN POURSOHI OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * The views and conclusions contained in the software and documentation are those of the - * authors and should not be interpreted as representing official policies, either expressed - * or implied. - */ -package ioio.lib.api; - -import ioio.lib.api.PulseInput.PulseMode; -import ioio.lib.api.TwiMaster.Rate; -import ioio.lib.api.Uart.Parity; -import ioio.lib.api.Uart.StopBits; -import ioio.lib.api.exception.ConnectionLostException; -import ioio.lib.api.exception.IncompatibilityException; -import ioio.lib.api.exception.OutOfResourceException; - -import java.io.Closeable; - -/** - * This interface provides control over all the IOIO board functions. - *
- * An instance of this interface is typically obtained by using the - * {@link IOIOFactory} class. Initially, a connection should be established, by - * calling {@link #waitForConnect()}. This method will block until the board is - * connected an a connection has been established. - *
- * During the connection process, this library verifies that the IOIO firmware - * is compatible with the required version. If not, {@link #waitForConnect()} - * will throw a {@link IncompatibilityException}, putting the {@link IOIO} - * instance in a "zombie" state: nothing could be done with it except calling - * {@link #disconnect()}, or waiting for the physical connection to drop via - * {@link #waitForDisconnect()}. - *
- * As soon as a connection is established, the IOIO can be used, typically, by - * calling the openXXX() functions to obtain additional interfaces for - * controlling specific function of the board. - *
- * Whenever a connection is lost as a result of physically disconnecting the - * board or as a result of calling {@link #disconnect()}, this instance and all - * the interfaces obtained from it become invalid, and will throw a - * {@link ConnectionLostException} on every operation. Once the connection is - * lost, those instances cannot be recycled, but rather it is required to create - * new ones and wait for a connection again. - *
- * Initially all pins are tri-stated (floating), and all functions are disabled. - * Whenever a connection is lost or dropped, the board will immediately return - * to the this initial state. - *
- * Typical usage: - * - *
- * IOIO ioio = IOIOFactory.create();
- * try {
- * ioio.waitForConnect();
- * DigitalOutput out = ioio.openDigitalOutput(10);
- * out.write(true);
- * ...
- * } catch (ConnectionLostException e) {
- * } catch (Exception e) {
- * ioio.disconnect();
- * } finally {
- * ioio.waitForDisconnect();
- * }
- *
- *
- * @see IOIOFactory#create()
- */
-public interface IOIO {
- /** An invalid pin number. */
- public static final int INVALID_PIN = -1;
- /** The pin number used to designate the on-board 'stat' LED. */
- public static final int LED_PIN = 0;
-
- /**
- * A versioned component in the system.
- *
- * @see IOIO#getImplVersion(VersionType)
- */
- public enum VersionType {
- /** Hardware version. */
- HARDWARE_VER,
- /** Bootloader version. */
- BOOTLOADER_VER,
- /** Application layer firmware version. */
- APP_FIRMWARE_VER,
- /** IOIOLib version. */
- IOIOLIB_VER
- }
-
- /**
- * Establishes connection with the IOIO board.
- * - * This method is blocking until connection is established. This method can - * be aborted by calling {@link #disconnect()}. In this case, it will throw - * a {@link ConnectionLostException}. - * - * @throws ConnectionLostException - * An error occurred during connection or disconnect() has been - * called during connection. The instance state is disconnected. - * @throws IncompatibilityException - * An incompatible board firmware of hardware has been detected. - * The instance state is disconnected. - * @see #disconnect() - * @see #waitForDisconnect() - */ - public void waitForConnect() throws ConnectionLostException, - IncompatibilityException; - - /** - * Closes the connection to the board, or aborts a connection process - * started with waitForConnect(). - *
- * Once this method is called, this IOIO instance and all the instances - * obtain from it become invalid and will throw an exception on every - * operation. - *
- * This method is asynchronous, i.e. it returns immediately, but it is not - * guaranteed that all connection-related resources has already been freed - * and can be reused upon return. In cases when this is important, client - * can call {@link #waitForDisconnect()}, which will block until all - * resources have been freed. - */ - public void disconnect(); - - /** - * Blocks until IOIO has been disconnected and all connection-related - * resources have been freed, so that a new connection can be attempted. - * - * @throws InterruptedException - * When interrupt() has been called on this thread. This might - * mean that an immediate attempt to create and connect a new - * IOIO object might fail for resource contention. - * @see #disconnect() - * @see #waitForConnect() - */ - public void waitForDisconnect() throws InterruptedException; - - /** - * Resets the entire state (returning to initial state), without dropping - * the connection. - *
- * It is equivalent to calling {@link Closeable#close()} on every interface - * obtained from this instance. A connection must have been established - * prior to calling this method, by invoking {@link #waitForConnect()}. - * - * @throws ConnectionLostException - * Connection was lost before or during the execution of this - * method. - * @see #hardReset() - */ - public void softReset() throws ConnectionLostException; - - /** - * Equivalent to disconnecting and reconnecting the board power supply. - *
- * The connection will be dropped and not reestablished. Full boot sequence - * will take place, so firmware upgrades can be performed. A connection must - * have been established prior to calling this method, by invoking - * {@link #waitForConnect()}. - * - * @throws ConnectionLostException - * Connection was lost before or during the execution of this - * method. - * @see #softReset() - */ - public void hardReset() throws ConnectionLostException; - - /** - * Query the implementation version of the system's components. The - * implementation version uniquely identifies a hardware revision or a - * software build. Returned version IDs are always 8-character long, - * according to the IOIO versioning system: first 4 characters are the - * version authority and last 4 characters are the revision. - * - * @param v - * The component whose version we query. - * @return An 8-character implementation version ID. - * @throws ConnectionLostException - * Connection was lost before or during the execution of this - * method. - */ - public String getImplVersion(VersionType v) throws ConnectionLostException; - - /** - * Open a pin for digital input. - *
- * A digital input pin can be used to read logic-level signals. The pin will - * operate in this mode until close() is invoked on the returned interface. - * It is illegal to open a pin that has already been opened and has not been - * closed. A connection must have been established prior to calling this - * method, by invoking {@link #waitForConnect()}. - * - * @param spec - * Pin specification, consisting of the pin number, as labeled on - * the board, and the mode, which determines whether the pin will - * be floating, pull-up or pull-down. See - * {@link DigitalInput.Spec.Mode} for more information. - * @return Interface of the assigned pin. - * @throws ConnectionLostException - * Connection was lost before or during the execution of this - * method. - * @see DigitalInput - */ - public DigitalInput openDigitalInput(DigitalInput.Spec spec) - throws ConnectionLostException; - - /** - * Shorthand for openDigitalInput(new DigitalInput.Spec(pin)). - * - * @see #openDigitalInput(ioio.lib.api.DigitalInput.Spec) - */ - public DigitalInput openDigitalInput(int pin) - throws ConnectionLostException; - - /** - * Shorthand for openDigitalInput(new DigitalInput.Spec(pin, mode)). - * - * @see #openDigitalInput(ioio.lib.api.DigitalInput.Spec) - */ - public DigitalInput openDigitalInput(int pin, DigitalInput.Spec.Mode mode) - throws ConnectionLostException; - - /** - * Open a pin for digital output. - *
- * A digital output pin can be used to generate logic-level signals. The pin - * will operate in this mode until close() is invoked on the returned - * interface. It is illegal to open a pin that has already been opened and - * has not been closed. A connection must have been established prior to - * calling this method, by invoking {@link #waitForConnect()}. - * - * @param spec - * Pin specification, consisting of the pin number, as labeled on - * the board, and the mode, which determines whether the pin will - * be normal or open-drain. See {@link DigitalOutput.Spec.Mode} - * for more information. - * @param startValue - * The initial logic level this pin will generate as soon at it - * is open. - * @return Interface of the assigned pin. - * @throws ConnectionLostException - * Connection was lost before or during the execution of this - * method. - * @see DigitalOutput - */ - public DigitalOutput openDigitalOutput(DigitalOutput.Spec spec, - boolean startValue) throws ConnectionLostException; - - /** - * Shorthand for openDigitalOutput(new DigitalOutput.Spec(pin, mode), - * startValue). - * - * @see #openDigitalOutput(ioio.lib.api.DigitalOutput.Spec, boolean) - */ - public DigitalOutput openDigitalOutput(int pin, - DigitalOutput.Spec.Mode mode, boolean startValue) - throws ConnectionLostException; - - /** - * Shorthand for openDigitalOutput(new DigitalOutput.Spec(pin), startValue). - * Pin mode will be "normal" (as opposed to "open-drain". - * - * @see #openDigitalOutput(ioio.lib.api.DigitalOutput.Spec, boolean) - */ - public DigitalOutput openDigitalOutput(int pin, boolean startValue) - throws ConnectionLostException; - - /** - * Shorthand for openDigitalOutput(new DigitalOutput.Spec(pin), false). Pin - * mode will be "normal" (as opposed to "open-drain". - * - * @see #openDigitalOutput(ioio.lib.api.DigitalOutput.Spec, boolean) - */ - public DigitalOutput openDigitalOutput(int pin) - throws ConnectionLostException; - - /** - * Open a pin for analog input. - *
- * An analog input pin can be used to measure voltage. Note that not every - * pin can be used as an analog input. See board documentation for the legal - * pins and permitted voltage range. - *
- * The pin will operate in this mode until close() is invoked on the - * returned interface. It is illegal to open a pin that has already been - * opened and has not been closed. A connection must have been established - * prior to calling this method, by invoking {@link #waitForConnect()}. - * - * @param pin - * Pin number, as labeled on the board. - * @return Interface of the assigned pin. - * @throws ConnectionLostException - * Connection was lost before or during the execution of this - * method. - * @see AnalogInput - */ - public AnalogInput openAnalogInput(int pin) throws ConnectionLostException; - - /** - * Open a pin for PWM (Pulse-Width Modulation) output. - *
- * A PWM pin produces a logic-level PWM signal. These signals are typically - * used for simulating analog outputs for controlling the intensity of LEDs, - * the rotation speed of motors, etc. They are also frequently used for - * controlling hobby servo motors. - *
- * Note that not every pin can be used as PWM output. In addition, the total - * number of concurrent PWM modules in use is limited. See board - * documentation for the legal pins and limit on concurrent usage. - *
- * The pin will operate in this mode until close() is invoked on the - * returned interface. It is illegal to open a pin that has already been - * opened and has not been closed. A connection must have been established - * prior to calling this method, by invoking {@link #waitForConnect()}. - * - * @param spec - * Pin specification, consisting of the pin number, as labeled on - * the board, and the mode, which determines whether the pin will - * be normal or open-drain. See {@link DigitalOutput.Spec.Mode} - * for more information. - * @param freqHz - * PWM frequency, in Hertz. - * @return Interface of the assigned pin. - * @throws ConnectionLostException - * Connection was lost before or during the execution of this - * method. - * @throws OutOfResourceException - * This is a runtime exception, so it is not necessary to catch - * it if the client guarantees that the total number of - * concurrent PWM resources is not exceeded. - * @see PwmOutput - */ - public PwmOutput openPwmOutput(DigitalOutput.Spec spec, int freqHz) - throws ConnectionLostException; - - /** - * Shorthand for openPwmOutput(new DigitalOutput.Spec(pin), freqHz). - * - * @see #openPwmOutput(ioio.lib.api.DigitalOutput.Spec, int) - */ - public PwmOutput openPwmOutput(int pin, int freqHz) - throws ConnectionLostException; - - /** - * Open a pin for pulse input. - *
- * The pulse input module is quite flexible. It enables several kinds of - * timing measurements on a digital signal: pulse width measurement - * (positive or negative pulse), and frequency of a periodic signal. - *
- * Note that not every pin can be used as pulse input. In addition, the - * total number of concurrent pulse input modules in use is limited. See - * board documentation for the legal pins and limit on concurrent usage. - *
- * The pin will operate in this mode until close() is invoked on the - * returned interface. It is illegal to open a pin that has already been - * opened and has not been closed. A connection must have been established - * prior to calling this method, by invoking {@link #waitForConnect()}. - * - * @param spec - * Pin specification, consisting of the pin number, as labeled on - * the board, and the mode, which determines whether the pin will - * be floating, pull-up or pull-down. See - * {@link DigitalInput.Spec.Mode} for more information. - * @param rate - * The clock rate to use for timing the signal. A faster clock - * rate will result in better precision but will only be able to - * measure narrow pulses / high frequencies. - * @param mode - * The mode in which to operate. Determines whether the module - * will measure pulse durations or frequency. - * @param doublePrecision - * Whether to open a double-precision pulse input module. Double- - * precision modules enable reading of much longer pulses and - * lower frequencies with high accuracy than single precision - * modules. However, their number is limited, so when possible, - * and if the resources are all needed, use single-precision. For - * more details on the exact spec of single- vs. double- - * precision, see {@link PulseInput}. - * @return An instance of the {@link PulseInput}, which can be used to - * obtain the data. - * @throws ConnectionLostException - * Connection was lost before or during the execution of this - * method. - * @throws OutOfResourceException - * This is a runtime exception, so it is not necessary to catch - * it if the client guarantees that the total number of - * concurrent PWM resources is not exceeded. - * @see PulseInput - */ - public PulseInput openPulseInput(DigitalInput.Spec spec, - PulseInput.ClockRate rate, PulseInput.PulseMode mode, - boolean doublePrecision) throws ConnectionLostException; - - /** - * Shorthand for openPulseInput(new DigitalInput.Spec(pin), rate, mode, - * true), i.e. opens a double-precision, 16MHz pulse input on the given pin - * with the given mode. - * - * @see #openPulseInput(ioio.lib.api.DigitalInput.Spec, - * ioio.lib.api.PulseInput.ClockRate, PulseMode, boolean) - */ - public PulseInput openPulseInput(int pin, PulseMode mode) - throws ConnectionLostException; - - /** - * Open a UART module, enabling a bulk transfer of byte buffers. - *
- * UART is a very common hardware communication protocol, enabling full- - * duplex, asynchronous point-to-point data transfer. It typically serves - * for opening consoles or as a basis for higher-level protocols, such as - * MIDI RS-232, and RS-485. - *
- * Note that not every pin can be used for UART RX or TX. In addition, the - * total number of concurrent UART modules in use is limited. See board - * documentation for the legal pins and limit on concurrent usage. - *
- * The UART module will operate, and the pins will work in their respective - * modes until close() is invoked on the returned interface. It is illegal - * to use pins that have already been opened and has not been closed. A - * connection must have been established prior to calling this method, by - * invoking {@link #waitForConnect()}. - * - * @param rx - * Pin specification for the RX pin, consisting of the pin - * number, as labeled on the board, and the mode, which - * determines whether the pin will be floating, pull-up or - * pull-down. See {@link DigitalInput.Spec.Mode} for more - * information. null can be passed to designate that we do not - * want RX input to this module. - * @param tx - * Pin specification for the TX pin, consisting of the pin - * number, as labeled on the board, and the mode, which - * determines whether the pin will be normal or open-drain. See - * {@link DigitalOutput.Spec.Mode} for more information. null can - * be passed to designate that we do not want TX output to this - * module. - * @param baud - * The clock frequency of the UART module in Hz. - * @param parity - * The parity mode, as in {@link Parity}. - * @param stopbits - * Number of stop bits, as in {@link StopBits}. - * @return Interface of the assigned module. - * @throws ConnectionLostException - * Connection was lost before or during the execution of this - * method. - * @throws OutOfResourceException - * This is a runtime exception, so it is not necessary to catch - * it if the client guarantees that the total number of - * concurrent UART resources is not exceeded. - * @see Uart - */ - public Uart openUart(DigitalInput.Spec rx, DigitalOutput.Spec tx, int baud, - Parity parity, StopBits stopbits) throws ConnectionLostException; - - /** - * Shorthand for - * {@link #openUart(ioio.lib.api.DigitalInput.Spec, ioio.lib.api.DigitalOutput.Spec, int, Parity, StopBits)} - * , where the input pins use their default specs. {@link #INVALID_PIN} can - * be used on either pin if a TX- or RX-only UART is needed. - * - * @see #openUart(ioio.lib.api.DigitalInput.Spec, - * ioio.lib.api.DigitalOutput.Spec, int, Parity, StopBits) - */ - public Uart openUart(int rx, int tx, int baud, Parity parity, - StopBits stopbits) throws ConnectionLostException; - - /** - * Open a SPI master module, enabling communication with multiple - * SPI-enabled slave modules. - *
- * SPI is a common hardware communication protocol, enabling full-duplex, - * synchronous point-to-multi-point data transfer. It requires MOSI, MISO - * and CLK lines shared by all nodes, as well as a SS line per slave, - * connected between this slave and a respective pin on the master. The MISO - * line should operate in pull-up mode, using either the internal pull-up or - * an external resistor. - *
- * Note that not every pin can be used for SPI MISO, MOSI or CLK. In - * addition, the total number of concurrent SPI modules in use is limited. - * See board documentation for the legal pins and limit on concurrent usage. - *
- * The SPI module will operate, and the pins will work in their respective - * modes until close() is invoked on the returned interface. It is illegal - * to use pins that have already been opened and has not been closed. A - * connection must have been established prior to calling this method, by - * invoking {@link #waitForConnect()}. - * - * @param miso - * Pin specification for the MISO (Master In Slave Out) pin, - * consisting of the pin number, as labeled on the board, and the - * mode, which determines whether the pin will be floating, - * pull-up or pull-down. See {@link DigitalInput.Spec.Mode} for - * more information. - * @param mosi - * Pin specification for the MOSI (Master Out Slave In) pin, - * consisting of the pin number, as labeled on the board, and the - * mode, which determines whether the pin will be normal or - * open-drain. See {@link DigitalOutput.Spec.Mode} for more - * information. - * @param clk - * Pin specification for the CLK pin, consisting of the pin - * number, as labeled on the board, and the mode, which - * determines whether the pin will be normal or open-drain. See - * {@link DigitalOutput.Spec.Mode} for more information. - * @param slaveSelect - * An array of pin specifications for each of the slaves' SS - * (Slave Select) pin. The index of this array designates the - * slave index, used later to refer to this slave. The spec is - * consisting of the pin number, as labeled on the board, and the - * mode, which determines whether the pin will be normal or - * open-drain. See {@link DigitalOutput.Spec.Mode} for more - * information. - * @param config - * The configuration of the SPI module. See - * {@link SpiMaster.Config} for details. - * @return Interface of the assigned module. - * @throws ConnectionLostException - * Connection was lost before or during the execution of this - * method. - * @throws OutOfResourceException - * This is a runtime exception, so it is not necessary to catch - * it if the client guarantees that the total number of - * concurrent SPI resources is not exceeded. - * @see SpiMaster - */ - public SpiMaster openSpiMaster(DigitalInput.Spec miso, - DigitalOutput.Spec mosi, DigitalOutput.Spec clk, - DigitalOutput.Spec[] slaveSelect, SpiMaster.Config config) - throws ConnectionLostException; - -/** - * Shorthand for {@link #openSpiMaster(ioio.lib.api.DigitalInput.Spec, ioio.lib.api.DigitalOutput.Spec, ioio.lib.api.DigitalOutput.Spec, ioio.lib.api.DigitalOutput.Spec[], ioio.lib.api.SpiMaster.Config), - * where the pins are all open with the default modes and default configuration values are used. - * @see #openSpiMaster(ioio.lib.api.DigitalInput.Spec, - * ioio.lib.api.DigitalOutput.Spec, ioio.lib.api.DigitalOutput.Spec, - * ioio.lib.api.DigitalOutput.Spec[], ioio.lib.api.SpiMaster.Config) - */ - public SpiMaster openSpiMaster(int miso, int mosi, int clk, - int[] slaveSelect, SpiMaster.Rate rate) - throws ConnectionLostException; - - /** - * Shorthand for {@link #openSpiMaster(ioio.lib.api.DigitalInput.Spec, ioio.lib.api.DigitalOutput.Spec, ioio.lib.api.DigitalOutput.Spec, ioio.lib.api.DigitalOutput.Spec[], ioio.lib.api.SpiMaster.Config), - * where the MISO pins is opened with pull up, and the other pins are open - * with the default modes and default configuration values are used. - * In this version, a single slave is used. - * @see #openSpiMaster(ioio.lib.api.DigitalInput.Spec, - * ioio.lib.api.DigitalOutput.Spec, ioio.lib.api.DigitalOutput.Spec, - * ioio.lib.api.DigitalOutput.Spec[], ioio.lib.api.SpiMaster.Config) - */ - public SpiMaster openSpiMaster(int miso, int mosi, int clk, - int slaveSelect, SpiMaster.Rate rate) - throws ConnectionLostException; - - /** - * Open a TWI (Two-Wire Interface, such as I2C/SMBus) master module, - * enabling communication with multiple TWI-enabled slave modules. - *
- * TWI is a common hardware communication protocol, enabling half-duplex, - * synchronous point-to-multi-point data transfer. It requires a physical - * connection of two lines (SDA, SCL) shared by all the bus nodes, where the - * SDA is open-drain and externally pulled-up. - *
- * Note that there is a fixed number of TWI modules, and the pins they use - * are static. Client has to make sure these pins are not already opened - * before calling this method. See board documentation for the number of - * modules and the respective pins they use. - *
- * The TWI module will operate, and the pins will work in their respective - * modes until close() is invoked on the returned interface. It is illegal - * to use pins that have already been opened and has not been closed. A - * connection must have been established prior to calling this method, by - * invoking {@link #waitForConnect()}. - * - * @param twiNum - * The TWI module index to use. Will also determine the pins - * used. - * @param rate - * The clock rate. Can be 100KHz / 400KHz / 1MHz. - * @param smbus - * When true, will use SMBus voltage levels. When false, I2C - * voltage levels. - * @return Interface of the assigned module. - * @throws ConnectionLostException - * Connection was lost before or during the execution of this - * method. - * @see TwiMaster - */ - public TwiMaster openTwiMaster(int twiNum, Rate rate, boolean smbus) - throws ConnectionLostException; - - /** - * Open an ICSP channel, enabling Flash programming of an external PIC MCU, - * and in particular, another IOIO board. - *
- * ICSP (In-Circuit Serial Programming) is a protocol intended for - * programming of PIC MCUs. It is a serial protocol over three wires: PGC - * (clock), PGD (data) and MCLR (reset), where PGC and MCLR are controlled - * by the master and PGD is shared by the master and slave, depending on the - * transaction state. - *
- * Note that there is only one ICSP modules, and the pins it uses are - * static. Client has to make sure that the ICSP module is not already in - * use, as well as those dedicated pins. See board documentation for the - * actual pins used for ICSP. - * - * @return Interface of the ICSP module. - * @see IcspMaster - * @throws ConnectionLostException - * Connection was lost before or during the execution of this - * method. - */ - public IcspMaster openIcspMaster() throws ConnectionLostException; -} \ No newline at end of file +/* + * Copyright 2011 Ytai Ben-Tsvi. All rights reserved. + * + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ARSHAN POURSOHI OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied. + */ +package ioio.lib.api; + +import ioio.lib.api.PulseInput.PulseMode; +import ioio.lib.api.TwiMaster.Rate; +import ioio.lib.api.Uart.Parity; +import ioio.lib.api.Uart.StopBits; +import ioio.lib.api.exception.ConnectionLostException; +import ioio.lib.api.exception.IncompatibilityException; +import ioio.lib.api.exception.OutOfResourceException; + +import java.io.Closeable; + +/** + * This interface provides control over all the IOIO board functions. + *
+ * An instance of this interface is typically obtained by using the + * {@link IOIOFactory} class. Initially, a connection should be established, by + * calling {@link #waitForConnect()}. This method will block until the board is + * connected an a connection has been established. + *
+ * During the connection process, this library verifies that the IOIO firmware + * is compatible with the required version. If not, {@link #waitForConnect()} + * will throw a {@link IncompatibilityException}, putting the {@link IOIO} + * instance in a "zombie" state: nothing could be done with it except calling + * {@link #disconnect()}, or waiting for the physical connection to drop via + * {@link #waitForDisconnect()}. + *
+ * As soon as a connection is established, the IOIO can be used, typically, by + * calling the openXXX() functions to obtain additional interfaces for + * controlling specific function of the board. + *
+ * Whenever a connection is lost as a result of physically disconnecting the + * board or as a result of calling {@link #disconnect()}, this instance and all + * the interfaces obtained from it become invalid, and will throw a + * {@link ConnectionLostException} on every operation. Once the connection is + * lost, those instances cannot be recycled, but rather it is required to create + * new ones and wait for a connection again. + *
+ * Initially all pins are tri-stated (floating), and all functions are disabled. + * Whenever a connection is lost or dropped, the board will immediately return + * to the this initial state. + *
+ * Typical usage: + * + *
+ * IOIO ioio = IOIOFactory.create();
+ * try {
+ * ioio.waitForConnect();
+ * DigitalOutput out = ioio.openDigitalOutput(10);
+ * out.write(true);
+ * ...
+ * } catch (ConnectionLostException e) {
+ * } catch (Exception e) {
+ * ioio.disconnect();
+ * } finally {
+ * ioio.waitForDisconnect();
+ * }
+ *
+ *
+ * @see IOIOFactory#create()
+ */
+public interface IOIO {
+ /** An invalid pin number. */
+ public static final int INVALID_PIN = -1;
+ /** The pin number used to designate the on-board 'stat' LED. */
+ public static final int LED_PIN = 0;
+
+ /**
+ * A versioned component in the system.
+ *
+ * @see IOIO#getImplVersion(VersionType)
+ */
+ public enum VersionType {
+ /** Hardware version. */
+ HARDWARE_VER,
+ /** Bootloader version. */
+ BOOTLOADER_VER,
+ /** Application layer firmware version. */
+ APP_FIRMWARE_VER,
+ /** IOIOLib version. */
+ IOIOLIB_VER
+ }
+
+ /**
+ * A state of a IOIO instance.
+ */
+ public enum State {
+ /** Connection not yet established. */
+ INIT,
+ /** Connected. */
+ CONNECTED,
+ /** Connection established, incompatible firmware detected. */
+ INCOMPATIBLE,
+ /** Disconnected. Instance is useless. */
+ DEAD
+ }
+
+ /**
+ * Establishes connection with the IOIO board.
+ * + * This method is blocking until connection is established. This method can + * be aborted by calling {@link #disconnect()}. In this case, it will throw + * a {@link ConnectionLostException}. + * + * @throws ConnectionLostException + * An error occurred during connection or disconnect() has been + * called during connection. The instance state is disconnected. + * @throws IncompatibilityException + * An incompatible board firmware of hardware has been detected. + * The instance state is disconnected. + * @see #disconnect() + * @see #waitForDisconnect() + */ + public void waitForConnect() throws ConnectionLostException, + IncompatibilityException; + + /** + * Closes the connection to the board, or aborts a connection process + * started with waitForConnect(). + *
+ * Once this method is called, this IOIO instance and all the instances + * obtain from it become invalid and will throw an exception on every + * operation. + *
+ * This method is asynchronous, i.e. it returns immediately, but it is not + * guaranteed that all connection-related resources has already been freed + * and can be reused upon return. In cases when this is important, client + * can call {@link #waitForDisconnect()}, which will block until all + * resources have been freed. + */ + public void disconnect(); + + /** + * Blocks until IOIO has been disconnected and all connection-related + * resources have been freed, so that a new connection can be attempted. + * + * @throws InterruptedException + * When interrupt() has been called on this thread. This might + * mean that an immediate attempt to create and connect a new + * IOIO object might fail for resource contention. + * @see #disconnect() + * @see #waitForConnect() + */ + public void waitForDisconnect() throws InterruptedException; + + /** + * Gets the connections state. + * + * @return The connection state. + */ + public State getState(); + + /** + * Resets the entire state (returning to initial state), without dropping + * the connection. + *
+ * It is equivalent to calling {@link Closeable#close()} on every interface + * obtained from this instance. A connection must have been established + * prior to calling this method, by invoking {@link #waitForConnect()}. + * + * @throws ConnectionLostException + * Connection was lost before or during the execution of this + * method. + * @see #hardReset() + */ + public void softReset() throws ConnectionLostException; + + /** + * Equivalent to disconnecting and reconnecting the board power supply. + *
+ * The connection will be dropped and not reestablished. Full boot sequence + * will take place, so firmware upgrades can be performed. A connection must + * have been established prior to calling this method, by invoking + * {@link #waitForConnect()}. + * + * @throws ConnectionLostException + * Connection was lost before or during the execution of this + * method. + * @see #softReset() + */ + public void hardReset() throws ConnectionLostException; + + /** + * Query the implementation version of the system's components. The + * implementation version uniquely identifies a hardware revision or a + * software build. Returned version IDs are always 8-character long, + * according to the IOIO versioning system: first 4 characters are the + * version authority and last 4 characters are the revision. + * + * @param v + * The component whose version we query. + * @return An 8-character implementation version ID. + * @throws ConnectionLostException + * Connection was lost before or during the execution of this + * method. + */ + public String getImplVersion(VersionType v) throws ConnectionLostException; + + /** + * Open a pin for digital input. + *
+ * A digital input pin can be used to read logic-level signals. The pin will + * operate in this mode until close() is invoked on the returned interface. + * It is illegal to open a pin that has already been opened and has not been + * closed. A connection must have been established prior to calling this + * method, by invoking {@link #waitForConnect()}. + * + * @param spec + * Pin specification, consisting of the pin number, as labeled on + * the board, and the mode, which determines whether the pin will + * be floating, pull-up or pull-down. See + * {@link DigitalInput.Spec.Mode} for more information. + * @return Interface of the assigned pin. + * @throws ConnectionLostException + * Connection was lost before or during the execution of this + * method. + * @see DigitalInput + */ + public DigitalInput openDigitalInput(DigitalInput.Spec spec) + throws ConnectionLostException; + + /** + * Shorthand for openDigitalInput(new DigitalInput.Spec(pin)). + * + * @see #openDigitalInput(ioio.lib.api.DigitalInput.Spec) + */ + public DigitalInput openDigitalInput(int pin) + throws ConnectionLostException; + + /** + * Shorthand for openDigitalInput(new DigitalInput.Spec(pin, mode)). + * + * @see #openDigitalInput(ioio.lib.api.DigitalInput.Spec) + */ + public DigitalInput openDigitalInput(int pin, DigitalInput.Spec.Mode mode) + throws ConnectionLostException; + + /** + * Open a pin for digital output. + *
+ * A digital output pin can be used to generate logic-level signals. The pin + * will operate in this mode until close() is invoked on the returned + * interface. It is illegal to open a pin that has already been opened and + * has not been closed. A connection must have been established prior to + * calling this method, by invoking {@link #waitForConnect()}. + * + * @param spec + * Pin specification, consisting of the pin number, as labeled on + * the board, and the mode, which determines whether the pin will + * be normal or open-drain. See {@link DigitalOutput.Spec.Mode} + * for more information. + * @param startValue + * The initial logic level this pin will generate as soon at it + * is open. + * @return Interface of the assigned pin. + * @throws ConnectionLostException + * Connection was lost before or during the execution of this + * method. + * @see DigitalOutput + */ + public DigitalOutput openDigitalOutput(DigitalOutput.Spec spec, + boolean startValue) throws ConnectionLostException; + + /** + * Shorthand for openDigitalOutput(new DigitalOutput.Spec(pin, mode), + * startValue). + * + * @see #openDigitalOutput(ioio.lib.api.DigitalOutput.Spec, boolean) + */ + public DigitalOutput openDigitalOutput(int pin, + DigitalOutput.Spec.Mode mode, boolean startValue) + throws ConnectionLostException; + + /** + * Shorthand for openDigitalOutput(new DigitalOutput.Spec(pin), startValue). + * Pin mode will be "normal" (as opposed to "open-drain". + * + * @see #openDigitalOutput(ioio.lib.api.DigitalOutput.Spec, boolean) + */ + public DigitalOutput openDigitalOutput(int pin, boolean startValue) + throws ConnectionLostException; + + /** + * Shorthand for openDigitalOutput(new DigitalOutput.Spec(pin), false). Pin + * mode will be "normal" (as opposed to "open-drain". + * + * @see #openDigitalOutput(ioio.lib.api.DigitalOutput.Spec, boolean) + */ + public DigitalOutput openDigitalOutput(int pin) + throws ConnectionLostException; + + /** + * Open a pin for analog input. + *
+ * An analog input pin can be used to measure voltage. Note that not every + * pin can be used as an analog input. See board documentation for the legal + * pins and permitted voltage range. + *
+ * The pin will operate in this mode until close() is invoked on the + * returned interface. It is illegal to open a pin that has already been + * opened and has not been closed. A connection must have been established + * prior to calling this method, by invoking {@link #waitForConnect()}. + * + * @param pin + * Pin number, as labeled on the board. + * @return Interface of the assigned pin. + * @throws ConnectionLostException + * Connection was lost before or during the execution of this + * method. + * @see AnalogInput + */ + public AnalogInput openAnalogInput(int pin) throws ConnectionLostException; + + /** + * Open a pin for PWM (Pulse-Width Modulation) output. + *
+ * A PWM pin produces a logic-level PWM signal. These signals are typically + * used for simulating analog outputs for controlling the intensity of LEDs, + * the rotation speed of motors, etc. They are also frequently used for + * controlling hobby servo motors. + *
+ * Note that not every pin can be used as PWM output. In addition, the total + * number of concurrent PWM modules in use is limited. See board + * documentation for the legal pins and limit on concurrent usage. + *
+ * The pin will operate in this mode until close() is invoked on the + * returned interface. It is illegal to open a pin that has already been + * opened and has not been closed. A connection must have been established + * prior to calling this method, by invoking {@link #waitForConnect()}. + * + * @param spec + * Pin specification, consisting of the pin number, as labeled on + * the board, and the mode, which determines whether the pin will + * be normal or open-drain. See {@link DigitalOutput.Spec.Mode} + * for more information. + * @param freqHz + * PWM frequency, in Hertz. + * @return Interface of the assigned pin. + * @throws ConnectionLostException + * Connection was lost before or during the execution of this + * method. + * @throws OutOfResourceException + * This is a runtime exception, so it is not necessary to catch + * it if the client guarantees that the total number of + * concurrent PWM resources is not exceeded. + * @see PwmOutput + */ + public PwmOutput openPwmOutput(DigitalOutput.Spec spec, int freqHz) + throws ConnectionLostException; + + /** + * Shorthand for openPwmOutput(new DigitalOutput.Spec(pin), freqHz). + * + * @see #openPwmOutput(ioio.lib.api.DigitalOutput.Spec, int) + */ + public PwmOutput openPwmOutput(int pin, int freqHz) + throws ConnectionLostException; + + /** + * Open a pin for pulse input. + *
+ * The pulse input module is quite flexible. It enables several kinds of + * timing measurements on a digital signal: pulse width measurement + * (positive or negative pulse), and frequency of a periodic signal. + *
+ * Note that not every pin can be used as pulse input. In addition, the + * total number of concurrent pulse input modules in use is limited. See + * board documentation for the legal pins and limit on concurrent usage. + *
+ * The pin will operate in this mode until close() is invoked on the + * returned interface. It is illegal to open a pin that has already been + * opened and has not been closed. A connection must have been established + * prior to calling this method, by invoking {@link #waitForConnect()}. + * + * @param spec + * Pin specification, consisting of the pin number, as labeled on + * the board, and the mode, which determines whether the pin will + * be floating, pull-up or pull-down. See + * {@link DigitalInput.Spec.Mode} for more information. + * @param rate + * The clock rate to use for timing the signal. A faster clock + * rate will result in better precision but will only be able to + * measure narrow pulses / high frequencies. + * @param mode + * The mode in which to operate. Determines whether the module + * will measure pulse durations or frequency. + * @param doublePrecision + * Whether to open a double-precision pulse input module. Double- + * precision modules enable reading of much longer pulses and + * lower frequencies with high accuracy than single precision + * modules. However, their number is limited, so when possible, + * and if the resources are all needed, use single-precision. For + * more details on the exact spec of single- vs. double- + * precision, see {@link PulseInput}. + * @return An instance of the {@link PulseInput}, which can be used to + * obtain the data. + * @throws ConnectionLostException + * Connection was lost before or during the execution of this + * method. + * @throws OutOfResourceException + * This is a runtime exception, so it is not necessary to catch + * it if the client guarantees that the total number of + * concurrent PWM resources is not exceeded. + * @see PulseInput + */ + public PulseInput openPulseInput(DigitalInput.Spec spec, + PulseInput.ClockRate rate, PulseInput.PulseMode mode, + boolean doublePrecision) throws ConnectionLostException; + + /** + * Shorthand for openPulseInput(new DigitalInput.Spec(pin), rate, mode, + * true), i.e. opens a double-precision, 16MHz pulse input on the given pin + * with the given mode. + * + * @see #openPulseInput(ioio.lib.api.DigitalInput.Spec, + * ioio.lib.api.PulseInput.ClockRate, + * ioio.lib.api.PulseInput.PulseMode, boolean) + */ + public PulseInput openPulseInput(int pin, PulseMode mode) + throws ConnectionLostException; + + /** + * Open a UART module, enabling a bulk transfer of byte buffers. + *
+ * UART is a very common hardware communication protocol, enabling full- + * duplex, asynchronous point-to-point data transfer. It typically serves + * for opening consoles or as a basis for higher-level protocols, such as + * MIDI RS-232, and RS-485. + *
+ * Note that not every pin can be used for UART RX or TX. In addition, the + * total number of concurrent UART modules in use is limited. See board + * documentation for the legal pins and limit on concurrent usage. + *
+ * The UART module will operate, and the pins will work in their respective + * modes until close() is invoked on the returned interface. It is illegal + * to use pins that have already been opened and has not been closed. A + * connection must have been established prior to calling this method, by + * invoking {@link #waitForConnect()}. + * + * @param rx + * Pin specification for the RX pin, consisting of the pin + * number, as labeled on the board, and the mode, which + * determines whether the pin will be floating, pull-up or + * pull-down. See {@link DigitalInput.Spec.Mode} for more + * information. null can be passed to designate that we do not + * want RX input to this module. + * @param tx + * Pin specification for the TX pin, consisting of the pin + * number, as labeled on the board, and the mode, which + * determines whether the pin will be normal or open-drain. See + * {@link DigitalOutput.Spec.Mode} for more information. null can + * be passed to designate that we do not want TX output to this + * module. + * @param baud + * The clock frequency of the UART module in Hz. + * @param parity + * The parity mode, as in {@link Parity}. + * @param stopbits + * Number of stop bits, as in {@link StopBits}. + * @return Interface of the assigned module. + * @throws ConnectionLostException + * Connection was lost before or during the execution of this + * method. + * @throws OutOfResourceException + * This is a runtime exception, so it is not necessary to catch + * it if the client guarantees that the total number of + * concurrent UART resources is not exceeded. + * @see Uart + */ + public Uart openUart(DigitalInput.Spec rx, DigitalOutput.Spec tx, int baud, + Parity parity, StopBits stopbits) throws ConnectionLostException; + + /** + * Shorthand for + * {@link #openUart(DigitalInput.Spec, DigitalOutput.Spec, int, Uart.Parity, Uart.StopBits)} + * , where the input pins use their default specs. {@link #INVALID_PIN} can + * be used on either pin if a TX- or RX-only UART is needed. + * + * @see #openUart(DigitalInput.Spec, DigitalOutput.Spec, int, Uart.Parity, + * Uart.StopBits) + */ + public Uart openUart(int rx, int tx, int baud, Parity parity, + StopBits stopbits) throws ConnectionLostException; + + /** + * Open a SPI master module, enabling communication with multiple + * SPI-enabled slave modules. + *
+ * SPI is a common hardware communication protocol, enabling full-duplex, + * synchronous point-to-multi-point data transfer. It requires MOSI, MISO + * and CLK lines shared by all nodes, as well as a SS line per slave, + * connected between this slave and a respective pin on the master. The MISO + * line should operate in pull-up mode, using either the internal pull-up or + * an external resistor. + *
+ * Note that not every pin can be used for SPI MISO, MOSI or CLK. In + * addition, the total number of concurrent SPI modules in use is limited. + * See board documentation for the legal pins and limit on concurrent usage. + *
+ * The SPI module will operate, and the pins will work in their respective + * modes until close() is invoked on the returned interface. It is illegal + * to use pins that have already been opened and has not been closed. A + * connection must have been established prior to calling this method, by + * invoking {@link #waitForConnect()}. + * + * @param miso + * Pin specification for the MISO (Master In Slave Out) pin, + * consisting of the pin number, as labeled on the board, and the + * mode, which determines whether the pin will be floating, + * pull-up or pull-down. See {@link DigitalInput.Spec.Mode} for + * more information. + * @param mosi + * Pin specification for the MOSI (Master Out Slave In) pin, + * consisting of the pin number, as labeled on the board, and the + * mode, which determines whether the pin will be normal or + * open-drain. See {@link DigitalOutput.Spec.Mode} for more + * information. + * @param clk + * Pin specification for the CLK pin, consisting of the pin + * number, as labeled on the board, and the mode, which + * determines whether the pin will be normal or open-drain. See + * {@link DigitalOutput.Spec.Mode} for more information. + * @param slaveSelect + * An array of pin specifications for each of the slaves' SS + * (Slave Select) pin. The index of this array designates the + * slave index, used later to refer to this slave. The spec is + * consisting of the pin number, as labeled on the board, and the + * mode, which determines whether the pin will be normal or + * open-drain. See {@link DigitalOutput.Spec.Mode} for more + * information. + * @param config + * The configuration of the SPI module. See + * {@link SpiMaster.Config} for details. + * @return Interface of the assigned module. + * @throws ConnectionLostException + * Connection was lost before or during the execution of this + * method. + * @throws OutOfResourceException + * This is a runtime exception, so it is not necessary to catch + * it if the client guarantees that the total number of + * concurrent SPI resources is not exceeded. + * @see SpiMaster + */ + public SpiMaster openSpiMaster(DigitalInput.Spec miso, + DigitalOutput.Spec mosi, DigitalOutput.Spec clk, + DigitalOutput.Spec[] slaveSelect, SpiMaster.Config config) + throws ConnectionLostException; + + /** + * Shorthand for + * {@link #openSpiMaster(ioio.lib.api.DigitalInput.Spec, ioio.lib.api.DigitalOutput.Spec, ioio.lib.api.DigitalOutput.Spec, ioio.lib.api.DigitalOutput.Spec[], ioio.lib.api.SpiMaster.Config)} + * , where the pins are all open with the default modes and default + * configuration values are used. + * + * @see #openSpiMaster(ioio.lib.api.DigitalInput.Spec, + * ioio.lib.api.DigitalOutput.Spec, ioio.lib.api.DigitalOutput.Spec, + * ioio.lib.api.DigitalOutput.Spec[], ioio.lib.api.SpiMaster.Config) + */ + public SpiMaster openSpiMaster(int miso, int mosi, int clk, + int[] slaveSelect, SpiMaster.Rate rate) + throws ConnectionLostException; + + /** + * Shorthand for + * {@link #openSpiMaster(ioio.lib.api.DigitalInput.Spec, ioio.lib.api.DigitalOutput.Spec, ioio.lib.api.DigitalOutput.Spec, ioio.lib.api.DigitalOutput.Spec[], ioio.lib.api.SpiMaster.Config)} + * , where the MISO pins is opened with pull up, and the other pins are open + * with the default modes and default configuration values are used. In this + * version, a single slave is used. + * + * @see #openSpiMaster(ioio.lib.api.DigitalInput.Spec, + * ioio.lib.api.DigitalOutput.Spec, ioio.lib.api.DigitalOutput.Spec, + * ioio.lib.api.DigitalOutput.Spec[], ioio.lib.api.SpiMaster.Config) + */ + public SpiMaster openSpiMaster(int miso, int mosi, int clk, + int slaveSelect, SpiMaster.Rate rate) + throws ConnectionLostException; + + /** + * Open a TWI (Two-Wire Interface, such as I2C/SMBus) master module, + * enabling communication with multiple TWI-enabled slave modules. + *
+ * TWI is a common hardware communication protocol, enabling half-duplex, + * synchronous point-to-multi-point data transfer. It requires a physical + * connection of two lines (SDA, SCL) shared by all the bus nodes, where the + * SDA is open-drain and externally pulled-up. + *
+ * Note that there is a fixed number of TWI modules, and the pins they use + * are static. Client has to make sure these pins are not already opened + * before calling this method. See board documentation for the number of + * modules and the respective pins they use. + *
+ * The TWI module will operate, and the pins will work in their respective + * modes until close() is invoked on the returned interface. It is illegal + * to use pins that have already been opened and has not been closed. A + * connection must have been established prior to calling this method, by + * invoking {@link #waitForConnect()}. + * + * @param twiNum + * The TWI module index to use. Will also determine the pins + * used. + * @param rate + * The clock rate. Can be 100KHz / 400KHz / 1MHz. + * @param smbus + * When true, will use SMBus voltage levels. When false, I2C + * voltage levels. + * @return Interface of the assigned module. + * @throws ConnectionLostException + * Connection was lost before or during the execution of this + * method. + * @see TwiMaster + */ + public TwiMaster openTwiMaster(int twiNum, Rate rate, boolean smbus) + throws ConnectionLostException; + + /** + * Open an ICSP channel, enabling Flash programming of an external PIC MCU, + * and in particular, another IOIO board. + *
+ * ICSP (In-Circuit Serial Programming) is a protocol intended for + * programming of PIC MCUs. It is a serial protocol over three wires: PGC + * (clock), PGD (data) and MCLR (reset), where PGC and MCLR are controlled + * by the master and PGD is shared by the master and slave, depending on the + * transaction state. + *
+ * Note that there is only one ICSP modules, and the pins it uses are
+ * static. Client has to make sure that the ICSP module is not already in
+ * use, as well as those dedicated pins. See board documentation for the
+ * actual pins used for ICSP.
+ *
+ * @return Interface of the ICSP module.
+ * @see IcspMaster
+ * @throws ConnectionLostException
+ * Connection was lost before or during the execution of this
+ * method.
+ */
+ public IcspMaster openIcspMaster() throws ConnectionLostException;
+
+ /**
+ * Start a batch of operations. This is strictly an optimization and will
+ * not change functionality: if the client knows that a sequence of several
+ * IOIO operations are going to be performed immediately following each
+ * other, a call to {@link #beginBatch()} before the sequence and
+ * {@link #endBatch()} after the sequence will cause the operations to be
+ * grouped into one transfer to the IOIO, thus reducing latency. A matching
+ * {@link #endBatch()} operation must always follow, or otherwise no
+ * operation will ever be actually executed. {@link #beginBatch()} /
+ * {@link #endBatch()} blocks may be nested - the transfer will occur when
+ * the outermost {@link #endBatch()} is invoked. Note that it is not
+ * guaranteed that no transfers will happen while inside a batch - it should
+ * be treated as a hint. Code running inside the block must be quick as it
+ * blocks all transfers to the IOIO, including those performed from
+ * other threads.
+ *
+ * @throws ConnectionLostException
+ * Connection was lost before or during the execution of this
+ * method.
+ */
+ public void beginBatch() throws ConnectionLostException;
+
+ /**
+ * End a batch of operations. For explanation, see {@link #beginBatch()}.
+ *
+ * @throws ConnectionLostException
+ * Connection was lost before or during the execution of this
+ * method.
+ */
+ public void endBatch() throws ConnectionLostException;
+}
diff --git a/IOIOLib/src/ioio/lib/api/IOIOConnection.java b/IOIOLib/src/ioio/lib/api/IOIOConnection.java
old mode 100755
new mode 100644
diff --git a/IOIOLib/src/ioio/lib/api/IOIOFactory.java b/IOIOLib/src/ioio/lib/api/IOIOFactory.java
index 947c48e..ed3a70f 100644
--- a/IOIOLib/src/ioio/lib/api/IOIOFactory.java
+++ b/IOIOLib/src/ioio/lib/api/IOIOFactory.java
@@ -28,10 +28,14 @@
*/
package ioio.lib.api;
-import java.lang.reflect.Constructor;
-
import ioio.lib.impl.IOIOImpl;
-import ioio.lib.impl.SocketIOIOConnection;
+import ioio.lib.spi.IOIOConnectionFactory;
+import ioio.lib.util.IOIOConnectionRegistry;
+
+import java.util.Collection;
+import java.util.NoSuchElementException;
+
+import android.util.Log;
/**
* Factory class for creating instances of the IOIO interface.
@@ -55,22 +59,21 @@ import ioio.lib.impl.SocketIOIOConnection;
*
*/
public class IOIOFactory {
- /** The TCP port used for communicating with the IOIO board. */
- public static final int IOIO_PORT = 4545;
-
/**
* Create a IOIO instance. This specific implementation creates a IOIO
* instance which works with the actual IOIO board connected via a TCP
* connection (typically over a wired USB connection).
- *
+ *
* @return The IOIO instance.
*/
public static IOIO create() {
+ Collection
* {@link PulseInput} instances are obtained by calling
- * {@link IOIO#openPulseInput(ioio.lib.api.DigitalInput.Spec, ClockRate, PulseMode, boolean)}
+ * {@link IOIO#openPulseInput(ioio.lib.api.DigitalInput.Spec, ioio.lib.api.PulseInput.ClockRate, ioio.lib.api.PulseInput.PulseMode, boolean)}
* . When created, some important configuration decisions have to be made: the
* precision (single or double), the clock rate and the mode of operation. Modes
* are straightforward: {@link PulseMode#POSITIVE} is used for measuring a
diff --git a/IOIOLib/src/ioio/lib/api/SpiMaster.java b/IOIOLib/src/ioio/lib/api/SpiMaster.java
old mode 100755
new mode 100644
index 6cf0db4..94cd087
--- a/IOIOLib/src/ioio/lib/api/SpiMaster.java
+++ b/IOIOLib/src/ioio/lib/api/SpiMaster.java
@@ -28,7 +28,6 @@
*/
package ioio.lib.api;
-import ioio.lib.api.DigitalInput.Spec;
import ioio.lib.api.exception.ConnectionLostException;
/**
@@ -41,7 +40,7 @@ import ioio.lib.api.exception.ConnectionLostException;
* between this slave and a respective pin on the master. The MISO line should
* operate in pull-up mode, using either the internal pull-up or an external
* resistor. SpiMaster instances are obtained by calling
- * {@link IOIO#openSpiMaster(Spec, ioio.lib.api.DigitalOutput.Spec, ioio.lib.api.DigitalOutput.Spec, ioio.lib.api.DigitalOutput.Spec[], Config)}.
+ * {@link IOIO#openSpiMaster(DigitalInput.Spec, DigitalOutput.Spec, DigitalOutput.Spec, DigitalOutput.Spec[], SpiMaster.Config)}.
*
* The SPI protocol is comprised of simultaneous sending and receiving of data
* between the bus master and a single slave. By the very nature of this
@@ -90,9 +89,11 @@ import ioio.lib.api.exception.ConnectionLostException;
* spi.writeRead(request, 2, 4, response, 3);
* ...
* spi.close(); // free SPI module and pins
- * }
+ * }
+ *
*
- * @see IOIO#openSpiMaster(Spec, ioio.lib.api.DigitalOutput.Spec, ioio.lib.api.DigitalOutput.Spec, ioio.lib.api.DigitalOutput.Spec[], Config)
+ * @see IOIO#openSpiMaster(DigitalInput.Spec, DigitalOutput.Spec,
+ * DigitalOutput.Spec, DigitalOutput.Spec[], SpiMaster.Config)
*/
public interface SpiMaster extends Closeable {
/** Possible data rates for SPI, in Hz. */
@@ -148,7 +149,7 @@ public interface SpiMaster extends Closeable {
* Constructor with common defaults. Equivalent to Config(rate, false,
* false)
*
- * @see #Config(Rate, boolean, boolean)
+ * @see SpiMaster.Config#Config(SpiMaster.Config.Rate, boolean, boolean)
*/
public Config(Rate rate) {
this(rate, false, false);
@@ -165,7 +166,7 @@ public interface SpiMaster extends Closeable {
* @param slave
* The slave index. It is determined by the index of its
* slave-select pin, as per the array passed to
- * {@link IOIO#openSpiMaster(Spec, ioio.lib.api.DigitalOutput.Spec, ioio.lib.api.DigitalOutput.Spec, ioio.lib.api.DigitalOutput.Spec[], Config)}
+ * {@link IOIO#openSpiMaster(DigitalInput.Spec, DigitalOutput.Spec, DigitalOutput.Spec, DigitalOutput.Spec[], SpiMaster.Config)}
* .
* @param writeData
* A byte array of data to write. May be null if writeSize is 0.
diff --git a/IOIOLib/src/ioio/lib/api/TwiMaster.java b/IOIOLib/src/ioio/lib/api/TwiMaster.java
old mode 100755
new mode 100644
index 9d25f12..317f27e
--- a/IOIOLib/src/ioio/lib/api/TwiMaster.java
+++ b/IOIOLib/src/ioio/lib/api/TwiMaster.java
@@ -39,7 +39,7 @@ import ioio.lib.api.exception.ConnectionLostException;
* requires a physical connection of two lines (SDA, SCL) shared by all the bus
* nodes, where the SDA is open-drain and externally pulled-up. TwiMaster
* instances are obtained by calling
- * {@link IOIO#openTwiMaster(int, Rate, boolean)}.
+ * {@link IOIO#openTwiMaster(int, ioio.lib.api.TwiMaster.Rate, boolean)}.
*
* TWI is the generic name for the specific I2C and SMBus protocols, differing
* mostly by the voltage levels they require. This module supports both.
@@ -79,7 +79,7 @@ import ioio.lib.api.exception.ConnectionLostException;
* twi.close(); // free TWI module and pins
* }
*
- * @see IOIO#openTwiMaster(int, Rate, boolean)
+ * @see IOIO#openTwiMaster(int, ioio.lib.api.TwiMaster.Rate, boolean)
*/
public interface TwiMaster extends Closeable {
enum Rate {
diff --git a/IOIOLib/src/ioio/lib/api/Uart.java b/IOIOLib/src/ioio/lib/api/Uart.java
index 309f478..30faa91 100644
--- a/IOIOLib/src/ioio/lib/api/Uart.java
+++ b/IOIOLib/src/ioio/lib/api/Uart.java
@@ -1,108 +1,107 @@
-/*
- * Copyright 2011 Ytai Ben-Tsvi. All rights reserved.
- *
- *
- * Redistribution and use in source and binary forms, with or without modification, are
- * permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this list of
- * conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice, this list
- * of conditions and the following disclaimer in the documentation and/or other materials
- * provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ARSHAN POURSOHI OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
- * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * The views and conclusions contained in the software and documentation are those of the
- * authors and should not be interpreted as representing official policies, either expressed
- * or implied.
- */
-package ioio.lib.api;
-
-import ioio.lib.api.DigitalInput.Spec;
-import ioio.lib.api.exception.ConnectionLostException;
-
-import java.io.InputStream;
-import java.io.OutputStream;
-
-/**
- * An interface for controlling a UART module.
- *
- * UART is a very common hardware communication protocol, enabling full- duplex,
- * asynchronous point-to-point data transfer. It typically serves for opening
- * consoles or as a basis for higher-level protocols, such as MIDI, RS-232 and
- * RS-485. Uart instances are obtained by calling
- * {@link IOIO#openUart(Spec, ioio.lib.api.DigitalOutput.Spec, int, Parity, StopBits)}.
- *
- * The UART protocol is completely symmetric - there is no "master" and "slave"
- * at this layer. Each end may send any number of bytes at arbitrary times,
- * making it very useful for terminals and terminal-controllable devices.
- *
- * Working with UART is very intuitive - it just provides a standard InputStream
- * and/or OutputStream. Optionally, one could create a read-only or write-only
- * UART instances, by passing null (or INVALID_PIN) for either TX or RX pins.
- *
- * The instance is alive since its creation. If the connection with the IOIO
- * drops at any point, the instance transitions to a disconnected state, which
- * every attempt to use it (except {@link #close()}) will throw a
- * {@link ConnectionLostException}. Whenever {@link #close()} is invoked the
- * instance may no longer be used. Any resources associated with it are freed
- * and can be reused.
- *
- * Typical usage:
- *
- *
+ * UART is a very common hardware communication protocol, enabling full- duplex,
+ * asynchronous point-to-point data transfer. It typically serves for opening
+ * consoles or as a basis for higher-level protocols, such as MIDI, RS-232 and
+ * RS-485. Uart instances are obtained by calling
+ * {@link IOIO#openUart(DigitalInput.Spec, DigitalOutput.Spec, int, Uart.Parity, Uart.StopBits)}.
+ *
+ * The UART protocol is completely symmetric - there is no "master" and "slave"
+ * at this layer. Each end may send any number of bytes at arbitrary times,
+ * making it very useful for terminals and terminal-controllable devices.
+ *
+ * Working with UART is very intuitive - it just provides a standard InputStream
+ * and/or OutputStream. Optionally, one could create a read-only or write-only
+ * UART instances, by passing null (or INVALID_PIN) for either TX or RX pins.
+ *
+ * The instance is alive since its creation. If the connection with the IOIO
+ * drops at any point, the instance transitions to a disconnected state, which
+ * every attempt to use it (except {@link #close()}) will throw a
+ * {@link ConnectionLostException}. Whenever {@link #close()} is invoked the
+ * instance may no longer be used. Any resources associated with it are freed
+ * and can be reused.
+ *
+ * Typical usage:
+ *
+ *
+ * When the application starts, call {@link #start()}, which will in turn
+ * attempt to create a thread for each possible IOIO connection channel. Each
+ * thread will have a respective {@link IOIOLooper}, which the client provides,
+ * through which the client gets context for working with the IOIO.
+ *
+ * When the application exits, call {@link #stop()}, which will disconnect all
+ * open connections and will abort and join all the threads.
+ *
+ */
+public class IOIOApplicationHelper {
+ /**
+ * An abstract class, which facilitates a thread dedicated for communication
+ * with a single physical IOIO device.
+ */
+ static private class IOIOThread extends Thread {
+ protected IOIO ioio_;
+ private boolean abort_ = false;
+ private boolean connected_ = true;
+ private final IOIOLooper looper_;
+ private final IOIOConnectionFactory connectionFactory_;
+
+ IOIOThread(IOIOLooper looper, IOIOConnectionFactory factory) {
+ looper_ = looper;
+ connectionFactory_ = factory;
+ }
+
+ @Override
+ public final void run() {
+ super.run();
+ while (!abort_) {
+ try {
+ synchronized (this) {
+ if (abort_) {
+ break;
+ }
+ ioio_ = IOIOFactory.create(connectionFactory_
+ .createConnection());
+ }
+ } catch (Exception e) {
+ Log.e(TAG, "Failed to create IOIO, aborting IOIOThread!");
+ return;
+ }
+ // if we got here, we have a ioio_!
+ try {
+ ioio_.waitForConnect();
+ connected_ = true;
+ looper_.setup(ioio_);
+ while (!abort_ && ioio_.getState() == IOIO.State.CONNECTED) {
+ looper_.loop();
+ }
+ } catch (ConnectionLostException e) {
+ } catch (InterruptedException e) {
+ ioio_.disconnect();
+ } catch (IncompatibilityException e) {
+ Log.e(TAG, "Incompatible IOIO firmware", e);
+ looper_.incompatible();
+ // nothing to do - just wait until physical
+ // disconnection
+ } catch (Exception e) {
+ Log.e(TAG, "Unexpected exception caught", e);
+ ioio_.disconnect();
+ break;
+ } finally {
+ try {
+ ioio_.waitForDisconnect();
+ } catch (InterruptedException e1) {
+ }
+ synchronized (this) {
+ ioio_ = null;
+ }
+ if (connected_) {
+ looper_.disconnected();
+ connected_ = false;
+ }
+ }
+ }
+ Log.d(TAG, "IOIOThread is exiting");
+ }
+
+ /** Not relevant to subclasses. */
+ public synchronized final void abort() {
+ abort_ = true;
+ if (ioio_ != null) {
+ ioio_.disconnect();
+ }
+ if (connected_) {
+ interrupt();
+ }
+ }
+ }
+
+ protected static final String TAG = "IOIOAndroidApplicationHelper";
+ protected final IOIOLooperProvider looperProvider_;
+ private Collection
+ * For advanced usage only!
+ *
+ * This class facilitates dynamic linkage and instantiation of different IOIO
+ * connection types. {@link IOIOConnectionBootstrap} classes enable creation of
+ * {@link IOIOConnectionFactory} instances, from which concrete
+ * {@link IOIOConnection}s are created. The binding to
+ * {@link IOIOConnectionBootstrap} is dynamic, thus enabling linkage to succeed
+ * with or without those bootstraps. Likewise, during runtime, the absence of
+ * bootstraps is handled gracefully.
+ *
+ * A typical usage will call {@link #addBootstraps(String[])} with a list of
+ * class names to be sought from a static initializer block. It may then call
+ * {@link #getBootstraps()} to obtain any bootstrap classes that are available
+ * in runtime, in case the bootstrap classes themselves need some runtime
+ * handling. Last, the {@link #getConnectionFactories()} will return a
+ * collection of {@link IOIOConnectionFactory}, each representing one possible
+ * communication channel to a IOIO.
+ *
+ */
+public class IOIOConnectionRegistry {
+ /**
+ * Get all available connection specifications. This is a list of all
+ * currently available communication channels in which a IOIO may be
+ * available. The client typically passes elements of this collection to
+ * {@link IOIOFactory#create(IOIOConnection)}, possibly after filtering based on the
+ * specification's properties.
+ *
+ * @return A collection of specifications.
+ */
+ public static Collection
+ * In the basic usage the client should extend this class and implement
+ * {@link #createIOIOLooper()}, which should return an implementation of the
+ * {@link IOIOLooper} interface. In this implementation, the client implements
+ * the {@link IOIOLooper#setup(ioio.lib.api.IOIO)} method, which gets called as
+ * soon as communication with the IOIO is established, and the
+ * {@link IOIOLooper#loop()} method, which gets called repetitively as long as
+ * the IOIO is connected.
+ *
+ * In addition, the {@link IOIOLooper#disconnected()} method may be overridden
+ * in order to execute logic as soon as a disconnection occurs for whichever
+ * reason. The {@link IOIOLooper#incompatible()} method may be overridden in
+ * order to take action in case where a IOIO whose firmware is incompatible with
+ * the IOIOLib version that application is built with.
+ *
+ * In a more advanced use case, more than one IOIO is available. In this case, a
+ * thread will be created for each IOIO, whose semantics are as defined above.
+ * If the client needs to be able to distinguish between them, it is possible to
+ * override {@link #createIOIOLooper(String, Object)} instead of
+ * {@link #createIOIOLooper()}. The first argument provided will contain the
+ * connection class name, such as ioio.lib.impl.SocketIOIOConnection for a
+ * connection established over a TCP socket (which is used over ADB). The second
+ * argument will contain information specific to the connection type. For
+ * example, in the case of {@link SocketIOIOConnection}, the second argument
+ * will contain an {@link Integer} representing the local port number.
+ */
+public abstract class IOIOActivity extends Activity implements
+ IOIOLooperProvider {
+ private final IOIOAndroidApplicationHelper helper_ = new IOIOAndroidApplicationHelper(
+ this, this);
+
+ /**
+ * Subclasses should call this method from their own onCreate() if
+ * overloaded. It takes care of connecting with the IOIO.
+ */
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ helper_.create();
+ }
+
+ /**
+ * Subclasses should call this method from their own onDestroy() if
+ * overloaded. It takes care of connecting with the IOIO.
+ */
+ @Override
+ protected void onDestroy() {
+ helper_.destroy();
+ super.onDestroy();
+ }
+
+ /**
+ * Subclasses should call this method from their own onStart() if
+ * overloaded. It takes care of connecting with the IOIO.
+ */
+ @Override
+ protected void onStart() {
+ super.onStart();
+ helper_.start();
+ }
+
+ /**
+ * Subclasses should call this method from their own onStop() if overloaded.
+ * It takes care of disconnecting from the IOIO.
+ */
+ @Override
+ protected void onStop() {
+ helper_.stop();
+ super.onStop();
+ }
+
+ @Override
+ protected void onNewIntent(Intent intent) {
+ super.onNewIntent(intent);
+ if ((intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
+ helper_.restart();
+ }
+ }
+
+ /**
+ * Subclasses must either implement this method or its other overload by
+ * returning an implementation of {@link IOIOLooper}. A dedicated thread
+ * will be created for each available IOIO, from which the
+ * {@link IOIOLooper}'s methods will be invoked.
+ * In the basic usage the client should extend this class and implement
+ * {@link #createIOIOLooper()}, which should return an implementation of the
+ * {@link IOIOLooper} interface. In this implementation, the client implements
+ * the {@link IOIOLooper#setup(ioio.lib.api.IOIO)} method, which gets called as
+ * soon as communication with the IOIO is established, and the
+ * {@link IOIOLooper#loop()} method, which gets called repetitively as long as
+ * the IOIO is connected.
+ *
+ * In addition, the {@link IOIOLooper#disconnected()} method may be overridden
+ * in order to execute logic as soon as a disconnection occurs for whichever
+ * reason. The {@link IOIOLooper#incompatible()} method may be overridden in
+ * order to take action in case where a IOIO whose firmware is incompatible with
+ * the IOIOLib version that application is built with.
+ *
+ * In a more advanced use case, more than one IOIO is available. In this case, a
+ * thread will be created for each IOIO, whose semantics are as defined above.
+ * If the client needs to be able to distinguish between them, it is possible to
+ * override {@link #createIOIOLooper(String, Object)} instead of
+ * {@link #createIOIOLooper()}. The first argument provided will contain the
+ * connection class name, such as ioio.lib.impl.SocketIOIOConnection for a
+ * connection established over a TCP socket (which is used over ADB). The second
+ * argument will contain information specific to the connection type. For
+ * example, in the case of {@link SocketIOIOConnection}, the second argument
+ * will contain an {@link Integer} representing the local port number.
+ */
+public abstract class IOIOService extends Service implements
+ IOIOLooperProvider {
+ private final IOIOAndroidApplicationHelper helper_ = new IOIOAndroidApplicationHelper(
+ this, this);
+ private boolean started_ = false;
+
+ /**
+ * Subclasses should call this method from their own onCreate() if
+ * overloaded. It takes care of connecting with the IOIO.
+ */
+ @Override
+ public void onCreate() {
+ super.onCreate();
+ helper_.create();
+ }
+
+ /**
+ * Subclasses should call this method from their own onDestroy() if
+ * overloaded. It takes care of connecting with the IOIO.
+ */
+ @Override
+ public void onDestroy() {
+ stop();
+ helper_.destroy();
+ super.onDestroy();
+ }
+
+ /**
+ * Subclasses should call this method from their own onStart() if
+ * overloaded. It takes care of connecting with the IOIO.
+ */
+ @Override
+ public void onStart(Intent intent, int startId) {
+ super.onStart(intent, startId);
+ if (!started_) {
+ helper_.start();
+ started_ = true;
+ } else {
+ if ((intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
+ helper_.restart();
+ }
+ }
+ }
+
+ /**
+ * Subclasses should call this method if they wish to disconnect from the
+ * IOIO(s) until the next onStart().
+ */
+ protected void stop() {
+ if (started_) {
+ helper_.stop();
+ started_ = false;
+ }
+ }
+
+ /**
+ * Subclasses must either implement this method or its other overload by
+ * returning an implementation of {@link IOIOLooper}. A dedicated thread
+ * will be created for each available IOIO, from which the
+ * {@link IOIOLooper}'s methods will be invoked.
- * Uart uart = ioio.openUart(3, 4, 19200, Parity.NONE, StopBits.ONE);
- * InputStream in = uart.getInputStream();
- * OutputStream out = uart.getOutputStream();
- * out.write(new String("Hello").getBytes());
- * int i = in.read(); // blocking
- * ...
- * uart.close(); // free UART module and pins
- *
- *
- * @see IOIO#openUart(Spec, ioio.lib.api.DigitalOutput.Spec, int, Parity,
- * StopBits)
- */
-public interface Uart extends Closeable {
- /** Parity-bit mode. */
- enum Parity {
- /** No parity. */
- NONE,
- /** Even parity. */
- EVEN,
- /** Odd parity. */
- ODD
- }
-
- /** Number of stop-bits. */
- enum StopBits {
- /** One stop bit. */
- ONE,
- /** Two stop bits. */
- TWO
- }
-
- /**
- * Gets the input stream.
- *
- * @return An input stream.
- */
- public InputStream getInputStream();
-
- /**
- * Gets the output stream.
- *
- * @return An output stream.
- */
- public OutputStream getOutputStream();
-}
+/*
+ * Copyright 2011 Ytai Ben-Tsvi. All rights reserved.
+ *
+ *
+ * Redistribution and use in source and binary forms, with or without modification, are
+ * permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this list of
+ * conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice, this list
+ * of conditions and the following disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ARSHAN POURSOHI OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and documentation are those of the
+ * authors and should not be interpreted as representing official policies, either expressed
+ * or implied.
+ */
+package ioio.lib.api;
+
+import ioio.lib.api.exception.ConnectionLostException;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+
+/**
+ * An interface for controlling a UART module.
+ *
+ * Uart uart = ioio.openUart(3, 4, 19200, Parity.NONE, StopBits.ONE);
+ * InputStream in = uart.getInputStream();
+ * OutputStream out = uart.getOutputStream();
+ * out.write(new String("Hello").getBytes());
+ * int i = in.read(); // blocking
+ * ...
+ * uart.close(); // free UART module and pins
+ *
+ *
+ * @see IOIO#openUart(DigitalInput.Spec, DigitalOutput.Spec, int, Uart.Parity,
+ * Uart.StopBits)
+ */
+public interface Uart extends Closeable {
+ /** Parity-bit mode. */
+ enum Parity {
+ /** No parity. */
+ NONE,
+ /** Even parity. */
+ EVEN,
+ /** Odd parity. */
+ ODD
+ }
+
+ /** Number of stop-bits. */
+ enum StopBits {
+ /** One stop bit. */
+ ONE,
+ /** Two stop bits. */
+ TWO
+ }
+
+ /**
+ * Gets the input stream.
+ *
+ * @return An input stream.
+ */
+ public InputStream getInputStream();
+
+ /**
+ * Gets the output stream.
+ *
+ * @return An output stream.
+ */
+ public OutputStream getOutputStream();
+}
diff --git a/IOIOLib/src/ioio/lib/api/exception/IncompatibilityException.java b/IOIOLib/src/ioio/lib/api/exception/IncompatibilityException.java
old mode 100755
new mode 100644
diff --git a/IOIOLib/src/ioio/lib/bluetooth/BluetoothIOIOConnection.java b/IOIOLib/src/ioio/lib/bluetooth/BluetoothIOIOConnection.java
index 5e5f8de..4dbfb47 100644
--- a/IOIOLib/src/ioio/lib/bluetooth/BluetoothIOIOConnection.java
+++ b/IOIOLib/src/ioio/lib/bluetooth/BluetoothIOIOConnection.java
@@ -1,3 +1,32 @@
+/*
+ * Copyright 2011 Ytai Ben-Tsvi. All rights reserved.
+ *
+ *
+ * Redistribution and use in source and binary forms, with or without modification, are
+ * permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this list of
+ * conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice, this list
+ * of conditions and the following disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ARSHAN POURSOHI OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and documentation are those of the
+ * authors and should not be interpreted as representing official policies, either expressed
+ * or implied.
+ */
+
package ioio.lib.bluetooth;
import ioio.lib.api.IOIOConnection;
@@ -8,43 +37,41 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
-import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
+import android.os.Build;
import android.util.Log;
public class BluetoothIOIOConnection implements IOIOConnection {
private static final String TAG = "BluetoothIOIOConnection";
private BluetoothSocket socket_ = null;
private boolean disconnect_ = false;
+ private final BluetoothDevice device_;
private final String name_;
private final String address_;
- public BluetoothIOIOConnection(String name, String address) {
- name_ = name;
- address_ = address;
+ public BluetoothIOIOConnection(BluetoothDevice device) {
+ device_ = device;
+ name_ = device.getName();
+ address_ = device.getAddress();
}
@Override
public void waitForConnect() throws ConnectionLostException {
- final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
- final BluetoothDevice ioioDevice = adapter.getRemoteDevice(address_);
synchronized (this) {
if (disconnect_) {
throw new ConnectionLostException();
}
- Log.v(TAG, name_ + " Creating socket");
try {
- socket_ = createSocket(ioioDevice);
+ socket_ = createSocket(device_);
} catch (IOException e) {
throw new ConnectionLostException(e);
}
- Log.v(TAG, name_ + " Created socket");
}
// keep trying to connect as long as we're not aborting
while (true) {
try {
- Log.v(TAG, name_ + "Connecting");
+ Log.v(TAG, "Attempting to connect to Bluetooth device: " + name_);
socket_.connect();
Log.v(TAG, "Established connection to device " + name_
+ " address: " + address_);
@@ -61,17 +88,15 @@ public class BluetoothIOIOConnection implements IOIOConnection {
}
}
- public BluetoothSocket createSocket(final BluetoothDevice device)
+ public static BluetoothSocket createSocket(final BluetoothDevice device)
throws IOException {
- try {
- // We're trying to create an insecure socket, which is
- // only supported
- // in API 10 and up. If we fail, we try a secure socket
- // with is in API
- // 7 and up.
+ if (Build.VERSION.SDK_INT >= 10 ) {
+ // We're trying to create an insecure socket, which is only
+ // supported in API 10 and up. Otherwise, we try a secure socket
+ // which is in API 7 and up.
return device.createInsecureRfcommSocketToServiceRecord(UUID
.fromString("00001101-0000-1000-8000-00805F9B34FB"));
- } catch (NoSuchMethodError e) {
+ } else {
return device.createRfcommSocketToServiceRecord(UUID
.fromString("00001101-0000-1000-8000-00805F9B34FB"));
}
@@ -82,7 +107,7 @@ public class BluetoothIOIOConnection implements IOIOConnection {
if (disconnect_) {
return;
}
- Log.d(TAG, "Client initiated disconnect");
+ Log.v(TAG, "Client initiated disconnect");
disconnect_ = true;
if (socket_ != null) {
try {
diff --git a/IOIOLib/src/ioio/lib/bluetooth/BluetoothIOIOConnectionBootstrap.java b/IOIOLib/src/ioio/lib/bluetooth/BluetoothIOIOConnectionBootstrap.java
new file mode 100644
index 0000000..0c910f4
--- /dev/null
+++ b/IOIOLib/src/ioio/lib/bluetooth/BluetoothIOIOConnectionBootstrap.java
@@ -0,0 +1,93 @@
+/*
+ * Copyright 2011 Ytai Ben-Tsvi. All rights reserved.
+ *
+ *
+ * Redistribution and use in source and binary forms, with or without modification, are
+ * permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this list of
+ * conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice, this list
+ * of conditions and the following disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ARSHAN POURSOHI OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and documentation are those of the
+ * authors and should not be interpreted as representing official policies, either expressed
+ * or implied.
+ */
+
+package ioio.lib.bluetooth;
+
+import ioio.lib.api.IOIOConnection;
+import ioio.lib.spi.IOIOConnectionBootstrap;
+import ioio.lib.spi.IOIOConnectionFactory;
+import ioio.lib.spi.NoRuntimeSupportException;
+
+import java.util.Collection;
+import java.util.Set;
+
+import android.bluetooth.BluetoothAdapter;
+import android.bluetooth.BluetoothDevice;
+import android.util.Log;
+
+public class BluetoothIOIOConnectionBootstrap implements
+ IOIOConnectionBootstrap {
+
+ private static final String TAG = "BluetoothIOIOConnectionDiscovery";
+ private final BluetoothAdapter adapter_;
+
+ public BluetoothIOIOConnectionBootstrap() throws NoRuntimeSupportException {
+ try {
+ adapter_ = BluetoothAdapter.getDefaultAdapter();
+ } catch (NoClassDefFoundError e) {
+ throw new NoRuntimeSupportException(
+ "Bluetooth is not supported on this device.");
+ }
+ }
+
+ @Override
+ public void getFactories(Collectionnull may be returned if the client
- * is not interested to connect a thread for this IOIO. In multi-IOIO
- * scenarios, where you want to identify which IOIO the thread is for,
- * consider using {@link #createIOIOThread()} instead.
- *
- * @return An implementation of {@link IOIOThread}, or null to
- * skip.
- */
- protected IOIOThread createIOIOThread() {
- return null;
- }
-
- /**
- * Subclasses should implement this method by returning a concrete subclass
- * of {@link IOIOThread}. This overload is useful in multi-IOIO scenarios,
- * where you want to identify which IOIO the thread is for. The provided
- * arguments should provide enough information to be unique per connection.
- * null may be returned if the client is not interested to
- * connect a thread for this IOIO. This can be used in order to filter out
- * unwanted connections, for example if the application is only intended for
- * wireless connection, any wired connection attempts may be rejected, thus
- * saving resources used for listening for incoming wired connections.
- *
- * @param connectionClass
- * The fully-qualified name of the connection class used to
- * connect to the IOIO.
- * @param connectionArgs
- * A list of arguments passed to the constructor of the
- * connection class. Should provide information that enables
- * distinguishing between different IOIO instances using the same
- * connection class.
- *
- * @return An implementation of {@link IOIOThread}, or null to
- * skip.
- */
- protected IOIOThread createIOIOThread(String connectionClass,
- Object[] connectionArgs) {
- return createIOIOThread();
- }
-
- /**
- * An abstract class, which facilitates a thread dedicated for communication
- * with a single physical IOIO device.
- */
- protected abstract class IOIOThread extends Thread {
- /** Subclasses should use this field for controlling the IOIO. */
- protected IOIO ioio_;
- private boolean abort_ = false;
- private boolean connected_ = true;
- private final IOIOConnectionSpec spec_ = currentSpec_;
-
- /**
- * Subclasses should override this method for performing operations to
- * be done once as soon as IOIO communication is established. Typically,
- * this will include opening pins and modules using the openXXX()
- * methods of the {@link #ioio_} field.
- */
- protected void setup() throws ConnectionLostException,
- InterruptedException {
- }
-
- /**
- * Subclasses should override this method for performing operations to
- * be done repetitively as long as IOIO communication persists.
- * Typically, this will be the main logic of the application, processing
- * inputs and producing outputs.
- */
- protected void loop() throws ConnectionLostException,
- InterruptedException {
- sleep(100000);
- }
-
- /**
- * Subclasses should override this method for performing operations to
- * be done once as soon as IOIO communication is lost or closed.
- * Typically, this will include GUI changes corresponding to the change.
- * This method will only be called if setup() has been called. The
- * {@link #ioio_} member must not be used from within this method.
- */
- protected void disconnected() throws InterruptedException {
- }
-
- /**
- * Subclasses should override this method for performing operations to
- * be done if an incompatible IOIO firmware is detected. The
- * {@link #ioio_} member must not be used from within this method. This
- * method will only be called once, until a compatible IOIO is connected
- * (i.e. {@link #setup()} gets called).
- */
- protected void incompatible() {
- }
-
- /** Not relevant to subclasses. */
- @Override
- public final void run() {
- super.run();
- Looper.prepare();
- while (true) {
- try {
- synchronized (this) {
- if (abort_) {
- break;
- }
- ioio_ = IOIOFactory.create(spec_.className, spec_.args);
- }
- ioio_.waitForConnect();
- connected_ = true;
- setup();
- while (!abort_) {
- loop();
- }
- ioio_.disconnect();
- } catch (ConnectionLostException e) {
- if (abort_) {
- break;
- }
- } catch (InterruptedException e) {
- ioio_.disconnect();
- break;
- } catch (IncompatibilityException e) {
- Log.e(TAG, "Incompatible IOIO firmware", e);
- incompatible();
- // nothing to do - just wait until physical disconnection
- try {
- ioio_.waitForDisconnect();
- } catch (InterruptedException e1) {
- ioio_.disconnect();
- }
- } catch (Exception e) {
- Log.e(TAG, "Unexpected exception caught", e);
- ioio_.disconnect();
- break;
- } finally {
- try {
- if (ioio_ != null) {
- ioio_.waitForDisconnect();
- if (connected_) {
- disconnected();
- }
- }
- } catch (InterruptedException e) {
- }
- }
- }
- }
-
- /** Not relevant to subclasses. */
- public synchronized final void abort() {
- abort_ = true;
- if (ioio_ != null) {
- ioio_.disconnect();
- }
- if (connected_) {
- interrupt();
- }
- }
- }
-
- private void abortAllThreads() {
- for (IOIOThread thread : threads_) {
- thread.abort();
- }
- }
-
- private void joinAllThreads() throws InterruptedException {
- for (IOIOThread thread : threads_) {
- thread.join();
- }
- }
-
- private void createAllThreads() {
- threads_.clear();
- Collectionnull
+ * may be returned if the client is not interested to create a thread for
+ * this IOIO. In multi-IOIO scenarios, where you want to identify which IOIO
+ * the thread is for, consider using {@link #createIOIOThread()} instead.
+ *
+ * @return An implementation of {@link IOIOThread}, or null to
+ * skip.
+ */
+ protected IOIOThread createIOIOThread() {
+ throw new RuntimeException(
+ "Client must override on of the createIOIOThread overloads!");
+ }
+
+ /**
+ * Subclasses should implement this method by returning a concrete subclass
+ * of {@link IOIOThread}. This overload is useful in multi-IOIO scenarios,
+ * where you want to identify which IOIO the thread is for. The provided
+ * arguments should provide enough information to be unique per connection.
+ * null may be returned if the client is not interested to
+ * connect a thread for this IOIO. This can be used in order to filter out
+ * unwanted connections, for example if the application is only intended for
+ * wireless connection, any wired connection attempts may be rejected, thus
+ * saving resources used for listening for incoming wired connections.
+ *
+ * @param connectionType
+ * A unique name of the connection type. Typically, the
+ * fully-qualified name of the connection class used to connect
+ * to the IOIO.
+ * @param extra
+ * A connection-type-specific object with extra information on
+ * the specific connection. Should provide information that
+ * enables distinguishing between different IOIO instances using
+ * the same connection class. For example, a Bluetooth connection
+ * type, might have the remote IOIO's Bluetooth name as extra.
+ *
+ * @return An implementation of {@link IOIOThread}, or null to
+ * skip.
+ */
+ protected IOIOThread createIOIOThread(String connectionType, Object extra) {
+ return createIOIOThread();
+ }
+
+ /**
+ * An abstract class, which facilitates a thread dedicated for communication
+ * with a single physical IOIO device.
+ */
+ protected abstract class IOIOThread extends Thread {
+ /** Subclasses should use this field for controlling the IOIO. */
+ protected IOIO ioio_;
+ private boolean abort_ = false;
+ private boolean connected_ = true;
+ private final IOIOConnectionFactory connectionFactory_ = currentConnectionFactory_;
+
+ /**
+ * Subclasses should override this method for performing operations to
+ * be done once as soon as IOIO communication is established. Typically,
+ * this will include opening pins and modules using the openXXX()
+ * methods of the {@link #ioio_} field.
+ */
+ protected void setup() throws ConnectionLostException,
+ InterruptedException {
+ }
+
+ /**
+ * Subclasses should override this method for performing operations to
+ * be done repetitively as long as IOIO communication persists.
+ * Typically, this will be the main logic of the application, processing
+ * inputs and producing outputs.
+ */
+ protected void loop() throws ConnectionLostException,
+ InterruptedException {
+ sleep(100000);
+ }
+
+ /**
+ * Subclasses should override this method for performing operations to
+ * be done once as soon as IOIO communication is lost or closed.
+ * Typically, this will include GUI changes corresponding to the change.
+ * This method will only be called if setup() has been called. The
+ * {@link #ioio_} member must not be used from within this method. This
+ * method should not block for long, since it may cause an ANR.
+ */
+ protected void disconnected() {
+ }
+
+ /**
+ * Subclasses should override this method for performing operations to
+ * be done if an incompatible IOIO firmware is detected. The
+ * {@link #ioio_} member must not be used from within this method. This
+ * method will only be called once, until a compatible IOIO is connected
+ * (i.e. {@link #setup()} gets called).
+ */
+ protected void incompatible() {
+ }
+
+ /** Not relevant to subclasses. */
+ @Override
+ public final void run() {
+ super.run();
+ while (!abort_) {
+ try {
+ synchronized (this) {
+ if (abort_) {
+ break;
+ }
+ ioio_ = IOIOFactory.create(connectionFactory_
+ .createConnection());
+ }
+ } catch (Exception e) {
+ Log.e(TAG, "Failed to create IOIO, aborting IOIOThread!");
+ return;
+ }
+ // if we got here, we have a ioio_!
+ try {
+ ioio_.waitForConnect();
+ connected_ = true;
+ setup();
+ while (!abort_) {
+ loop();
+ }
+ } catch (ConnectionLostException e) {
+ } catch (InterruptedException e) {
+ ioio_.disconnect();
+ } catch (IncompatibilityException e) {
+ Log.e(TAG, "Incompatible IOIO firmware", e);
+ incompatible();
+ // nothing to do - just wait until physical disconnection
+ } catch (Exception e) {
+ Log.e(TAG, "Unexpected exception caught", e);
+ ioio_.disconnect();
+ break;
+ } finally {
+ try {
+ ioio_.waitForDisconnect();
+ } catch (InterruptedException e1) {
+ }
+ synchronized (this) {
+ ioio_ = null;
+ }
+ if (connected_) {
+ disconnected();
+ connected_ = false;
+ }
+ }
+ }
+ Log.d(TAG, "IOIOThread is exiting");
+ }
+
+ /** Not relevant to subclasses. */
+ public synchronized final void abort() {
+ abort_ = true;
+ if (ioio_ != null) {
+ ioio_.disconnect();
+ }
+ if (connected_) {
+ interrupt();
+ }
+ }
+ }
+
+ private void abortAllThreads() {
+ for (IOIOThread thread : threads_) {
+ thread.abort();
+ }
+ }
+
+ private void joinAllThreads() throws InterruptedException {
+ for (IOIOThread thread : threads_) {
+ thread.join();
+ }
+ }
+
+ private void createAllThreads() {
+ threads_.clear();
+ Collectionnull may be
+ * returned if the client is not interested to create a thread for this
+ * IOIO. This can be used in order to filter out unwanted connections, for
+ * example if the application is only intended for wireless connection, any
+ * wired connection attempts may be rejected, thus saving resources used for
+ * listening for incoming wired connections.
+ *
+ * @param connectionType
+ * A unique name of the connection type. Typically, the
+ * fully-qualified name of the connection class used to connect
+ * to the IOIO.
+ * @param extra
+ * A connection-type-specific object with extra information on
+ * the specific connection. Should provide information that
+ * enables distinguishing between different IOIO instances using
+ * the same connection class. For example, a Bluetooth connection
+ * type, might have the remote IOIO's Bluetooth name as extra.
+ *
+ * @return An implementation of {@link IOIOLooper}, or null to
+ * skip.
+ */
+ public abstract IOIOLooper createIOIOLooper(String connectionType,
+ Object extra);
+
+}
\ No newline at end of file
diff --git a/IOIOLib/src/ioio/lib/util/SocketIOIOConnectionDiscovery.java b/IOIOLib/src/ioio/lib/util/SocketIOIOConnectionDiscovery.java
deleted file mode 100644
index 88f76ee..0000000
--- a/IOIOLib/src/ioio/lib/util/SocketIOIOConnectionDiscovery.java
+++ /dev/null
@@ -1,15 +0,0 @@
-package ioio.lib.util;
-
-import ioio.lib.api.IOIOFactory;
-import ioio.lib.impl.SocketIOIOConnection;
-
-import java.util.Collection;
-
-public class SocketIOIOConnectionDiscovery implements IOIOConnectionDiscovery {
-
- @Override
- public void getSpecs(Collectionnull may be
+ * returned if the client is not interested to create a thread for this
+ * IOIO. In multi-IOIO scenarios, where you want to identify which IOIO the
+ * thread is for, consider overriding
+ * {@link #createIOIOLooper(String, Object)} instead.
+ *
+ * @return An implementation of {@link IOIOLooper}, or null to
+ * skip.
+ */
+ protected IOIOLooper createIOIOLooper() {
+ throw new RuntimeException(
+ "Client must override one of the createIOIOLooper overloads!");
+ }
+
+ @Override
+ public IOIOLooper createIOIOLooper(String connectionType, Object extra) {
+ return createIOIOLooper();
+ }
+
+}
diff --git a/IOIOLib/src/ioio/lib/util/android/IOIOAndroidApplicationHelper.java b/IOIOLib/src/ioio/lib/util/android/IOIOAndroidApplicationHelper.java
new file mode 100644
index 0000000..a229a63
--- /dev/null
+++ b/IOIOLib/src/ioio/lib/util/android/IOIOAndroidApplicationHelper.java
@@ -0,0 +1,69 @@
+package ioio.lib.util.android;
+
+import ioio.lib.spi.IOIOConnectionBootstrap;
+import ioio.lib.util.IOIOApplicationHelper;
+import ioio.lib.util.IOIOConnectionRegistry;
+import ioio.lib.util.IOIOLooperProvider;
+
+import android.content.ContextWrapper;
+
+public class IOIOAndroidApplicationHelper extends IOIOApplicationHelper {
+ private final ContextWrapper contextWrapper_;
+
+ public IOIOAndroidApplicationHelper(ContextWrapper wrapper,
+ IOIOLooperProvider provider) {
+ super(provider);
+ contextWrapper_ = wrapper;
+ }
+
+ static {
+ IOIOConnectionRegistry
+ .addBootstraps(new String[] {
+ "ioio.lib.android.accessory.AccessoryConnectionBootstrap",
+ "ioio.lib.android.bluetooth.BluetoothIOIOConnectionBootstrap" });
+ }
+
+ public void create() {
+ for (IOIOConnectionBootstrap bootstrap : bootstraps_) {
+ if (bootstrap instanceof ContextWrapperDependent) {
+ ((ContextWrapperDependent) bootstrap).onCreate(contextWrapper_);
+ }
+ }
+ }
+
+ public void destroy() {
+ for (IOIOConnectionBootstrap bootstrap : bootstraps_) {
+ if (bootstrap instanceof ContextWrapperDependent) {
+ ((ContextWrapperDependent) bootstrap).onDestroy();
+ }
+ }
+ }
+
+ @Override
+ public void start() {
+ for (IOIOConnectionBootstrap bootstrap : bootstraps_) {
+ if (bootstrap instanceof ContextWrapperDependent) {
+ ((ContextWrapperDependent) bootstrap).open();
+ }
+ }
+ super.start();
+ }
+
+ @Override
+ public void stop() {
+ super.stop();
+ for (IOIOConnectionBootstrap bootstrap : bootstraps_) {
+ if (bootstrap instanceof ContextWrapperDependent) {
+ ((ContextWrapperDependent) bootstrap).close();
+ }
+ }
+ }
+
+ public void restart() {
+ for (IOIOConnectionBootstrap bootstrap : bootstraps_) {
+ if (bootstrap instanceof ContextWrapperDependent) {
+ ((ContextWrapperDependent) bootstrap).reopen();
+ }
+ }
+ }
+}
diff --git a/IOIOLib/src/ioio/lib/util/android/IOIOService.java b/IOIOLib/src/ioio/lib/util/android/IOIOService.java
new file mode 100644
index 0000000..8e15062
--- /dev/null
+++ b/IOIOLib/src/ioio/lib/util/android/IOIOService.java
@@ -0,0 +1,149 @@
+/*
+ * Copyright 2011 Ytai Ben-Tsvi. All rights reserved.
+ *
+ *
+ * Redistribution and use in source and binary forms, with or without modification, are
+ * permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this list of
+ * conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice, this list
+ * of conditions and the following disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ARSHAN POURSOHI OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and documentation are those of the
+ * authors and should not be interpreted as representing official policies, either expressed
+ * or implied.
+ */
+
+package ioio.lib.util.android;
+
+import ioio.lib.impl.SocketIOIOConnection;
+import ioio.lib.util.IOIOLooper;
+import ioio.lib.util.IOIOLooperProvider;
+import android.app.Service;
+import android.content.Intent;
+
+/**
+ * A convenience class for easy creation of IOIO-based services.
+ *
+ * It is used by creating a concrete {@link Service} in your application, which
+ * extends this class. This class then takes care of proper creation and
+ * abortion of the IOIO connection and of a dedicated thread for IOIO
+ * communication.
+ * null may be
+ * returned if the client is not interested to create a thread for this
+ * IOIO. In multi-IOIO scenarios, where you want to identify which IOIO the
+ * thread is for, consider overriding
+ * {@link #createIOIOLooper(String, Object)} instead.
+ *
+ * @return An implementation of {@link IOIOLooper}, or null to
+ * skip.
+ */
+ protected IOIOLooper createIOIOLooper() {
+ throw new RuntimeException(
+ "Client must override one of the createIOIOLooper overloads!");
+ }
+
+ @Override
+ public IOIOLooper createIOIOLooper(String connectionType, Object extra) {
+ return createIOIOLooper();
+ }
+
+}