From 0b420ff516bb8cc95a27f960e5497b669c5aad80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Monda?= Date: Fri, 30 Mar 2018 18:59:50 +0200 Subject: [PATCH] Extract uhk.switchKeymap() --- packages/usb/switch-keymap.js | 11 +++++------ packages/usb/uhk.js | 7 +++++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/usb/switch-keymap.js b/packages/usb/switch-keymap.js index 30ff1efb..deb9e5a5 100755 --- a/packages/usb/switch-keymap.js +++ b/packages/usb/switch-keymap.js @@ -1,9 +1,8 @@ #!/usr/bin/env node const uhk = require('./uhk'); -const device = uhk.getUhkDevice(); -const sendData = Buffer.concat([new Buffer([uhk.usbCommands.switchKeymap, 3]), new Buffer('TES')]); -console.log(sendData) -device.write(uhk.getTransferData(sendData)); -const response = Buffer.from(device.readSync()); -console.log(response); +(async function() { + const device = uhk.getUhkDevice(); + const sendData = await uhk.switchKeymap(device, 'TES'); + console.log(sendData) +})(); diff --git a/packages/usb/uhk.js b/packages/usb/uhk.js index dc5b908f..9e144d76 100644 --- a/packages/usb/uhk.js +++ b/packages/usb/uhk.js @@ -206,6 +206,12 @@ async function jumpToModuleBootloader(device, moduleSlotId) { await uhk.writeDevice(device, [uhk.usbCommands.jumpToModuleBootloader, moduleSlotId]); }; +async function switchKeymap(device, keymapAbbreviation) { + const keymapAbbreviationAscii = keymapAbbreviation.split('').map(char => char.charCodeAt(0)); + const payload = [uhk.usbCommands.switchKeymap, keymapAbbreviation.length, ...keymapAbbreviationAscii]; + return await uhk.writeDevice(device, payload); +} + async function waitForKbootIdle(device) { const intervalMs = 100; const pingMessageInterval = 500; @@ -279,6 +285,7 @@ uhk = exports = module.exports = moduleExports = { reenumerate, sendKbootCommandToModule, jumpToModuleBootloader, + switchKeymap, waitForKbootIdle, updateModuleFirmware, usbCommands: {