From c762d9e2f4ab34d70ceff2dca2e2684034c4b4b9 Mon Sep 17 00:00:00 2001 From: Ricky Barrette Date: Mon, 12 Mar 2012 11:44:57 -0400 Subject: [PATCH] Updated to meet api 3.22RC Change-Id: I1577dedaef5b0267a6abdebcc1cf72eeb0b8d3db Signed-off-by: Ricky Barrette --- .../android/IOIOTruck/CameraActivity.java | 61 +++++++--- ...r.java => IOIOTruckConnectionManager.java} | 78 ++++++++++--- .../android/IOIOTruck/NavigationActivity.java | 65 ++++++++--- .../android/IOIOTruck/TestActivity.java | 108 ++++++++++-------- 4 files changed, 224 insertions(+), 88 deletions(-) rename IOIOTruck/src/com/TwentyCodes/android/IOIOTruck/{IOIOTruckManager.java => IOIOTruckConnectionManager.java} (76%) diff --git a/IOIOTruck/src/com/TwentyCodes/android/IOIOTruck/CameraActivity.java b/IOIOTruck/src/com/TwentyCodes/android/IOIOTruck/CameraActivity.java index 16a46ce..eec8277 100644 --- a/IOIOTruck/src/com/TwentyCodes/android/IOIOTruck/CameraActivity.java +++ b/IOIOTruck/src/com/TwentyCodes/android/IOIOTruck/CameraActivity.java @@ -20,7 +20,7 @@ */ package com.TwentyCodes.android.IOIOTruck; -import com.TwentyCodes.android.IOIOTruck.IOIOTruckManager.IOIOTruckThreadListener; +import com.TwentyCodes.android.IOIOTruck.IOIOTruckConnectionManager.IOIOTruckThreadListener; import android.content.Context; import android.os.Bundle; @@ -40,7 +40,7 @@ import android.widget.TextView; public class CameraActivity extends FragmentActivity implements IOIOTruckThreadListener { private static final String TAG = "CameraActivity"; - private IOIOTruckManager mIOIOManager; + private IOIOTruckConnectionManager mIOIOManager; private WakeLock mWakeLock; private TextView mLogTextView; @@ -54,6 +54,28 @@ public class CameraActivity extends FragmentActivity implements IOIOTruckThreadL super.onCreate(icicle); setContentView(R.layout.camera_activity); mLogTextView = (TextView) findViewById(R.id.log_textView); + mIOIOManager = new IOIOTruckConnectionManager(this, this); + mIOIOManager.getIOIOAndroidApplicationHelper().create(); + } + + /** + * (non-Javadoc) + * @see android.support.v4.app.FragmentActivity#onDestroy() + */ + @Override + protected void onDestroy() { + mIOIOManager.getIOIOAndroidApplicationHelper().destroy(); + super.onDestroy(); + } + + /** + * Called when the IOIO Manager wants to log something + * (non-Javadoc) + * @see com.TwentyCodes.android.IOIOTruck.IOIOTruckConnectionManager.IOIOTruckThreadListener#onLogUpdate(java.lang.String) + */ + @Override + public void onLogUpdate(String log) { + mLogTextView.setText(log); } /** @@ -64,15 +86,20 @@ public class CameraActivity extends FragmentActivity implements IOIOTruckThreadL @Override protected void onPause() { super.onPause(); - try { - mIOIOManager.abort(); - } catch (InterruptedException e) { - e.printStackTrace(); - } if(mWakeLock.isHeld()) mWakeLock.release(); } + /** + * (non-Javadoc) + * @see android.app.Activity#onRestart() + */ + @Override + protected void onRestart() { + mIOIOManager.getIOIOAndroidApplicationHelper().restart(); + super.onRestart(); + } + /** * Called when the activity is resuming * (non-Javadoc) @@ -80,8 +107,6 @@ public class CameraActivity extends FragmentActivity implements IOIOTruckThreadL */ @Override protected void onResume() { - mIOIOManager = new IOIOTruckManager(this, this); - mIOIOManager.start(); PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE); mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, TAG); @@ -90,13 +115,23 @@ public class CameraActivity extends FragmentActivity implements IOIOTruckThreadL } /** - * Called when the IOIO Manager wants to log something * (non-Javadoc) - * @see com.TwentyCodes.android.IOIOTruck.IOIOTruckManager.IOIOTruckThreadListener#onLogUpdate(java.lang.String) + * @see android.support.v4.app.FragmentActivity#onStart() */ @Override - public void onLogUpdate(String log) { - mLogTextView.setText(log); + protected void onStart() { + mIOIOManager.getIOIOAndroidApplicationHelper().start(); + super.onStart(); + } + + /** + * (non-Javadoc) + * @see android.support.v4.app.FragmentActivity#onStop() + */ + @Override + protected void onStop() { + mIOIOManager.getIOIOAndroidApplicationHelper().stop(); + super.onStop(); } } diff --git a/IOIOTruck/src/com/TwentyCodes/android/IOIOTruck/IOIOTruckManager.java b/IOIOTruck/src/com/TwentyCodes/android/IOIOTruck/IOIOTruckConnectionManager.java similarity index 76% rename from IOIOTruck/src/com/TwentyCodes/android/IOIOTruck/IOIOTruckManager.java rename to IOIOTruck/src/com/TwentyCodes/android/IOIOTruck/IOIOTruckConnectionManager.java index 6539d7d..29c5f98 100644 --- a/IOIOTruck/src/com/TwentyCodes/android/IOIOTruck/IOIOTruckManager.java +++ b/IOIOTruck/src/com/TwentyCodes/android/IOIOTruck/IOIOTruckConnectionManager.java @@ -25,15 +25,17 @@ import ioio.lib.api.DigitalOutput; import ioio.lib.api.IOIO; import ioio.lib.api.PwmOutput; import ioio.lib.api.exception.ConnectionLostException; +import ioio.lib.util.IOIOLooper; +import ioio.lib.util.IOIOLooperProvider; +import ioio.lib.util.android.IOIOAndroidApplicationHelper; import android.app.Activity; - -import com.TwentyCodes.android.ioio.IOIOManager; +import android.util.Log; /** * This IOIO thread will be used to drive a rc truck * @author ricky barrette */ -public class IOIOTruckManager extends IOIOManager { +public class IOIOTruckConnectionManager implements IOIOLooper, IOIOLooperProvider { /** * This listener will be used to notify the owner of this thread to update the onscreen log @@ -55,6 +57,10 @@ public class IOIOTruckManager extends IOIOManager { private DigitalOutput mMotorDriverStandBy; private DigitalInput mLeftFrontBumber; private DigitalInput mRightFrontBumber; + private boolean isStatLedEnabled; + private IOIOAndroidApplicationHelper mIOIOAndroidApplicationHelper; + private IOIO mIOIO; + private DigitalOutput mStatLed; /** * Creates a new IOIOTruckThread @@ -62,8 +68,9 @@ public class IOIOTruckManager extends IOIOManager { * @param listener * @author ricky barrette */ - public IOIOTruckManager(Activity activity, IOIOTruckThreadListener listener){ + public IOIOTruckConnectionManager(Activity activity, IOIOTruckThreadListener listener){ super(); + mIOIOAndroidApplicationHelper = new IOIOAndroidApplicationHelper(activity, this); mActivity = activity; mListener = listener; updateLog(R.string.wait_ioio); @@ -111,9 +118,11 @@ public class IOIOTruckManager extends IOIOManager { * @see com.TwentyCodes.android.ioio.IOIOThread#onConnected() */ @Override - public void onConnected(IOIO ioio) throws ConnectionLostException { + public void setup(IOIO ioio) throws ConnectionLostException,InterruptedException { updateLog(R.string.ioio_connected); + mIOIO = ioio; + mStatLed = ioio.openDigitalOutput(0, true); mShifter = ioio.openPwmOutput(IOIOTruckValues.SHIFTER_PORT, IOIOTruckValues.RC_PWM_FRQ); mLeftMotor = new TB6612FNGMotorDriver(ioio, IOIOTruckValues.MOTOR_DRIVER_PWMA, IOIOTruckValues.MOTOR_DRIVER_A1, IOIOTruckValues.MOTOR_DRIVER_A2); mRightMotor = new TB6612FNGMotorDriver(ioio, IOIOTruckValues.MOTOR_DRIVER_PWMB, IOIOTruckValues.MOTOR_DRIVER_B1, IOIOTruckValues.MOTOR_DRIVER_B2); @@ -133,7 +142,7 @@ public class IOIOTruckManager extends IOIOManager { * @see com.TwentyCodes.android.ioio.IOIOThread#onDisconnect() */ @Override - public void onDisconnected() { + public void disconnected() { updateLog(R.string.wait_ioio); } @@ -145,35 +154,40 @@ public class IOIOTruckManager extends IOIOManager { @Override public void loop() throws ConnectionLostException, InterruptedException { - this.setStatLedEnabled(isStatLedEnabled()); - - mShifter.setPulseWidth(mShifterValue); - /* * we need to check our sensors before we can make a move. */ if(mLeftFrontBumber.read()){ //TODO backup, spin right, drive forward - setStatLedEnabled(false); + isStatLedEnabled = false; } else if(mRightFrontBumber.read()){ //TODO backup, spin left, drive forward - setStatLedEnabled(false); + isStatLedEnabled = false; } - mMotorDriverStandBy.write(isStatLedEnabled()); +// mIOIO.beginBatch(); + + mStatLed.write(!isStatLedEnabled); + + mShifter.setPulseWidth(mShifterValue); + + + mMotorDriverStandBy.write(isStatLedEnabled); /* * if the autonomous routine is running * then drive the truck * else stop the truck */ - if(isStatLedEnabled()){ + if(isStatLedEnabled){ arcadeDrive(); } else{ mLeftMotor.setSpeed(0); mRightMotor.setSpeed(0); } + +// mIOIO.endBatch(); } /** @@ -211,4 +225,40 @@ public class IOIOTruckManager extends IOIOManager { } }); } + + @Override + public void incompatible() { + // TODO Auto-generated method stub + + } + + @Override + public IOIOLooper createIOIOLooper(String connectionType, Object extra) { + Log.v(TAG, connectionType); + return this; + } + + /** + * @return the mIOIOAndroidApplicationHelper + */ + public IOIOAndroidApplicationHelper getIOIOAndroidApplicationHelper() { + return mIOIOAndroidApplicationHelper; + } + + /** + * set the status of the led + * @param b + * @author ricky barrette + */ + public void setStatLedEnabled(boolean b) { + this.isStatLedEnabled = b; + } + + /** + * @return state of status led + * @author ricky barrette + */ + public boolean isStatLedEnabled(){ + return this.isStatLedEnabled; + } } \ No newline at end of file diff --git a/IOIOTruck/src/com/TwentyCodes/android/IOIOTruck/NavigationActivity.java b/IOIOTruck/src/com/TwentyCodes/android/IOIOTruck/NavigationActivity.java index c855569..f81d0cb 100644 --- a/IOIOTruck/src/com/TwentyCodes/android/IOIOTruck/NavigationActivity.java +++ b/IOIOTruck/src/com/TwentyCodes/android/IOIOTruck/NavigationActivity.java @@ -41,7 +41,7 @@ import android.widget.Switch; import android.widget.TextView; import android.widget.Toast; -import com.TwentyCodes.android.IOIOTruck.IOIOTruckManager.IOIOTruckThreadListener; +import com.TwentyCodes.android.IOIOTruck.IOIOTruckConnectionManager.IOIOTruckThreadListener; import com.TwentyCodes.android.location.CompassListener; import com.TwentyCodes.android.location.GeoPointLocationListener; import com.TwentyCodes.android.location.GeoUtils; @@ -117,7 +117,7 @@ public class NavigationActivity extends FragmentActivity implements CompassListe } private static final String TAG = "NavigationActivity"; - private IOIOTruckManager mIOIOManager; + private IOIOTruckConnectionManager mIOIOManager; private MapFragment mMap; private TextView mLog; private ProgressBar mProgress; @@ -214,6 +214,7 @@ public class NavigationActivity extends FragmentActivity implements CompassListe protected void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.nav_activity); + /* * init UI */ @@ -233,6 +234,19 @@ public class NavigationActivity extends FragmentActivity implements CompassListe findViewById(R.id.mark_my_lcoation_button).setOnClickListener(this); findViewById(R.id.my_location_button).setOnClickListener(this); findViewById(R.id.map_button).setOnClickListener(this); + + mIOIOManager = new IOIOTruckConnectionManager(this, this); + mIOIOManager.getIOIOAndroidApplicationHelper().create(); + } + + /** + * (non-Javadoc) + * @see android.support.v4.app.FragmentActivity#onDestroy() + */ + @Override + protected void onDestroy() { + mIOIOManager.getIOIOAndroidApplicationHelper().destroy(); + super.onDestroy(); } /** @@ -266,7 +280,7 @@ public class NavigationActivity extends FragmentActivity implements CompassListe public void onFirstFix(boolean isFirstFix) { mMap.disableGPSProgess(); } - + /** * Called when android's location services have an update * (non-Javadoc) @@ -338,7 +352,7 @@ public class NavigationActivity extends FragmentActivity implements CompassListe } } - + /** * Called when the user selects a point for the truck to drive to * (non-Javadoc) @@ -358,13 +372,13 @@ public class NavigationActivity extends FragmentActivity implements CompassListe /** * Called when the IOIOTruckThread has a log it wants to display * (non-Javadoc) - * @see com.TwentyCodes.android.IOIOTruck.IOIOTruckManager.IOIOTruckThreadListener#onLogUpdate(java.lang.String) + * @see com.TwentyCodes.android.IOIOTruck.IOIOTruckConnectionManager.IOIOTruckThreadListener#onLogUpdate(java.lang.String) */ @Override public void onLogUpdate(String log) { updateLog(log); } - + /** * Called when the application is paused. We want to disconnect with the * IOIO at this point, as the user is no longer interacting with our @@ -373,12 +387,6 @@ public class NavigationActivity extends FragmentActivity implements CompassListe */ @Override protected void onPause() { - try { - mIOIOManager.abort(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - if(mLoggerThread != null) mLoggerThread.abort(); @@ -387,6 +395,16 @@ public class NavigationActivity extends FragmentActivity implements CompassListe super.onPause(); } + /** + * (non-Javadoc) + * @see android.app.Activity#onRestart() + */ + @Override + protected void onRestart() { + mIOIOManager.getIOIOAndroidApplicationHelper().restart(); + super.onRestart(); + } + /** * (non-Javadoc) * @see android.support.v4.app.FragmentActivity#onResume() @@ -401,14 +419,32 @@ public class NavigationActivity extends FragmentActivity implements CompassListe mMap.setDirectionsCompleteListener(this); mMap.setRadius((int) (Debug.RADIUS * 1E3)); mMap.enableGPSProgess(); - mIOIOManager = new IOIOTruckManager(this, this); - mIOIOManager.start(); PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE); mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, TAG); mWakeLock.acquire(); } + /** + * (non-Javadoc) + * @see android.support.v4.app.FragmentActivity#onStart() + */ + @Override + protected void onStart() { + mIOIOManager.getIOIOAndroidApplicationHelper().start(); + super.onStart(); + } + + /** + * (non-Javadoc) + * @see android.support.v4.app.FragmentActivity#onStop() + */ + @Override + protected void onStop() { + mIOIOManager.getIOIOAndroidApplicationHelper().stop(); + super.onStop(); + } + /** * updates the go/stop button based on isRunning * thread safe @@ -465,6 +501,7 @@ public class NavigationActivity extends FragmentActivity implements CompassListe runOnUiThread(new Runnable() { @Override public void run() { + mLog.append("\n"+log); /* diff --git a/IOIOTruck/src/com/TwentyCodes/android/IOIOTruck/TestActivity.java b/IOIOTruck/src/com/TwentyCodes/android/IOIOTruck/TestActivity.java index 8ceacfd..edbd61d 100755 --- a/IOIOTruck/src/com/TwentyCodes/android/IOIOTruck/TestActivity.java +++ b/IOIOTruck/src/com/TwentyCodes/android/IOIOTruck/TestActivity.java @@ -32,7 +32,7 @@ import android.widget.TextView; import com.MobileAnarchy.Android.Widgets.Joystick.JoystickMovedListener; import com.MobileAnarchy.Android.Widgets.Joystick.JoystickView; -import com.TwentyCodes.android.IOIOTruck.IOIOTruckManager.IOIOTruckThreadListener; +import com.TwentyCodes.android.IOIOTruck.IOIOTruckConnectionManager.IOIOTruckThreadListener; import com.TwentyCodes.android.exception.ExceptionHandler; /** @@ -50,7 +50,17 @@ public class TestActivity extends Activity implements JoystickMovedListener, OnS private TextView mDriveTextView; private TextView mSteerTextView; private TextView mShifterTextView; - private IOIOTruckManager mIOIOManager; + private IOIOTruckConnectionManager mIOIOManager; + + /** + * Called when the led swich is toggled + * (non-Javadoc) + * @see android.widget.CompoundButton.OnCheckedChangeListener#onCheckedChanged(android.widget.CompoundButton, boolean) + */ + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + mIOIOManager.setStatLedEnabled(isChecked); + } /** * Called when the activity is first created @@ -82,34 +92,28 @@ public class TestActivity extends Activity implements JoystickMovedListener, OnS mDriveTextView.setText(getString(R.string.drive)+1500); mSteerTextView.setText(getString(R.string.steer)+1500); mShifterTextView.setText(getString(R.string.shifter)+1500); + mIOIOManager = new IOIOTruckConnectionManager(this, this); + mIOIOManager.getIOIOAndroidApplicationHelper().create(); } /** - * Called when the application is resumed (also when first started). Here is - * where we'll create our IOIO thread. - * @author ricky barrette + * (non-Javadoc) + * @see android.app.Activity#onDestroy() */ @Override - protected void onResume() { - super.onResume(); - mIOIOManager = new IOIOTruckManager(this, this); - mIOIOManager.start(); + protected void onDestroy() { + mIOIOManager.getIOIOAndroidApplicationHelper().destroy(); + super.onDestroy(); } /** - * Called when the application is paused. We want to disconnect with the - * IOIO at this point, as the user is no longer interacting with our - * application. - * @author ricky barrette + * Called when the IOIOTruckThread has a log to publish + * (non-Javadoc) + * @see com.TwentyCodes.android.IOIOTruck.IOIOTruckConnectionManager.IOIOTruckThreadListener#onLogUpdate(java.lang.String) */ @Override - protected void onPause() { - super.onPause(); - try { - mIOIOManager.abort(); - } catch (InterruptedException e) { - e.printStackTrace(); - } + public void onLogUpdate(String log) { + mStatusTextView.setText(log); } /** @@ -127,6 +131,19 @@ public class TestActivity extends Activity implements JoystickMovedListener, OnS mSteerTextView.setText(getString(R.string.steer)+mIOIOManager.getSteerValue());//steer); } + /** + * Called when the shifter seekbar is adjusted + * (non-Javadoc) + * @see android.widget.SeekBar.OnSeekBarChangeListener#onProgressChanged(android.widget.SeekBar, int, boolean) + */ + @Override + public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { + float shifter = progress + 1000; + mIOIOManager.setShifterValue(progress + 1000); + + mShifterTextView.setText(getString(R.string.shifter)+ shifter); + } + /** * Called when the joystick is released * (non-Javadoc) @@ -137,6 +154,16 @@ public class TestActivity extends Activity implements JoystickMovedListener, OnS //NOT USED } + /** + * (non-Javadoc) + * @see android.app.Activity#onRestart() + */ + @Override + protected void onRestart() { + mIOIOManager.getIOIOAndroidApplicationHelper().restart(); + super.onRestart(); + } + /** * Called when the joystick ir returned to center * (non-Javadoc) @@ -150,16 +177,13 @@ public class TestActivity extends Activity implements JoystickMovedListener, OnS } /** - * Called when the shifter seekbar is adjusted * (non-Javadoc) - * @see android.widget.SeekBar.OnSeekBarChangeListener#onProgressChanged(android.widget.SeekBar, int, boolean) + * @see android.app.Activity#onStart() */ @Override - public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { - float shifter = progress + 1000; - mIOIOManager.setShifterValue(progress + 1000); - - mShifterTextView.setText(getString(R.string.shifter)+ shifter); + protected void onStart() { + mIOIOManager.getIOIOAndroidApplicationHelper().start(); + super.onStart(); } @Override @@ -167,28 +191,18 @@ public class TestActivity extends Activity implements JoystickMovedListener, OnS //NOT USED } + /** + * (non-Javadoc) + * @see android.app.Activity#onStop() + */ + @Override + protected void onStop() { + mIOIOManager.getIOIOAndroidApplicationHelper().stop(); + super.onStop(); + } + @Override public void onStopTrackingTouch(SeekBar seekBar) { //NOT USED } - - /** - * Called when the led swich is toggled - * (non-Javadoc) - * @see android.widget.CompoundButton.OnCheckedChangeListener#onCheckedChanged(android.widget.CompoundButton, boolean) - */ - @Override - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - mIOIOManager.setStatLedEnabled(isChecked); - } - - /** - * Called when the IOIOTruckThread has a log to publish - * (non-Javadoc) - * @see com.TwentyCodes.android.IOIOTruck.IOIOTruckManager.IOIOTruckThreadListener#onLogUpdate(java.lang.String) - */ - @Override - public void onLogUpdate(String log) { - mStatusTextView.setText(log); - } } \ No newline at end of file