Skip to content

Commit 7769527

Browse files
author
Federico Fissore
committed
working on #223: Auto-detection of serial ports. Linux version ready
1 parent 5b8ac97 commit 7769527

File tree

9 files changed

+107
-15
lines changed

9 files changed

+107
-15
lines changed

app/build.xml

+8-8
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@
7676
<os family="unix"/>
7777
</condition>
7878

79-
<!--
80-
<dirname property="blah" file="${java.home}" />
81-
<echo message="here! ${java.home}/lib/tools.jar or there: ${blah}" />
82-
<echo message="override ${env.JAVA_HOME}/lib/tools.jar" />
83-
<fail />
84-
-->
85-
<javac source="1.5" target="1.5"
79+
<!--
80+
<dirname property="blah" file="${java.home}" />
81+
<echo message="here! ${java.home}/lib/tools.jar or there: ${blah}" />
82+
<echo message="override ${env.JAVA_HOME}/lib/tools.jar" />
83+
<fail />
84+
-->
85+
<javac source="1.6" target="1.6"
8686
srcdir="src"
8787
destdir="bin"
8888
encoding="UTF-8"
@@ -97,7 +97,7 @@
9797
<target name="test" depends="compile" description="Runs the test">
9898
<mkdir dir="test-bin"/>
9999

100-
<javac source="1.5" target="1.5"
100+
<javac source="1.6" target="1.6"
101101
srcdir="test"
102102
destdir="test-bin"
103103
encoding="UTF-8"

app/lib/commons-exec-1.1.jar

51.3 KB
Binary file not shown.

