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 {
@@ -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}
0 commit comments