@@ -1258,22 +1258,11 @@ function web_editor(config) {
1258
1258
webusbErrorHandler ( error ) ;
1259
1259
}
1260
1260
1261
- function clearDapWrapper ( event ) {
1262
- if ( window . dapwrapper || window . previousDapWrapper ) {
1263
- window . dapwrapper = null ;
1264
- window . previousDapWrapper = null ;
1265
- }
1266
- }
1267
-
1268
1261
function doConnect ( serial ) {
1269
1262
// Change button to connecting
1270
1263
$ ( "#command-connect" ) . hide ( ) ;
1271
1264
$ ( "#command-connecting" ) . show ( ) ;
1272
1265
$ ( "#command-disconnect" ) . hide ( ) ;
1273
-
1274
- // Device disconnect listener
1275
- // Clears dapwrapper
1276
- navigator . usb . addEventListener ( 'disconnect' , clearDapWrapper ) ;
1277
1266
1278
1267
// Show error on WebUSB Disconnect Events
1279
1268
navigator . usb . addEventListener ( 'disconnect' , showDisconnectError ) ;
@@ -1340,19 +1329,16 @@ function web_editor(config) {
1340
1329
console . log ( err ) ;
1341
1330
console . trace ( ) ;
1342
1331
1343
- // If there was an error and quick flash is in use, then clear dapwrapper
1344
- if ( usePartialFlashing ) {
1345
- if ( window . dapwrapper ) {
1332
+ // Disconnect from the microbit
1333
+ doDisconnect ( ) . then ( function ( ) {
1334
+ // As there has been an error clear the partial flashing DAPWrapper
1335
+ if ( window . dapwrapper ) {
1346
1336
window . dapwrapper = null ;
1347
1337
}
1348
-
1349
- if ( window . previousDapWrapper ) {
1338
+ if ( window . previousDapWrapper ) {
1350
1339
window . previousDapWrapper = null ;
1351
1340
}
1352
- }
1353
-
1354
- // Disconnect from the microbit
1355
- doDisconnect ( ) ;
1341
+ } ) ;
1356
1342
1357
1343
var errorType ;
1358
1344
var errorTitle ;
@@ -1462,7 +1448,7 @@ function web_editor(config) {
1462
1448
$ ( '#flashing-overlay' ) . keydown ( function ( e ) {
1463
1449
if ( e . which == 27 ) {
1464
1450
flashErrorClose ( ) ;
1465
- }
1451
+ }
1466
1452
} ) ;
1467
1453
1468
1454
// Send event
@@ -1478,22 +1464,13 @@ function web_editor(config) {
1478
1464
}
1479
1465
1480
1466
function doDisconnect ( ) {
1481
-
1482
- // Remove disconnect listenr
1467
+ // Remove disconnect listener
1483
1468
navigator . usb . removeEventListener ( 'disconnect' , showDisconnectError ) ;
1484
1469
1485
1470
// Hide serial and disconnect if open
1486
1471
if ( $ ( "#repl" ) . css ( 'display' ) != 'none' ) {
1487
- $ ( "#repl" ) . hide ( ) ;
1488
- $ ( "#request-repl" ) . hide ( ) ;
1489
- $ ( "#request-serial" ) . hide ( ) ;
1490
- $ ( "#editor-container" ) . show ( ) ;
1472
+ closeSerial ( ) ;
1491
1473
}
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 ;
1497
1474
1498
1475
// Change button to connect
1499
1476
$ ( "#command-disconnect" ) . hide ( ) ;
@@ -1506,22 +1483,23 @@ function web_editor(config) {
1506
1483
1507
1484
var p = Promise . resolve ( ) ;
1508
1485
1509
- if ( usePartialFlashing ) {
1510
- if ( window . dapwrapper ) {
1511
- console . log ( "Disconnecting: Using Quick Flash" ) ;
1512
- p = p . then ( function ( ) { window . dapwrapper . daplink . stopSerialRead ( ) } )
1513
- . then ( function ( ) { window . dapwrapper . disconnectAsync ( ) } ) ;
1514
- }
1486
+ if ( usePartialFlashing && window . dapwrapper ) {
1487
+ console . log ( 'Disconnecting: Using Quick Flash' ) ;
1488
+ p = p . then ( function ( ) { return window . dapwrapper . disconnectAsync ( ) } ) ;
1515
1489
}
1516
- else {
1517
- if ( window . daplink ) {
1518
- console . log ( "Disconnecting: Using Full Flash" ) ;
1519
- p = p . then ( function ( ) { window . daplink . stopSerialRead ( ) } )
1520
- . then ( function ( ) { window . daplink . disconnect ( ) } ) ;
1521
- }
1490
+ else if ( window . daplink ) {
1491
+ console . log ( 'Disconnecting: Using Full Flash' ) ;
1492
+ p = p . then ( function ( ) { return window . daplink . disconnect ( ) } ) ;
1522
1493
}
1523
1494
1524
- p . finally ( function ( ) {
1495
+ p = p . catch ( function ( ) {
1496
+ console . log ( 'Error during disconnection' ) ;
1497
+ document . dispatchEvent ( new CustomEvent ( 'webusb' , { 'detail' : {
1498
+ 'flash-type' : 'webusb' ,
1499
+ 'event-type' : 'error' ,
1500
+ 'message' : 'error-disconnecting'
1501
+ } } ) ) ;
1502
+ } ) . finally ( function ( ) {
1525
1503
console . log ( 'Disconnection Complete' ) ;
1526
1504
document . dispatchEvent ( new CustomEvent ( 'webusb' , { 'detail' : {
1527
1505
'flash-type' : 'webusb' ,
@@ -1538,21 +1516,7 @@ function web_editor(config) {
1538
1516
1539
1517
// Hide serial and disconnect if open
1540
1518
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
- }
1519
+ closeSerial ( ) ;
1556
1520
}
1557
1521
1558
1522
// Get the hex to flash in bytes format, exit if there is an error
@@ -1580,9 +1544,6 @@ function web_editor(config) {
1580
1544
1581
1545
var p = Promise . resolve ( ) ;
1582
1546
if ( usePartialFlashing ) {
1583
- REPL = null ;
1584
- $ ( "#repl" ) . empty ( ) ;
1585
-
1586
1547
p = window . dapwrapper . disconnectAsync ( )
1587
1548
. then ( function ( ) {
1588
1549
return PartialFlashing . connectDapAsync ( ) ;
@@ -1640,28 +1601,35 @@ function web_editor(config) {
1640
1601
} ) ;
1641
1602
}
1642
1603
1604
+ function closeSerial ( keepSession ) {
1605
+ console . log ( "Closing Serial Terminal" ) ;
1606
+ $ ( '#repl' ) . empty ( ) ;
1607
+ $ ( '#repl' ) . hide ( ) ;
1608
+ $ ( '#request-repl' ) . hide ( ) ;
1609
+ $ ( '#request-serial' ) . hide ( ) ;
1610
+ $ ( '#editor-container' ) . show ( ) ;
1611
+
1612
+ var serialButton = config [ 'translate' ] [ 'static-strings' ] [ 'buttons' ] [ 'command-serial' ] ;
1613
+ $ ( '#command-serial' ) . attr ( 'title' , serialButton [ 'title' ] ) ;
1614
+ $ ( '#command-serial > .roundlabel' ) . text ( serialButton [ 'label' ] ) ;
1615
+
1616
+ var daplink = usePartialFlashing ? window . dapwrapper . daplink : window . daplink ;
1617
+ daplink . stopSerialRead ( ) ;
1618
+ daplink . removeAllListeners ( DAPjs . DAPLink . EVENT_SERIAL_DATA ) ;
1619
+ REPL . uninstallKeyboard ( ) ;
1620
+ REPL . io . pop ( ) ;
1621
+ REPL = null ;
1622
+ }
1623
+
1643
1624
function doSerial ( ) {
1644
- console . log ( "Setting Up Serial Terminal" ) ;
1645
- // Hide terminal
1625
+ // Hide terminal if it is currently shown
1646
1626
var serialButton = config [ "translate" ] [ "static-strings" ] [ "buttons" ] [ "command-serial" ] ;
1647
1627
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
- }
1628
+ closeSerial ( ) ;
1662
1629
return ;
1663
1630
}
1664
1631
1632
+ console . log ( "Setting Up Serial Terminal" ) ;
1665
1633
// Check if we need to connect
1666
1634
if ( $ ( "#command-connect" ) . is ( ":visible" ) ) {
1667
1635
doConnect ( true ) ;
@@ -1757,8 +1725,8 @@ function web_editor(config) {
1757
1725
$ ( overlayContainer ) . keydown ( function ( e ) {
1758
1726
if ( e . which == 27 ) {
1759
1727
modalMsgClose ( ) ;
1760
- }
1761
- } ) ;
1728
+ }
1729
+ } ) ;
1762
1730
}
1763
1731
1764
1732
function formatMenuContainer ( parentButtonId , containerId ) {
0 commit comments