app/src/processing/app/Editor.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,13 @@ protected void populateSerialMenu() {
10021002
{
10031003
//System.out.println("Adding port to serial port menu: " + commportidentifier);
10041004
String curr_port = commportidentifier.getName();
1005-
rbMenuItem = new JCheckBoxMenuItem(curr_port, curr_port.equals(Preferences.get("serial.port")));
1005+
1006+
String description = curr_port;
1007+
String additionalDescription = Base.getPlatform().resolveDeviceAttachedTo(curr_port, Base.packages);
1008+
if (additionalDescription != null) {
1009+
description += " (" + additionalDescription + ")";
1010+
}
1011+
rbMenuItem = new JCheckBoxMenuItem(description, curr_port.equals(Preferences.get("serial.port")));
10061012
rbMenuItem.addActionListener(serialMenuListener);
10071013
//serialGroup.add(rbMenuItem);
10081014
serialMenu.add(rbMenuItem);

app/src/processing/app/Platform.java

+29-4
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@
2424
import static processing.app.I18n._;
2525

2626
import java.io.File;
27+
import java.util.Map;
2728

2829
import javax.swing.UIManager;
2930

3031
import com.sun.jna.Library;
3132
import com.sun.jna.Native;
33+
import processing.app.debug.TargetPackage;
34+
import processing.app.debug.TargetPlatform;
35+
import processing.app.helpers.PreferencesMap;
3236
import processing.core.PConstants;
3337

3438

@@ -129,10 +133,31 @@ public void openFolder(File file) throws Exception {
129133
showLauncherWarning();
130134
}
131135
}
132-
133-
134-
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
135-
136+
137+
public String resolveDeviceAttachedTo(String device, Map<String, TargetPackage> packages) {
138+
return null;
139+
}
140+
141+
public String resolveDeviceByVendorIdProductId(Map<String, TargetPackage> packages, String vendorId, String productId) {
142+
for (TargetPackage targetPackage : packages.values()) {
143+
for (TargetPlatform targetPlatform : targetPackage.getPlatforms().values()) {
144+
for (PreferencesMap board : targetPlatform.getBoards().values()) {
145+
if (board.containsKey("vid_pid")) {
146+
String[] vidPids = board.get("vid_pid").split(",");
147+
for (String vidPid : vidPids) {
148+
if (vidPid.toUpperCase().equals(vendorId + "_" + productId)) {
149+
return board.get("name");
150+
}
151+
}
152+
}
153+
}
154+
}
155+
}
156+
return null;
157+
}
158+
159+
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
160+
136161

137162
public interface CLibrary extends Library {
138163
CLibrary INSTANCE = (CLibrary)Native.loadLibrary("c", CLibrary.class);

app/src/processing/app/linux/Platform.java

+55-1
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,18 @@
2222

2323
package processing.app.linux;
2424

25-
import java.io.File;
25+
import java.io.*;
26+
import java.util.Map;
27+
import java.util.Properties;
2628

2729
import javax.swing.UIManager;
2830

31+
import org.apache.commons.exec.CommandLine;
32+
import org.apache.commons.exec.DefaultExecutor;
33+
import org.apache.commons.exec.ExecuteStreamHandler;
34+
import org.apache.commons.exec.Executor;
2935
import processing.app.Preferences;
36+
import processing.app.debug.TargetPackage;
3037
import processing.core.PConstants;
3138

3239

@@ -124,4 +131,51 @@ public void openFolder(File file) throws Exception {
124131
public String getName() {
125132
return PConstants.platformNames[PConstants.LINUX];
126133
}
134+
135+
@Override
136+
public String resolveDeviceAttachedTo(String serial, Map<String, TargetPackage> packages) {
137+
Executor executor = new DefaultExecutor();
138+
139+
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
140+
executor.setStreamHandler(new ExecuteStreamHandler() {
141+
@Override
142+
public void setProcessInputStream(OutputStream outputStream) throws IOException {
143+
}
144+
145+
@Override
146+
public void setProcessErrorStream(InputStream inputStream) throws IOException {
147+
}
148+
149+
@Override
150+
public void setProcessOutputStream(InputStream inputStream) throws IOException {
151+
byte[] buf = new byte[4096];
152+
int bytes = -1;
153+
while ((bytes = inputStream.read(buf)) != -1) {
154+
baos.write(buf, 0, bytes);
155+
}
156+
}
157+
158+
@Override
159+
public void start() throws IOException {
160+
}
161+
162+
@Override
163+
public void stop() {
164+
}
165+
});
166+
167+
try {
168+
CommandLine toDevicePath = CommandLine.parse("udevadm info -q path -n " + serial);
169+
executor.execute(toDevicePath);
170+
String devicePath = new String(baos.toByteArray());
171+
baos.reset();
172+
CommandLine commandLine = CommandLine.parse("udevadm info --query=property -p " + devicePath);
173+
executor.execute(commandLine);
174+
Properties properties = new Properties();
175+
properties.load(new ByteArrayInputStream(baos.toByteArray()));
176+
return super.resolveDeviceByVendorIdProductId(packages, properties.get("ID_VENDOR_ID").toString().toUpperCase(), properties.get("ID_MODEL_ID").toString().toUpperCase());
177+
} catch (IOException e) {
178+
return null;
179+
}
180+
}
127181
}

build/build.xml

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<include name="core/core.jar" />
2626
<include name="app/pde.jar" />
2727
<include name="app/lib/ecj.jar" />
28+
<include name="app/lib/commons-exec-1.1.jar" />
2829
<include name="app/lib/jna.jar" />
2930
<include name="app/lib/RXTXcomm.jar" />
3031
<include name="app/lib/ant.jar" />

core/build.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<methods dir="${basedir}/src/processing/core" />
1414

1515
<mkdir dir="bin" />
16-
<javac source="1.5" target="1.5"
16+
<javac source="1.6" target="1.6"
1717
encoding="UTF-8"
1818
includeAntRuntime="false"
1919
srcdir="src" destdir="bin"/>

hardware/arduino/avr/boards.txt

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ menu.cpu=Processor
55
##############################################################
66

77
uno.name=Arduino Uno
8+
uno.vid_pid=2341_0043
89
uno.upload.tool=avrdude
910
uno.upload.protocol=arduino
1011
uno.upload.maximum_size=32256
@@ -119,6 +120,7 @@ nano.menu.cpu.atmega168.build.mcu=atmega168
119120
##############################################################
120121

121122
mega2560.name=Arduino Mega 2560 or Mega ADK
123+
mega2560.vid_pid=2341_0044,2341_003f
122124
mega2560.cpu=2560 or ADK
123125

124126
mega2560.upload.tool=avrdude
@@ -167,6 +169,7 @@ mega.build.variant=mega
167169
##############################################################
168170

169171
leonardo.name=Arduino Leonardo
172+
leonardo.vid_pid=2341_0036,2341_8036
170173
leonardo.upload.tool=avrdude
171174
leonardo.upload.protocol=avr109
172175
leonardo.upload.maximum_size=28672
@@ -225,6 +228,7 @@ micro.build.extra_flags=-DUSB_VID={build.vid} -DUSB_PID={build.pid}
225228
##############################################################
226229

227230
esplora.name=Arduino Esplora
231+
esplora.vid_pid=2341_003c,2341_8036
228232
esplora.upload.tool=avrdude
229233
esplora.upload.protocol=avr109
230234
esplora.upload.maximum_size=28672

hardware/arduino/sam/boards.txt

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
arduino_due_x_dbg.name=Arduino Due (Programming Port)
3+
arduino_due_x_dbg.vid_pid=2341_003d
34
arduino_due_x_dbg.upload.tool=bossac
45
arduino_due_x_dbg.upload.protocol=sam-ba
56
arduino_due_x_dbg.upload.maximum_size=524288
@@ -18,6 +19,7 @@ arduino_due_x_dbg.build.vid=0x2341
1819
arduino_due_x_dbg.build.pid=0x003e
1920

2021
arduino_due_x.name=Arduino Due (Native USB Port)
22+
arduino_due_x.vid_pid=2341_003e
2123
arduino_due_x.upload.tool=bossac
2224
arduino_due_x.upload.protocol=sam-ba
2325
arduino_due_x.upload.maximum_size=524288

0 commit comments

Comments
 (0)