Changeset 60b4520 in orbit for android/src
- Timestamp:
- 11/15/12 18:57:06 (8 years ago)
- Branches:
- master, RawEEG, Raw_EEG_Plot, Servo, Tab_Interface, pyramid
- Children:
- 41c4762
- Parents:
- 8a0fa760
- Location:
- android/src/info/puzzlebox/orbit
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
android/src/info/puzzlebox/orbit/MainActivity.java
r3909100 r60b4520 1 1 package info.puzzlebox.orbit; 2 2 3 import java.io.IOException; 4 import java.util.concurrent.ExecutorService; 5 import java.util.concurrent.Executors; 6 7 import com.hoho.android.usbserial.driver.UsbSerialDriver; 8 import com.hoho.android.usbserial.driver.UsbSerialProber; 9 import com.hoho.android.usbserial.util.SerialInputOutputManager; 10 import com.neurosky.thinkgear.TGDevice; 11 3 12 import android.os.Bundle; 13 import android.os.Handler; 14 import android.os.Message; 15 import android.annotation.SuppressLint; 4 16 import android.app.Activity; 17 import android.bluetooth.BluetoothAdapter; 18 import android.content.Context; 19 import android.graphics.Color; 20 import android.graphics.drawable.ClipDrawable; 21 import android.graphics.drawable.ShapeDrawable; 22 import android.graphics.drawable.shapes.RoundRectShape; 23 import android.hardware.usb.UsbManager; 24 import android.view.Gravity; 5 25 import android.view.Menu; 6 26 import android.view.MenuItem; 27 import android.view.View; 28 import android.widget.Button; 29 import android.widget.ProgressBar; 30 import android.widget.SeekBar; 31 import android.widget.TextView; 32 import android.widget.Toast; 7 33 import android.support.v4.app.NavUtils; 8 9 public class MainActivity extends Activity { 10 11 @Override 12 public void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.activity_main); 15 getActionBar().setDisplayHomeAsUpEnabled(true); 16 } 17 18 @Override 19 public boolean onCreateOptionsMenu(Menu menu) { 20 getMenuInflater().inflate(R.menu.activity_main, menu); 21 return true; 22 } 23 24 25 @Override 26 public boolean onOptionsItemSelected(MenuItem item) { 27 switch (item.getItemId()) { 28 case android.R.id.home: 29 NavUtils.navigateUpFromSameTask(this); 30 return true; 31 } 32 return super.onOptionsItemSelected(item); 33 } 34 import android.text.Layout; 35 import android.text.method.ScrollingMovementMethod; 36 37 public class MainActivity extends Activity implements SeekBar.OnSeekBarChangeListener { 38 39 // @Override 40 // public void onCreate(Bundle savedInstanceState) { 41 // super.onCreate(savedInstanceState); 42 // setContentView(R.layout.activity_main); 43 // getActionBar().setDisplayHomeAsUpEnabled(true); 44 // } 45 // 46 // @Override 47 // public boolean onCreateOptionsMenu(Menu menu) { 48 // getMenuInflater().inflate(R.menu.activity_main, menu); 49 // return true; 50 // } 51 // 52 // 53 // @Override 54 // public boolean onOptionsItemSelected(MenuItem item) { 55 // switch (item.getItemId()) { 56 // case android.R.id.home: 57 // NavUtils.navigateUpFromSameTask(this); 58 // return true; 59 // } 60 // return super.onOptionsItemSelected(item); 61 // } 62 63 BluetoothAdapter bluetoothAdapter; 64 65 ProgressBar progressBarAttention; 66 ProgressBar progressBarMeditation; 67 ProgressBar progressBarSignal; 68 ProgressBar progressBarPower; 69 SeekBar seekBarAttention; 70 SeekBar seekBarMeditation; 71 TextView tv; 72 Button b; 73 74 int tgSignal = 0; 75 int[] thresholdValuesAttention = new int[101]; 76 int[] thresholdValuesMeditation = new int[101]; 77 // int minimumPower = 60; // minimum power to which the flame effect can be set 78 int minimumPower = 0; // minimum power to which the flame effect can be set 79 int maximumPower = 100; 80 81 SerialDevice serial; 82 // int serialBaudRate = 9600; 83 int serialBaudRate = 133333; 84 85 86 TGDevice tgDevice; 87 final boolean rawEnabled = false; 88 89 90 private static final String MAC_ADDRESS = "D0:DF:9A:69:5D:42"; // MindWave Mobile Dev (p1-dev-60-01) 91 //private static final String MAC_ADDRESS = "00:16:53:09:0B:B9"; 92 93 /** 94 * The device currently in use, or {@code null}. 95 */ 96 private UsbSerialDriver mSerialDevice; 97 98 /** 99 * The system's USB service. 100 */ 101 private UsbManager mUsbManager; 102 103 104 private final ExecutorService mExecutor = Executors.newSingleThreadExecutor(); 105 106 private SerialInputOutputManager mSerialIoManager; 107 108 private final SerialInputOutputManager.Listener mListener = 109 new SerialInputOutputManager.Listener() { 110 111 public void onRunError(Exception e) { 112 // Log.d(TAG, "Runner stopped."); 113 } 114 115 public void onNewData(final byte[] data) { 116 // PuzzleboxBrainstormsHelicopter.this.runOnUiThread(new Runnable() { 117 // @Override 118 // public void run() { 119 // PuzzleboxBrainstormsHelicopter.this.updateReceivedData(data); 120 // } 121 // }); 122 } 123 }; 124 125 126 @SuppressWarnings("deprecation") 127 @Override 128 public void onCreate(Bundle savedInstanceState) { 129 /** Called when the activity is first created. */ 130 super.onCreate(savedInstanceState); 131 // setContentView(R.layout.main); 132 setContentView(R.layout.activity_main); 133 134 mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE); 135 136 progressBarAttention = (ProgressBar)findViewById(R.id.progressBarAttention); 137 final float[] roundedCorners = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 }; 138 ShapeDrawable progressBarAttentionDrawable = new ShapeDrawable(new RoundRectShape(roundedCorners, null,null)); 139 String progressBarAttentionColor = "#FF0000"; 140 progressBarAttentionDrawable.getPaint().setColor(Color.parseColor(progressBarAttentionColor)); 141 ClipDrawable progressAttention = new ClipDrawable(progressBarAttentionDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL); 142 progressBarAttention.setProgressDrawable(progressAttention); 143 progressBarAttention.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.progress_horizontal)); 144 // progressBarAttention.setProgress(80); 145 146 progressBarMeditation = (ProgressBar)findViewById(R.id.progressBarMeditation); 147 ShapeDrawable progressBarMeditationDrawable = new ShapeDrawable(new RoundRectShape(roundedCorners, null,null)); 148 String progressBarMeditationColor = "#0000FF"; 149 progressBarMeditationDrawable.getPaint().setColor(Color.parseColor(progressBarMeditationColor)); 150 ClipDrawable progressMeditation = new ClipDrawable(progressBarMeditationDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL); 151 progressBarMeditation.setProgressDrawable(progressMeditation); 152 progressBarMeditation.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.progress_horizontal)); 153 // progressBarMeditation.setProgress(60); 154 155 progressBarSignal = (ProgressBar)findViewById(R.id.progressBarSignal); 156 ShapeDrawable progressBarSignalDrawable = new ShapeDrawable(new RoundRectShape(roundedCorners, null,null)); 157 String progressBarSignalColor = "#00FF00"; 158 progressBarSignalDrawable.getPaint().setColor(Color.parseColor(progressBarSignalColor)); 159 ClipDrawable progressSignal = new ClipDrawable(progressBarSignalDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL); 160 progressBarSignal.setProgressDrawable(progressSignal); 161 progressBarSignal.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.progress_horizontal)); 162 // progressBarSignal.setProgress(40); 163 progressBarSignal.setProgress(tgSignal); 164 165 progressBarPower = (ProgressBar)findViewById(R.id.progressBarPower); 166 ShapeDrawable progressBarPowerDrawable = new ShapeDrawable(new RoundRectShape(roundedCorners, null,null)); 167 String progressBarPowerColor = "#FFFF00"; 168 progressBarPowerDrawable.getPaint().setColor(Color.parseColor(progressBarPowerColor)); 169 ClipDrawable progressPower = new ClipDrawable(progressBarPowerDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL); 170 progressBarPower.setProgressDrawable(progressPower); 171 progressBarPower.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.progress_horizontal)); 172 //progressBarPower.setProgress(20); 173 174 //tv = (TextView)findViewById(R.id.textViewDebugConsole); 175 //tv.setText(""); 176 //tv.append("Android version: " + Integer.valueOf(android.os.Build.VERSION.SDK) + "\n" ); 177 178 seekBarAttention = (SeekBar)findViewById(R.id.seekBarAttention); 179 seekBarAttention.setOnSeekBarChangeListener(this); 180 seekBarMeditation = (SeekBar)findViewById(R.id.seekBarMeditation); 181 seekBarMeditation.setOnSeekBarChangeListener(this); 182 183 184 updatePowerThresholds(); 185 // updateNXTPower(); 186 updatePower(); 187 188 189 tv = (TextView)findViewById(R.id.textViewDebugConsole); 190 tv.setMovementMethod(new ScrollingMovementMethod()); 191 tv.setText(""); 192 appendTextAndScroll("Android version: " + Integer.valueOf(android.os.Build.VERSION.SDK) + "\n" ); 193 194 195 bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 196 if(bluetoothAdapter == null) { 197 // Alert user that Bluetooth is not available 198 Toast.makeText(this, "Bluetooth not available", Toast.LENGTH_LONG).show(); 199 finish(); 200 return; 201 }else { 202 /* create the TGDevice */ 203 tgDevice = new TGDevice(bluetoothAdapter, handler); 204 } 205 206 207 serial = new SerialDevice(); 208 serial.setSerialDevice(mSerialDevice); 209 210 211 } // onCreate 212 213 214 @Override 215 protected void onResume() { 216 super.onResume(); 217 mSerialDevice = UsbSerialProber.acquire(mUsbManager); 218 // Log.d(TAG, "Resumed, mSerialDevice=" + mSerialDevice); 219 if (mSerialDevice == null) { 220 // mTitleTextView.setText("No serial device."); 221 // tv.setText("No serial device.\n"); 222 appendTextAndScroll("No serial device.\n"); 223 } else { 224 try { 225 mSerialDevice.open(); 226 mSerialDevice.setBaudRate(serialBaudRate); 227 serial.setSerialDevice(mSerialDevice); 228 serial.execute(new String[] {""} ); 229 } catch (IOException e) { 230 // Log.e(TAG, "Error setting up device: " + e.getMessage(), e); 231 // mTitleTextView.setText("Error opening device: " + e.getMessage()); 232 // tv.setText("Error opening device: " + e.getMessage() + "\n"); 233 appendTextAndScroll("Error opening device: " + e.getMessage() + "\n"); 234 235 try { 236 mSerialDevice.close(); 237 } catch (IOException e2) { 238 // Ignore. 239 } 240 mSerialDevice = null; 241 serial.setSerialDevice(null); 242 return; 243 } 244 appendTextAndScroll("Serial device: " + mSerialDevice + "\n"); 245 } 246 onDeviceStateChange(); 247 } 248 249 250 @Override 251 public void onDestroy() { 252 tgDevice.close(); 253 if (mSerialDevice != null) { 254 try { 255 mSerialDevice.close(); 256 } catch (IOException e) { 257 // TODO Auto-generated catch block 258 e.printStackTrace(); 259 } 260 } 261 super.onDestroy(); 262 } 263 264 265 private final Handler handler = new Handler() { 266 267 /** 268 * Handles messages from TGDevice 269 */ 270 271 @SuppressLint({ "HandlerLeak", "HandlerLeak" }) 272 @Override 273 public void handleMessage(Message msg) { 274 switch (msg.what) { 275 case TGDevice.MSG_STATE_CHANGE: 276 277 switch (msg.arg1) { 278 case TGDevice.STATE_IDLE: 279 break; 280 case TGDevice.STATE_CONNECTING: 281 appendTextAndScroll("Connecting: " + MAC_ADDRESS + "\n"); 282 break; 283 case TGDevice.STATE_CONNECTED: 284 appendTextAndScroll("Connected.\n"); 285 tgDevice.start(); 286 break; 287 case TGDevice.STATE_NOT_FOUND: 288 appendTextAndScroll("Can't find\n"); 289 break; 290 case TGDevice.STATE_NOT_PAIRED: 291 appendTextAndScroll("not paired\n"); 292 break; 293 case TGDevice.STATE_DISCONNECTED: 294 appendTextAndScroll("Disconnected mang\n"); 295 break; 296 } 297 298 break; 299 300 case TGDevice.MSG_POOR_SIGNAL: 301 tgSignal = calculateSignal(msg.arg1); 302 //signal = msg.arg1; 303 // tv.append("PoorSignal: " + msg.arg1 + "\n"); 304 appendTextAndScroll("\nPoorSignal: " + msg.arg1 + "\n"); 305 progressBarSignal.setProgress(tgSignal); 306 307 308 // updateNXTPower(); 309 updatePower(); 310 311 312 break; 313 case TGDevice.MSG_RAW_DATA: 314 //raw1 = msg.arg1; 315 //tv.append("Got raw: " + msg.arg1 + "\n"); 316 break; 317 case TGDevice.MSG_HEART_RATE: 318 // tv.append("Heart rate: " + msg.arg1 + "\n"); 319 appendTextAndScroll("Heart rate: " + msg.arg1 + "\n"); 320 break; 321 case TGDevice.MSG_ATTENTION: 322 //att = msg.arg1; 323 // tv.append("Attention: " + msg.arg1 + "\n"); 324 appendTextAndScroll("Attention: " + msg.arg1 + "\n"); 325 progressBarAttention.setProgress(msg.arg1); 326 //Log.v("HelloA", "Attention: " + att + "\n"); 327 328 // updateNXTPower(); 329 updatePower(); 330 331 break; 332 case TGDevice.MSG_MEDITATION: 333 // tv.append("Meditation: " + msg.arg1 + "\n"); 334 appendTextAndScroll("Meditation: " + msg.arg1 + "\n"); 335 progressBarMeditation.setProgress(msg.arg1); 336 337 // updateNXTPower(); 338 updatePower(); 339 340 break; 341 case TGDevice.MSG_BLINK: 342 //tv.append("Blink: " + msg.arg1 + "\n"); 343 break; 344 case TGDevice.MSG_RAW_COUNT: 345 //tv.append("Raw Count: " + msg.arg1 + "\n"); 346 break; 347 case TGDevice.MSG_LOW_BATTERY: 348 Toast.makeText(getApplicationContext(), "Low battery!", Toast.LENGTH_SHORT).show(); 349 break; 350 case TGDevice.MSG_RAW_MULTI: 351 //TGRawMulti rawM = (TGRawMulti)msg.obj; 352 //tv.append("Raw1: " + rawM.ch1 + "\nRaw2: " + rawM.ch2); 353 default: 354 break; 355 } 356 } 357 }; 358 359 360 public void connectHeadset(View view) { 361 362 if(tgDevice.getState() != TGDevice.STATE_CONNECTING && tgDevice.getState() != TGDevice.STATE_CONNECTED) 363 tgDevice.connect(rawEnabled); 364 365 366 } 367 368 369 public int calculateSignal(int signal) { 370 int value; 371 372 switch (signal) { 373 case 200: 374 value = 0; 375 case 0: 376 value = 100; 377 default: 378 value = (int)(100 - ((signal / 200.0) * 100)); 379 } 380 return(value); 381 } // calculateSignal 382 383 384 private void appendTextAndScroll(String text) { 385 if (tv != null) { 386 // tv.append(text + "\n"); 387 tv.append(text); 388 final Layout layout = tv.getLayout(); 389 if (layout != null) { 390 int scrollDelta = layout.getLineBottom(tv.getLineCount() - 1) 391 - tv.getScrollY() - tv.getHeight(); 392 if (scrollDelta > 0) 393 tv.scrollBy(0, scrollDelta); 394 } 395 } 396 } // appendTextAndScroll 397 398 399 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) { 400 //appendTextAndScroll(progress + " " + 401 //getString(R.string.seekbar_from_touch) + "=" + fromTouch); 402 403 updatePowerThresholds(); 404 updatePower(); 405 406 } 407 408 409 public void onStartTrackingTouch(SeekBar seekBar) { 410 //appendTextAndScroll(getString(R.string.seekbar_tracking_on)); 411 } 412 413 414 public void onStopTrackingTouch(SeekBar seekBar) { 415 //appendTextAndScroll(getString(R.string.seekbar_tracking_off)); 416 } 417 418 419 public void updatePowerThresholds() { 420 421 int power; 422 int attentionSeekValue; 423 int meditationSeekValue; 424 float percentOfMaxPower; 425 426 // Reset all values to zero 427 for (int i = 0; i < thresholdValuesAttention.length; i++) { 428 thresholdValuesAttention[i] = 0; 429 thresholdValuesMeditation[i] = 0; 430 } 431 432 attentionSeekValue = seekBarAttention.getProgress(); 433 if (attentionSeekValue > 0) { 434 for (int i = attentionSeekValue; i < thresholdValuesAttention.length; i++) { 435 436 // Slider @ 70 437 // Attention @ 70 438 // Percentage = 0% ((100-70) - (100-70)) / (100-70) 439 // Power = 60 (minimumPower) 440 441 // Slider @ 70 442 // Attention @ 80 443 // Percentage = 33% ((100-70) - (100-80)) / (100-70) 444 // Power = 73 445 446 // Slider @ 70 447 // Attention @ 90 448 // Percentage = 66% ((100-70) - (100-90)) / (100-70) 449 // Power = 86 450 451 // Slider @ 70 452 // Attention @ 100 453 // Percentage = 100% ((100-70) - (100-100)) / (100-70) 454 // Power = 100 455 456 percentOfMaxPower = (float)( ((100 - attentionSeekValue) - (100 - i)) / (float)(100 - attentionSeekValue) ); 457 power = thresholdValuesAttention[i] + (int)( minimumPower + ((maximumPower - minimumPower) * percentOfMaxPower) ); 458 thresholdValuesAttention[i] = power; 459 460 // appendTextAndScroll(i + ":" + power + " "); 461 } 462 } 463 464 meditationSeekValue = seekBarMeditation.getProgress(); 465 if (meditationSeekValue > 0) { 466 for (int i = meditationSeekValue; i < thresholdValuesMeditation.length; i++) { 467 percentOfMaxPower = (float)( ((100 - meditationSeekValue) - (100 - i)) / (float)(100 - meditationSeekValue) ); 468 power = thresholdValuesMeditation[i] + (int)( minimumPower + ((maximumPower - minimumPower) * percentOfMaxPower) ); 469 thresholdValuesMeditation[i] = power; 470 } 471 } 472 473 } 474 475 476 public void updatePower() { 477 478 int new_speed = calculateSpeed(); 479 String command; 480 progressBarPower.setProgress(new_speed); 481 482 if (new_speed > 0) { 483 command = "hover"; 484 } else { 485 command = "neutral"; 486 } 487 488 appendTextAndScroll("Command: " + command + "\n"); 489 490 // String command = getPowerCommand(new_speed); 491 492 // writeCommand(command); 493 494 if (mSerialDevice != null) { 495 serial.setCommand(command); 496 } 497 498 } 499 500 501 public String getPowerCommand(int speed) { 502 503 String command = "%"; 504 505 if (speed >= 100) 506 command = "%100"; 507 else if (speed >= 10) 508 command = "%0" + speed; 509 else if (speed >= 0) 510 command = "%00" + speed; 511 else 512 command = "%000"; 513 514 appendTextAndScroll("Command for serial device: " + command + "\n"); 515 516 return(command); 517 518 } 519 520 521 public void demoMode(View view) { 522 523 String command = "neutral"; 524 525 appendTextAndScroll("Command for serial device: " + command + "\n"); 526 serial.setCommand(command); 527 528 try { 529 Thread.sleep(5000); 530 } catch (InterruptedException e) { 531 // TODO Auto-generated catch block 532 e.printStackTrace(); 533 } 534 535 command = "hover"; 536 appendTextAndScroll("Command for serial device: " + command + "\n"); 537 serial.setCommand(command); 538 539 try { 540 Thread.sleep(2500); 541 } catch (InterruptedException e) { 542 // TODO Auto-generated catch block 543 e.printStackTrace(); 544 } 545 546 547 command = "neutral"; 548 appendTextAndScroll("Command for serial device: " + command + "\n"); 549 serial.setCommand(command); 550 551 552 } // demoMode 553 554 555 public int calculateSpeed() { 556 557 int attention = progressBarAttention.getProgress(); 558 int meditation = progressBarMeditation.getProgress(); 559 int attentionSeekValue = seekBarAttention.getProgress(); 560 int meditationSeekValue = seekBarMeditation.getProgress(); 561 562 int speed = 0; 563 564 if (attention > attentionSeekValue) 565 speed = thresholdValuesAttention[attention]; 566 if (meditation > meditationSeekValue) 567 speed = speed + thresholdValuesMeditation[meditation]; 568 569 if (speed > maximumPower) 570 speed = maximumPower; 571 if (speed < minimumPower) 572 speed = 0; 573 574 return(speed); 575 576 } 577 578 579 private void stopIoManager() { 580 if (mSerialIoManager != null) { 581 // Log.i(TAG, "Stopping io manager .."); 582 mSerialIoManager.stop(); 583 mSerialIoManager = null; 584 } 585 } 586 587 588 private void startIoManager() { 589 if (mSerialDevice != null) { 590 // Log.i(TAG, "Starting io manager .."); 591 mSerialIoManager = new SerialInputOutputManager(mSerialDevice, mListener); 592 mExecutor.submit(mSerialIoManager); 593 } 594 } 595 596 597 private void onDeviceStateChange() { 598 stopIoManager(); 599 startIoManager(); 600 } 34 601 35 602 }
Note: See TracChangeset
for help on using the changeset viewer.