Added a way to check if the stat led is enabled from IOIOManager

This commit is contained in:
2012-01-28 18:09:01 -05:00
parent 8633e303bf
commit c04832c94e
2 changed files with 75 additions and 51 deletions

View File

@@ -50,49 +50,7 @@ public abstract class IOIOManager implements IOIOListener{
thread.abort(); thread.abort();
joinAllThreads(); joinAllThreads();
} }
/**
* Joins all the threads
* @throws InterruptedException
* @author ricky barrette
*/
private void joinAllThreads() throws InterruptedException {
for (IOIOThread thread : mThreads)
thread.join();
}
/**
* Creates all the required IOIO connectivity threads
* @author ricky barrette
*/
private void createAllThreads() {
mThreads.clear();
Collection<IOIOConnectionSpec> specs = getConnectionSpecs();
for (IOIOConnectionSpec spec : specs)
mThreads.add(new IOIOThread(spec.className, spec.args, this));
}
/**
* Starts IOIO connectivity threads
* @author ricky barrette
*/
public void start() {
createAllThreads();
for (IOIOThread thread : mThreads)
thread.start();
}
/**
* @return
* @author Ytai Ben-Tsvi
*/
private Collection<IOIOConnectionSpec> getConnectionSpecs() {
Collection<IOIOConnectionSpec> result = new LinkedList<IOIOConnectionSpec>();
addConnectionSpecs(SocketIOIOConnectionDiscovery.class.getName(),result);
addConnectionSpecs(BluetoothIOIOConnectionDiscovery.class.getName(), result);
return result;
}
/** /**
* @param discoveryClassName * @param discoveryClassName
* @param result * @param result
@@ -108,6 +66,49 @@ public abstract class IOIOManager implements IOIOListener{
} catch (Exception e) { } catch (Exception e) {
Log.w(TAG,"Exception caught while discovering connections - not adding connections of class "+ discoveryClassName, e); Log.w(TAG,"Exception caught while discovering connections - not adding connections of class "+ discoveryClassName, e);
} }
}
/**
* Creates all the required IOIO connectivity threads
* @author ricky barrette
*/
private void createAllThreads() {
mThreads.clear();
Collection<IOIOConnectionSpec> specs = getConnectionSpecs();
for (IOIOConnectionSpec spec : specs)
mThreads.add(new IOIOThread(spec.className, spec.args, this));
}
/**
* @return
* @author Ytai Ben-Tsvi
*/
private Collection<IOIOConnectionSpec> getConnectionSpecs() {
Collection<IOIOConnectionSpec> result = new LinkedList<IOIOConnectionSpec>();
addConnectionSpecs(SocketIOIOConnectionDiscovery.class.getName(),result);
addConnectionSpecs(BluetoothIOIOConnectionDiscovery.class.getName(), result);
return result;
}
/**
* @return true is the stat led is enabled
* @author ricky barrette
*/
public boolean isStatLedEnabled() {
for(IOIOThread thread : mThreads)
if(thread.isConnected())
return thread.isStatLedEnabled();
return false;
}
/**
* Joins all the threads
* @throws InterruptedException
* @author ricky barrette
*/
private void joinAllThreads() throws InterruptedException {
for (IOIOThread thread : mThreads)
thread.join();
} }
/** /**
@@ -129,4 +130,14 @@ public abstract class IOIOManager implements IOIOListener{
thread.setUpdateInverval(ms); thread.setUpdateInverval(ms);
} }
/**
* Starts IOIO connectivity threads
* @author ricky barrette
*/
public void start() {
createAllThreads();
for (IOIOThread thread : mThreads)
thread.start();
}
} }

View File

@@ -50,6 +50,7 @@ public class IOIOThread extends Thread{
private IOIOListener mListener; private IOIOListener mListener;
private String mClassName; private String mClassName;
private Object[] mArgs; private Object[] mArgs;
private boolean isConnected;
public IOIOThread(IOIOListener listener){ public IOIOThread(IOIOListener listener){
super(); super();
@@ -70,20 +71,28 @@ public class IOIOThread extends Thread{
* the case of abortion happening before the IOIO instance is created or * the case of abortion happening before the IOIO instance is created or
* during its creation. * during its creation.
*/ */
synchronized public void abort() { public synchronized void abort() {
isAborted = true; isAborted = true;
if (mIOIO != null) { if (mIOIO != null) {
mIOIO.disconnect(); mIOIO.disconnect();
} }
} }
/**
* @return true if this tread is connected to a IOIO
* @author ricky barrette
*/
public synchronized boolean isConnected() {
return isConnected;
}
/** /**
* @return the isStatLedEnabled * @return the isStatLedEnabled
*/ */
public boolean isStatLedEnabled() { public synchronized boolean isStatLedEnabled() {
return isStatLedEnabled; return isStatLedEnabled;
} }
/** /**
* Thread Body * Thread Body
@@ -105,7 +114,8 @@ public class IOIOThread extends Thread{
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
isConnected = true;
try { try {
/* /*
* Here we will try to connect to the IOIO board. * Here we will try to connect to the IOIO board.
@@ -130,11 +140,13 @@ public class IOIOThread extends Thread{
} }
} catch (ConnectionLostException e) { } catch (ConnectionLostException e) {
mListener.onDisconnected(); mListener.onDisconnected();
isConnected = false;
} catch (InterruptedException e) { } catch (InterruptedException e) {
Log.e(TAG, e.getMessage()); Log.e(TAG, e.getMessage());
e.printStackTrace(); e.printStackTrace();
mIOIO.disconnect(); mIOIO.disconnect();
mListener.onDisconnected(); mListener.onDisconnected();
isConnected = false;
break; break;
} catch (IncompatibilityException e) { } catch (IncompatibilityException e) {
Log.e(TAG, e.getMessage()); Log.e(TAG, e.getMessage());
@@ -142,7 +154,8 @@ public class IOIOThread extends Thread{
} finally { } finally {
try { try {
mIOIO.waitForDisconnect(); mIOIO.waitForDisconnect();
mListener.onDisconnected(); mListener.onDisconnected();
isConnected = false;
} catch (InterruptedException e) { } catch (InterruptedException e) {
} }
} }
@@ -155,8 +168,8 @@ public class IOIOThread extends Thread{
*/ */
public synchronized void setStatLedEnabled(boolean isStatLedEnabled) { public synchronized void setStatLedEnabled(boolean isStatLedEnabled) {
this.isStatLedEnabled = isStatLedEnabled; this.isStatLedEnabled = isStatLedEnabled;
} }
/** /**
* Sets the update interval of the IOIO thread * Sets the update interval of the IOIO thread
* @param ms * @param ms