From 33de26b3abcd9d27568da8f14ceb2593641a39df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Monda?= Date: Tue, 28 Feb 2017 00:12:17 +0100 Subject: [PATCH] Make jump-to-bootloader.js wait for the UHK and time out. --- usb/jump-to-bootloader.js | 25 ++++++++++++++++++++++++- usb/uhk.js | 19 ++++++++++++++----- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/usb/jump-to-bootloader.js b/usb/jump-to-bootloader.js index 6b1a4f9e..e6094b4e 100755 --- a/usb/jump-to-bootloader.js +++ b/usb/jump-to-bootloader.js @@ -1,3 +1,26 @@ #!/usr/bin/env node let uhk = require('./uhk'); -uhk.sendUsbPacket(new Buffer([uhk.usbCommands.jumpToBootloader])); + +let timeoutMs = 10000; +let pollingIntervalMs = 100; + +console.log('Trying to jump to the bootloader...'); +setInterval(() => { + timeoutMs -= pollingIntervalMs; + + if (uhk.getBootloaderDevice()) { + console.log('Bootloader is up'); + process.exit(0); + } + + if (timeoutMs <= 0) { + console.log("Couldn't jump to the bootloader"); + process.exit(1); + } + + if (uhk.getUhkDevice()) { + console.log('UHK found, jumping to bootloader'); + uhk.sendUsbPacket(new Buffer([uhk.usbCommands.jumpToBootloader])); + } + +}, pollingIntervalMs); diff --git a/usb/uhk.js b/usb/uhk.js index 742f019d..dc880bad 100755 --- a/usb/uhk.js +++ b/usb/uhk.js @@ -14,11 +14,17 @@ function bufferToString(buffer) { let usbEndpoints; -function getUsbEndpoints() { - let vid = 0x16d3; - let pid = 0x05ea; +function getUhkDevice() { + return usb.findByIds(0x16d3, 0x05ea); +} - let device = usb.findByIds(vid, pid); +function getBootloaderDevice() { + return usb.findByIds(0x15a2, 0x0073); +} + +function getUsbEndpoints() { + let device = getUhkDevice(); + device = getUhkDevice(); device.open(); let usbInterface = device.interface(0); @@ -87,6 +93,8 @@ function sendUsbPackets(packets) { exports = module.exports = { DelayMs, bufferToString, + getUhkDevice, + getBootloaderDevice, getUsbEndpoints, sendUsbPacket, sendUsbPackets, @@ -102,7 +110,8 @@ exports = module.exports = { readMergeSensor: 7, uploadConfig: 8, applyConfig: 9, - setLedPwm: 10 + setLedPwm: 10, + getBridgeMessageCounter: 11 }, leftLedDriverAddress: 0b1110100, rightLedDriverAddress: 0b1110111