refactor: use Buffer.from and Buffer.alloc instead of new Buffer() (#957)

This commit is contained in:
Róbert Kiss
2019-05-29 21:56:01 +02:00
committed by László Monda
parent 999feea488
commit 82c9126d82
24 changed files with 47 additions and 47 deletions

View File

@@ -8,5 +8,5 @@ setInterval(() => {
areLedsEnabled = !areLedsEnabled;
const brightnessPercent = areLedsEnabled ? 100 : 0;
device.write(uhk.getTransferData(new Buffer([uhk.usbCommands.setLedPwmBrightness, brightnessPercent])));
device.write(uhk.getTransferData(Buffer.from([uhk.usbCommands.setLedPwmBrightness, brightnessPercent])));
}, 500);

View File

@@ -7,5 +7,5 @@ const device = uhk.getUhkDevice();
setInterval(() => {
areLedsEnabled = !areLedsEnabled;
device.write(uhk.getTransferData(new Buffer([uhk.usbCommands.setTestLed, areLedsEnabled ? 1 : 0])));
device.write(uhk.getTransferData(Buffer.from([uhk.usbCommands.setTestLed, areLedsEnabled ? 1 : 0])));
}, 500);

View File

@@ -4,7 +4,7 @@ const uhk = require('./uhk');
(async function() {
const device = uhk.getUhkDevice();
const buffer = new Buffer(Array(2**15-64).fill(0xff));
const buffer = Buffer.from(Array(2**15-64).fill(0xff));
await uhk.writeConfig(device, buffer, false);
await uhk.launchEepromTransfer(device, uhk.eepromOperations.write, uhk.configBufferIds.stagingUserConfig);
})();

View File

@@ -4,7 +4,7 @@ const uhk = require('./uhk');
const device = uhk.getUhkDevice();
function getAdcValue() {
const data = uhk.getTransferData(new Buffer([uhk.usbCommands.getAdcValue]));
const data = uhk.getTransferData(Buffer.from([uhk.usbCommands.getAdcValue]));
console.log('Sending ', data);
device.write(data);
const receivedBuffer = Buffer.from(device.readSync());

View File

@@ -2,7 +2,7 @@
const uhk = require('./uhk');
const device = uhk.getUhkDevice();
const sendData = new Buffer([uhk.usbCommands.getDeviceProperty, uhk.devicePropertyIds.configSizes]);
const sendData = Buffer.from([uhk.usbCommands.getDeviceProperty, uhk.devicePropertyIds.configSizes]);
device.write(uhk.getTransferData(sendData));
const response = Buffer.from(device.readSync());

View File

@@ -13,7 +13,7 @@ function getUint16(buffer, offset) {
let prevGeneric, prevBasic, prevMedia, prevSystem, prevMouse;
function getDebugInfo() {
const payload = new Buffer([uhk.usbCommands.getDebugBuffer]);
const payload = Buffer.from([uhk.usbCommands.getDebugBuffer]);
// console.log(payload)
// console.log('Sending ', uhk.bufferToString(payload));
device.write(uhk.getTransferData(payload));

View File

@@ -3,7 +3,7 @@ const uhk = require('./uhk');
const device = uhk.getUhkDevice();
function readKeyboardState() {
const payload = new Buffer([uhk.usbCommands.getDeviceState]);
const payload = Buffer.from([uhk.usbCommands.getDeviceState]);
console.log('Sending ', uhk.bufferToString(payload));
device.write(uhk.getTransferData(payload));
const receivedBuffer = device.readSync();

View File

@@ -51,20 +51,20 @@ function convertMs(milliseconds) {
const device = uhk.getUhkDevice();
device.write(uhk.getTransferData(new Buffer([uhk.usbCommands.getDeviceProperty, uhk.devicePropertyIds.uptime])));
device.write(uhk.getTransferData(Buffer.from([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.usbCommands.getDeviceProperty, uhk.devicePropertyIds.i2cBaudRate])));
device.write(uhk.getTransferData(Buffer.from([uhk.usbCommands.getDeviceProperty, uhk.devicePropertyIds.i2cBaudRate])));
response = device.readSync();
let requestedBaudRate = uhk.getUint32(response, 2);
let actualBaudRate = uhk.getUint32(response, 6);
console.log(`requestedBaudRate:${requestedBaudRate} | actualBaudRate:${actualBaudRate} | I2C0_F:0b${response[1].toString(2).padStart(8, '0')}`)
for (let slaveId=0; slaveId<6; slaveId++) {
device.write(uhk.getTransferData(new Buffer([uhk.usbCommands.getSlaveI2cErrors, slaveId])));
device.write(uhk.getTransferData(Buffer.from([uhk.usbCommands.getSlaveI2cErrors, slaveId])));
let response = Buffer.from(device.readSync());
let str = slaveI2cErrorBufferToString(response, slaveId);
console.log(str);

View File

@@ -2,7 +2,7 @@
const uhk = require('./uhk');
const device = uhk.getUhkDevice();
const sendData = new Buffer([uhk.usbCommands.getModuleProperty, uhk.moduleSlotToId.leftHalf, uhk.modulePropertyIds.protocolVersions]);
const sendData = Buffer.from([uhk.usbCommands.getModuleProperty, uhk.moduleSlotToId.leftHalf, uhk.modulePropertyIds.protocolVersions]);
//console.log(sendData)
device.write(uhk.getTransferData(sendData));
const response = Buffer.from(device.readSync());

View File

@@ -2,7 +2,7 @@
const uhk = require('./uhk');
const device = uhk.getUhkDevice();
const sendData = new Buffer([uhk.usbCommands.getDeviceProperty, uhk.devicePropertyIds.protocolVersions]);
const sendData = Buffer.from([uhk.usbCommands.getDeviceProperty, uhk.devicePropertyIds.protocolVersions]);
//console.log(sendData)
device.write(uhk.getTransferData(sendData));
const response = Buffer.from(device.readSync());

View File

@@ -5,7 +5,7 @@ const uhk = require('./uhk');
const slaveId = parseInt(process.argv[2]);
const device = uhk.getUhkDevice();
const sendData = new Buffer([uhk.usbCommands.getSlaveI2cErrors, slaveId]);
const sendData = Buffer.from([uhk.usbCommands.getSlaveI2cErrors, slaveId]);
device.write(uhk.getTransferData(sendData));
const response = Buffer.from(device.readSync());

View File

@@ -15,7 +15,7 @@ function convertMs(milliseconds) {
return {days, hours, minutes, seconds};
}
let buffer = new Buffer([uhk.usbCommands.getDeviceProperty, uhk.devicePropertyIds.uptime]);
let buffer = Buffer.from([uhk.usbCommands.getDeviceProperty, uhk.devicePropertyIds.uptime]);
//console.log(buffer);
device.write(uhk.getTransferData(buffer));
let response = device.readSync();

View File

@@ -6,7 +6,7 @@ let counter = 1;
while (true) {
console.log(`hidapi sync test ${counter++}`);
const sendData = new Buffer([uhk.usbCommands.getDeviceProperty, uhk.devicePropertyIds.configSizes]);
const sendData = Buffer.from([uhk.usbCommands.getDeviceProperty, uhk.devicePropertyIds.configSizes]);
device.write(uhk.getTransferData(sendData));
device.readSync()
}

View File

@@ -7,10 +7,10 @@ const chunkSize = 63;
let isHardwareConfig = process.argv[2] === 'h';
let configTypeString = isHardwareConfig ? 'hardware' : 'user';
let offset = 0;
let configBuffer = new Buffer(0);
let configBuffer = Buffer.alloc(0);
let chunkSizeToRead;
const payload = new Buffer([uhk.usbCommands.getDeviceProperty, uhk.devicePropertyIds.configSizes]);
const payload = Buffer.from([uhk.usbCommands.getDeviceProperty, uhk.devicePropertyIds.configSizes]);
device.write(uhk.getTransferData(payload));
let buffer = Buffer.from(device.readSync());
const hardwareConfigMaxSize = buffer[1] + (buffer[2]<<8);
@@ -27,7 +27,7 @@ while (offset < configSize) {
device.write(uhk.getTransferData(buffer));
buffer = Buffer.from(device.readSync());
console.log('read-config-chunk', uhk.bufferToString(buffer));
configBuffer = Buffer.concat([configBuffer, new Buffer(buffer.slice(1, chunkSizeToRead + 1))]);
configBuffer = Buffer.concat([configBuffer, Buffer.from(buffer.slice(1, chunkSizeToRead + 1))]);
offset += chunkSizeToRead
}
console.log('read ', uhk.bufferToString(configBuffer));

View File

@@ -10,4 +10,4 @@ if (process.argv.length !== 3) {
}
let leftBrightnessPercent = process.argv[2] || '';
device.write(uhk.getTransferData(new Buffer([uhk.usbCommands.setLedPwmBrightness, +leftBrightnessPercent])));
device.write(uhk.getTransferData(Buffer.from([uhk.usbCommands.setLedPwmBrightness, +leftBrightnessPercent])));

View File

@@ -43,7 +43,7 @@ function uint32ToArray(value) {
}
function writeDevice(device, data, options={}) {
const dataBuffer = new Buffer(data);
const dataBuffer = Buffer.from(data);
if (!options.noDebug) {
writeLog('W: ', dataBuffer);
}
@@ -396,7 +396,7 @@ async function writeHca(device, isIso) {
}
async function eraseHca(device) {
const buffer = new Buffer(Array(64).fill(0xff));
const buffer = Buffer.from(Array(64).fill(0xff));
await uhk.writeConfig(device, buffer, true);
await uhk.launchEepromTransfer(device, uhk.eepromOperations.write, configBufferIds.hardwareConfig);
}