Skip to content

Commit

Permalink
Added checkboxes for LED and button state. LED checkbox works as
Browse files Browse the repository at this point in the history
expected.
  • Loading branch information
bcr committed May 12, 2011
1 parent 54f2d34 commit 3d9bed5
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 15 deletions.
15 changes: 15 additions & 0 deletions res/layout/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,20 @@
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textSize="30sp"
/>
<CheckBox
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button State"
android:textSize="30sp"
android:id="@+id/buttoncheck"
/>
<CheckBox
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="LED State"
android:textSize="30sp"
android:id="@+id/ledcheck"
/>
</LinearLayout>
66 changes: 51 additions & 15 deletions src/com/prototype_eng/hellofez/HelloFez.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,26 @@
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.android.future.usb.UsbAccessory;
import com.android.future.usb.UsbManager;

class FezRunnable implements Runnable
class FezController
{
private FileDescriptor fileDescriptor;
private FileInputStream inputStream;
private FileOutputStream outputStream;

public FezRunnable(UsbManager usbManager, UsbAccessory accessory)
public FezController(UsbManager usbManager, UsbAccessory accessory)
{
fileDescriptor = usbManager.openAccessory(accessory).getFileDescriptor();
inputStream = new FileInputStream(fileDescriptor);
outputStream = new FileOutputStream(fileDescriptor);
}

@Override
public void run()
{
}

public int ledOn() throws IOException
{
return sendCommand(1);
Expand All @@ -42,39 +40,77 @@ public int ledOff() throws IOException

private int sendCommand(int command) throws IOException
{
outputStream.write(command);
return inputStream.read();
synchronized (outputStream)
{
outputStream.write(command);
return inputStream.read();
}
}
}

public class HelloFez extends Activity {
private static final String TAG = "HelloFez";
private FezController fezController;
private CheckBox ledcheck;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
Log.v(TAG, "onCreate");

maybeCreateFezController();
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ledcheck = (CheckBox) findViewById(R.id.ledcheck);
ledcheck.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked)
{
Log.v(TAG, "OnCheckedChanged " + isChecked);
if (fezController != null)
{
try
{
if (isChecked)
{
fezController.ledOn();
}
else
{
fezController.ledOff();
}
}
catch (IOException e)
{
Log.v(TAG, "Unexpected exception", e);
}
}
}
});
}

private void maybeCreateFezController()
{
UsbAccessory accessory = UsbManager.getAccessory(getIntent());
Log.v(TAG, (accessory != null) ? "accessory! I haz it!" : "No accessory for you!");
UsbManager usbManager = UsbManager.getInstance(this);
Log.v(TAG, (usbManager != null) ? "manager! I haz it!" : "No manager for you!");
if ((accessory != null) && (usbManager != null))
{
Log.v(TAG, "Got an accessory and a manager");
FezRunnable runnable = new FezRunnable(usbManager, accessory);
Log.v(TAG, "Runnable constructed");
fezController = new FezController(usbManager, accessory);
Log.v(TAG, "Controller constructed");
try
{
Log.v(TAG, "ledOn returned " + runnable.ledOn());
Log.v(TAG, "ledOff returned " + runnable.ledOff());
Log.v(TAG, "ledOn returned " + fezController.ledOn());
Log.v(TAG, "ledOff returned " + fezController.ledOff());
}
catch (IOException e)
{
Log.v(TAG, "Unexpected exception", e);
}
}
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}

0 comments on commit 3d9bed5

Please sign in to comment.