Skip to content

Commit 570f047

Browse files
PaulStoffregenfacchinm
authored andcommitted
Add BoardPort identificationPrefs and searchMatchingBoard
1 parent c0e50b6 commit 570f047

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 {
@@ -38,15 +42,18 @@ public class BoardPort {
3842
private String boardName;
3943
private String boardId;
4044
private String label; // friendly name shown in Ports menu
45+
private final PreferencesMap identificationPrefs; // data to match with boards.txt
4146
private final PreferencesMap prefs; // "vendorId", "productId", "serialNumber"
4247
private boolean online; // used by SerialBoardsLister (during upload??)
4348

4449
public BoardPort() {
4550
this.prefs = new PreferencesMap();
51+
this.identificationPrefs = new PreferencesMap();
4652
}
4753

4854
public BoardPort(BoardPort bp) {
4955
prefs = new PreferencesMap(bp.prefs);
56+
identificationPrefs = new PreferencesMap(bp.identificationPrefs);
5057
address = bp.address;
5158
protocol = bp.protocol;
5259
boardName = bp.boardName;
@@ -142,4 +149,39 @@ public String getISerial() {
142149
public String toString() {
143150
return this.address+"_"+getVID()+"_"+getPID();
144151
}
152+
153+
// Search for the board which matches identificationPrefs.
154+
// If found, boardName is set to the name from boards.txt
155+
// and the board is returned. If not found, null is returned.
156+
public TargetBoard searchMatchingBoard() {
157+
if (identificationPrefs.isEmpty()) return null;
158+
for (TargetPackage targetPackage : BaseNoGui.packages.values()) {
159+
for (TargetPlatform targetPlatform : targetPackage.getPlatforms().values()) {
160+
for (TargetBoard board : targetPlatform.getBoards().values()) {
161+
if (matchesIdentificationPrefs(board)) {
162+
setBoardName(board.getName());
163+
return board;
164+
}
165+
}
166+
}
167+
}
168+
return null;
169+
}
170+
// Check whether a board matches all identificationPrefs fields
171+
private boolean matchesIdentificationPrefs(TargetBoard board) {
172+
for (String key : identificationPrefs.keySet()) {
173+
if (!matchesIdentificationPref(board, key)) return false;
174+
}
175+
return true;
176+
}
177+
// Check whether a board matches a single identificationPrefs field
178+
private boolean matchesIdentificationPref(TargetBoard board, String key) {
179+
String value = identificationPrefs.get(key);
180+
if (value == null) return false;
181+
for (String property : board.getPreferences().subTree(key).values()) {
182+
if (property.equalsIgnoreCase(value)) return true;
183+
}
184+
return false;
185+
}
186+
145187
}

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)