Created
December 13, 2019 02:37
-
-
Save alexanderson1993/5e280410dd10639555bf90538cf40812 to your computer and use it in GitHub Desktop.
Sending DMX messages to an ENTTEC Pro USB controller over WebUSB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function setUpLights() { | |
const lightingDevice = await navigator.usb.requestDevice({ filters: [] }); | |
await lightingDevice.open(); | |
await lightingDevice.claimInterface(0); | |
// This comes from https://github.com/NERDDISCO/webusb-dmx512-controller | |
lightingDevice.controlTransferOut({ | |
// It's a USB class request | |
requestType: "class", | |
// The destination of this request is the interface | |
recipient: "interface", | |
// CDC: Communication Device Class | |
// 0x22: SET_CONTROL_LINE_STATE | |
// RS-232 signal used to tell the USB device that the computer is now present. | |
request: 0x22, | |
// Yes | |
value: 0x01, | |
// Interface #2 | |
index: 0x00 | |
}); | |
// This comes from https://github.com/node-dmx/dmx | |
const universe = new Array(512).fill(0); | |
const ENTTEC_PRO_DMX_STARTCODE = 0x00; | |
const ENTTEC_PRO_START_OF_MSG = 0x7e; | |
const ENTTEC_PRO_END_OF_MSG = 0xe7; | |
const ENTTEC_PRO_SEND_DMX_RQ = 0x06; | |
const hdr = Uint8Array.from([ | |
ENTTEC_PRO_START_OF_MSG, | |
ENTTEC_PRO_SEND_DMX_RQ, | |
universe.length & 0xff, | |
(universe.length >> 8) & 0xff, | |
ENTTEC_PRO_DMX_STARTCODE | |
]); | |
// Send a message with a blank DMX universe. This turns off all the lights. | |
lightingDevice.transferOut( | |
2, | |
Uint8Array.from([...hdr, ...universe, ENTTEC_PRO_END_OF_MSG]) | |
); | |
let count = 0; | |
// This does a sine pulse. | |
const loop = delta => { | |
count = delta / 1000; | |
universe[2] = Math.round(Math.sin(count) * 127 + 128); | |
lightingDevice | |
.transferOut( | |
2, | |
Uint8Array.from([...hdr, ...universe, ENTTEC_PRO_END_OF_MSG]) | |
) | |
.then(() => { | |
window.frame = requestAnimationFrame(loop); | |
}); | |
}; | |
loop(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment