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