From fb4e05fdc407485871ea4d8498a2bd88a1a47042 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Monda?= Date: Mon, 2 Apr 2018 00:30:03 +0200 Subject: [PATCH] Dump USB reads and writes via writeDevice() --- packages/usb/uhk.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/usb/uhk.js b/packages/usb/uhk.js index 651e52aa..519b52df 100644 --- a/packages/usb/uhk.js +++ b/packages/usb/uhk.js @@ -1,7 +1,6 @@ const util = require('util'); const HID = require('node-hid'); -// const debug = process.env.DEBUG; -const debug = true; +const debug = process.env.DEBUG; function bufferToString(buffer) { let str = ''; @@ -28,11 +27,23 @@ function uint32ToArray(value) { } function writeDevice(device, data, options={}) { - device.write(getTransferData(new Buffer(data))); + const dataBuffer = new Buffer(data); + writeLog('W: ', dataBuffer); + device.write(getTransferData(dataBuffer)); if (options.noRead) { return Promise.resolve(); } - return util.promisify(device.read.bind(device))(); + + return new Promise((resolve, reject) => { + device.read((err, data) => { + if (err) { + reject(err); + } else { + writeLog('R: ', data); + resolve(data); + } + }); + }); } function getUhkDevice() {