Skip to content

Commit d7143d6

Browse files
PaulStoffregencmaglie
authored andcommitted
Add BoardPort identificationPrefs and searchMatchingBoard
1 parent 05092bf commit d7143d6

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

arduino-core/src/cc/arduino/packages/BoardPort.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929

3030
package cc.arduino.packages;
3131

32+
import processing.app.BaseNoGui;
33+
import processing.app.debug.TargetBoard;
34+
import processing.app.debug.TargetPackage;
35+
import processing.app.debug.TargetPlatform;
3236
import processing.app.helpers.PreferencesMap;
3337

3438
public class BoardPort {
@@ -37,15 +41,18 @@ public class BoardPort {
3741
private String protocol; // how to communicate, used for Ports menu sections
3842
private String boardName;
3943
private String label; // friendly name shown in Ports menu
44+
private final PreferencesMap identificationPrefs; // data to match with boards.txt
4045
private final PreferencesMap prefs; // "vendorId", "productId", "serialNumber"
4146
private boolean online; // used by SerialBoardsLister (during upload??)
4247

4348
public BoardPort() {
4449
this.prefs = new PreferencesMap();
50+
this.identificationPrefs = new PreferencesMap();
4551
}
4652

4753
public BoardPort(BoardPort bp) {
4854
prefs = new PreferencesMap(bp.prefs);
55+
identificationPrefs = new PreferencesMap(bp.identificationPrefs);
4956
address = bp.address;
5057
protocol = bp.protocol;
5158
boardName = bp.boardName;
@@ -133,4 +140,39 @@ public String getISerial() {
133140
public String toString() {
134141
return this.address+"_"+getVID()+"_"+getPID();
135142
}
143+
144+
// Search for the board which matches identificationPrefs.
145+
// If found, boardName is set to the name from boards.txt
146+
// and the board is returned. If not found, null is returned.
147+
public TargetBoard searchMatchingBoard() {
148+
if (identificationPrefs.isEmpty()) return null;
149+
for (TargetPackage targetPackage : BaseNoGui.packages.values()) {
150+
for (TargetPlatform targetPlatform : targetPackage.getPlatforms().values()) {
151+
for (TargetBoard board : targetPlatform.getBoards().values()) {
152+
if (matchesIdentificationPrefs(board)) {
153+
setBoardName(board.getName());
154+
return board;
155+
}
156+
}
157+
}
158+
}
159+
return null;
160+
}
161+
// Check whether a board matches all identificationPrefs fields
162+
private boolean matchesIdentificationPrefs(TargetBoard board) {
163+
for (String key : identificationPrefs.keySet()) {
164+
if (!matchesIdentificationPref(board, key)) return false;
165+
}
166+
return true;
167+
}
168+
// Check whether a board matches a single identificationPrefs field
169+
private boolean matchesIdentificationPref(TargetBoard board, String key) {
170+
String value = identificationPrefs.get(key);
171+
if (value == null) return false;
172+
for (String property : board.getPreferences().subTree(key).values()) {
173+
if (property.equalsIgnoreCase(value)) return true;
174+
}
175+
return false;
176+
}
177+
136178
}

arduino-core/src/cc/arduino/packages/discoverers/PluggableDiscovery.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
import com.fasterxml.jackson.databind.DeserializationFeature;
4444
import com.fasterxml.jackson.core.JsonFactory;
4545
import com.fasterxml.jackson.core.JsonParser;
46+
import com.fasterxml.jackson.annotation.PropertyAccessor;
47+
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
4648

4749
public class PluggableDiscovery implements Discovery {
4850

@@ -69,6 +71,8 @@ public void run() {
6971
JsonFactory factory = new JsonFactory();
7072
JsonParser parser = factory.createParser(input);
7173
ObjectMapper mapper = new ObjectMapper();
74+
mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
75+
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
7276
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
7377

7478
while (program != null && program.isAlive()) {
@@ -82,6 +86,9 @@ public void run() {
8286
startPolling();
8387
}
8488
} else {
89+
if (event.equals("add")) {
90+
msg.searchMatchingBoard();
91+
}
8592
update(msg);
8693
}
8794
}
@@ -175,8 +182,13 @@ private synchronized void update(PluggableDiscoveryMessage port) {
175182
}
176183
if (port.getEventType().equals("add")) {
177184
if (port.getLabel() == null) {
178-
// if no label, use address
179-
port.setLabel(address);
185+
// if no label, use address & name, or just address if no name
186+
String name = port.getBoardName();
187+
if (name == null) {
188+
port.setLabel(address);
189+
} else {
190+
port.setLabel(address + " (" + name + ")");
191+
}
180192
}
181193
if (port.getProtocol() == null) {
182194
// if no protocol, assume serial

0 commit comments

Comments
 (0)