Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebUSB: Always reset the serial terminal when closed #312

Merged
merged 4 commits into from
Dec 23, 2019

Conversation

microbit-carlos
Copy link
Collaborator

It does this by refactoring the "serial close" code from multiple places into a single function.

By leaving the content of the terminal when closing the serial it might look like the REPL is still running, which it isn't after a serial reconnection.

In this GIF you can see me opening the REPL, execute a command, and then trying to go back and press the UP key in the keyboard to try to repeat the last command.
Every time the UP key is pressed we can see an error in the console (different issue), and it isn't until Ctrl+C is pressed that the REPL is running again.

ezgif-2-48539878020a

I think this also possibly fixes the issue that was reported by some users where typing in the terminal would print on the screen the same character multiple times.

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.
@microbit-carlos
Copy link
Collaborator Author

@microbit-sam could you review when you find some time after everything else? This is very low priority at the moment.


var daplink = usePartialFlashing ? window.dapwrapper.daplink : window.daplink;
daplink.stopSerialRead();
daplink.removeAllListeners(DAPjs.DAPLink.EVENT_SERIAL_DATA);
Copy link
Collaborator Author

@microbit-carlos microbit-carlos Dec 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the main reason sometimes we were seeing double characters on the terminal.
I don't quite have a way to replicate the original issue reported by some users, @microbit-mark do you have any additional info on the problem? I couldn't find an issue in this repo.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's mentioned in #253 and originally from
https://support.microbit.org/a/tickets/24489 However the user says that they are unsure whether they accidentally typed the extra characters or not, so it maybe hard to reproduce.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, this is not the same then.
This issue would have caused all typed characters to appear twice. Before this PR I only saw this type of double characters very sporadically, but this refactor somehow exuberated the issue and this line fixed it, I was hoping it was the same, but it isn't.

}
if (usePartialFlashing && window.dapwrapper) {
console.log('Disconnecting: Using Quick Flash');
p = p.then(function() { return window.dapwrapper.disconnectAsync() });
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@microbit-sam I think these two were meant to return the disconnect promise?

I assume this was originally an arrow function .then(() => window.dapwrapper.disconnectAsync()); that returned the promise from disconnectAsync(), but then when converting to ES5 the return was accidentally left out?

On the other hand stopSerialRead() does not return a promise, so the original line removed in this PR (p.then(function() { window.dapwrapper.daplink.stopSerialRead() } )) is correct in not using return in the function.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, looks right!

It also captures any possible error during WebUSB disconnection.

// Device disconnect listener
// Clears dapwrapper
navigator.usb.addEventListener('disconnect', clearDapWrapper);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed from here since the disconnect event already executes showDisconnectError().

showDisconnectError() -> webusbErrorHandler() -> clearDapWrapper()

Comment on lines -1261 to -1267
function clearDapWrapper(event) {
if(window.dapwrapper || window.previousDapWrapper) {
window.dapwrapper = null;
window.previousDapWrapper = null;
}
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function removed from here, but code still present in webusbErrorHandler():

https://github.com/bbcmicrobit/PythonEditor/blob/0efa8a8dadc09161b89be3ce607621a7827b01a0/python-main.js#L1332-LL1342

}

// Disconnect from the microbit
doDisconnect();
Copy link
Collaborator Author

@microbit-carlos microbit-carlos Dec 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before this PR, at this point of we had already set to null the window.dapwrapper and window.previousDapWrapper variables above before executing doDisconnect().

So doDisconnect() never attempted to execute disconnectAsync() since we had if (window.dapwrapper) window.dapwrapper.disconnectAsync(); and dapwrapper was already falsy.

@microbit-carlos microbit-carlos merged commit 7b197b3 into master Dec 23, 2019
@microbit-carlos microbit-carlos deleted the serial-close branch December 23, 2019 15:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants