Skip to content

Commit 91b94c8

Browse files
committed
Rework Serial ports handling and add Board info menu
This commit introduces the concept of stateful board list (vs. original stateless) and board serial number. The board is now an "entity" composed by the triplet port/vid/pid. These informations come from libListSerial "light" function. When the board list changes, it triggers a request for the additional infos to libListSerial. These information contains the serial number of the boards. These brings a lighter and faster scanning process. Some logic has been introduced to handle a board with the S/N only exposed in the bootloader (like 32u4). In this case the disappearing port acquires the bootloader's S/N A menu (under Ports menu) shows the currently connected port info and can be used for bugreporting
1 parent 2c6d8d0 commit 91b94c8

File tree

11 files changed

+257
-38
lines changed

11 files changed

+257
-38
lines changed

Diff for: app/src/processing/app/Editor.java

+49
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,9 @@ public void actionPerformed(ActionEvent e) {
841841
portMenu = new JMenu(tr("Port"));
842842
populatePortMenu();
843843
toolsMenu.add(portMenu);
844+
item = new JMenuItem(tr("Get Board Info"));
845+
item.addActionListener(e -> handleBoardInfo());
846+
toolsMenu.add(item);
844847
toolsMenu.addSeparator();
845848

846849
base.rebuildProgrammerMenu();
@@ -2782,6 +2785,52 @@ private void handleBurnBootloader() {
27822785
}).start();
27832786
}
27842787

2788+
private void handleBoardInfo() {
2789+
console.clear();
2790+
2791+
String selectedPort = PreferencesData.get("serial.port");
2792+
List<BoardPort> ports = Base.getDiscoveryManager().discovery();
2793+
2794+
String label = "";
2795+
String vid = "";
2796+
String pid = "";
2797+
String iserial = "";
2798+
boolean found = false;
2799+
2800+
for (BoardPort port : ports) {
2801+
if (port.getAddress().equals(selectedPort)) {
2802+
label = port.getBoardName();
2803+
vid = port.getVID();
2804+
pid = port.getPID();
2805+
iserial = port.getISerial();
2806+
found = true;
2807+
break;
2808+
}
2809+
}
2810+
2811+
if (!found) {
2812+
statusNotice(tr("Please select a port to obtain board info"));
2813+
return;
2814+
}
2815+
2816+
if (vid == null || vid.equals("") || vid.equals("0000")) {
2817+
statusNotice(tr("Native serial port, can't obtain info"));
2818+
return;
2819+
}
2820+
2821+
if (iserial == null || iserial.equals("")) {
2822+
iserial = tr("Upload any sketch to obtain it");
2823+
}
2824+
2825+
if (label == null) {
2826+
label = tr("Unknown board");
2827+
}
2828+
2829+
String infos = I18n.format("BN: {0}\nVID: {1}\nPID: {2}\nSN: {3}", label, vid, pid, iserial);
2830+
JTextArea textArea = new JTextArea(infos);
2831+
2832+
JOptionPane.showMessageDialog(this, textArea, tr("Board Info"), JOptionPane.PLAIN_MESSAGE);
2833+
}
27852834

27862835
/**
27872836
* Handler for File &rarr; Page Setup.

Diff for: app/src/processing/app/EditorLineStatus.java

+5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public class EditorLineStatus extends JComponent {
4949
String text = "";
5050
String name = "";
5151
String serialport = "";
52+
String serialnumber = "";
5253

5354

5455
public EditorLineStatus() {
@@ -125,6 +126,10 @@ public void setSerialPort(String serialport) {
125126
this.serialport = serialport;
126127
}
127128

129+
public void setSerialNumber(String serialnumber) {
130+
this.serialnumber = serialnumber;
131+
}
132+
128133
public Dimension getPreferredSize() {
129134
return new Dimension(300, high);
130135
}

Diff for: arduino-core/src/cc/arduino/packages/BoardPort.java

+36
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@ public class BoardPort {
3636
private String address;
3737
private String protocol;
3838
private String boardName;
39+
private String vid;
40+
private String pid;
41+
private String iserial;
3942
private String label;
4043
private final PreferencesMap prefs;
44+
private boolean online;
4145

4246
public BoardPort() {
4347
this.prefs = new PreferencesMap();
@@ -79,4 +83,36 @@ public String getLabel() {
7983
return label;
8084
}
8185

86+
public void setOnlineStatus(boolean online) {
87+
this.online = online;
88+
}
89+
90+
public boolean isOnline() {
91+
return online;
92+
}
93+
94+
public void setVIDPID(String vid, String pid) {
95+
this.vid = vid;
96+
this.pid = pid;
97+
}
98+
99+
public String getVID() {
100+
return vid;
101+
}
102+
103+
public String getPID() {
104+
return pid;
105+
}
106+
107+
public void setISerial(String iserial) {
108+
this.iserial = iserial;
109+
}
110+
public String getISerial() {
111+
return iserial;
112+
}
113+
114+
@Override
115+
public String toString() {
116+
return this.address+"_"+this.vid+"_"+this.pid;
117+
}
82118
}

Diff for: arduino-core/src/cc/arduino/packages/Discovery.java

+1
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,6 @@ public interface Discovery {
5151
* @return
5252
*/
5353
List<BoardPort> listDiscoveredBoards();
54+
List<BoardPort> listDiscoveredBoards(boolean complete);
5455

