//User must modify the below package with their package name
package com.example.chuankoudemo;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import Database.Global;
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.SharedPreferences;
import android.hardware.usb.UsbAccessory;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import android.widget.Toast;
/****************************** FT311 GPIO interface class ******************************************/
public abstract class FT311UARTInterface extends Activity {
private static final String ACTION_USB_PERMISSION = "com.UARTTest.USB_PERMISSION";
public UsbManager usbmanager;
public UsbAccessory usbaccessory;
public PendingIntent mPermissionIntent;
public ParcelFileDescriptor filedescriptor = null;
public FileInputStream inputstream = null;
public FileOutputStream outputstream = null;
public boolean mPermissionRequestPending = false;
public read_thread readThread;
private byte[] writeusbdata;
//public byte[] readBuffer; /* circular buffer */
private byte status;
//final int maxnumbytes = 1024;
final int maxnumLimit=256;
public boolean datareceived = false;
public boolean READ_ENABLE = false;
public boolean accessory_attached = false;
public static String ManufacturerString = "mManufacturer=FTDI";
public static String ModelString1 = "mModel=FTDIUARTDemo";
public static String ModelString2 = "mModel=Android Accessory FT312D";
public static String VersionString = "mVersion=1.0";
public SharedPreferences intsharePrefSettings;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Global.context = this;
intsharePrefSettings = null;// sharePrefSettings;
writeusbdata = new byte[maxnumLimit];
/* 128(make it 256, but looks like bytes should be enough) */
//readBuffer = new byte[maxnumbytes];
usbmanager = (UsbManager) this.getSystemService(Context.USB_SERVICE);
// Log.d("LED", "usbmanager" +usbmanager);
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(
ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
this.registerReceiver(mUsbReceiver, filter);
}
public void SetConfig(int baud, byte dataBits, byte stopBits, byte parity,
byte flowControl) {
//配置信息写到前8个byte
/* prepare the baud rate buffer */
writeusbdata[0] = (byte) baud;
writeusbdata[1] = (byte) (baud >> 8);
writeusbdata[2] = (byte) (baud >> 16);
writeusbdata[3] = (byte) (baud >> 24);
/* data bits */
writeusbdata[4] = dataBits;
/* stop bits */
writeusbdata[5] = stopBits;
/* parity */
writeusbdata[6] = parity;
/* flow control */
writeusbdata[7] = flowControl;
/* send the UART configuration packet */
SendPacket((int) 8);
}
/* write data */
public byte SendData(int numBytes, byte[] buffer) {
status = 0x00; /* success by default */
/*
* if num bytes are more than maximum limit
*/
if (numBytes < 1) {
/* return the status with the error in the command */
return status;
}
/* check for maximum limit */
if (numBytes > maxnumLimit) {
numBytes = maxnumLimit;
}
/* prepare the packet to be sent */
for (int count = 0; count < numBytes; count++) {
writeusbdata[count] = buffer[count];
}
if (numBytes != 64) {
SendPacket(numBytes);
} else {
byte temp = writeusbdata[63];
SendPacket(63);
writeusbdata[0] = temp;
SendPacket(1);
}
return status;
}
/* method to send on USB */
private void SendPacket(int numBytes) {
try {
if (outputstream != null) {
outputstream.write(writeusbdata, 0, numBytes);
}
} catch (IOException e) {
e.printStackTrace();
}
}
/* resume accessory */
public int ResumeAccessory() {
// Intent intent = getIntent();
if (inputstream != null && outputstream != null) {
return 1;
}
UsbAccessory[] accessories = usbmanager.getAccessoryList();
if (accessories != null) {
Toast.makeText(Global.context, "Accessory Attached",
Toast.LENGTH_SHORT).show();
} else {
// return 2 for accessory detached case
accessory_attached = false;
return 2;
}
UsbAccessory accessory = (accessories == null ? null : accessories[0]);
if (accessory != null) {
if (-1 == accessory.toString().indexOf(ManufacturerString)) {
Toast.makeText(Global.context, "Manufacturer is not matched!",
Toast.LENGTH_SHORT).show();
return 1;
}
if (-1 == accessory.toString().indexOf(ModelString1)
&& -1 == accessory.toString().indexOf(ModelString2)) {
Toast.makeText(Global.context, "Model is not matched!",
Toast.LENGTH_SHORT).show();
return 1;
}
if (-1 == accessory.toString().indexOf(VersionString)) {
Toast.makeText(Global.context, "Version is not matched!",
Toast.LENGTH_SHORT).show();
return 1;
}
Toast.makeText(Global.context,
"Manufacturer, Model & Version are matched!",
Toast.LENGTH_SHORT).show();
accessory_attached = true;
if (usbmanager.hasPermission(accessory)) {
OpenAccessory(accessory);
} else {
synchronized (mUsbReceiver) {
if (!mPermissionRequestPending) {
Toast.makeText(Global.context,
"Request USB Permission", Toast.LENGTH_SHORT)
.show();
usbmanager.requestPermission(accessory,
mPermissionIntent);
mPermissionRequestPending = true;
}
}
}
} else {
}
return 0;
}
/* destroy accessory */
public void DestroyAccessory(boolean bConfiged) {
if (true == bConfiged) {
READ_ENABLE = false; // set false condition for handler_thread to
// exit waiting data loop
writeusbdata[0] = 0; // send dummy data for instream.read going
SendPacket(1);
} else {
SetConfig(9600, (byte) 1, (byte) 8, (byte) 0, (byte) 0); // send
// default
// setting
// data
// for