Files
rat8plus-rgb/MadCatz_RAT8Plus.js
T

212 lines
7.8 KiB
JavaScript
Raw Normal View History

// Mad Catz R.A.T. 8+ (USB 12CF:0C05), no vendor software needed.
// Live colours use a firmware command the vendor app never sends (found by disassembling
// firmware v18): [12, 0xC4, 3, 4, 1, zone, R, G, B] writes straight to the LED driver,
// with no storage writes, so it can run at full frame rate. The first such command pauses
// the mouse's own effect; [12, 0xC4, 3, 3, 3] hands the LEDs back to it.
// Profile commands (from the vendor app, RAT8P.dll) are only used for "Save to mouse".
export function Name() { return "Mad Catz R.A.T. 8+"; }
export function VendorId() { return 0x12CF; }
export function ProductId() { return 0x0C05; }
export function Publisher() { return "Custom"; }
export function Size() { return [3, 3]; }
export function DefaultPosition() { return [240, 120]; }
export function DefaultScale() { return 8.0; }
export function DeviceType() { return "mouse"; }
export function Validate(endpoint) { return endpoint.interface === 1; }
export function ImageUrl() { return "https://assets.signalrgb.com/devices/default/misc/usb-drive-render.png"; }
/* global
LightingMode:readonly
forcedColor:readonly
shutdownMode:readonly
shutdownColor:readonly
colorCorrection:readonly
whiteBalance:readonly
thumbBrightness: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:"colorCorrection", group:"lighting", label:"Colour Correction", description:"The LEDs respond linearly, so in-between colours look pale next to a screen. Higher values darken mid-tones and make colours richer", type:"combobox", values:["Off", "Light (1.8)", "Normal (2.2)", "Strong (2.8)"], default:"Normal (2.2)"},
{property:"whiteBalance", group:"lighting", label:"White Balance", description:"The scroll-wheel and logo LEDs have much weaker blue than the thumb-wheel LED, so colours there look pale and warm. Balanced trims their red and green to match (reds and greens get dimmer there)", type:"combobox", values:["Balanced", "Off"], default:"Balanced"},
{property:"thumbBrightness", group:"lighting", label:"Thumb Wheel Brightness", description:"With White Balance on, the scroll wheel and logo are dimmer, so turn the thumb-wheel light down until all three match", type:"number", min:"0", max:"100", default:"40"},
{property:"shutdownMode", group:"lighting", label:"On Exit", description:"What the mouse shows when SignalRGB closes. 'Save last colours' stores them in the active profile (one storage write), so they survive replugging", type:"combobox", values:["Mouse's own lighting", "Shutdown Color", "Save last colours to mouse"], default:"Mouse's own lighting"},
{property:"shutdownColor", group:"lighting", label:"Shutdown Color", min:"0", max:"360", type:"color", default:"#000000"},
];
}
// Zones: 0 = under the scroll wheel, 1 = palm logo, 2 = under the thumb wheel (left side)
const vLedNames = ["Scroll Wheel", "Thumb Wheel", "Palm Logo"];
const vLedPositions = [[1, 0], [0, 1], [1, 2]];
const vZoneForLed = [0, 2, 1];
const GAMMA = {"Off": 1, "Light (1.8)": 1.8, "Normal (2.2)": 2.2, "Strong (2.8)": 2.8};
// Per-zone R/G/B scale, measured by eye against the thumb-wheel LED (2026-11-19)
const ZONE_BALANCE = [[0.15, 0.10, 1], [0.15, 0.10, 1], [1, 1, 1]];
function corrected(c, zone) {
const g = GAMMA[colorCorrection] || 1;
let k = whiteBalance === "Off" ? [1, 1, 1] : ZONE_BALANCE[zone];
if (whiteBalance !== "Off" && zone === 2) {
const t = Math.min(100, Math.max(0, Number(thumbBrightness))) / 100;
k = k.map(v => v * (isNaN(t) ? 0.4 : t));
}
return c.map((v, i) => Math.round(255 * Math.pow(v / 255, g) * k[i]));
}
const FEAT_LEN = 2053;
const PROFILE_LEN = 733;
const LED_OFFSET = 160;
const LED_ZONE_LEN = 191;
const SLOT_CHECK_MS = 3000;
const RESEND_MS = 1000; // resend unchanged colours this often, in case the mouse reset them
let sent = [null, null, null];
let lastSend = 0;
let slot = -1;
let lastSlotCheck = 0;
export function LedNames() { return vLedNames; }
export function LedPositions() { return vLedPositions; }
export function Initialize() {
device.set_endpoint(1, 0x004B, 0x0008, 0x0003);
device.setName("Mad Catz R.A.T. 8+");
device.setControllableLeds(vLedNames, vLedPositions);
slot = readActiveSlot();
}
export function Render() {
const now = Date.now();
// A profile switch on the mouse reloads its own lighting; release and retake
// control so the firmware pauses its effect again
if (now - lastSlotCheck > SLOT_CHECK_MS) {
lastSlotCheck = now;
const s = readActiveSlot();
if (s >= 0 && s !== slot) {
slot = s;
releaseLeds();
sent = [null, null, null];
}
}
const colors = currentColors();
const resend = now - lastSend > RESEND_MS;
for (let z = 0; z < 3; z++) {
if (resend || !sameColor(colors[z], sent[z])) {
setZone(z, colors[z]);
}
}
if (resend) { lastSend = now; }
}
export function Shutdown(SystemSuspending) {
if (shutdownMode === "Shutdown Color") {
const c = hexToRgb(shutdownColor);
for (let z = 0; z < 3; z++) { setZone(z, c); }
return;
}
if (shutdownMode === "Save last colours to mouse" && sent[0]) {
saveToProfile(sent);
}
releaseLeds();
}
function setZone(zone, c) {
const o = corrected(c, zone);
device.send_report([12, 0xC4, 3, 4, 1, zone, o[0], o[1], o[2]], FEAT_LEN);
sent[zone] = c; // uncorrected, so unchanged canvas colours aren't resent
}
function releaseLeds() {
device.send_report([12, 0xC4, 3, 3, 3], FEAT_LEN);
}
function currentColors() {
const out = [null, null, null];
for (let i = 0; i < vLedPositions.length; i++) {
const [x, y] = vLedPositions[i];
out[vZoneForLed[i]] = LightingMode === "Forced" ? hexToRgb(forcedColor) : device.color(x, y);
}
return out;
}
function sameColor(a, b) {
return !!a && !!b && a[0] === b[0] && a[1] === b[1] && a[2] === b[2];
}
function readActiveSlot() {
device.send_report([12, 0xC4, 22], FEAT_LEN);
for (let i = 0; i < 10; i++) {
device.pause(10);
const r = device.get_report([12], FEAT_LEN);
if (r[0] === 0xB0 && r[1] === 0xC6) { return r[2]; }
}
return -1;
}
// One profile write: read the active profile, put static colours in, write it back
function saveToProfile(colors) {
const s = readActiveSlot();
if (s < 0) { return; }
device.send_report([12, 0xC4, 12, s], FEAT_LEN);
let profile = null;
for (let i = 0; i < 10 && !profile; i++) {
device.pause(50);
const r = device.get_report([5], FEAT_LEN);
if (r[0] === 5 && r[1] === 0xB0 && r[2] === s && r[3] !== 0 && r[3] === checksum(r, 4, 736)) {
profile = r.slice(4, 4 + PROFILE_LEN);
}
}
if (!profile) {
device.log(`R.A.T. 8+: could not read profile ${s}, colours not saved`);
return;
}
for (let z = 0; z < 3; z++) {
const o = LED_OFFSET + z * LED_ZONE_LEN;
profile[o] = 1; // static
profile[o + 1] = 255; // brightness
const c = corrected(colors[z], z);
profile[o + 2] = c[0];
profile[o + 3] = c[1];
profile[o + 4] = c[2];
}
device.send_report([5, 0, s, checksum(profile, 0, PROFILE_LEN)].concat(profile), FEAT_LEN);
for (let i = 0; i < 25; i++) {
device.pause(20);
const r = device.get_report([12], FEAT_LEN);
if (r[0] === 0xB0 && r[1] === 5 && r[2] === s && r[3] === r[4]) {
device.send_report([12, 0xC4, 0], FEAT_LEN); // reload
return;
}
}
device.log("R.A.T. 8+: mouse did not confirm the profile write");
}
function checksum(data, start, length) {
let sum = 0;
for (let i = start; i < start + length; i++) { sum = (sum + data[i]) & 0xFF; }
return (256 - sum) & 0xFF;
}
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)];
}