From 2310320b8a5e6676d2482a36165518cd5f6b10c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Monda?= Date: Tue, 30 Jan 2018 05:30:32 +0100 Subject: [PATCH] Replace pushUint32() with uint32ToArray() and don't use it where not needed. --- packages/usb/get-i2c-baud-rate.js | 2 +- packages/usb/get-i2c-health.js | 4 ++-- packages/usb/get-uptime.js | 2 +- packages/usb/set-i2c-baud-rate.js | 2 +- packages/usb/uhk.js | 10 +++------- 5 files changed, 8 insertions(+), 12 deletions(-) diff --git a/packages/usb/get-i2c-baud-rate.js b/packages/usb/get-i2c-baud-rate.js index 2dd0c38e..adfd6e46 100755 --- a/packages/usb/get-i2c-baud-rate.js +++ b/packages/usb/get-i2c-baud-rate.js @@ -3,7 +3,7 @@ const path = require('path'); const uhk = require('./uhk'); const device = uhk.getUhkDevice(); -let buffer = new Buffer(uhk.pushUint32([uhk.usbCommands.getDeviceProperty, uhk.devicePropertyIds.i2cBaudRate])); +let buffer = new Buffer([uhk.usbCommands.getDeviceProperty, uhk.devicePropertyIds.i2cBaudRate]); //console.log(buffer); device.write(uhk.getTransferData(buffer)); let response = device.readSync(); diff --git a/packages/usb/get-i2c-health.js b/packages/usb/get-i2c-health.js index 3ace691c..7fba5248 100755 --- a/packages/usb/get-i2c-health.js +++ b/packages/usb/get-i2c-health.js @@ -51,13 +51,13 @@ function convertMs(milliseconds) { const device = uhk.getUhkDevice(); -device.write(uhk.getTransferData(new Buffer(uhk.pushUint32([uhk.usbCommands.getDeviceProperty, uhk.devicePropertyIds.uptime])))); +device.write(uhk.getTransferData(new Buffer([uhk.usbCommands.getDeviceProperty, uhk.devicePropertyIds.uptime]))); let response = device.readSync(); let uptimeMs = uhk.getUint32(response, 1); let uptime = convertMs(uptimeMs); console.log(`uptime: ${uptime.days}d ${String(uptime.hours).padStart(2, '0')}:${String(uptime.minutes).padStart(2, '0')}:${String(uptime.seconds).padStart(2, '0')}`) -device.write(uhk.getTransferData(new Buffer(uhk.pushUint32([uhk.usbCommands.getDeviceProperty, uhk.devicePropertyIds.i2cBaudRate])))); +device.write(uhk.getTransferData(new Buffer([uhk.usbCommands.getDeviceProperty, uhk.devicePropertyIds.i2cBaudRate]))); response = device.readSync(); let requestedBaudRate = uhk.getUint32(response, 2); let actualBaudRate = uhk.getUint32(response, 6); diff --git a/packages/usb/get-uptime.js b/packages/usb/get-uptime.js index 6f8e7c83..e63fa17f 100755 --- a/packages/usb/get-uptime.js +++ b/packages/usb/get-uptime.js @@ -15,7 +15,7 @@ function convertMs(milliseconds) { return {days, hours, minutes, seconds}; } -let buffer = new Buffer(uhk.pushUint32([uhk.usbCommands.getDeviceProperty, uhk.devicePropertyIds.uptime])); +let buffer = new Buffer([uhk.usbCommands.getDeviceProperty, uhk.devicePropertyIds.uptime]); //console.log(buffer); device.write(uhk.getTransferData(buffer)); let response = device.readSync(); diff --git a/packages/usb/set-i2c-baud-rate.js b/packages/usb/set-i2c-baud-rate.js index 7de2746f..74760d69 100755 --- a/packages/usb/set-i2c-baud-rate.js +++ b/packages/usb/set-i2c-baud-rate.js @@ -17,7 +17,7 @@ You're free to use any value in between and test the results.`); } let bps = process.argv[2]; -let buffer = new Buffer(uhk.pushUint32([uhk.usbCommands.setI2cBaudRate], +bps)); +let buffer = new Buffer([uhk.usbCommands.setI2cBaudRate, ...uhk.uint32ToArray(+bps)]); //console.log(buffer); device.write(uhk.getTransferData(buffer)); let response = device.readSync(); diff --git a/packages/usb/uhk.js b/packages/usb/uhk.js index 3d49e217..414de70a 100644 --- a/packages/usb/uhk.js +++ b/packages/usb/uhk.js @@ -22,12 +22,8 @@ function getUint32(buffer, offset) { return (buffer[offset]) + (buffer[offset+1] * 2**8) + (buffer[offset+2] * 2**16) + (buffer[offset+3] * 2**24); } -function pushUint32(array, value) { - array.push((value >> 0) & 0xff); - array.push((value >> 8) & 0xff); - array.push((value >> 16) & 0xff); - array.push((value >> 24) & 0xff); - return array; +function uint32ToArray(value) { + return [(value >> 0) & 0xff, (value >> 8) & 0xff, (value >> 16) & 0xff, (value >> 24) & 0xff]; } function getUhkDevice() { @@ -75,7 +71,7 @@ exports = module.exports = moduleExports = { bufferToString, getUint16, getUint32, - pushUint32, + uint32ToArray, getUhkDevice, getBootloaderDevice, getTransferData,