From 8650fef7aec2c0e393361e43effc395bd8d928e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Monda?= Date: Wed, 31 Jan 2018 04:26:49 +0100 Subject: [PATCH] Make reenumerate() more reliable. --- packages/usb/uhk.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/usb/uhk.js b/packages/usb/uhk.js index 73868b54..3ca7f188 100644 --- a/packages/usb/uhk.js +++ b/packages/usb/uhk.js @@ -29,6 +29,9 @@ function uint32ToArray(value) { function writeDevice(device, data, options={}) { device.write(getTransferData(new Buffer(data))); + if (options.noRead) { + return Promise.resolve(); + } return util.promisify(device.read.bind(device))(); } @@ -164,7 +167,7 @@ function reenumerate(enumerationMode) { } console.log(`Trying to reenumerate as ${enumerationMode}...`); - const intervalId = setInterval(() => { + const intervalId = setInterval(async function() { pollingTimeoutMs -= pollingIntervalMs; const foundDevice = HID.devices().find(device => @@ -187,8 +190,7 @@ function reenumerate(enumerationMode) { let device = exports.getUhkDevice(); if (device && !jumped) { console.log(`UHK found, reenumerating as ${enumerationMode}`); - let message = new Buffer([exports.usbCommands.reenumerate, enumerationModeId, ...uint32ToArray(bootloaderTimeoutMs)]); - device.write(getTransferData(message)); + await writeDevice(device, [exports.usbCommands.reenumerate, enumerationModeId, ...uint32ToArray(bootloaderTimeoutMs)], {noRead:true}); jumped = true; }