package t4069.ardroid;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.hardware.usb.UsbAccessory;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
public class ArdroidActivity extends Activity {
private static final String TAG = "Ardroid";
/** Called when the activity is first created. */
boolean on = false, connected = false, reverse = false;
int currentRPM = 0;
SeekBar seekBar;
TextView rpmView, activeView;
Button activeButton, connectButton;
UsbManager mUsbManager;
UsbAccessory accessory;
FileOutputStream mOutputStream;
FileInputStream mInputStream;
ParcelFileDescriptor fd;
Thread thread;
boolean mPermissionRequestPending = false;
final Runnable runnable = new Runnable() {
public void run() {
if (mOutputStream != null) {
try {
for (int i = 0; i < 255; i++)
mOutputStream.write(255);
if (!connected) Toast.makeText(getApplicationContext(), "Not connected!", Toast.LENGTH_SHORT).show();
mOutputStream.write(on ? 1 : 0);
mOutputStream.write(reverse ? 1 : 0);
mOutputStream.write(currentRPM);
} catch (IOException e) {
e.printStackTrace();
}
}
}
};
private static final String ACTION_USB_PERMISSION = "t4069.ardroid.USB_PERMISSION";
PendingIntent mPermissionIntent;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(
ACTION_USB_PERMISSION), 0);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
registerReceiver(mUsbReceiver, filter);
mUsbManager = (UsbManager) getSystemService(USB_SERVICE);
seekBar = (SeekBar) findViewById(R.id.seekBar1);
rpmView = (TextView) findViewById(R.id.textView1);
activeView = (TextView) findViewById(R.id.textView2);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
char send = 0;
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
currentRPM = (progress % 255);
Log.d(TAG, "RPM equals:" + currentRPM);
reverse = (progress - 255 < 0);
rpmView.setText("RPM: " + (progress - 255));
send++;
if (send % 50 == 0 && connected) {thread.run();}
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
if (connected) thread.run();
}
});
seekBar.setMax(510);
seekBar.setProgress(255);
connectButton = (Button) findViewById(R.id.button2);
connectButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (mUsbManager.getAccessoryList() == null) {
Toast toast = Toast.makeText(getApplicationContext(),
"No accessories detected!", Toast.LENGTH_SHORT);
toast.show();
Log.d(TAG, "No list of accessories!");
return;
}
accessory = (UsbAccessory) mUsbManager.getAccessoryList()[0];
while (!mUsbManager.hasPermission(accessory)) {
synchronized (mUsbReceiver) {
if (!mPermissionRequestPending) {
mUsbManager.requestPermission(accessory,
mPermissionIntent);
mPermissionRequestPending = true;
}
}
}
openAccessory(accessory);
}
});
activeButton = (Button) findViewById(R.id.button1);
activeButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
toggleOn();
activeView.setText((on ? "Active" : "Inactive"));
}
});
}
public void onDestroy() {
super.onDestroy();
unregisterReceiver(mUsbReceiver);
}
public void onResume() {
super.onResume();
if (mOutputStream != null && mInputStream != null) {
return;
}
UsbAccessory[] accessories = mUsbManager.getAccessoryList();
if (accessories == null) return;
UsbAccessory accessory = accessories[0];
if (accessory != null) {
if (mUsbManager.hasPermission(accessory)) {
fd = mUsbManager.openAccessory(accessory);
mOutputStream = new FileOutputStream(fd.getFileDescriptor());
mInputStream = new FileInputStream(fd.getFileDescriptor());
} else {
synchronized (mUsbReceiver) {
if (!mPermissionRequestPending) {
mUsbManager.requestPermission(accessory,mPermissionIntent);
mPermissionRequestPending = true;
}
}
}
} else {
Log.d(TAG, "mAccessory is null");
}
}
private void openAccessory(UsbAccessory accessory) {
fd = mUsbManager.openAccessory(accessory);
if (fd != null) {
FileDescriptor mFileDescriptor = fd.getFileDescriptor();
mInputStream = new FileInputStream(mFileDescriptor);
thread = new Thread(runnable, "ArduinoCOMM");
if (!thread.isAlive())thread.start();
else {thread.stop();thread.start();}
Log.d(TAG, "accessory opened");
connected = true;
} else {
Log.d(TAG, "accessory open fail");
}
}
private void closeAccessory() {
if (fd != null && mOutputStream != null && mInputStream != null) {
try {
mInputStream.close();
mOutputStream.close();
fd.close();
} catch (IOException e) {
Log.w(TAG, "Couldn't close port!");
return;
}
}
connected = false;
on = false;
fd = null;
mInputStream = null;
mOutputStream = null;
accessory = null;
}
protected void toggleOn() {
if (!connected)
return;
on = !on;
thread.run();
}
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
if (intent.getBooleanExtra(
UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if (accessory != null) {
connected = true;
}
} else {
Log.d(TAG, "permission denied for accessory "
+ accessory);
}
}
} else if (UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action)) {
UsbAccessory mAccessory = (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
if (accessory != null && mAccessory.equals(accessory)) {
closeAccessory();
}
}
}
};
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
}