I am working on a project that need notification alert via browser using Blink(1) mk2 device. I have tried the following code for connection to the usb using WEBUSB API.
const VENDOR_ID = 0x27b8;
navigator.usb.requestDevice({
filters: [{
vendorId: VENDOR_ID
}]
}).then(selectedDevice => {
device = selectedDevice;
console.log("open")
var tOpen = device.open();
console.log("opened")
return tOpen;
}).then(() => {
console.log("selectConfiguration")
return device.selectConfiguration(1);
}).then(() => {
console.log("claimInterface")
return device.claimInterface(0);
}).then(() => {
console.log("controlTransferOut")
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;
var rgb = new Uint8Array(3);
rgb[0] = r;
rgb[1] = g;
rgb[2] = b;
return device.controlTransferOut({
requestType: 'standard',
recipient: 'interface',
request: 0x09,
value: 1,
index: 0
},data);
}).then(result => {
console.log(result);
}).catch(error => {
console.log(error);
});
I am able to connect to usb after setting the permissions popup. After that above code opens it, selectConfiguration, claimInterface also worked fine and when I call controlTransferOut it also sends out the command and return the result as:
USBOutTransferResult {bytesWritten: 8, status: "ok"}
But blinking or color change on the USB does not reflected.
Am I missing something or is there any other configuration I need to use in order to get the light up on USB?
I ran this code myself and found that when I set
requestType
to"standard"
I got a"stall"
but if I set it to"class"
(which is correct since this is the HID class control transfer SET_REPORT) then I get"ok"
and the color of the LED on my blink(1) mk2 changes.Note, the RGB values in your snippet above are set randomly so you may be getting a very dim color.