I just started looking into webusb and trying to use it to turn on a blink(1) mk2. I am able to discover the device, open it, claim an interface and call controlTransferOut
. The trouble I am now having is not knowing what data I should be sending it to cause it to flash or light up.
I have been using this example where someone has been able to control it using a Chrome extension using the chrome.usb
interface as inspiration in trying to get it to work. I have written the following code:
const VENDOR_ID = 0x27b8;
navigator.usb.requestDevice({
filters: [{
vendorId: VENDOR_ID
}]
}).then(selectedDevice => {
device = selectedDevice;
return device.open();
}).then(() => {
return device.selectConfiguration(1);
}).then(() => {
return device.claimInterface(0);
}).then(() => {
return device.controlTransferOut({
requestType: 'class',
recipient: 'interface',
request: 0x09,
value: 1,
index: 0
});
}).then(() => {
const r = Math.floor((Math.random() * 255) + 0);
const g = Math.floor((Math.random() * 255) + 0);
const b = Math.floor((Math.random() * 255) + 0);
// not entirely sure what is going on below...
const fadeMillis = 500;
const th = (fadeMillis / 10) >> 8;
const tl = (fadeMillis / 10) & 0xff;
const data = new Uint8Array([0x01, 0x63, r, g, b, th, tl, 0x00, 0x00]).buffer;
return device.transferIn(1, data);
}).then(result => {
console.log(result);
}).catch(error => {
console.log(error);
});
This fails with a transfer error when calling controlTransferOut
. However, if I change the requestType
to standard, it then goes on to fail when calling transferIn
.
How can I find out what data and what format the data needs to be in to get this to work correctly?