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 {
@@ -38,15 +42,18 @@ public class BoardPort {
38
42
private String boardName ;
39
43
private String boardId ;
40
44
private String label ; // friendly name shown in Ports menu
45
+ private final PreferencesMap identificationPrefs ; // data to match with boards.txt
41
46
private final PreferencesMap prefs ; // "vendorId", "productId", "serialNumber"
42
47
private boolean online ; // used by SerialBoardsLister (during upload??)
43
48
44
49
public BoardPort () {
45
50
this .prefs = new PreferencesMap ();
51
+ this .identificationPrefs = new PreferencesMap ();
46
52
}
47
53
48
54
public BoardPort (BoardPort bp ) {
49
55
prefs = new PreferencesMap (bp .prefs );
56
+ identificationPrefs = new PreferencesMap (bp .identificationPrefs );
50
57
address = bp .address ;
51
58
protocol = bp .protocol ;
52
59
boardName = bp .boardName ;
@@ -142,4 +149,39 @@ public String getISerial() {
142
149
public String toString () {
143
150
return this .address +"_" +getVID ()+"_" +getPID ();
144
151
}
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
+
145
187
}
0 commit comments