Files

153 lines
5.3 KiB
JavaScript
Raw Permalink Normal View History

// 8BitDo Ultimate 2 Wireless through its 2.4 GHz dongle (USB 2DC8:310B).
// Protocol from 8BitDoAdvance.dll (Ultimate Software V2 1.30), disassembled:
// out report 0x81: [0x81, 0x04, u16 cmd, u16 arg, u32 len | crc16 << 16, u32 total, u32 offset, data]
// in report 0x02: [0x02, 0x04, u16 4, u16 cmd, ...]
// cmd 1 writes the config buffer. Without the commit command (cmd 6) it only changes the
// live lighting, so streaming doesn't wear anything. cmd 0x40 selects the lighting theme
// (4 = custom per-LED), cmd 0x41 reads it. LEDs: 0-11 left stick ring, 12-23 right stick
// ring, each starting at the bottom and going clockwise.
export function Name() { return "8BitDo Ultimate 2 Wireless"; }
export function VendorId() { return 0x2DC8; }
export function ProductId() { return 0x310B; }
export function Publisher() { return "Custom"; }
export function Size() { return [20, 13]; }
export function DefaultPosition() { return [180, 140]; }
export function DefaultScale() { return 6.0; }
export function DeviceType() { return "other"; }
export function Validate(endpoint) { return endpoint.interface === 2; }
export function ImageUrl() { return "https://assets.signalrgb.com/devices/default/misc/usb-drive-render.png"; }
/* global
LightingMode:readonly
forcedColor:readonly
shutdownMode:readonly
shutdownColor:readonly
*/
export function ControllableParameters() {
return [
{property:"LightingMode", group:"lighting", label:"Lighting Mode", description:"Canvas follows the active effect, Forced uses one fixed colour", type:"combobox", values:["Canvas", "Forced"], default:"Canvas"},
{property:"forcedColor", group:"lighting", label:"Forced Color", min:"0", max:"360", type:"color", default:"#009bde"},
{property:"shutdownMode", group:"lighting", label:"On Exit", description:"What the controller shows when SignalRGB closes", type:"combobox", values:["Controller's own lighting", "Shutdown Color"], default:"Controller's own lighting"},
{property:"shutdownColor", group:"lighting", label:"Shutdown Color", min:"0", max:"360", type:"color", default:"#000000"},
];
}
const RECORD_LEN = 0x638;
const SINGLE_OFFSET = 0x50C; // profile slot 0: u32 flag, then 24 x u32 0x00RRGGBB
const THEME_CUSTOM = 4;
const CHUNK = 45;
const vLedNames = [];
const vLedPositions = [];
for (const [side, cx, cy] of [["Left", 4, 4], ["Right", 15, 8]]) {
for (let k = 0; k < 12; k++) {
const a = (90 + k * 30) * Math.PI / 180; // bottom, then clockwise
vLedNames.push(`${side} Stick ${k + 1}`);
vLedPositions.push([Math.round(cx + 4 * Math.cos(a)), Math.round(cy + 4 * Math.sin(a))]);
}
}
let originalTheme = -1;
let lastFrame = null;
let lastSend = 0;
export function LedNames() { return vLedNames; }
export function LedPositions() { return vLedPositions; }
export function Initialize() {
device.set_endpoint(2, 0x0001, 0xFF7A, 0x0000);
device.setName("8BitDo Ultimate 2");
device.setControllableLeds(vLedNames, vLedPositions);
originalTheme = readTheme();
if (originalTheme !== THEME_CUSTOM) {
setTheme(THEME_CUSTOM);
}
}
export function Render() {
const colors = vLedPositions.map(([x, y]) =>
LightingMode === "Forced" ? hexToRgb(forcedColor) : device.color(x, y));
// Skip unchanged frames, but resend once a second in case the controller
// reconnected or woke up with its stored colours
const now = Date.now();
const key = colors.join();
if (key === lastFrame && now - lastSend < 1000) { return; }
sendColors(colors);
lastFrame = key;
lastSend = now;
}
export function Shutdown(SystemSuspending) {
if (shutdownMode === "Shutdown Color") {
const c = hexToRgb(shutdownColor);
sendColors(vLedPositions.map(() => c));
return;
}
// Back to the stored lighting: re-selecting a theme reloads it from storage
if (originalTheme >= 0 && originalTheme !== THEME_CUSTOM) {
setTheme(originalTheme);
} else {
setTheme(THEME_CUSTOM);
}
}
function sendColors(colors) {
const data = [];
for (const [r, g, b] of colors) { data.push(b, g, r, 0); } // u32 LE 0x00RRGGBB
for (let pos = 0; pos < data.length; pos += CHUNK) {
const chunk = data.slice(pos, pos + CHUNK);
send(1, 0, chunk.length, crc16(chunk), RECORD_LEN, SINGLE_OFFSET + 4 + pos, chunk);
readReply(1, 50);
}
}
function setTheme(theme) {
send(0x40, theme, 0, 0, 0, 0, []);
readReply(0x40, 1500);
}
function readTheme() {
send(0x41, 0, 0, 0, 0, 0, []);
const r = readReply(0x41, 500);
return r ? r[18] : -1;
}
function send(cmd, arg, len, crc, total, offset, data) {
const pkt = [0x81, 0x04, cmd & 0xFF, cmd >> 8, arg & 0xFF, arg >> 8,
len & 0xFF, len >> 8, crc & 0xFF, crc >> 8]
.concat(u32(total), u32(offset), data);
while (pkt.length < 64) { pkt.push(0); }
device.write(pkt, 64);
}
function readReply(cmd, timeoutMs) {
const end = Date.now() + timeoutMs;
do {
const r = device.read([0x02], 64, 20);
if (device.getLastReadSize() > 0 && r[0] === 0x02 && r[1] === 0x04 && r[2] === 4 && r[4] === (cmd & 0xFF)) {
return r;
}
} while (Date.now() < end);
return null;
}
function u32(v) { return [v & 0xFF, (v >> 8) & 0xFF, (v >> 16) & 0xFF, (v >>> 24) & 0xFF]; }
function crc16(data) {
let c = 0xFFFF;
for (const x of data) {
c ^= x;
for (let i = 0; i < 8; i++) { c = (c & 1) ? (c >>> 1) ^ 0xA001 : c >>> 1; }
}
return c;
}
function hexToRgb(hex) {
const r = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return [parseInt(r[1], 16), parseInt(r[2], 16), parseInt(r[3], 16)];
}