Jump to the bootloader without crashing.
This commit is contained in:
@@ -3,6 +3,7 @@ let uhk = require('./uhk');
|
||||
|
||||
let timeoutMs = 10000;
|
||||
let pollingIntervalMs = 100;
|
||||
let jumped = false;
|
||||
|
||||
console.log('Trying to jump to the bootloader...');
|
||||
setInterval(() => {
|
||||
@@ -18,9 +19,10 @@ setInterval(() => {
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (uhk.getUhkDevice()) {
|
||||
if (uhk.getUhkDevice() && !jumped) {
|
||||
console.log('UHK found, jumping to bootloader');
|
||||
uhk.sendUsbPacket(new Buffer([uhk.usbCommands.jumpToBootloader]));
|
||||
uhk.sendUsbPacket(new Buffer([uhk.usbCommands.jumpToBootloader]), {noReceive:true});
|
||||
jumped = true;
|
||||
}
|
||||
|
||||
}, pollingIntervalMs);
|
||||
|
||||
38
usb/uhk.js
38
usb/uhk.js
@@ -47,13 +47,9 @@ class DelayMs {
|
||||
}
|
||||
}
|
||||
|
||||
function sendUsbPacketsByCallback(packetProvider) {
|
||||
function sendUsbPacketsByCallback(packetProvider, options) {
|
||||
let packet = packetProvider()
|
||||
|
||||
if (!packet) {
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (packet instanceof DelayMs) {
|
||||
setTimeout(() => {
|
||||
sendUsbPacketsByCallback(packetProvider);
|
||||
@@ -61,6 +57,10 @@ function sendUsbPacketsByCallback(packetProvider) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(packet instanceof Buffer)) {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('Sending: ', bufferToString(packet));
|
||||
|
||||
let [endpointIn, endpointOut] = usbEndpoints || getUsbEndpoints();
|
||||
@@ -69,25 +69,31 @@ function sendUsbPacketsByCallback(packetProvider) {
|
||||
console.error("USB error: %s", err);
|
||||
process.exit(1);
|
||||
}
|
||||
endpointIn.transfer(64, function(err2, receivedBuffer) {
|
||||
if (err2) {
|
||||
console.error("USB error: %s", err2);
|
||||
process.exit(2);
|
||||
}
|
||||
console.log('Received:', bufferToString(receivedBuffer));
|
||||
|
||||
if (options.noReceive) {
|
||||
sendUsbPacketsByCallback(packetProvider);
|
||||
})
|
||||
} else {
|
||||
endpointIn.transfer(64, function(err2, receivedBuffer) {
|
||||
if (err2) {
|
||||
console.error("USB error: %s", err2);
|
||||
process.exit(2);
|
||||
}
|
||||
console.log('Received:', bufferToString(receivedBuffer));
|
||||
sendUsbPacketsByCallback(packetProvider);
|
||||
})
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
function sendUsbPacket(packet) {
|
||||
sendUsbPackets([packet]);
|
||||
function sendUsbPacket(packet, options={}) {
|
||||
sendUsbPackets([packet], options);
|
||||
}
|
||||
|
||||
function sendUsbPackets(packets) {
|
||||
function sendUsbPackets(packets, options={}) {
|
||||
sendUsbPacketsByCallback(() => {
|
||||
return packets.shift();
|
||||
})
|
||||
}, options)
|
||||
}
|
||||
|
||||
exports = module.exports = {
|
||||
|
||||
Reference in New Issue
Block a user