Skip to content

Commit aaf3ee2

Browse files
sandeepmistryfacchinm
authored andcommitted
Select (first) target board when port is selected and has known board
1 parent d3e2741 commit aaf3ee2

File tree

6 files changed

+64
-8
lines changed

6 files changed

+64
-8
lines changed

app/src/processing/app/Base.java

+25-1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ public class Base {
109109
// int editorCount;
110110
List<Editor> editors = Collections.synchronizedList(new ArrayList<Editor>());
111111
Editor activeEditor;
112+
113+
private static JMenu boardMenu;
112114

113115
// these menus are shared so that the board and serial port selections
114116
// are the same for all windows (since the board and serial port that are
@@ -1310,6 +1312,28 @@ public void rebuildExamplesMenu(JMenu menu) {
13101312

13111313
private static String priorPlatformFolder;
13121314
private static boolean newLibraryImported;
1315+
1316+
public void selectTargetBoard(TargetBoard targetBoard) {
1317+
for (int i = 0; i < boardMenu.getItemCount(); i++) {
1318+
JMenuItem menuItem = boardMenu.getItem(i);
1319+
if (!(menuItem instanceof JRadioButtonMenuItem)) {
1320+
continue;
1321+
}
1322+
1323+
JRadioButtonMenuItem radioButtonMenuItem = ((JRadioButtonMenuItem) menuItem);
1324+
if (targetBoard.getName().equals(radioButtonMenuItem.getText())) {
1325+
radioButtonMenuItem.setSelected(true);
1326+
break;
1327+
}
1328+
}
1329+
1330+
BaseNoGui.selectBoard(targetBoard);
1331+
filterVisibilityOfSubsequentBoardMenus(boardsCustomMenus, targetBoard, 1);
1332+
1333+
onBoardOrPortChange();
1334+
rebuildImportMenu(Editor.importMenu);
1335+
rebuildExamplesMenu(Editor.examplesMenu);
1336+
}
13131337

13141338
public synchronized void onBoardOrPortChange() {
13151339
BaseNoGui.onBoardOrPortChange();
@@ -1404,7 +1428,7 @@ public void rebuildBoardsMenu() throws Exception {
14041428
boardsCustomMenus = new LinkedList<>();
14051429

14061430
// The first custom menu is the "Board" selection submenu
1407-
JMenu boardMenu = new JMenu(tr("Board"));
1431+
boardMenu = new JMenu(tr("Board"));
14081432
boardMenu.putClientProperty("removeOnWindowDeactivation", true);
14091433
MenuScroller.setScrollerFor(boardMenu).setTopFixedCount(1);
14101434

app/src/processing/app/Editor.java

+18-5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
import com.jcraft.jsch.JSchException;
3434
import jssc.SerialPortException;
3535
import processing.app.debug.RunnerException;
36+
import processing.app.debug.TargetBoard;
37+
import processing.app.debug.TargetPackage;
38+
import processing.app.debug.TargetPlatform;
3639
import processing.app.forms.PasswordAuthorizationDialog;
3740
import processing.app.helpers.DocumentTextChangeListener;
3841
import processing.app.helpers.Keys;
@@ -1050,19 +1053,21 @@ private void addInternalTools(JMenu menu) {
10501053
class SerialMenuListener implements ActionListener {
10511054

10521055
private final String serialPort;
1056+
private final String boardId;
10531057

1054-
public SerialMenuListener(String serialPort) {
1058+
public SerialMenuListener(String serialPort, String boardId) {
10551059
this.serialPort = serialPort;
1060+
this.boardId = boardId;
10561061
}
10571062

10581063
public void actionPerformed(ActionEvent e) {
1059-
selectSerialPort(serialPort);
1064+
selectSerialPort(serialPort, boardId);
10601065
base.onBoardOrPortChange();
10611066
}
10621067

10631068
}
10641069

1065-
private void selectSerialPort(String name) {
1070+
private void selectSerialPort(String name, String boardId) {
10661071
if(portMenu == null) {
10671072
System.out.println(tr("serialMenu is null"));
10681073
return;
@@ -1102,6 +1107,13 @@ private void selectSerialPort(String name) {
11021107
// ignore
11031108
}
11041109
}
1110+
1111+
if (boardId != null) {
1112+
TargetBoard targetBoard = BaseNoGui.getPlatform().resolveBoardById(BaseNoGui.packages, boardId);
1113+
if (targetBoard != null) {
1114+
base.selectTargetBoard(targetBoard);
1115+
}
1116+
}
11051117

11061118
onBoardOrPortChange();
11071119
base.onBoardOrPortChange();
@@ -1146,9 +1158,10 @@ public int compare(BoardPort o1, BoardPort o2) {
11461158
}
11471159
String address = port.getAddress();
11481160
String label = port.getLabel();
1161+
String boardId = port.getBoardId();
11491162

11501163
JCheckBoxMenuItem item = new JCheckBoxMenuItem(label, address.equals(selectedPort));
1151-
item.addActionListener(new SerialMenuListener(address));
1164+
item.addActionListener(new SerialMenuListener(address, boardId));
11521165
portMenu.add(item);
11531166
}
11541167

@@ -2182,7 +2195,7 @@ private boolean serialPrompt() {
21822195
names,
21832196
0);
21842197
if (result == null) return false;
2185-
selectSerialPort(result);
2198+
selectSerialPort(result, null);
21862199
base.onBoardOrPortChange();
21872200
return true;
21882201
}

arduino-core/src/cc/arduino/packages/BoardPort.java

+9
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class BoardPort {
3636
private String address;
3737
private String protocol;
3838
private String boardName;
39+
private String boardId;
3940
private String vid;
4041
private String pid;
4142
private String iserial;
@@ -71,6 +72,14 @@ public void setBoardName(String boardName) {
7172
this.boardName = boardName;
7273
}
7374

75+
public String getBoardId() {
76+
return boardId;
77+
}
78+
79+
public void setBoardId(String boardId) {
80+
this.boardId = boardId;
81+
}
82+
7483
public PreferencesMap getPrefs() {
7584
return prefs;
7685
}

arduino-core/src/cc/arduino/packages/discoverers/NetworkDiscovery.java

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ public void serviceResolved(ServiceEvent serviceEvent) {
122122

123123
port.setAddress(address);
124124
port.setBoardName(name);
125+
port.setBoardId(board);
125126
port.setProtocol("network");
126127
port.setLabel(label);
127128

arduino-core/src/cc/arduino/packages/discoverers/serial/SerialBoardsLister.java

+1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ public synchronized void retriggerDiscovery(boolean polled) {
155155
label += " (" + boardName + ")";
156156
}
157157
boardPort.setBoardName(boardName);
158+
boardPort.setBoardId(board.getId());
158159
}
159160
} else {
160161
if (!parts[1].equals("0000")) {

arduino-core/src/processing/app/Platform.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -269,21 +269,29 @@ public synchronized Map<String, Object> resolveDeviceByVendorIdProductId(String
269269
return null;
270270
}
271271

272-
public String resolveDeviceByBoardID(Map<String, TargetPackage> packages, String boardId) {
272+
public TargetBoard resolveBoardById(Map<String, TargetPackage> packages, String boardId) {
273273
assert packages != null;
274274
assert boardId != null;
275275
for (TargetPackage targetPackage : packages.values()) {
276276
for (TargetPlatform targetPlatform : targetPackage.getPlatforms().values()) {
277277
for (TargetBoard board : targetPlatform.getBoards().values()) {
278278
if (boardId.equals(board.getId())) {
279-
return board.getName();
279+
return board;
280280
}
281281
}
282282
}
283283
}
284284
return null;
285285
}
286286

287+
public String resolveDeviceByBoardID(Map<String, TargetPackage> packages, String boardId) {
288+
TargetBoard targetBoard = resolveBoardById(packages, boardId);
289+
if (targetBoard != null) {
290+
return targetBoard.getName();
291+
}
292+
return null;
293+
}
294+
287295
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
288296

289297
public String getName() {

0 commit comments

Comments
 (0)