Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Threading finally correct. TachoCount as seperate class. USBSend as m…

…ethod. No hangs.
  • Loading branch information...
commit f796147fe783cdba70e5644499686cb4876651b0 1 parent 442a7e5
Shawn-in-Tokyo authored
View
BIN  leJOS-Droid/bin/leJOS-Droid.apk
Binary file not shown
View
75 leJOS-Droid/src/lejos/android/TachoCount.java
@@ -7,46 +7,49 @@
import lejos.nxt.Sound;
import lejos.nxt.remote.NXTCommand;
import lejos.pc.comm.NXTConnector;
-import android.os.AsyncTask;
import android.util.Log;
-public class TachoCount extends AsyncTask {
-
-
-
- @Override
- protected Object doInBackground(Object... params) {
- NXTConnector conn;
- conn = test.connect(test.TACHO_COUNT, CONN_TYPE.LEGO);
- NXTCommand.getSingleton().setNXTComm(conn.getNXTComm());
-
- Log.i(test.TACHO_COUNT, "Tachometer A: " + Motor.A.getTachoCount());
- Log.i(test.TACHO_COUNT, "Tachometer C: " + Motor.C.getTachoCount());
- Motor.A.rotate(5000);
- Motor.C.rotate(-5000);
- try {
- Thread.sleep(10000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- Sound.playTone(1000, 1000);
- Log.i(test.TACHO_COUNT, "Tachometer A: " + Motor.A.getTachoCount());
- Log.i(test.TACHO_COUNT, "Tachometer C: " + Motor.C.getTachoCount());
+public class TachoCount {
+
+ public void countThoseTachos(){
- if( conn != null ){
- try {
- conn.close();
- } catch (IOException e) {
- Log.e(test.TACHO_COUNT,"Error closing connection",e);
- }
- }
-
+ Thread ctt= new Thread(){
+
+ public void run(){
+
+ NXTConnector conn = test.connect(test.TACHO_COUNT, CONN_TYPE.LEGO);
+ NXTCommand.getSingleton().setNXTComm(conn.getNXTComm());
+
+ Log.i(test.TACHO_COUNT, "Tachometer A1: " + Motor.A.getTachoCount());
+ //only have one moment one hand for the moment to test!
+ //Log.i(test.TACHO_COUNT, "Tachometer C1: " + Motor.C.getTachoCount());
+ Motor.A.rotate(500);
+ //Motor.C.rotate(-5000);
+ try {
+ Thread.sleep(1000);
+ } catch (InterruptedException e) {
+ Log.e(test.TACHO_COUNT,"Thread.sleep error",e); }
+ Sound.playTone(1000, 1000);
+ Log.i(test.TACHO_COUNT, "Tachometer A2: " + Motor.A.getTachoCount());
+ //Log.i(test.TACHO_COUNT, "Tachometer C2: " + Motor.C.getTachoCount());
+
+ if( conn != null ){
+ try {
+ conn.close();
+ } catch (IOException e) {
+ Log.e(test.TACHO_COUNT,"Error closing connection",e);
+ }
+ }
+ Log.i(test.TACHO_COUNT, "run finished");
+ }
+
+
+ };
+
+ ctt.start();
- return null; //alter to send feedback to ui via onPostExecute
}
-
- protected void onPostExecute(Object result) {//executes back on UI thread
-
- }
+
+
}
View
307 leJOS-Droid/src/lejos/android/test.java
@@ -23,42 +23,41 @@
import android.widget.Button;
public class test extends Activity {
-
- public static enum CONN_TYPE {LEJOS,LEGO}
-
-
+
+ public static enum CONN_TYPE {
+ LEJOS, LEGO
+ }
public static final String TACHO_COUNT = "TachoCount";
- static final String BT_SEND = "BTSend";
- static final String YOUR_TURN = "YourTurn";
- static final String CONNECTING = "Connecting...";
-
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
-
- seupNXJCache();
- setupTachoCount();
- setupBTSend();
- setupNewTemplate();
-
- }
+ static final String BT_SEND = "BTSend";
+ static final String YOUR_TURN = "YourTurn";
+ static final String CONNECTING = "Connecting...";
+
+ /** Called when the activity is first created. */
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.main);
+
+ seupNXJCache();
+ setupTachoCount();
+ setupBTSend();
+ setupNewTemplate();
+
+ }
private void setupNewTemplate() {
Button button;
button = (Button) findViewById(R.id.button3);
button.setOnClickListener(new View.OnClickListener() {
- public void onClick(View arg0) {
- try {
- newApp();
- } catch (Exception e) {
- Log.e(YOUR_TURN, e.getMessage());
+ public void onClick(View arg0) {
+ try {
+ newApp();
+ } catch (Exception e) {
+ Log.e(YOUR_TURN, e.getMessage());
+ }
}
- }
});
}
@@ -67,14 +66,15 @@ private void setupBTSend() {
button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
- public void onClick(View arg0) {
- try {
- btSend();
- } catch (Exception e) {
- Log.e(BT_SEND, "failed to run:" + e.getMessage());
+ public void onClick(View arg0) {
+ try {
+ Log.e(BT_SEND, " please run!!");
+ btSend();
+ } catch (Exception e) {
+ Log.e(BT_SEND, "failed to run:" + e.getMessage());
+ }
+
}
-
- }
});
}
@@ -82,142 +82,161 @@ private void setupTachoCount() {
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
- public void onClick(View arg0) {
- try {
- TachoCount tc = new TachoCount();
- tc.doInBackground(new Object[0]);//not passing in any args
- } catch (Exception e) {
- Log.e(TACHO_COUNT, "failed to run:" + e.getMessage());
+ public void onClick(View arg0) {
+ try {
+ TachoCount tc = new TachoCount();
+ tc.countThoseTachos();
+ } catch (Exception e) {
+ Log.e(TACHO_COUNT, "failed to run:" + e.getMessage());
+ }
}
- }
});
}
- public static NXTConnector connect(final String source, CONN_TYPE connection_type) {
- NXTConnector conn = new NXTConnector();
-
-
- conn.addLogListener(new NXTCommLogListener() {
-
- public void logEvent(String arg0) {
- Log.e(source+" NXJ log:", arg0);
-
- }
-
- public void logEvent(Throwable arg0) {
- Log.e(source+" NXJ log:", arg0.getMessage(), arg0);
-
- }
-
- });
-
- conn.setDebug(true);
-
-
- switch (connection_type) {
- case LEGO:
- Log.e(source," about to attempt LEGO connection ");
- conn.connectTo("btspp://", NXTComm.LCP);
-
- // conn.connectTo("btspp://NXT", NXTComm.LCP) ;
- break;
- case LEJOS:
- Log.e(source," about to attempt LEJOS connection " );
- conn.connectTo("btspp://");
- break;
-
-
- }
-
-
-
- return conn;
- }
-
- protected void btSend() throws Exception {
- //we are going to talk to the LeJOS firmware so use LEJOS
- NXTConnector conn = connect(BT_SEND,CONN_TYPE.LEJOS);
-
- DataOutputStream dos = conn.getDataOut();
- DataInputStream dis = conn.getDataIn();
-
- for(int i=0;i<100;i++) {
- try {
- Log.e(BT_SEND,"Sending " + (i*30000));
- dos.writeInt((i*30000));
- dos.flush();
-
- } catch (IOException ioe) {
- Log.e(BT_SEND,"IO Exception writing bytes:");
- Log.e(BT_SEND,ioe.getMessage());
+ public static NXTConnector connect(final String source,
+ final CONN_TYPE connection_type) {
+
+ NXTConnector conn;
+ conn = new NXTConnector();
+
+ conn.addLogListener(new NXTCommLogListener() {
+
+ public void logEvent(String arg0) {
+ Log.e(source + " NXJ log:", arg0);
+
+ }
+
+ public void logEvent(Throwable arg0) {
+ Log.e(source + " NXJ log:", arg0.getMessage(), arg0);
+
+ }
+
+ });
+
+ conn.setDebug(true);
+
+ switch (connection_type) {
+ case LEGO:
+ Log.e(source, " about to attempt LEGO connection ");
+ conn.connectTo("btspp://", NXTComm.LCP);
+
+ // conn.connectTo("btspp://NXT", NXTComm.LCP) ;
break;
- }
-
- try {
- Log.e(BT_SEND,"Received " + dis.readInt());
- } catch (IOException ioe) {
- Log.e(BT_SEND,"IO Exception reading bytes:");
- Log.e(BT_SEND,ioe.getMessage());
+ case LEJOS:
+ Log.e(source, " about to attempt LEJOS connection ");
+ conn.connectTo("btspp://");
break;
+
}
+
+ return conn;
+
}
-
- try {
- dis.close();
- dos.close();
- conn.close();
- } finally {
- dis=null;
- dos=null;
- conn=null;
+
+ protected void btSend() throws Exception {
+ Thread t = new Thread() {
+
+ public void run() {
+
+ // we are going to talk to the LeJOS firmware so use LEJOS
+ NXTConnector conn = connect(BT_SEND, CONN_TYPE.LEJOS);
+ Log.e(BT_SEND, " after connect:");
+ DataOutputStream dos = conn.getDataOut();
+ DataInputStream dis = conn.getDataIn();
+
+ for (int i = 0; i < 100; i++) {
+ try {
+ Log.e(BT_SEND, "Sending " + (i * 30000));
+ dos.writeInt((i * 30000));
+ dos.flush();
+
+ } catch (IOException ioe) {
+ Log.e(BT_SEND, "IO Exception writing bytes:");
+ Log.e(BT_SEND, ioe.getMessage());
+ break;
+ }
+
+ try {
+ Log.e(BT_SEND, "Received " + dis.readInt());
+ } catch (IOException ioe) {
+ Log.e(BT_SEND, "IO Exception reading bytes:");
+ Log.e(BT_SEND, ioe.getMessage());
+ break;
+ }
+ }
+
+ try {
+ try {
+ dis.close();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ try {
+ dos.close();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ try {
+ conn.close();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ } finally {
+ dis = null;
+ dos = null;
+ conn = null;
+ }
+ }
+
+ };
+ t.start();
}
-
-}
-
+ protected void newApp() throws Exception {
+ Environment.getExternalStorageDirectory();
+ Log.i(YOUR_TURN, "Now get to work and write a great app!");
- protected void newApp() throws Exception {
- Environment.getExternalStorageDirectory();
- Log.i(YOUR_TURN,"Now get to work and write a great app!");
-
-
-
- }
+ }
private void seupNXJCache() {
File root = Environment.getExternalStorageDirectory();
- Log.i(YOUR_TURN,"Can we write to sdcard?:"+root.canWrite());
-
+ Log.i(YOUR_TURN, "Can we write to sdcard?:" + root.canWrite());
try {
- String androidCacheFile ="nxj.cache";
- File _cache_file = new File(root+"/LeJOS/", androidCacheFile);
- if (root.canWrite()&&!_cache_file.exists()){
+ String androidCacheFile = "nxj.cache";
+ File _cache_file = new File(root + "/LeJOS/", androidCacheFile);
+ if (root.canWrite() && !_cache_file.exists()) {
FileWriter gpxwriter = new FileWriter(_cache_file);
- BufferedWriter out = new BufferedWriter(gpxwriter);
- out.write("Hello world!");
- out.flush();
- out.close();
- Log.e(YOUR_TURN, "File seems written to:"+ _cache_file.getName());
- }
+ BufferedWriter out = new BufferedWriter(gpxwriter);
+ out.write("Hello world!");
+ out.flush();
+ out.close();
+ Log.e(YOUR_TURN, "File seems written to:"
+ + _cache_file.getName());
+ }
} catch (IOException e) {
- Log.e(YOUR_TURN, "Could not write nxj.cache " + e.getMessage(),e);
+ Log.e(YOUR_TURN, "Could not write nxj.cache " + e.getMessage(), e);
}
}
- //prepare for threading
+ // prepare for threading
private class threadTemplate extends AsyncTask {
-
- protected void onPostExecute(Object result) {//executes back on UI thread
-
- }
+
+ protected void onPostExecute(Object result) {// executes back on UI
+ // thread
+
+ }
@Override
- protected Object doInBackground(Object... arg0) {//splits off to a background thread
+ protected Object doInBackground(Object... arg0) {// splits off to a
+ // background thread
// TODO Auto-generated method stub
return null;
}
- }
-
+ }
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.