29
29
30
30
package cc .arduino .packages ;
31
31
32
+ import processing .app .BaseNoGui ;
33
+ import processing .app .debug .TargetBoard ;
34
+ import processing .app .debug .TargetPackage ;
35
+ import processing .app .debug .TargetPlatform ;
32
36
import processing .app .helpers .PreferencesMap ;
33
37
34
38
public class BoardPort {
@@ -37,15 +41,18 @@ public class BoardPort {
37
41
private String protocol ; // how to communicate, used for Ports menu sections
38
42
private String boardName ;
39
43
private String label ; // friendly name shown in Ports menu
44
+ private final PreferencesMap identificationPrefs ; // data to match with boards.txt
40
45
private final PreferencesMap prefs ; // "vendorId", "productId", "serialNumber"
41
46
private boolean online ; // used by SerialBoardsLister (during upload??)
42
47
43
48
public BoardPort () {
44
49
this .prefs = new PreferencesMap ();
50
+ this .identificationPrefs = new PreferencesMap ();
45
51
}
46
52
47
53
public BoardPort (BoardPort bp ) {
48
54
prefs = new PreferencesMap (bp .prefs );
55
+ identificationPrefs = new PreferencesMap (bp .identificationPrefs );
49
56
address = bp .address ;
50
57
protocol = bp .protocol ;
51
58
boardName = bp .boardName ;
@@ -133,4 +140,39 @@ public String getISerial() {
133
140
public String toString () {
134
141
return this .address +"_" +getVID ()+"_" +getPID ();
135
142
}
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
+
136
178
}
0 commit comments