From cae0d02dd279dd2f412dc4125aac5322ff9ae58d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Monda?= Date: Tue, 28 Feb 2017 01:23:56 +0100 Subject: [PATCH] Jump to the bootloader without crashing. --- usb/jump-to-bootloader.js | 6 ++++-- usb/uhk.js | 38 ++++++++++++++++++++++---------------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/usb/jump-to-bootloader.js b/usb/jump-to-bootloader.js index e6094b4e..eb2b232f 100755 --- a/usb/jump-to-bootloader.js +++ b/usb/jump-to-bootloader.js @@ -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); diff --git a/usb/uhk.js b/usb/uhk.js index dc880bad..844884e5 100755 --- a/usb/uhk.js +++ b/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 = {