5556
}

Diff for: arduino-core/src/cc/arduino/packages/DiscoveryManager.java

+25-2
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,13 @@
4040
public class DiscoveryManager {
4141

4242
private final List<Discovery> discoverers;
43+
private final SerialDiscovery serialDiscoverer = new SerialDiscovery();
44+
private final NetworkDiscovery networkDiscoverer = new NetworkDiscovery();
4345

4446
public DiscoveryManager() {
4547
discoverers = new ArrayList<Discovery>();
46-
discoverers.add(new SerialDiscovery());
47-
discoverers.add(new NetworkDiscovery());
48+
discoverers.add(serialDiscoverer);
49+
discoverers.add(networkDiscoverer);
4850

4951
// Start all discoverers
5052
for (Discovery d : discoverers) {
@@ -69,6 +71,10 @@ public DiscoveryManager() {
6971
Runtime.getRuntime().addShutdownHook(closeHook);
7072
}
7173

74+
public SerialDiscovery getSerialDiscoverer() {
75+
return serialDiscoverer;
76+
}
77+
7278
public List<BoardPort> discovery() {
7379
List<BoardPort> res = new ArrayList<BoardPort>();
7480
for (Discovery d : discoverers) {
@@ -77,6 +83,14 @@ public List<BoardPort> discovery() {
7783
return res;
7884
}
7985

86+
public List<BoardPort> discovery(boolean complete) {
87+
List<BoardPort> res = new ArrayList<BoardPort>();
88+
for (Discovery d : discoverers) {
89+
res.addAll(d.listDiscoveredBoards(complete));
90+
}
91+
return res;
92+
}
93+
8094
public BoardPort find(String address) {
8195
for (BoardPort boardPort : discovery()) {
8296
if (boardPort.getAddress().equals(address)) {
@@ -86,4 +100,13 @@ public BoardPort find(String address) {
86100
return null;
87101
}
88102

103+
public BoardPort find(String address, boolean complete) {
104+
for (BoardPort boardPort : discovery(complete)) {
105+
if (boardPort.getAddress().equals(address)) {
106+
return boardPort;
107+
}
108+
}
109+
return null;
110+
}
111+
89112
}

Diff for: arduino-core/src/cc/arduino/packages/discoverers/NetworkDiscovery.java

+7
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ public List<BoardPort> listDiscoveredBoards() {
6767
}
6868
}
6969

70+
@Override
71+
public List<BoardPort> listDiscoveredBoards(boolean complete) {
72+
synchronized (reachableBoardPorts) {
73+
return new LinkedList<>(reachableBoardPorts);
74+
}
75+
}
76+
7077
public void setReachableBoardPorts(List<BoardPort> newReachableBoardPorts) {
7178
synchronized (reachableBoardPorts) {
7279
this.reachableBoardPorts.clear();

Diff for: arduino-core/src/cc/arduino/packages/discoverers/SerialDiscovery.java

+26-8
Original file line numberDiff line numberDiff line change
@@ -41,33 +41,51 @@ public class SerialDiscovery implements Discovery {
4141

4242
private Timer serialBoardsListerTimer;
4343
private final List<BoardPort> serialBoardPorts;
44+
private SerialBoardsLister serialBoardsLister = new SerialBoardsLister(this);;
4445

4546
public SerialDiscovery() {
4647
this.serialBoardPorts = new LinkedList<>();
4748
}
4849

4950
@Override
5051
public List<BoardPort> listDiscoveredBoards() {
51-
return getSerialBoardPorts();
52+
return getSerialBoardPorts(false);
5253
}
5354

54-
private List<BoardPort> getSerialBoardPorts() {
55-
synchronized (serialBoardPorts) {
56-
return new LinkedList<>(serialBoardPorts);
57-
}
55+
public List<BoardPort> listDiscoveredBoards(boolean complete) {
56+
return getSerialBoardPorts(complete);
57+
}
58+
59+
private List<BoardPort> getSerialBoardPorts(boolean complete) {
60+
if (complete) {
61+
return new LinkedList<>(serialBoardPorts);
62+
}
63+
List<BoardPort> onlineBoardPorts = new LinkedList<>();
64+
for (BoardPort port : serialBoardPorts) {
65+
if (port.isOnline() == true) {
66+
onlineBoardPorts.add(port);
67+
}
68+
}
69+
return onlineBoardPorts;
5870
}
5971

6072
public void setSerialBoardPorts(List<BoardPort> newSerialBoardPorts) {
61-
synchronized (serialBoardPorts) {
6273
serialBoardPorts.clear();
6374
serialBoardPorts.addAll(newSerialBoardPorts);
64-
}
75+
}
76+
77+
public void forceRefresh() {
78+
serialBoardsLister.retriggerDiscovery();
79+
}
80+
81+
public void setUploadInProgress(boolean param) {
82+
serialBoardsLister.uploadInProgress = param;
6583
}
6684

6785
@Override
6886
public void start() {
6987
this.serialBoardsListerTimer = new Timer(SerialBoardsLister.class.getName());
70-
new SerialBoardsLister(this).start(serialBoardsListerTimer);
88+
serialBoardsLister.start(serialBoardsListerTimer);
7189
}
7290

7391
@Override

0 commit comments

Comments
 (0)