From 9589398834ce63defd56a22214fbc97c86a2acab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Monda?= Date: Wed, 31 Jan 2018 02:25:37 +0100 Subject: [PATCH] Move waitForKbootIdle() to uhk.js --- packages/usb/uhk.js | 30 +++++++++++++++++++++++++++ packages/usb/wait-for-kboot-idle.js | 32 +---------------------------- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/packages/usb/uhk.js b/packages/usb/uhk.js index 6b34a591..255f181d 100644 --- a/packages/usb/uhk.js +++ b/packages/usb/uhk.js @@ -204,6 +204,35 @@ async function jumpToModuleBootloader(device, moduleSlotId) { await uhk.writeDevice(device, [uhk.usbCommands.jumpToModuleBootloader, moduleSlotId]); }; +async function waitForKbootIdle(device) { + const intervalMs = 100; + const pingMessageInterval = 500; + let timeoutMs = 10000; + + return new Promise((resolve, reject) => { + const intervalId = setInterval(async function() { + const response = await uhk.writeDevice(device, [uhk.usbCommands.getDeviceProperty, uhk.devicePropertyIds.currentKbootCommand]); + const currentKbootCommand = response[1]; + if (currentKbootCommand == 0) { + console.log('Bootloader pinged.'); + resolve(); + clearInterval(intervalId); + return; + } else if (timeoutMs % pingMessageInterval === 0) { + console.log("Cannot ping the bootloader. Please reconnect the left keyboard half. It probably needs several tries, so keep reconnecting until you see this message."); + }; + + timeoutMs -= intervalMs; + + if (timeoutMs < 0) { + reject(); + clearInterval(intervalId); + return; + } + }, intervalMs); + }); +} + uhk = exports = module.exports = moduleExports = { bufferToString, getUint16, @@ -221,6 +250,7 @@ uhk = exports = module.exports = moduleExports = { reenumerate, sendKbootCommandToModule, jumpToModuleBootloader, + waitForKbootIdle, usbCommands: { getDeviceProperty : 0x00, reenumerate : 0x01, diff --git a/packages/usb/wait-for-kboot-idle.js b/packages/usb/wait-for-kboot-idle.js index a4d8a3fc..7387c5af 100755 --- a/packages/usb/wait-for-kboot-idle.js +++ b/packages/usb/wait-for-kboot-idle.js @@ -1,37 +1,7 @@ #!/usr/bin/env node const uhk = require('./uhk'); - -let intervalMs = 100; -let pingMessageInterval = 500; - -async function waitForKbootIdle(device) { - let timeoutMs = 10000; - return new Promise((resolve, reject) => { - const intervalId = setInterval(async function() { - const response = await uhk.writeDevice(device, [uhk.usbCommands.getDeviceProperty, uhk.devicePropertyIds.currentKbootCommand]); - const currentKbootCommand = response[1]; - if (currentKbootCommand == 0) { - console.log('Bootloader pinged.'); - resolve(); - clearInterval(intervalId); - return; - } else if (timeoutMs % pingMessageInterval === 0) { - console.log("Cannot ping the bootloader. Please reconnect the left keyboard half. It probably needs several tries, so keep reconnecting until you see this message."); - }; - - timeoutMs -= intervalMs; - - if (timeoutMs < 0) { - reject(); - clearInterval(intervalId); - return; - } - }, intervalMs); - }); -} - const device = uhk.getUhkDevice(); (async function() { - await waitForKbootIdle(device); + await uhk.waitForKbootIdle(device); })();