Skip to content

Commit e5e792b

Browse files
WebUSB: Refactor code to close serial terminal into own function.
Same code was in 3 different places. Now when the serial button is used to close the terminal the terminal itself is reset as well, as it can be deceiving to have the old content when the micro:bit serial is reset on reconnects.
1 parent 3b27fe8 commit e5e792b

File tree

1 file changed

+22
-45
lines changed

1 file changed

+22
-45
lines changed

python-main.js

Lines changed: 22 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,16 +1484,8 @@ function web_editor(config) {
14841484

14851485
// Hide serial and disconnect if open
14861486
if ($("#repl").css('display') != 'none') {
1487-
$("#repl").hide();
1488-
$("#request-repl").hide();
1489-
$("#request-serial").hide();
1490-
$("#editor-container").show();
1487+
closeSerial();
14911488
}
1492-
$("#command-serial").attr("title", config["translate"]["static-strings"]["buttons"]["command-serial"]["title"]);
1493-
$("#command-serial > .roundlabel").text(config["translate"]["static-strings"]["buttons"]["command-serial"]["label"]);
1494-
1495-
$("#repl").empty();
1496-
REPL = null;
14971489

14981490
// Change button to connect
14991491
$("#command-disconnect").hide();
@@ -1509,15 +1501,13 @@ function web_editor(config) {
15091501
if (usePartialFlashing) {
15101502
if (window.dapwrapper) {
15111503
console.log("Disconnecting: Using Quick Flash");
1512-
p = p.then(function() { window.dapwrapper.daplink.stopSerialRead() } )
1513-
.then(function() { window.dapwrapper.disconnectAsync() } );
1504+
p = p.then(function() { window.dapwrapper.disconnectAsync() } );
15141505
}
15151506
}
15161507
else {
15171508
if (window.daplink) {
15181509
console.log("Disconnecting: Using Full Flash");
1519-
p = p.then(function() { window.daplink.stopSerialRead() } )
1520-
.then(function() { window.daplink.disconnect() } );
1510+
p = p.then(function() { window.daplink.disconnect() } );
15211511
}
15221512
}
15231513

@@ -1538,21 +1528,7 @@ function web_editor(config) {
15381528

15391529
// Hide serial and disconnect if open
15401530
if ($("#repl").css('display') != 'none') {
1541-
$("#repl").hide();
1542-
$("#request-repl").hide();
1543-
$("#request-serial").hide();
1544-
$("#editor-container").show();
1545-
$("#command-serial").attr("title", config["translate"]["static-strings"]["buttons"]["command-serial"]["title"]);
1546-
$("#command-serial > .roundlabel").text(config["translate"]["static-strings"]["buttons"]["command-serial"]["label"]);
1547-
1548-
if (usePartialFlashing) {
1549-
if (window.dapwrapper) {
1550-
window.dapwrapper.daplink.stopSerialRead();
1551-
}
1552-
}
1553-
else {
1554-
window.daplink.stopSerialRead();
1555-
}
1531+
closeSerial();
15561532
}
15571533

15581534
// Get the hex to flash in bytes format, exit if there is an error
@@ -1580,9 +1556,6 @@ function web_editor(config) {
15801556

15811557
var p = Promise.resolve();
15821558
if (usePartialFlashing) {
1583-
REPL = null;
1584-
$("#repl").empty();
1585-
15861559
p = window.dapwrapper.disconnectAsync()
15871560
.then(function() {
15881561
return PartialFlashing.connectDapAsync();
@@ -1640,25 +1613,29 @@ function web_editor(config) {
16401613
});
16411614
}
16421615

1616+
function closeSerial(keepSession) {
1617+
console.log("Closing Serial Terminal");
1618+
$("#repl").empty();
1619+
$("#repl").hide();
1620+
$("#request-repl").hide();
1621+
$("#request-serial").hide();
1622+
$("#editor-container").show();
1623+
1624+
var serialButton = config["translate"]["static-strings"]["buttons"]["command-serial"];
1625+
$("#command-serial").attr("title", serialButton["title"]);
1626+
$("#command-serial > .roundlabel").text(serialButton["label"]);
1627+
1628+
var daplink = usePartialFlashing ? window.dapwrapper.daplink : window.daplink;
1629+
daplink.stopSerialRead();
1630+
REPL = null;
1631+
}
1632+
16431633
function doSerial() {
16441634
console.log("Setting Up Serial Terminal");
16451635
// Hide terminal
16461636
var serialButton = config["translate"]["static-strings"]["buttons"]["command-serial"];
16471637
if ($("#repl").css('display') != 'none') {
1648-
$("#repl").hide();
1649-
$("#request-repl").hide();
1650-
$("#request-serial").hide();
1651-
$("#editor-container").show();
1652-
$("#command-serial").attr("title", serialButton["label"]);
1653-
$("#command-serial > .roundlabel").text(serialButton["label"]);
1654-
if (usePartialFlashing) {
1655-
if (window.dapwrapper) {
1656-
window.dapwrapper.daplink.stopSerialRead();
1657-
}
1658-
}
1659-
else {
1660-
window.daplink.stopSerialRead();
1661-
}
1638+
closeSerial();
16621639
return;
16631640
}
16641641

0 commit comments

Comments
 (0)