Hi Guys!
I have a small looper class (from the helloioio sample), what I instantiate
so:
private Looper ls;
ls = new Looper();
but the ioio connection is dead. Of course the ioio harwdare is connected
to the tablet. What have to do to the connection be alive?
It is important, the Looper class can't connect to any activity.
thanks in advance
--
You received this message because you are subscribed to the Google Groups
"ioio-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/ioio-users.
For more options, visit https://groups.google.com/d/optout.
package anywheresoftware.b4a.sample;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.DependsOn;
import anywheresoftware.b4a.BA.Events;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
import ioio.lib.api.DigitalOutput;
import ioio.lib.api.IOIO;
import ioio.lib.api.IOIO.VersionType;
import ioio.lib.api.Uart;
import ioio.lib.api.exception.ConnectionLostException;
import ioio.lib.spi.Log;
import ioio.lib.util.BaseIOIOLooper;
import ioio.lib.util.IOIOLooper;
/**
* This is the thread on which all the IOIO activity happens. It will be run
* every time the application is resumed and aborted when it is paused. The
* method setup() will be called right after a connection with the IOIO has
* been established (which might happen several times!). Then, loop() will
* be called repetitively until the IOIO gets disconnected.
*/
@ShortName("BLoop")
@Events(values={"SetText (S As string)"})
@DependsOn(values={"IOIOLibCore-5.05"})
public class Looper extends BaseIOIOLooper {
/**
* The on-board LED.
*/
private DigitalOutput led_;
Uart uart;
private OutputStream out;
private InputStream in;
private BA lba;
private String EventName = "";
int szamlalo = 0;
//private final IOIOAndroidApplicationHelper helper_ = new IOIOAndroidApplicationHelper(this, this);
/**
* Start Looper
* @param ba
*/
/**
* Called every time a connection with IOIO has been established.
* Typically used to open pins.
*
* @see IOIOLooper#setup()
*/
@Override
protected void setup() throws ConnectionLostException {
try {
BA.Log("Connected event");
showVersions(ioio_, "IOIO connected!");
led_ = ioio_.openDigitalOutput(IOIO.LED_PIN, true);
//enableUi(true);
uart = ioio_.openUart(4, 3, 19200, Uart.Parity.NONE, Uart.StopBits.ONE);
in = uart.getInputStream();
out = uart.getOutputStream();
lba.raiseEvent(this, EventName + "_SetText", String.format(new String("Setup")));
}catch (ConnectionLostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Return the IOIO connection state string
* @return ioio state string
*/
public String getState() {
return ( ioio_ != null ? ioio_.getState().toString() : "NULL");
}
/**
* Called repetitively while the IOIO is connected.
*
* @throws ConnectionLostException When IOIO connection is lost.
* @throws InterruptedException When the IOIO thread has been interrupted.
* @see IOIOLooper#loop()
*/
@Override
public void loop() throws ConnectionLostException, InterruptedException {
/*led_.write(!button_.isChecked());*/
Log.i("Looper loop works...", "B4A");
try {
InputStream reading = uart.getInputStream();
if (in.available() > 0) {
BufferedReader rd = new BufferedReader(new InputStreamReader(reading));
String line = rd.readLine();
//rd.close();
lba.raiseEvent(this, EventName + "_SetText", "Loop" + (szamlalo++) + ": " + line);
if (line.length() > 5) {
led_.write(false);
//Thread.sleep(500);
String codetext = "V\r\n";
byte[] wrBuff = codetext.toString().getBytes();
out.write(wrBuff);
lba.raiseEvent(this, EventName + "_SetText", "Loop " + (szamlalo++) + ": V");
Thread.sleep(100);
led_.write(true);
//Thread.sleep(500);
}
}
//writeAlert(line);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnectionLostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void disconnected() {
//enableUi(false);
BA.Log("Disconnect event");
lba.raiseEvent(this, EventName + "_SetText", String.format("IOIO disconnected"));
}
/**
* Called when the IOIO is connected, but has an incompatible firmware version.
*
* @see IOIOLooper#incompatible(IOIO)
*/
@Override
public void incompatible() {
showVersions(ioio_, "Incompatible firmware version!");
}
private void showVersions(IOIO ioio, String title) {
lba.raiseEvent(this, EventName + "_SetText", String.format(String.format("%s\n" +
"IOIOLib: %s\n" +
"Application firmware: %s\n" +
"Bootloader firmware: %s\n" +
"Hardware: %s",
title,
ioio.getImplVersion(VersionType.IOIOLIB_VER),
ioio.getImplVersion(VersionType.APP_FIRMWARE_VER),
ioio.getImplVersion(VersionType.BOOTLOADER_VER),
ioio.getImplVersion(VersionType.HARDWARE_VER))));
}
}