Changeset 403fb9a in orbit for android/src
- Timestamp:
- 12/11/12 04:42:08 (8 years ago)
- Branches:
- master, RawEEG, Raw_EEG_Plot, Servo, Tab_Interface, pyramid
- Children:
- e511c74
- Parents:
- cd5d285
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
android/src/info/puzzlebox/orbit/MainActivity.java
rcd5d285 r403fb9a 15 15 */ 16 16 17 //import java.io.IOException; 18 //import java.util.ArrayList; 19 //import java.util.Arrays; 20 //import java.util.Set; 21 //import java.util.concurrent.ExecutorService; 22 //import java.util.concurrent.Executors; 23 17 24 import android.media.AudioFormat; 18 25 import android.media.AudioManager; … … 26 33 import android.app.Activity; 27 34 import android.bluetooth.BluetoothAdapter; 35 //import android.bluetooth.BluetoothDevice; 28 36 //import android.content.Context; 29 37 import android.graphics.Color; … … 43 51 import android.text.method.ScrollingMovementMethod; 44 52 45 //import java.io.IOException;46 //import java.util.concurrent.ExecutorService;47 //import java.util.concurrent.Executors;48 49 53 import com.neurosky.thinkgear.TGDevice; 50 54 … … 56 60 public class MainActivity extends Activity implements SeekBar.OnSeekBarChangeListener { 57 61 58 BluetoothAdapter bluetoothAdapter; 59 62 /** 63 * UI 64 */ 60 65 ProgressBar progressBarAttention; 61 66 ProgressBar progressBarMeditation; … … 67 72 Button b; 68 73 74 75 /** 76 * Logging 77 */ 69 78 String TAG = "MainActivity"; 70 79 71 /**72 * The Bluetooth MAC address is temporarily hard-coded into the source code73 */74 private static final String MAC_ADDRESS = "D0:DF:9A:69:5D:42"; // MindWave Mobile Dev (p1-dev-60-01)75 // private static final String MAC_ADDRESS = "9C:B7:0D:5E:E5:6A"; // UK So Television76 // private static final String MAC_ADDRESS = "9C:B7:0D:75:A8:05"; // Discovery Channel Canada77 // private static final String MAC_ADDRESS = "9C:B7:0D:90:EB:92"; // Steve Harvey Show78 80 79 81 /** 82 * Audio 80 83 * Currently the flight control command is hard-coded into WAV files 81 84 * In a future release the tones used to communicate with the infrared dongle … … 84 87 int audioFile = R.raw.throttle_hover_android_common; 85 88 // int audioFile = R.raw.throttle_hover_android_htc_one_x; 86 87 88 TGDevice tgDevice;89 int tgSignal = 0;90 int[] thresholdValuesAttention = new int[101];91 int[] thresholdValuesMeditation = new int[101];92 int minimumPower = 0; // minimum power for the helicopter throttle93 int maximumPower = 100; // maximum power for the helicopter throttle94 String currentCommand = "neutral";95 final boolean rawEnabled = false;96 97 // SerialDevice serial;98 // int serialBaudRate = 9600;99 89 100 90 private SoundPool soundPool; … … 119 109 120 110 /** 121 * The device currently in use, or {@code null}.111 * Bluetooth 122 112 */ 113 BluetoothAdapter bluetoothAdapter; 114 // ArrayList<String> pairedBluetoothDevices; 115 116 117 /** 118 * Neurosky ThinkGear Device 119 */ 120 TGDevice tgDevice; 121 int tgSignal = 0; 122 int[] thresholdValuesAttention = new int[101]; 123 int[] thresholdValuesMeditation = new int[101]; 124 int minimumPower = 0; // minimum power for the helicopter throttle 125 int maximumPower = 100; // maximum power for the helicopter throttle 126 String currentCommand = "neutral"; 127 final boolean rawEnabled = false; 128 129 130 /** 131 * USB Serial 132 */ 133 // SerialDevice serial; 134 // int serialBaudRate = 9600; 135 136 /** The device currently in use, or {@code null}. */ 123 137 // private UsbSerialDriver mSerialDevice; 124 138 125 /** 126 * The system's USB service. 127 */ 139 /** The system's USB service. */ 128 140 // private UsbManager mUsbManager; 129 141 130 142 /** Thread Service */ 131 143 // private final ExecutorService mExecutor = Executors.newSingleThreadExecutor(); 132 144 … … 153 165 @Override 154 166 public void onCreate(Bundle savedInstanceState) { 155 /** Called when the activity is first created. */ 167 168 /** 169 * This method called when the program is first loaded 170 * or when it is re-loaded into memory after being killed 171 * by the Task Manager. 172 */ 173 174 175 /** 176 * Configure the UI elements 177 */ 156 178 super.onCreate(savedInstanceState); 157 179 setContentView(R.layout.activity_main); … … 206 228 207 229 208 bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 209 if(bluetoothAdapter == null) { 210 // Alert user that Bluetooth is not available 211 Toast.makeText(this, "Bluetooth not available", Toast.LENGTH_LONG).show(); 212 finish(); 213 return; 214 } else { 215 /** create the TGDevice */ 216 tgDevice = new TGDevice(bluetoothAdapter, handler); 217 } 218 219 220 // mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE); 221 // serial = new SerialDevice(); 222 // serial.setSerialDevice(mSerialDevice); 223 224 225 // genTone(); 226 230 /** 231 * Prepare audio stream 232 */ 227 233 228 234 /** Set the hardware buttons to control the audio output */ … … 239 245 soundID = soundPool.load(this, audioFile, 1); 240 246 247 // genTone(); 248 249 250 /** 251 * Prepare Bluetooth and NeuroSky ThinkGear EEG interface 252 */ 253 bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 254 255 if(bluetoothAdapter == null) { 256 // Alert user that Bluetooth is not available 257 Toast.makeText(this, "Bluetooth not available", Toast.LENGTH_LONG).show(); 258 finish(); 259 return; 260 } else { 261 /** create the TGDevice */ 262 tgDevice = new TGDevice(bluetoothAdapter, handler); 263 264 /** Retrieve a list of paired Bluetooth adapters */ 265 // Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices(); 266 // pairedBluetoothDevices = new ArrayList<String>(Arrays.asList(pairedDevices.toString())); 267 /** 268 * NOTE: To get device names iterate through pairedBluetoothDevices 269 * and call the getName() method on each BluetoothDevice object. 270 */ 271 } 272 273 274 /** 275 * Prepare USB Serial 276 */ 277 // mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE); 278 // serial = new SerialDevice(); 279 // serial.setSerialDevice(mSerialDevice); 280 241 281 242 282 } // onCreate … … 245 285 @Override 246 286 protected void onResume() { 287 288 /** 289 * This method is called when the Activity has been 290 * resumed after being placed in the background 291 */ 292 247 293 super.onResume(); 248 /** This section handles the USB Serial device when the program is resumed */ 294 295 /** This section handles the USB Serial device when the program is resumed */ 249 296 // mSerialDevice = UsbSerialProber.acquire(mUsbManager); 250 297 // // Log.d(TAG, "Resumed, mSerialDevice=" + mSerialDevice); … … 273 320 // appendTextAndScroll("Serial device: " + mSerialDevice + "\n"); 274 321 // } 275 onDeviceStateChange(); 322 // 323 // onDeviceStateChange(); 276 324 } 277 325 … … 280 328 public void onDestroy() { 281 329 330 /** 331 * This method is called when the Activity is terminated 332 */ 333 282 334 tgDevice.close(); 283 335 336 /** Handle USB Serial */ 284 337 // if (mSerialDevice != null) { 285 338 // try { … … 296 349 297 350 private void appendTextAndScroll(String text) { 351 352 /** 353 * This method is called to write a status message 354 * to the text display area, then automatically 355 * scroll to the bottom of visible text. 356 */ 357 298 358 if (tv != null) { 299 359 // tv.append(text + "\n"); … … 312 372 public void setButtonText(int buttonId, String text) { 313 373 374 /** 375 * Shortcut for changing the text on a button 376 */ 377 314 378 Button button = (Button) findViewById(buttonId); 315 379 button.setText(text); … … 329 393 330 394 public void onStartTrackingTouch(SeekBar seekBar) { 395 396 /** 397 * Method required by SeekBar.OnSeekBarChangeListener 398 */ 399 331 400 //appendTextAndScroll(getString(R.string.seekbar_tracking_on)); 401 332 402 } // onStartTrackingTouch 333 403 334 404 335 405 public void onStopTrackingTouch(SeekBar seekBar) { 406 407 /** 408 * Method required by SeekBar.OnSeekBarChangeListener 409 */ 410 336 411 //appendTextAndScroll(getString(R.string.seekbar_tracking_off)); 412 337 413 } // onStopTrackingTouch 338 414 339 415 340 private void stopIoManager() { 341 // if (mSerialIoManager != null) { 342 // // Log.i(TAG, "Stopping io manager .."); 343 // mSerialIoManager.stop(); 344 // mSerialIoManager = null; 345 // } 346 } // stopIoManager 347 348 349 private void startIoManager() { 350 // if (mSerialDevice != null) { 351 // // Log.i(TAG, "Starting io manager .."); 352 // mSerialIoManager = new SerialInputOutputManager(mSerialDevice, mListener); 353 // mExecutor.submit(mSerialIoManager); 354 // } 355 } // startIoManager 356 357 358 private void onDeviceStateChange() { 359 stopIoManager(); 360 startIoManager(); 361 } // onDeviceStateChange 416 // private void stopIoManager() { 417 // 418 // /** 419 // * Method required by USB Serial 420 // */ 421 // 422 // if (mSerialIoManager != null) { 423 // // Log.i(TAG, "Stopping io manager .."); 424 // mSerialIoManager.stop(); 425 // mSerialIoManager = null; 426 // } 427 // } // stopIoManager 428 429 430 // private void startIoManager() { 431 // 432 // /** 433 // * Method required by USB Serial 434 // */ 435 // 436 // if (mSerialDevice != null) { 437 // // Log.i(TAG, "Starting io manager .."); 438 // mSerialIoManager = new SerialInputOutputManager(mSerialDevice, mListener); 439 // mExecutor.submit(mSerialIoManager); 440 // } 441 // } // startIoManager 442 443 444 // private void onDeviceStateChange() { 445 // 446 // /** 447 // * Called by onResume() to manage USB Serial 448 // */ 449 // 450 // stopIoManager(); 451 // startIoManager(); 452 // } // onDeviceStateChange 362 453 363 454 … … 365 456 366 457 /** 367 * Handles messages from TGDevice458 * Handles data packets from NeuroSky ThinkGear device 368 459 */ 369 460 … … 378 469 break; 379 470 case TGDevice.STATE_CONNECTING: 380 Log.d(TAG, "Connecting to EEG: " + MAC_ADDRESS); 381 appendTextAndScroll("Connecting to EEG: " + MAC_ADDRESS + "\n"); 471 // Log.d(TAG, "Connecting to EEG: " + MAC_ADDRESS); 472 // appendTextAndScroll("Connecting to EEG: " + MAC_ADDRESS + "\n"); 473 Log.d(TAG, "Connecting to EEG"); 474 appendTextAndScroll("Connecting to EEG\n"); 382 475 break; 383 476 case TGDevice.STATE_CONNECTED: … … 398 491 Log.d(TAG, "EEG Disconnected"); 399 492 appendTextAndScroll("EEG Disconnected\n"); 493 disconnectHeadset(); 400 494 break; 401 495 } … … 455 549 public void connectHeadset(View view) { 456 550 551 /** 552 * Called when the "Connect" button is pressed 553 */ 554 457 555 if (tgDevice.getState() != TGDevice.STATE_CONNECTING && tgDevice.getState() != TGDevice.STATE_CONNECTED) 458 556 tgDevice.connect(rawEnabled); 459 557 460 558 else if (tgDevice.getState() == TGDevice.STATE_CONNECTED) 559 /** "Disconnect" button was pressed */ 461 560 disconnectHeadset(); 462 561 … … 466 565 467 566 public void disconnectHeadset() { 567 568 /** 569 * Called when "Disconnect" button is pressed 570 */ 571 468 572 tgDevice.stop(); 469 573 tgDevice.close(); … … 474 578 progressBarPower.setProgress(0); 475 579 setButtonText(R.id.button1, "Connect"); 476 appendTextAndScroll("EEG Disconnected\n");477 580 } // disconnectHeadset 478 581 … … 503 606 504 607 public void updatePowerThresholds() { 608 609 /** 610 * The "Power" level refers to the Puzzlebox Orbit helicopter's 611 * throttle setting. Typically this is an "off" or "on" state, 612 * meaning the helicopter is either flying or not flying at all. 613 * However this method could be used to increase the throttle 614 * or perhaps the forward motion of the helicopter to a level 615 * proportionate to how far past their target brainwave levels 616 * are set (via the progress bar sliders). 617 */ 505 618 506 619 int power; … … 563 676 public int calculateSpeed() { 564 677 678 /** 679 * This method is used when calculating whether 680 * or not the "Attention" or "Meditation" levels 681 * are sufficient to trigger the helicopter throttle 682 */ 683 565 684 int attention = progressBarAttention.getProgress(); 566 685 int meditation = progressBarMeditation.getProgress(); … … 587 706 public void updatePower() { 588 707 708 /** 709 * This method updates the power level of the 710 * "Throttle" and triggers the audio stream 711 * which is used to fly the helicopter 712 */ 713 589 714 int new_speed = calculateSpeed(); 590 715 String command = ""; … … 593 718 if (new_speed > 0) { 594 719 720 /** Start playback of audio control stream */ 595 721 if (currentCommand == "neutral") { 596 722 playControl(); … … 601 727 } else { 602 728 603 // if (currentCommand == "hover") { 604 // stopControl(); 605 // } 729 /** Land the helicopter */ 606 730 stopControl(); 607 731 … … 612 736 Log.v(TAG, "Command: " + command); 613 737 614 // String command = getPowerCommand(new_speed); 615 616 // writeCommand(command); 617 738 /** This sequence is used for managing the USB Serial control */ 739 // command = getPowerCommand(new_speed); 740 // 618 741 // if (mSerialDevice != null) { 619 742 // serial.setCommand(command); … … 625 748 626 749 627 public String getPowerCommand(int speed) { 628 629 String command = "%"; 630 631 if (speed >= 100) 632 command = "%100"; 633 else if (speed >= 10) 634 command = "%0" + speed; 635 else if (speed >= 0) 636 command = "%00" + speed; 637 else 638 command = "%000"; 639 640 Log.v(TAG, "Power command: " + command); 641 // Log.v(TAG, "Command for serial device: " + command); 642 // appendTextAndScroll("Command for serial device: " + command + "\n"); 643 644 return(command); 645 646 } // getPowerCommand 750 // public String getPowerCommand(int speed) { 751 // 752 // /** 753 // * This method is used to set the throttle 754 // * level using USB Serial to communicate 755 // * with an Arduino device 756 // */ 757 // 758 // String command = "%"; 759 // 760 // if (speed >= 100) 761 // command = "%100"; 762 // else if (speed >= 10) 763 // command = "%0" + speed; 764 // else if (speed >= 0) 765 // command = "%00" + speed; 766 // else 767 // command = "%000"; 768 // 769 // Log.v(TAG, "Power command: " + command); 770 // // Log.v(TAG, "Command for serial device: " + command); 771 // // appendTextAndScroll("Command for serial device: " + command + "\n"); 772 // 773 // return(command); 774 // 775 // } // getPowerCommand 647 776 648 777 649 778 public void playControl() { 650 779 651 780 /** 652 781 * Play audio control file … … 668 797 669 798 public void stopControl() { 670 671 /** 672 * Stop playing audio file799 800 /** 801 * Stop playing audio control file 673 802 */ 674 803 … … 684 813 685 814 void genTone(){ 686 /** fill out the array */ 815 816 /** 817 * Generate a carrier signal for communication 818 * with the IR Dongle 819 */ 820 821 /** Fill out the array */ 687 822 for (int i = 0; i < numSamples; ++i) { 688 823 sample[i] = Math.sin(2 * Math.PI * i / (sampleRate/freqOfTone)); … … 690 825 691 826 /** 692 * convert to 16 bit pcm sound array693 * assumes the sample buffer isnormalized.827 * Convert to 16 bit pcm sound array. 828 * The sample buffer is assumed to be normalized. 694 829 */ 695 830 int idx = 0; 696 831 for (final double dVal : sample) { 697 /** scale to maximum amplitude */832 /** Scale to maximum amplitude */ 698 833 final short val = (short) ((dVal * 32767)); 699 /** in 16 bit wav PCM, first byte is the low order byte */834 /** In 16 bit wav PCM, first byte is the low order byte */ 700 835 generatedSnd[idx++] = (byte) (val & 0x00ff); 701 836 generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8); … … 706 841 707 842 void playTone(){ 843 844 /** 845 * Play the generated carrier signal 846 */ 847 708 848 final AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 709 849 // sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO, … … 720 860 721 861 public void demoMode(View view) { 862 863 /** 864 * Demo mode is called when the "Test Helicopter" button is pressed. 865 * This method can be easily adjusted for testing new features 866 * during development. 867 */ 722 868 723 869 // playTone();
Note: See TracChangeset
for help on using the changeset viewer.