Advertisement
Guest User

Untitled

a guest
Aug 8th, 2012
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.06 KB | None | 0 0
  1. package t4069.ardroid;
  2.  
  3. import java.io.FileDescriptor;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7.  
  8. import android.app.Activity;
  9. import android.app.PendingIntent;
  10. import android.content.BroadcastReceiver;
  11. import android.content.Context;
  12. import android.content.Intent;
  13. import android.content.IntentFilter;
  14. import android.content.pm.ActivityInfo;
  15. import android.content.res.Configuration;
  16. import android.hardware.usb.UsbAccessory;
  17. import android.hardware.usb.UsbManager;
  18. import android.os.Bundle;
  19. import android.os.ParcelFileDescriptor;
  20. import android.util.Log;
  21. import android.view.View;
  22. import android.view.View.OnClickListener;
  23. import android.widget.Button;
  24. import android.widget.SeekBar;
  25. import android.widget.SeekBar.OnSeekBarChangeListener;
  26. import android.widget.TextView;
  27. import android.widget.Toast;
  28.  
  29. public class ArdroidActivity extends Activity {
  30.     private static final String TAG = "Ardroid";
  31.     /** Called when the activity is first created. */
  32.     boolean on = false, connected = false, reverse = false;
  33.     int currentRPM = 0;
  34.     SeekBar seekBar;
  35.     TextView rpmView, activeView;
  36.     Button activeButton, connectButton;
  37.     UsbManager mUsbManager;
  38.     UsbAccessory accessory;
  39.     FileOutputStream mOutputStream;
  40.     FileInputStream mInputStream;
  41.     ParcelFileDescriptor fd;
  42.     Thread thread;
  43.     boolean mPermissionRequestPending = false;
  44.     final Runnable runnable = new Runnable() {
  45.         public void run() {
  46.                 if (mOutputStream != null) {
  47.                     try {
  48.                         for (int i = 0; i < 255; i++)
  49.                         mOutputStream.write(255);
  50.                        
  51.                         if (!connected) Toast.makeText(getApplicationContext(), "Not connected!", Toast.LENGTH_SHORT).show();
  52.                         mOutputStream.write(on ? 1 : 0);
  53.                         mOutputStream.write(reverse ? 1 : 0);
  54.                         mOutputStream.write(currentRPM);
  55.                     } catch (IOException e) {
  56.                         e.printStackTrace();
  57.                     }
  58.                 }
  59.         }
  60.     };
  61.     private static final String ACTION_USB_PERMISSION = "t4069.ardroid.USB_PERMISSION";
  62.     PendingIntent mPermissionIntent;
  63.  
  64.     public void onCreate(Bundle savedInstanceState) {
  65.         super.onCreate(savedInstanceState);
  66.         setContentView(R.layout.main);
  67.         mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(
  68.                 ACTION_USB_PERMISSION), 0);
  69.         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  70.         IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
  71.         filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
  72.         registerReceiver(mUsbReceiver, filter);
  73.         mUsbManager = (UsbManager) getSystemService(USB_SERVICE);
  74.         seekBar = (SeekBar) findViewById(R.id.seekBar1);
  75.         rpmView = (TextView) findViewById(R.id.textView1);
  76.         activeView = (TextView) findViewById(R.id.textView2);
  77.         seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
  78.             char send = 0;
  79.             public void onProgressChanged(SeekBar seekBar, int progress,
  80.                     boolean fromUser) {
  81.                 currentRPM = (progress % 255);
  82.                 Log.d(TAG, "RPM equals:" + currentRPM);
  83.                 reverse = (progress - 255 < 0);
  84.                 rpmView.setText("RPM: " + (progress - 255));
  85.                 send++;
  86.                 if (send % 50 == 0 && connected) {thread.run();}
  87.             }
  88.  
  89.             public void onStartTrackingTouch(SeekBar seekBar) {
  90.             }
  91.  
  92.             public void onStopTrackingTouch(SeekBar seekBar) {
  93.                 if (connected) thread.run();
  94.             }
  95.  
  96.         });
  97.         seekBar.setMax(510);
  98.         seekBar.setProgress(255);
  99.         connectButton = (Button) findViewById(R.id.button2);
  100.         connectButton.setOnClickListener(new OnClickListener() {
  101.             public void onClick(View v) {
  102.                 if (mUsbManager.getAccessoryList() == null) {
  103.                     Toast toast = Toast.makeText(getApplicationContext(),
  104.                             "No accessories detected!", Toast.LENGTH_SHORT);
  105.                     toast.show();
  106.                     Log.d(TAG, "No list of accessories!");
  107.                     return;
  108.                 }
  109.                 accessory = (UsbAccessory) mUsbManager.getAccessoryList()[0];
  110.                 while (!mUsbManager.hasPermission(accessory)) {
  111.                     synchronized (mUsbReceiver) {
  112.                         if (!mPermissionRequestPending) {
  113.                             mUsbManager.requestPermission(accessory,
  114.                                     mPermissionIntent);
  115.                             mPermissionRequestPending = true;
  116.                         }
  117.                     }
  118.                 }
  119.                     openAccessory(accessory);
  120.             }
  121.             });
  122.         activeButton = (Button) findViewById(R.id.button1);
  123.         activeButton.setOnClickListener(new OnClickListener() {
  124.             public void onClick(View v) {
  125.                 toggleOn();
  126.                 activeView.setText((on ? "Active" : "Inactive"));
  127.             }
  128.         });
  129.     }
  130.     public void onDestroy() {
  131.         super.onDestroy();
  132.         unregisterReceiver(mUsbReceiver);
  133.     }
  134.    
  135.     public void onResume() {
  136.         super.onResume();
  137.         if (mOutputStream != null && mInputStream != null) {
  138.             return;
  139.         }
  140.         UsbAccessory[] accessories = mUsbManager.getAccessoryList();
  141.         if (accessories == null) return;
  142.         UsbAccessory accessory = accessories[0];
  143.         if (accessory != null) {
  144.             if (mUsbManager.hasPermission(accessory)) {
  145.                 fd =  mUsbManager.openAccessory(accessory);
  146.                 mOutputStream = new FileOutputStream(fd.getFileDescriptor());
  147.                 mInputStream = new FileInputStream(fd.getFileDescriptor());
  148.             } else {
  149.                 synchronized (mUsbReceiver) {
  150.                     if (!mPermissionRequestPending) {
  151.                         mUsbManager.requestPermission(accessory,mPermissionIntent);
  152.                         mPermissionRequestPending = true;
  153.                     }
  154.                 }
  155.             }
  156.         } else {
  157.             Log.d(TAG, "mAccessory is null");
  158.         }
  159.     }
  160.    
  161.     private void openAccessory(UsbAccessory accessory) {
  162.         fd = mUsbManager.openAccessory(accessory);
  163.         if (fd != null) {
  164.             FileDescriptor mFileDescriptor = fd.getFileDescriptor();
  165.             mInputStream = new FileInputStream(mFileDescriptor);
  166.             thread = new Thread(runnable, "ArduinoCOMM");
  167.             if (!thread.isAlive())thread.start();
  168.             else {thread.stop();thread.start();}
  169.             Log.d(TAG, "accessory opened");
  170.             connected = true;
  171.         } else {
  172.             Log.d(TAG, "accessory open fail");
  173.         }
  174.     }
  175.    
  176.     private void closeAccessory() {
  177.         if (fd != null && mOutputStream != null && mInputStream != null) {
  178.             try {
  179.                 mInputStream.close();
  180.                 mOutputStream.close();
  181.                 fd.close();
  182.             } catch (IOException e) {
  183.                 Log.w(TAG, "Couldn't close port!");
  184.                 return;
  185.             }
  186.         }
  187.             connected = false;
  188.             on = false;
  189.             fd = null;
  190.             mInputStream = null;
  191.             mOutputStream = null;
  192.             accessory = null;
  193.     }
  194.  
  195.  
  196.     protected void toggleOn() {
  197.         if (!connected)
  198.             return;
  199.         on = !on;
  200.        
  201.         thread.run();
  202.     }
  203.  
  204.  
  205.     private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
  206.         public void onReceive(Context context, Intent intent) {
  207.             String action = intent.getAction();
  208.             if (ACTION_USB_PERMISSION.equals(action)) {
  209.                 synchronized (this) {
  210.                     if (intent.getBooleanExtra(
  211.                             UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
  212.                         if (accessory != null) {
  213.                             connected = true;
  214.                         }
  215.                     } else {
  216.                         Log.d(TAG, "permission denied for accessory "
  217.                                 + accessory);
  218.                     }
  219.                 }
  220.             } else if (UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action)) {
  221.                 UsbAccessory mAccessory = (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
  222.                 if (accessory != null && mAccessory.equals(accessory)) {
  223.                     closeAccessory();
  224.                 }
  225.             }
  226.         }
  227.     };
  228.    
  229.     public void onConfigurationChanged(Configuration newConfig) {
  230.         super.onConfigurationChanged(newConfig);
  231.     }
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement