Expose usb.sendUsbPacketsByCallback() and use it to vastly simplify blink-test-led.js and led-pwm.js

This commit is contained in:
László Monda
2017-01-15 03:22:13 +01:00
parent e87fbb3c11
commit 73f682193a
3 changed files with 56 additions and 41 deletions

View File

@@ -1,25 +1,15 @@
#!/usr/bin/env node
'use strict';
let uhk = require('./uhk');
let [endpointIn, endpointOut] = uhk.getUsbEndpoints();
var state = 1;
let delayCycle = true;
let areLedsEnabled = true;
setInterval(function() {
state = state ? 0 : 1
console.log('Sending ', state);
endpointOut.transfer(new Buffer([uhk.usbCommands.setTestLed, state]), 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));
})
});
}, 500)
uhk.sendUsbPacketsByCallback(() => {
delayCycle = !delayCycle;
if (delayCycle) {
return new uhk.DelayMs(500);
} else {
areLedsEnabled = !areLedsEnabled;
return new Buffer([uhk.usbCommands.setTestLed, areLedsEnabled ? 1 : 0]);
}
});