1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-01-19 08:52:15 +01:00

Add BoardPort identificationPrefs and searchMatchingBoard

This commit is contained in:
PaulStoffregen 2018-10-02 17:08:43 -07:00 committed by Cristian Maglie
parent 05092bf17f
commit d7143d6859
2 changed files with 56 additions and 2 deletions

View File

@ -29,6 +29,10 @@
package cc.arduino.packages; package cc.arduino.packages;
import processing.app.BaseNoGui;
import processing.app.debug.TargetBoard;
import processing.app.debug.TargetPackage;
import processing.app.debug.TargetPlatform;
import processing.app.helpers.PreferencesMap; import processing.app.helpers.PreferencesMap;
public class BoardPort { public class BoardPort {
@ -37,15 +41,18 @@ public class BoardPort {
private String protocol; // how to communicate, used for Ports menu sections private String protocol; // how to communicate, used for Ports menu sections
private String boardName; private String boardName;
private String label; // friendly name shown in Ports menu private String label; // friendly name shown in Ports menu
private final PreferencesMap identificationPrefs; // data to match with boards.txt
private final PreferencesMap prefs; // "vendorId", "productId", "serialNumber" private final PreferencesMap prefs; // "vendorId", "productId", "serialNumber"
private boolean online; // used by SerialBoardsLister (during upload??) private boolean online; // used by SerialBoardsLister (during upload??)
public BoardPort() { public BoardPort() {
this.prefs = new PreferencesMap(); this.prefs = new PreferencesMap();
this.identificationPrefs = new PreferencesMap();
} }
public BoardPort(BoardPort bp) { public BoardPort(BoardPort bp) {
prefs = new PreferencesMap(bp.prefs); prefs = new PreferencesMap(bp.prefs);
identificationPrefs = new PreferencesMap(bp.identificationPrefs);
address = bp.address; address = bp.address;
protocol = bp.protocol; protocol = bp.protocol;
boardName = bp.boardName; boardName = bp.boardName;
@ -133,4 +140,39 @@ public class BoardPort {
public String toString() { public String toString() {
return this.address+"_"+getVID()+"_"+getPID(); return this.address+"_"+getVID()+"_"+getPID();
} }
// Search for the board which matches identificationPrefs.
// If found, boardName is set to the name from boards.txt
// and the board is returned. If not found, null is returned.
public TargetBoard searchMatchingBoard() {
if (identificationPrefs.isEmpty()) return null;
for (TargetPackage targetPackage : BaseNoGui.packages.values()) {
for (TargetPlatform targetPlatform : targetPackage.getPlatforms().values()) {
for (TargetBoard board : targetPlatform.getBoards().values()) {
if (matchesIdentificationPrefs(board)) {
setBoardName(board.getName());
return board;
}
}
}
}
return null;
}
// Check whether a board matches all identificationPrefs fields
private boolean matchesIdentificationPrefs(TargetBoard board) {
for (String key : identificationPrefs.keySet()) {
if (!matchesIdentificationPref(board, key)) return false;
}
return true;
}
// Check whether a board matches a single identificationPrefs field
private boolean matchesIdentificationPref(TargetBoard board, String key) {
String value = identificationPrefs.get(key);
if (value == null) return false;
for (String property : board.getPreferences().subTree(key).values()) {
if (property.equalsIgnoreCase(value)) return true;
}
return false;
}
} }

View File

@ -43,6 +43,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
public class PluggableDiscovery implements Discovery { public class PluggableDiscovery implements Discovery {
@ -69,6 +71,8 @@ public class PluggableDiscovery implements Discovery {
JsonFactory factory = new JsonFactory(); JsonFactory factory = new JsonFactory();
JsonParser parser = factory.createParser(input); JsonParser parser = factory.createParser(input);
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
while (program != null && program.isAlive()) { while (program != null && program.isAlive()) {
@ -82,6 +86,9 @@ public class PluggableDiscovery implements Discovery {
startPolling(); startPolling();
} }
} else { } else {
if (event.equals("add")) {
msg.searchMatchingBoard();
}
update(msg); update(msg);
} }
} }
@ -175,8 +182,13 @@ public class PluggableDiscovery implements Discovery {
} }
if (port.getEventType().equals("add")) { if (port.getEventType().equals("add")) {
if (port.getLabel() == null) { if (port.getLabel() == null) {
// if no label, use address // if no label, use address & name, or just address if no name
port.setLabel(address); String name = port.getBoardName();
if (name == null) {
port.setLabel(address);
} else {
port.setLabel(address + " (" + name + ")");
}
} }
if (port.getProtocol() == null) { if (port.getProtocol() == null) {
// if no protocol, assume serial // if no protocol, assume serial