Skip to content

Commit 9a006be

Browse files
microbit-sammicrobit-carlos
authored andcommitted
WebUSB: Change Download/Flash and Connect/Disconnect buttons and improve actions (#261)
1 parent 9883146 commit 9a006be

File tree

4 files changed

+75
-30
lines changed

4 files changed

+75
-30
lines changed

editor.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,16 @@ <h2><i class="fa fa-unlock-alt"></i> <strong>{{ title }}</strong></h2>
186186
</div>
187187
<div class="roundlabel">Download</div>
188188
</a>
189+
<a href="#" class="roundbutton action" id="command-flash"
190+
tabindex="11"
191+
title="Download a hex file to flash onto your micro:bit">
192+
<div class="roundsymbol">
193+
<svg class="svg svg-icon-touchdevelop" viewBox="50 50 380 380" xmlns="http://www.w3.org/2000/svg" role="presentation" aria-label="download">
194+
<path d="m 185.96612,110.36806 41.029,41.029 0,-72.834699 27.352,0 0.001,72.834699 41.029,-41.029 0,31.911 -54.706,54.706 -54.705,-54.706 0,-31.911 z m 98.203,181.9 c 0,-9.128 7.406,-16.539 16.541,-16.539 9.146,0 16.535,7.411 16.535,16.539 0,9.131 -7.389,16.548 -16.535,16.548 -9.135,0 -16.541,-7.417 -16.541,-16.548 z m -121.494,0.003 c 0,-9.129 7.413,-16.542 16.548,-16.542 9.129,0 16.537,7.413 16.537,16.542 0,9.131 -7.408,16.547 -16.537,16.547 -9.135,0 -16.548,-7.416 -16.548,-16.547 z m 16.544,-81.93 122.554,0.202 c 45.667,0 82.285,36.553 82.285,81.732 0,45.179 -36.757,81.934 -81.937,81.934 l -122.902,0 c -45.177,0 -81.934395,-36.755 -81.934395,-81.934 0,-45.179 36.757395,-81.934 81.934395,-81.934 z m -49.164,81.934 c 0,27.105 22.059,49.164 49.164,49.164 l 122.902,0 c 27.107,0 49.159,-22.059 49.159,-49.164 0,-27.112 -22.052,-49.164 -49.159,-49.164 l -122.902,0 c -27.105,0 -49.164,22.052 -49.164,49.164 z" id="path3341" inkscape:connector-curvature="0" style="fill:currentColor"></path>
195+
</svg>
196+
</div>
197+
<div class="roundlabel">Flash</div>
198+
</a>
189199
<a href="#" class="roundbutton action" id="command-connect"
190200
tabindex="20"
191201
title="Connect to your micro:bit">
@@ -196,6 +206,16 @@ <h2><i class="fa fa-unlock-alt"></i> <strong>{{ title }}</strong></h2>
196206
</div>
197207
<div class="roundlabel">Connect</div>
198208
</a>
209+
<a href="#" class="roundbutton action" id="command-disconnect"
210+
tabindex="21"
211+
title="Disconnect your micro:bit">
212+
<div class="roundsymbol">
213+
<svg class="svg svg-icon-touchdevelop" viewBox="50 50 380 380" xmlns="http://www.w3.org/2000/svg" role="presentation" aria-label="disconnect">
214+
<path d="m 295.37672,362.40336 -41.029,-41.029 0,72.8347 -27.352,0 -0.001,-72.8347 -41.029,41.029 0,-31.911 34.68462,-34.68462 20.02138,-20.02138 54.705,54.706 z m -98.203,-181.9 c 0,9.128 -7.406,16.539 -16.541,16.539 -9.146,0 -16.535,-7.411 -16.535,-16.539 0,-9.131 7.389,-16.548 16.535,-16.548 9.135,0 16.541,7.417 16.541,16.548 z m 121.494,-0.003 c 0,9.129 -7.413,16.542 -16.548,16.542 -9.129,0 -16.537,-7.413 -16.537,-16.542 0,-9.131 7.408,-16.547 16.537,-16.547 9.135,0 16.548,7.416 16.548,16.547 z m -16.544,81.93 -122.554,-0.202 c -45.667,0 -82.284997,-36.553 -82.284997,-81.732 0,-45.179 36.756997,-81.934 81.936997,-81.934 l 122.902,0 c 45.177,0 81.9344,36.755 81.9344,81.934 0,45.179 -36.7574,81.934 -81.9344,81.934 z m 49.164,-81.934 c 0,-27.105 -22.059,-49.164 -49.164,-49.164 l -122.902,0 c -27.107,0 -49.159,22.059 -49.159,49.164 0,27.112 22.052,49.164 49.159,49.164 l 122.902,0 c 27.105,0 49.164,-22.052 49.164,-49.164 z" id="path3342" inkscape:connector-curvature="0" style="fill:currentColor" sodipodi:nodetypes="cccccccccccssssssssssccsssscsssssss" />
215+
</svg>
216+
</div>
217+
<div class="roundlabel">Disconnect</div>
218+
</a>
199219
<a href="#" class="roundbutton action" id="command-files"
200220
tabindex="30"
201221
title="Load/Save files">

partial-flashing.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,18 @@ let PartialFlashing = {
634634
dapwrapper.daplink.on(DAPjs.DAPLink.EVENT_PROGRESS, function(progress) {
635635
$("#webusb-flashing-progress").val(progress).css("display", "inline-block");
636636
});
637-
return dapwrapper.daplink.flash(image);
637+
return dapwrapper.transport.open()
638+
.then(() => { return dapwrapper.daplink.flash(image) } )
639+
.then(() => {
640+
// Send event
641+
var details = {
642+
"flash-type": "partial-flash",
643+
"event-type": "info",
644+
"message": "full-flash-successful"
645+
};
646+
647+
document.dispatchEvent(new CustomEvent('webusb', { detail: details }));
648+
} );
638649
},
639650

640651
// Connect to the micro:bit using WebUSB and setup DAPWrapper.
@@ -707,6 +718,14 @@ let PartialFlashing = {
707718
// Fall back to full flash if attempting to reset times out.
708719
if (err === "Timeout") {
709720
PartialFlashingUtils.log("Partial flashing failed. Attempting Full Flash");
721+
// Send event
722+
var details = {
723+
"flash-type": "partial-flash",
724+
"event-type": "error",
725+
"message": "flash-failed" + "/" + "attempting-full-flash"
726+
};
727+
728+
document.dispatchEvent(new CustomEvent('webusb', { detail: details }));
710729
return this.fullFlashAsync(dapwrapper, image);
711730
}
712731
return Promise.reject(err);

python-main.js

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,14 +1243,12 @@ function web_editor(config) {
12431243

12441244
return p.then(function() {
12451245
// Change button to disconnect
1246-
$("#command-connect").attr("id", "command-disconnect");
1247-
$("#command-disconnect > .roundlabel").text(config["translate"]["static-strings"]["buttons"]["command-disconnect"]["label"]);
1248-
$("#command-disconnect").attr("title", config["translate"]["static-strings"]["buttons"]["command-disconnect"]["title"]);
1246+
$("#command-connect").hide();
1247+
$("#command-disconnect").show();
12491248

12501249
// Change download to flash
1251-
$("#command-download").attr("id", "command-flash");
1252-
$("#command-flash > .roundlabel").text(config["translate"]["static-strings"]["buttons"]["command-flash"]["label"]);
1253-
$("#command-flash").attr("title", config["translate"]["static-strings"]["buttons"]["command-flash"]["title"]);
1250+
$("#command-download").hide();
1251+
$("#command-flash").show();
12541252

12551253
if (serial) {
12561254
doSerial();
@@ -1307,10 +1305,10 @@ function web_editor(config) {
13071305
errorDescription + '</div>' + '<div class="flashing-overlay-buttons"><hr />' +
13081306
((err.name === 'device-disconnected' && $("#flashing-overlay-error").html() === "")
13091307
? ""
1310-
: '<a href="#" id="flashing-overlay-download">' +
1308+
: '<a href="#" id="flashing-overlay-download" class="action" onclick="actionClickListener(event)">' +
13111309
config["translate"]["webusb"]["download"] +
13121310
'</a> | ' +
1313-
'<a href="https://support.microbit.org/a/solutions/articles/19000105428-webusb-troubleshooting/en" id="flashing-overlay-troubleshoot">' +
1311+
'<a href="https://support.microbit.org/a/solutions/articles/19000105428-webusb-troubleshooting/en" id="flashing-overlay-troubleshoot" class="action" onclick="actionClickListener(event)">' +
13141312
config["translate"]["webusb"]["troubleshoot"] +
13151313
'</a> | ') +
13161314
'<a href="#" onclick="flashErrorClose()">' +
@@ -1329,7 +1327,13 @@ function web_editor(config) {
13291327
$("#flashing-overlay-download").click(doDownload);
13301328

13311329
// Send event
1332-
var details = {"flash-type": (usePartialFlashing ? "partial-flash" : "full-flash"), "event-type": "error", "message": errorType};
1330+
// Append error message, replace all special chars with '-', if last char is '-' remove it
1331+
var details = {
1332+
"flash-type": (usePartialFlashing ? "partial-flash" : "full-flash"),
1333+
"event-type": ((err.name == "device-disconnected") ? "info" : "error"),
1334+
"message": errorType + "/" + err.message.replace(/\W+/g, '-').replace(/\W$/, '').toLowerCase()
1335+
};
1336+
13331337
document.dispatchEvent(new CustomEvent('webusb', { detail: details }));
13341338
}
13351339

@@ -1352,14 +1356,12 @@ function web_editor(config) {
13521356
REPL = null;
13531357

13541358
// Change button to connect
1355-
$("#command-disconnect").attr("id", "command-connect");
1356-
$("#command-connect > .roundlabel").text(config["translate"]["static-strings"]["buttons"]["command-connect"]["label"]);
1357-
$("#command-connect").attr("title", config["translate"]["static-strings"]["buttons"]["command-connect"]["title"]);
1359+
$("#command-disconnect").hide();
1360+
$("#command-connect").show();
13581361

13591362
// Change flash to download
1360-
$("#command-flash").attr("id", "command-download");
1361-
$("#command-download > .roundlabel").text(config["translate"]["static-strings"]["buttons"]["command-download"]["label"]);
1362-
$("#command-download").attr("title", config["translate"]["static-strings"]["buttons"]["command-download"]["title"]);
1363+
$("#command-flash").hide();
1364+
$("#command-download").show();
13631365

13641366
var p = Promise.resolve();
13651367

@@ -1388,9 +1390,6 @@ function web_editor(config) {
13881390
function doFlash() {
13891391
var startTime = new Date().getTime();
13901392

1391-
// Listen for unhandled rejections in DAPjs
1392-
window.addEventListener("unhandledrejection", webusbErrorHandler);
1393-
13941393
// Hide serial and disconnect if open
13951394
if ($("#repl").css('display') != 'none') {
13961395
$("#repl").hide();
@@ -1508,7 +1507,7 @@ function web_editor(config) {
15081507
}
15091508

15101509
// Check if we need to connect
1511-
if ($("#command-connect").length){
1510+
if ($("#command-connect").is(":visible")){
15121511
doConnect(true);
15131512
} else {
15141513
// Change Serial button to close
@@ -1614,11 +1613,10 @@ function web_editor(config) {
16141613
// handling what to do when they're clicked.
16151614
function setupButtons() {
16161615
$("#command-download").click(function () {
1617-
if ($("#command-download").length) {
1618-
doDownload();
1619-
} else {
1620-
doFlash();
1621-
}
1616+
doDownload();
1617+
});
1618+
$("#command-flash").click(function () {
1619+
doFlash();
16221620
});
16231621
$("#command-files").click(function () {
16241622
doFiles();
@@ -1634,11 +1632,10 @@ function web_editor(config) {
16341632
});
16351633
if (navigator.usb) {
16361634
$("#command-connect").click(function () {
1637-
if ($("#command-connect").length) {
1638-
doConnect();
1639-
} else {
1640-
doDisconnect();
1641-
}
1635+
doConnect();
1636+
});
1637+
$("#command-disconnect").click(function () {
1638+
doDisconnect();
16421639
});
16431640
$("#command-serial").click(function () {
16441641
doSerial();

static/css/style.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,3 +1034,12 @@ ul.tree li:last-child:before {
10341034
display:none;
10351035
margin-bottom: 10px;
10361036
}
1037+
1038+
/* Hide disconnect initially */
1039+
#command-disconnect {
1040+
display: none;
1041+
}
1042+
1043+
#command-flash {
1044+
display: none;
1045+
}

0 commit comments

Comments
 (0)