Skip to content

Commit 774bb9e

Browse files
Merge branch 'master' into beta
Conflicts: Android/AndroidManifest.xml
2 parents 731fd85 + 970782a commit 774bb9e

File tree

9 files changed

+229
-101
lines changed

9 files changed

+229
-101
lines changed

Android/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:tools="http://schemas.android.com/tools"
44
package="org.droidplanner"
5-
android:versionCode="89"
5+
android:versionCode="90"
66
android:versionName="please run version.sh to get the version name">
77

88
<uses-sdk

Android/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ dependencies {
1414
compile 'org.osmdroid:osmdroid-android:4.1'
1515

1616
compile files('libs/usbseriallibrary.jar')
17+
compile files('libs/j2xx.jar')
1718
compile files('libs/protobuf-java-2.5.0.jar')
1819
compile files('libs/osmbonuspack_v4.4.jar')
1920
compile files('libs/jeromq-0.3.4.jar')

Android/libs/j2xx.jar

85.6 KB
Binary file not shown.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package org.droidplanner.android.communication.connection;
2+
3+
import java.io.IOException;
4+
5+
import android.content.Context;
6+
import android.hardware.usb.UsbManager;
7+
import android.util.Log;
8+
9+
import com.hoho.android.usbserial.driver.UsbSerialDriver;
10+
import com.hoho.android.usbserial.driver.UsbSerialProber;
11+
12+
public class UsbCDCConnection extends UsbConnection {
13+
private static UsbSerialDriver sDriver = null;
14+
15+
protected UsbCDCConnection(Context parentContext) {
16+
super(parentContext);
17+
}
18+
19+
@Override
20+
protected void openConnection() throws IOException {
21+
// Get UsbManager from Android.
22+
UsbManager manager = (UsbManager) parentContext.getSystemService(Context.USB_SERVICE);
23+
24+
// Find the first available driver.
25+
sDriver = UsbSerialProber.findFirstDevice(manager);
26+
27+
if (sDriver == null) {
28+
Log.d("USB", "No Devices found");
29+
throw new IOException("No Devices found");
30+
} else {
31+
Log.d("USB", "Opening using Baud rate " + baud_rate);
32+
try {
33+
sDriver.open();
34+
sDriver.setParameters(baud_rate, 8, UsbSerialDriver.STOPBITS_1,
35+
UsbSerialDriver.PARITY_NONE);
36+
} catch (IOException e) {
37+
Log.e("USB", "Error setting up device: " + e.getMessage(), e);
38+
try {
39+
sDriver.close();
40+
} catch (IOException e2) {
41+
// Ignore.
42+
}
43+
sDriver = null;
44+
}
45+
}
46+
}
47+
48+
@Override
49+
protected void readDataBlock() throws IOException {
50+
// Read data from driver. This call will return upto readData.length
51+
// bytes.
52+
// If no data is received it will timeout after 200ms (as set by
53+
// parameter 2)
54+
try {
55+
iavailable = sDriver.read(readData, 200);
56+
} catch (NullPointerException e) {
57+
Log.e("USB", "Error Reading: " + e.getMessage()
58+
+ "\nAssuming inaccessible USB device. Closing connection.", e);
59+
closeConnection();
60+
}
61+
if (iavailable == 0)
62+
iavailable = -1;
63+
// Log.d("USB", "Bytes read" + iavailable);
64+
}
65+
66+
@Override
67+
protected void sendBuffer(byte[] buffer) {
68+
// Write data to driver. This call should write buffer.length bytes
69+
// if data cant be sent , then it will timeout in 500ms (as set by
70+
// parameter 2)
71+
if (connected && sDriver != null) {
72+
try {
73+
sDriver.write(buffer, 500);
74+
} catch (IOException e) {
75+
Log.e("USB", "Error Sending: " + e.getMessage(), e);
76+
}
77+
}
78+
}
79+
80+
@Override
81+
protected void closeConnection() throws IOException {
82+
if (sDriver != null) {
83+
try {
84+
sDriver.close();
85+
} catch (IOException e) {
86+
// Ignore.
87+
}
88+
sDriver = null;
89+
}
90+
}
91+
}
Lines changed: 18 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,19 @@
11
package org.droidplanner.android.communication.connection;
22

3-
import java.io.IOException;
3+
import java.util.Map.Entry;
44

55
import android.content.Context;
66
import android.content.SharedPreferences;
7+
import android.hardware.usb.UsbDevice;
78
import android.hardware.usb.UsbManager;
8-
import android.util.Log;
99

10-
import com.hoho.android.usbserial.driver.UsbSerialDriver;
11-
import com.hoho.android.usbserial.driver.UsbSerialProber;
10+
public abstract class UsbConnection extends MAVLinkConnection {
11+
protected static int baud_rate = 57600;
1212

13-
/**
14-
* This version is modified by Helibot to use the
15-
* "USB Serial for Android Library" //See
16-
* https://code.google.com/p/usb-serial-for-android/ // It should allow support
17-
* of FDTI and other Serial to USB converters. // It should allow APM v2.0 and
18-
* 2.5 to connect via USB cable straight to APM. // Be sure to set the
19-
* Telementry speed in the setting menu to // 115200 when connecting directly
20-
* with USB cable.
21-
*/
22-
public class UsbConnection extends MAVLinkConnection {
23-
private static int baud_rate = 57600;
24-
private static UsbSerialDriver sDriver = null;
25-
26-
public UsbConnection(Context parentContext) {
13+
protected UsbConnection(Context parentContext) {
2714
super(parentContext);
2815
}
2916

30-
@Override
31-
protected void openConnection() throws IOException {
32-
openCOM();
33-
}
34-
35-
@Override
36-
protected void readDataBlock() throws IOException {
37-
// Read data from driver. This call will return upto readData.length
38-
// bytes.
39-
// If no data is received it will timeout after 200ms (as set by
40-
// parameter 2)
41-
try {
42-
iavailable = sDriver.read(readData, 200);
43-
} catch (NullPointerException e) {
44-
Log.e("USB", "Error Reading: " + e.getMessage() + "\nAssuming inaccessible USB device. Closing connection.", e);
45-
closeConnection();
46-
}
47-
if (iavailable == 0)
48-
iavailable = -1;
49-
// Log.d("USB", "Bytes read" + iavailable);
50-
}
51-
52-
@Override
53-
protected void sendBuffer(byte[] buffer) {
54-
// Write data to driver. This call should write buffer.length bytes
55-
// if data cant be sent , then it will timeout in 500ms (as set by
56-
// parameter 2)
57-
if (connected && sDriver != null) {
58-
try {
59-
sDriver.write(buffer, 500);
60-
} catch (IOException e) {
61-
Log.e("USB", "Error Sending: " + e.getMessage(), e);
62-
}
63-
}
64-
}
65-
66-
@Override
67-
protected void closeConnection() throws IOException {
68-
if (sDriver != null) {
69-
try {
70-
sDriver.close();
71-
} catch (IOException e) {
72-
// Ignore.
73-
}
74-
sDriver = null;
75-
}
76-
}
77-
7817
@Override
7918
protected void getPreferences(SharedPreferences prefs) {
8019
String baud_type = prefs.getString("pref_baud_type", "57600");
@@ -86,33 +25,21 @@ else if (baud_type.equals("57600"))
8625
baud_rate = 115200;
8726
}
8827

89-
private void openCOM() throws IOException {
90-
// Get UsbManager from Android.
91-
UsbManager manager = (UsbManager) parentContext.getSystemService(Context.USB_SERVICE);
92-
93-
// Find the first available driver.
94-
// **TODO: We should probably step through all available USB Devices
95-
// ...but its unlikely to happen on a Phone/tablet running DroidPlanner.
96-
sDriver = UsbSerialProber.findFirstDevice(manager);
97-
98-
if (sDriver == null) {
99-
Log.d("USB", "No Devices found");
100-
throw new IOException("No Devices found");
28+
public static MAVLinkConnection getUSBConnection(Context context) {
29+
if (isFTDIdevice(context)) {
30+
return new UsbFTDIConnection(context);
10131
} else {
102-
Log.d("USB", "Opening using Baud rate " + baud_rate);
103-
try {
104-
sDriver.open();
105-
sDriver.setParameters(baud_rate, 8, UsbSerialDriver.STOPBITS_1,
106-
UsbSerialDriver.PARITY_NONE);
107-
} catch (IOException e) {
108-
Log.e("USB", "Error setting up device: " + e.getMessage(), e);
109-
try {
110-
sDriver.close();
111-
} catch (IOException e2) {
112-
// Ignore.
113-
}
114-
sDriver = null;
32+
return new UsbCDCConnection(context);
33+
}
34+
}
35+
36+
private static boolean isFTDIdevice(Context context) {
37+
UsbManager manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
38+
for (Entry<String, UsbDevice> device : manager.getDeviceList().entrySet()) {
39+
if (device.getValue().getVendorId() == 0x0403) {
40+
return true;
11541
}
11642
}
43+
return false;
11744
}
11845
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package org.droidplanner.android.communication.connection;
2+
3+
import java.io.IOException;
4+
5+
import android.content.Context;
6+
import android.util.Log;
7+
8+
import com.ftdi.j2xx.D2xxManager;
9+
import com.ftdi.j2xx.FT_Device;
10+
11+
public class UsbFTDIConnection extends UsbConnection {
12+
private static final byte LATENCY_TIMER = 32;
13+
14+
private FT_Device ftDev;
15+
16+
protected UsbFTDIConnection(Context parentContext) {
17+
super(parentContext);
18+
}
19+
20+
@Override
21+
protected void openConnection() throws IOException {
22+
D2xxManager ftD2xx = null;
23+
try {
24+
ftD2xx = D2xxManager.getInstance(parentContext);
25+
} catch (D2xxManager.D2xxException ex) {
26+
ex.printStackTrace();
27+
}
28+
29+
int DevCount = ftD2xx.createDeviceInfoList(parentContext);
30+
if (DevCount < 1) {
31+
throw new IOException("No Devices found");
32+
}
33+
34+
ftDev = ftD2xx.openByIndex(parentContext, 0);
35+
36+
if (ftDev == null) {
37+
throw new IOException("No Devices found");
38+
}
39+
40+
Log.d("USB", "Opening using Baud rate " + baud_rate);
41+
ftDev.setBitMode((byte) 0, D2xxManager.FT_BITMODE_RESET);
42+
ftDev.setBaudRate(baud_rate);
43+
ftDev.setDataCharacteristics(D2xxManager.FT_DATA_BITS_8, D2xxManager.FT_STOP_BITS_1,
44+
D2xxManager.FT_PARITY_NONE);
45+
ftDev.setFlowControl(D2xxManager.FT_FLOW_NONE, (byte) 0x00, (byte) 0x00);
46+
ftDev.setLatencyTimer(LATENCY_TIMER);
47+
ftDev.purge((byte) (D2xxManager.FT_PURGE_TX | D2xxManager.FT_PURGE_RX));
48+
49+
if (!ftDev.isOpen()) {
50+
throw new IOException();
51+
} else {
52+
Log.d("USB", "COM open");
53+
}
54+
}
55+
56+
@Override
57+
protected void readDataBlock() throws IOException {
58+
iavailable = ftDev.getQueueStatus();
59+
if (iavailable > 0) {
60+
if (iavailable > 4096)
61+
iavailable = 4096;
62+
try {
63+
ftDev.read(readData, iavailable);
64+
65+
} catch (NullPointerException e) {
66+
Log.e("USB", "Error Reading: " + e.getMessage()
67+
+ "\nAssuming inaccessible USB device. Closing connection.", e);
68+
closeConnection();
69+
}
70+
}
71+
72+
if (iavailable == 0) {
73+
iavailable = -1;
74+
}
75+
}
76+
77+
@Override
78+
protected void sendBuffer(byte[] buffer) {
79+
if (connected & ftDev != null) {
80+
try {
81+
ftDev.write(buffer);
82+
} catch (Exception e) {
83+
Log.e("USB", "Error Sending: " + e.getMessage(), e);
84+
}
85+
}
86+
}
87+
88+
@Override
89+
protected void closeConnection() throws IOException {
90+
if (ftDev != null) {
91+
try {
92+
ftDev.close();
93+
} catch (Exception e) {
94+
// Ignore.
95+
}
96+
ftDev = null;
97+
}
98+
}
99+
}

Android/src/org/droidplanner/android/utils/Utils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public MAVLinkConnection getConnection(Context context) {
4040
USB {
4141
@Override
4242
public MAVLinkConnection getConnection(Context context) {
43-
return new UsbConnection(context);
43+
return UsbConnection.getUSBConnection(context);
4444
}
4545
},
4646
TCP {

ChangeLog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ All the changes are logged below (preferable with the pull request numbers in pa
99

1010
# Releases
1111

12+
## Droidplanner v2.6.2
13+
* Parameter download on start-up (#976)
14+
* Use FTDI usb library for FTDI devices (#975)
15+
* Fix NullPointer error on editor (#972)
16+
* Fix a couple of mission items unpack method (#971)
17+
1218
## Droidplanner v2.6.1
1319
* Failsafe messages (#922)
1420
* Fixed the circle item on mission planning (#970)

0 commit comments

Comments
 (0)