Skip to content

Commit 5ef9acb

Browse files
authored
Refactor: Use serial path (#4548)
* Use serial path * Remove debug * Remove unused import * Options are ignored for virtual * All together now
1 parent 6cc7443 commit 5ef9acb

File tree

4 files changed

+26
-37
lines changed

4 files changed

+26
-37
lines changed

src/js/protocols/VirtualSerial.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class VirtualSerial {
1818
this.transmitting = false;
1919
this.outputBuffer = [];
2020
}
21-
connect(callback) {
21+
connect(port, options, callback) {
2222
if (!this.openCanceled) {
2323
this.connected = true;
2424
this.connectionId = VIRTUAL;

src/js/serial.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import CONFIGURATOR from "./data_storage";
21
import WebSerial from "./protocols/WebSerial.js";
32
import WebBluetooth from "./protocols/WebBluetooth.js";
43
import Websocket from "./protocols/WebSocket.js";
@@ -160,21 +159,15 @@ class Serial extends EventTarget {
160159
* @param {string|function} path - Port path or callback for virtual mode
161160
* @param {object} options - Connection options (baudRate, etc.)
162161
*/
163-
async connect(path, options) {
162+
async connect(path, options, callback) {
163+
// Select the appropriate protocol based directly on the port path
164+
this.selectProtocol(path);
165+
164166
if (!this._protocol) {
165-
console.error(`${this.logHead} No protocol selected, cannot connect`);
167+
console.error(`${this.logHead} No valid protocol selected for connection`);
166168
return false;
167169
}
168170

169-
// If path is a function, it's a callback for virtual mode
170-
const isCallback = typeof path === "function";
171-
172-
// In virtual mode, a callback is passed as the first parameter
173-
if (isCallback && CONFIGURATOR.virtualMode) {
174-
console.log(`${this.logHead} Connecting in virtual mode`);
175-
return this._protocol.connect(path);
176-
}
177-
178171
// Check if already connected
179172
if (this._protocol.connected) {
180173
console.warn(`${this.logHead} Protocol already connected, not connecting again`);
@@ -195,11 +188,11 @@ class Serial extends EventTarget {
195188
}
196189

197190
console.log(`${this.logHead} Reconnecting to new port:`, path);
198-
return this._protocol.connect(path, options);
191+
return this._protocol.connect(path, options, callback);
199192
}
200193

201194
console.log(`${this.logHead} Connecting to port:`, path, "with options:", options);
202-
return this._protocol.connect(path, options);
195+
return this._protocol.connect(path, options, callback);
203196
}
204197

205198
/**

src/js/serial_backend.js

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,12 @@ export function initializeSerialBackend() {
8282

8383
function connectDisconnect() {
8484
const selectedPort = PortHandler.portPicker.selectedPort;
85-
const portName = selectedPort === "manual" ? PortHandler.portPicker.portOverride : selectedPort;
8685

87-
if (!GUI.connect_lock && selectedPort !== "noselection" && !selectedPort.path?.startsWith("usb_")) {
86+
if (!GUI.connect_lock && selectedPort !== "noselection" && !selectedPort.path?.startsWith("usb")) {
8887
// GUI control overrides the user control
8988

9089
GUI.configuration_loaded = false;
9190

92-
const baudRate = PortHandler.portPicker.selectedBauds;
93-
9491
if (!isConnected) {
9592
// prevent connection when we do not have permission
9693
if (selectedPort.startsWith("requestpermission")) {
@@ -103,34 +100,29 @@ function connectDisconnect() {
103100
return;
104101
}
105102

103+
const portName = selectedPort === "manual" ? PortHandler.portPicker.portOverride : selectedPort;
104+
106105
console.log(`${logHead} Connecting to: ${portName}`);
107106
GUI.connecting_to = portName;
108107

109108
// lock port select & baud while we are connecting / connected
110109
PortHandler.portPickerDisabled = true;
111110
$("div.connection_button__label").text(i18n.getMessage("connecting"));
112111

113-
// Set configuration flags for consistency with other code
114-
CONFIGURATOR.virtualMode = selectedPort === "virtual";
115-
116-
// Select the appropriate protocol based directly on the port path
117-
serial.selectProtocol(selectedPort);
118-
119-
if (CONFIGURATOR.virtualMode) {
120-
CONFIGURATOR.virtualApiVersion = PortHandler.portPicker.virtualMspVersion;
121-
// Virtual mode uses a callback instead of port path
122-
serial.connect(onOpenVirtual);
123-
} else {
124-
// Set up event listeners for all non-virtual connections
112+
// Set up event listeners for non-virtual connections
113+
if (selectedPort !== "virtual") {
125114
serial.removeEventListener("connect", connectHandler);
126115
serial.addEventListener("connect", connectHandler);
127116

128117
serial.removeEventListener("disconnect", disconnectHandler);
129118
serial.addEventListener("disconnect", disconnectHandler);
130-
131-
// All non-virtual modes pass the port path and options
132-
serial.connect(portName, { baudRate });
133119
}
120+
121+
serial.connect(
122+
portName,
123+
{ baudRate: PortHandler.portPicker.selectedBauds },
124+
selectedPort === "virtual" ? onOpenVirtual : undefined,
125+
);
134126
} else {
135127
// If connected, start disconnection sequence
136128
GUI.timeout_kill_all();
@@ -348,6 +340,9 @@ function onOpenVirtual() {
348340
GUI.connecting_to = false;
349341

350342
CONFIGURATOR.connectionValid = true;
343+
CONFIGURATOR.virtualMode = true;
344+
CONFIGURATOR.virtualApiVersion = PortHandler.portPicker.virtualMspVersion;
345+
351346
isConnected = true;
352347

353348
mspHelper = new MspHelper();

src/js/utils/AutoDetect.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,13 @@ class AutoDetect {
5656

5757
gui_log(i18n.getMessage("firmwareFlasherDetectBoardQuery"));
5858

59-
if (port.startsWith("serial")) {
59+
if (!port.startsWith("virtual")) {
6060
serial.addEventListener("connect", this.boundHandleConnect, { once: true });
6161
serial.addEventListener("disconnect", this.boundHandleDisconnect, { once: true });
6262

63-
serial.selectProtocol("serial");
64-
serial.connect(port, { baudRate: 115200 });
63+
console.log("Connecting to serial port", port, serial.connected, serial.connectionId);
64+
65+
serial.connect(port, { baudRate: PortHandler.portPicker.selectedBauds || 115200 });
6566
} else {
6667
gui_log(i18n.getMessage("serialPortOpenFail"));
6768
}

0 commit comments

Comments
 (0)