Expose uhk.sendUsbPacket() and vastly simplify {write,read}-eeprom.js by using it. Make led_pwm.js use uhk.js instead of the obsoleted util.js file.
This commit is contained in:
@@ -1,14 +1,13 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
let util = require('./util');
|
let uhk = require('./uhk');
|
||||||
|
|
||||||
let LED_PWM_COMMAND_ID = 10;
|
let [endpointIn, endpointOut] = uhk.getUsbEndpoints();
|
||||||
let [endpointIn, endpointOut] = util.getUsbEndpoints();
|
|
||||||
let brightnessPercent = 0;
|
let brightnessPercent = 0;
|
||||||
|
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
brightnessPercent = brightnessPercent ? 0 : 100
|
brightnessPercent = brightnessPercent ? 0 : 100
|
||||||
console.log('Sending ', brightnessPercent);
|
console.log('Sending ', brightnessPercent);
|
||||||
endpointOut.transfer(new Buffer([LED_PWM_COMMAND_ID, brightnessPercent]), err => {
|
endpointOut.transfer(new Buffer([uhk.usbCommands.setLedPwm, brightnessPercent]), err => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error("USB error: %s", err);
|
console.error("USB error: %s", err);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
@@ -18,7 +17,7 @@ setInterval(() => {
|
|||||||
console.error("USB error: %s", err2);
|
console.error("USB error: %s", err2);
|
||||||
process.exit(2);
|
process.exit(2);
|
||||||
}
|
}
|
||||||
console.log('Received', util.bufferToString(receivedBuffer));
|
console.log('Received', uhk.bufferToString(receivedBuffer));
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
}, 500);
|
}, 500);
|
||||||
|
|||||||
@@ -1,19 +1,3 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
let uhk = require('./uhk');
|
let uhk = require('./uhk');
|
||||||
let [endpointIn, endpointOut] = uhk.getUsbEndpoints();
|
uhk.sendUsbPacket(new Buffer([uhk.usbCommands.readEeprom, 63, 0x00, 0x00]))
|
||||||
|
|
||||||
var payload = new Buffer([uhk.usbCommands.readEeprom, 63, 0x00, 0x00]);
|
|
||||||
console.log('Sending ', uhk.bufferToString(payload));
|
|
||||||
endpointOut.transfer(payload, function(err) {
|
|
||||||
if (err) {
|
|
||||||
console.error("USB error: %s", err);
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
endpointIn.transfer(64, function(err2, receivedBuffer) {
|
|
||||||
if (err2) {
|
|
||||||
console.error("USB error: %s", err2);
|
|
||||||
process.exit(2);
|
|
||||||
}
|
|
||||||
console.log('Received', uhk.bufferToString(receivedBuffer));
|
|
||||||
})
|
|
||||||
});
|
|
||||||
|
|||||||
86
usb/uhk.js
86
usb/uhk.js
@@ -1,27 +1,63 @@
|
|||||||
let usb = require('usb');
|
let usb = require('usb');
|
||||||
|
|
||||||
exports = module.exports = {
|
function bufferToString(buffer) {
|
||||||
getUsbEndpoints: () => {
|
let str = '';
|
||||||
let vid = 0x16d3;
|
for (let i = 0; i < buffer.length; i++) {
|
||||||
let pid = 0x05ea;
|
let hex = buffer[i].toString(16) + ' ';
|
||||||
|
if (hex.length <= 2) {
|
||||||
let device = usb.findByIds(vid, pid);
|
hex = '0' + hex;
|
||||||
device.open();
|
|
||||||
|
|
||||||
let usbInterface = device.interface(0);
|
|
||||||
|
|
||||||
// https://github.com/tessel/node-usb/issues/147
|
|
||||||
// The function 'isKernelDriverActive' is not available on Windows and not even needed.
|
|
||||||
if (process.platform !== 'win32' && usbInterface.isKernelDriverActive()) {
|
|
||||||
usbInterface.detachKernelDriver();
|
|
||||||
}
|
}
|
||||||
usbInterface.claim();
|
str += hex;
|
||||||
|
};
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
let endpointIn = usbInterface.endpoints[0];
|
let usbEndpoints;
|
||||||
let endpointOut = usbInterface.endpoints[1];
|
|
||||||
return [endpointIn, endpointOut];
|
|
||||||
},
|
|
||||||
|
|
||||||
|
function getUsbEndpoints() {
|
||||||
|
let vid = 0x16d3;
|
||||||
|
let pid = 0x05ea;
|
||||||
|
|
||||||
|
let device = usb.findByIds(vid, pid);
|
||||||
|
device.open();
|
||||||
|
|
||||||
|
let usbInterface = device.interface(0);
|
||||||
|
|
||||||
|
// https://github.com/tessel/node-usb/issues/147
|
||||||
|
// The function 'isKernelDriverActive' is not available on Windows and not even needed.
|
||||||
|
if (process.platform !== 'win32' && usbInterface.isKernelDriverActive()) {
|
||||||
|
usbInterface.detachKernelDriver();
|
||||||
|
}
|
||||||
|
usbInterface.claim();
|
||||||
|
|
||||||
|
let endpointIn = usbInterface.endpoints[0];
|
||||||
|
let endpointOut = usbInterface.endpoints[1];
|
||||||
|
return [endpointIn, endpointOut];
|
||||||
|
}
|
||||||
|
|
||||||
|
function sendUsbPacket(packet) {
|
||||||
|
console.log('Sending: ', bufferToString(packet));
|
||||||
|
|
||||||
|
let [endpointIn, endpointOut] = usbEndpoints || getUsbEndpoints();
|
||||||
|
endpointOut.transfer(packet, function(err) {
|
||||||
|
if (err) {
|
||||||
|
console.error("USB error: %s", err);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
endpointIn.transfer(64, function(err2, receivedBuffer) {
|
||||||
|
if (err2) {
|
||||||
|
console.error("USB error: %s", err2);
|
||||||
|
process.exit(2);
|
||||||
|
}
|
||||||
|
console.log('Received:', bufferToString(receivedBuffer));
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
exports = module.exports = {
|
||||||
|
bufferToString,
|
||||||
|
getUsbEndpoints,
|
||||||
|
sendUsbPacket,
|
||||||
usbCommands: {
|
usbCommands: {
|
||||||
getProperty: 0,
|
getProperty: 0,
|
||||||
jumpToBootloader: 1,
|
jumpToBootloader: 1,
|
||||||
@@ -35,16 +71,4 @@ exports = module.exports = {
|
|||||||
applyConfig: 9,
|
applyConfig: 9,
|
||||||
setLedPwm: 10
|
setLedPwm: 10
|
||||||
},
|
},
|
||||||
|
|
||||||
bufferToString: buffer => {
|
|
||||||
let str = '';
|
|
||||||
for (let i = 0; i < buffer.length; i++) {
|
|
||||||
let hex = buffer[i].toString(16) + ' ';
|
|
||||||
if (hex.length <= 2) {
|
|
||||||
hex = '0' + hex;
|
|
||||||
}
|
|
||||||
str += hex;
|
|
||||||
};
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
let uhk = require('./uhk');
|
let uhk = require('./uhk');
|
||||||
let [endpointIn, endpointOut] = uhk.getUsbEndpoints();
|
|
||||||
var arg = process.argv[2] || '';
|
var arg = process.argv[2] || '';
|
||||||
|
|
||||||
if (arg.length === 0) {
|
if (arg.length === 0) {
|
||||||
@@ -8,18 +7,4 @@ if (arg.length === 0) {
|
|||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
var payload = Buffer.concat([new Buffer([uhk.usbCommands.writeEeprom, arg.length+2, 0x00, 0x00]), new Buffer(arg, 'utf8')]);
|
uhk.sendUsbPacket(Buffer.concat([new Buffer([uhk.usbCommands.writeEeprom, arg.length+2, 0x00, 0x00]), new Buffer(arg, 'utf8')]))
|
||||||
console.log('Sending ', uhk.bufferToString(payload));
|
|
||||||
endpointOut.transfer(payload, function(err) {
|
|
||||||
if (err) {
|
|
||||||
console.error("USB error: %s", err);
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
endpointIn.transfer(64, function(err2, receivedBuffer) {
|
|
||||||
if (err2) {
|
|
||||||
console.error("USB error: %s", err2);
|
|
||||||
process.exit(2);
|
|
||||||
}
|
|
||||||
console.log('Received', uhk.bufferToString(receivedBuffer));
|
|
||||||
})
|
|
||||||
});
|
|
||||||
|
|||||||
Reference in New Issue
Block a user