2929
3030package 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 ;
3236import processing .app .helpers .PreferencesMap ;
3337
3438public 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}
0 commit comments