/** * Angry Miao Infinity .100 mouse (model AM30) on the Yichip (VID 0x3151) block * protocol. This codec owns everything the Angry Miao driver * (`src/drivers/angry-miao/`) sends; it does not share constants with the * other Yichip brands, so their changes cannot move this mouse. * * Sources: replies read over the USB cable and the vendor configurator * (ammaster.angrymiao.com, model page am30). See * docs/angry-miao-infinity.md. */ /** Yichip's vendor id. Other brands ship on it, so it never identifies this mouse alone. */ export const ANGRY_MIAO_VENDOR_ID = 0x3151; /** Product names: cable "Angry Miao Infinity", receiver "AM INFINITY 8K MOUSE", Bluetooth "AM INFINITY BT MOUSE". */ export const ANGRY_MIAO_PRODUCT_NAME = /angry\s*miao|\bAM\s+INFINITY\b/i; /** One unnumbered 64-byte feature report on usage page 0xFFFF, usage 0x02. */ export const AM30_REPORT_ID = 0; export const AM30_REPORT_SIZE = 64; export const AM30_USAGE_PAGE = 0xffff; export const AM30_USAGE = 0x02; /** Bluetooth: the same commands go out and come back as report 6 on 0xFF55:0x0202, after a `55` byte. */ export const AM30_BT_USAGE_PAGE = 0xff55; export const AM30_BT_USAGE = 0x0202; export const AM30_BT_REPORT_ID = 6; export const AM30_BT_REPORT_SIZE = 65; export const AM30_BT_ENVELOPE = 0x55; /** `77` asks for the battery (byte 1, percent); a reply of `88` means the mouse is asleep. */ export const AM30_BT_BATTERY = 0x77; export const AM30_BT_SLEEPING = 0x88; export function am30BluetoothPacket(encoded: Uint8Array): Uint8Array { const packet = new Uint8Array(AM30_BT_REPORT_SIZE); packet[0] = AM30_BT_ENVELOPE; packet.set(encoded.subarray(0, AM30_BT_REPORT_SIZE - 1), 1); return packet; } /** Mouse commands. Blocks are read whole and written back whole. */ export const AM30_CMD = { GET_FIRMWARE: 0x80, GET_DEVICE_ID: 0x8f, GET_OPTIONS: 0xd3, SET_OPTIONS: 0x53, GET_DPI: 0xd4, SET_DPI: 0x54, GET_KEYMATRIX: 0xd0, SET_KEYMATRIX: 0x50, } as const; /** Receiver relay commands. Raw: no checksum. */ export const AM30_RELAY = { SELECT_TARGET: 0xf6, GET_STATUS: 0xf7, NOTICE_READ: 0xfc, SET_LENGTH: 0xfe, } as const; export const AM30_TARGET_MOUSE = 0x05; /** `F6 00` releases the receiver after a relay fails, as the vendor app does. */ export const AM30_TARGET_NONE = 0x00; /** The vendor app tries a relayed command this many times, 100 ms apart. */ export const AM30_RELAY_ATTEMPTS = 3; /** The vendor app sends `FE 40` before every relayed command. */ export const AM30_RELAY_LENGTH = 0x40; /** Pad to 9 bytes; byte 7 is 255 minus the sum of bytes 0-6. */ export function am30EncodeCommand(bytes: readonly number[]): Uint8Array { const out = new Uint8Array(Math.max(9, bytes.length)); out.set(bytes); let sum = 0; for (let i = 0; i < 7; i++) sum = (sum + out[i]) & 0xff; out[7] = (255 - sum) & 0xff; return out; } /** Options block (`D3` / `53`) byte offsets. */ export const AM30_OPT0_REPORT_RATE = 9; export const AM30_OPT0_DEBOUNCE = 10; export const AM30_OPT0_FLAGS = 12; export const AM30_OPT0_SLEEP_BT = 40; export const AM30_OPT0_SLEEP_24G = 44; export const AM30_OPT0_LIFT_OFF = 52; export const AM30_OPT0_ANGLE_SNAP = 53; /** Report rate byte: a code, not an index. */ export const AM30_RATE_ENCODE: Readonly> = { 125: 8, 250: 4, 500: 2, 1000: 1, 2000: 132, 4000: 130, 8000: 129, }; export const AM30_RATE_DECODE: Readonly> = Object.fromEntries( Object.entries(AM30_RATE_ENCODE).map(([hz, code]) => [code, Number(hz)]), ); export const AM30_RATES: readonly number[] = Object.keys(AM30_RATE_ENCODE).map(Number).sort((a, b) => a - b); /** Lift-off byte 52: 0, 1, 2. */ export const AM30_LIFT_OFF_LEVELS = ["Low", "Medium", "High"] as const; export type Am30LiftOff = (typeof AM30_LIFT_OFF_LEVELS)[number]; /** DPI block (`D4` / `54`): byte 2 active stage, byte 3 count, then X, Y and RGB per stage. */ export const AM30_MAX_DPI_STAGES = 8; export const AM30_DPI_X_OFFSET = 8; export const AM30_DPI_Y_OFFSET = 24; export const AM30_DPI_RGB_OFFSET = 40; /** OPTIONPARAM0 debounce bytes for the 2.4 GHz and Bluetooth links. */ export const AM30_OPT0_DEBOUNCE_24G = 61; export const AM30_OPT0_DEBOUNCE_BT = 62; /** Ripple control on is byte 12 = 1 (captured from the vendor app). */ export const AM30_OPT0_FLAG_RIPPLE = 1 << 0; /** Motion sync, 1 = on. */ export const AM30_OPT0_MOTION_SYNC = 63; /** Mouse light: on/off switch and two RGB slots, which the vendor panel sets to one colour. */ export const AM30_OPT0_LIGHT_COLOR = 54; export const AM30_OPT0_LIGHT_COLOR_2 = 57; export const AM30_OPT0_LIGHT = 60; /** LE16 standby value meaning "never". */ export const AM30_SLEEP_NEVER = 0xffff; /** Standby stops in seconds, 0 = never (the vendor presets). */ export const AM30_SLEEP_OPTIONS: readonly number[] = [0, 15, 30, 60, 600, 1800, 3600]; /** The vendor app's one debounce slider, by gear index (30..0 ms). */ export const AM30_DEBOUNCE_GEARS_MS: readonly number[] = [30, 25, 20, 15, 10, 5, 0]; const LOW_RATE_DEBOUNCE = [10, 8, 6, 4, 3, 2, 0] as const; /** USB and 2.4 GHz raw debounce by gear, per report rate. Bluetooth always uses the 1000 Hz list. */ export const AM30_DEBOUNCE_RAW: Readonly> = { 125: LOW_RATE_DEBOUNCE, 250: LOW_RATE_DEBOUNCE, 500: LOW_RATE_DEBOUNCE, 1000: LOW_RATE_DEBOUNCE, 2000: [20, 16, 12, 8, 4, 2, 0], 4000: [39, 31, 23, 15, 7, 3, 0], 8000: [79, 63, 47, 31, 15, 7, 0], }; function debounceRawList(rateHz: number | undefined): readonly number[] { return (rateHz !== undefined && AM30_DEBOUNCE_RAW[rateHz]) || LOW_RATE_DEBOUNCE; } /** Gear index for a raw USB debounce byte: exact match, else the nearest. */ export function am30DebounceGear(rateHz: number | undefined, raw: number): number { const list = debounceRawList(rateHz); const exact = list.indexOf(raw); if (exact >= 0) return exact; const nearest = Math.min(...list.map((value) => Math.abs(value - raw))); return list.findIndex((value) => Math.abs(value - raw) === nearest); } /** Raw bytes for one gear at a report rate: USB, 2.4 GHz and Bluetooth. */ export function am30DebounceRaw( rateHz: number | undefined, gear: number, ): { usb: number; rf: number; bt: number } { const list = debounceRawList(rateHz); return { usb: list[gear], rf: list[gear], bt: LOW_RATE_DEBOUNCE[gear] }; } /** Battery read: byte 1 = charge state, byte 2 = percent valid, byte 3 = percent. */ export const AM30_GET_BATTERY = 0xd6; /** Blast Mode (sensor at its top frame rate): read byte 1, write `62 <0|1>`. */ export const AM30_GET_BLAST_MODE = 0xe2; export const AM30_SET_BLAST_MODE = 0x62; /** Onboard profiles: read `85` byte 1 (active, 0-based), switch with `05 `. */ export const AM30_GET_PROFILE = 0x85; export const AM30_SET_PROFILE = 0x05; export const AM30_PROFILE_COUNT = 2; /** USB firmware read (`AD`): bytes 1-2, LE16. The SoC version is the shared `80` read. */ export const AM30_GET_USB_VERSION = 0xad; const hexCode = (code: number) => `0x${code.toString(16).padStart(4, "0")}`; /** * Raw firmware codes. The vendor app's names for them (SoC 0x0318 = 1.1.1, * package 1.6) come from a table on its server, so they are not repeated here. */ export function am30FirmwareLabels(usbCode: number | null, socCode: number | null): string[] { return [ ...(socCode === null ? [] : [`Mouse SoC ${hexCode(socCode)}`]), ...(usbCode === null ? [] : [`Mouse USB ${hexCode(usbCode)}`]), ]; } /** Receiver firmware reads, sent to the receiver itself. The vendor app reads these instead of `AD` on 2.4 GHz. */ export const AM30_RECEIVER_GET_USB_VERSION = 0xf0; export const AM30_RECEIVER_GET_RF_VERSION = 0xfb; export const AM30_RECEIVER_GET_LED_VERSION = 0xae; /** `F0` and `AE` hold LE16 codes at bytes 1-2; `FB` does not echo and holds the RF code in byte 3. */ export function am30ReceiverFirmwareLabels( usb: Uint8Array | null, rf: Uint8Array | null, led: Uint8Array | null, ): string[] { const le16 = (reply: Uint8Array) => reply[1] | (reply[2] << 8); return [ ...(usb ? [`Receiver USB ${hexCode(le16(usb))}`] : []), ...(rf ? [`Receiver RF 0x${rf[3].toString(16).padStart(2, "0")}`] : []), ...(led ? [`Receiver LED ${hexCode(le16(led))}`] : []), ]; } export type Am30ChargeState = "Discharging" | "Charging" | "Full" | "Unknown"; /** Receiver status (`F7`) byte 4: 0 while the mouse is linked. */ export const AM30_STATUS_MOUSE_OFFLINE = 4; /** Receiver status (`F7`) byte 10: the spare battery in the dock, 1-100; 0 = none. */ export const AM30_STATUS_SPARE_BATTERY = 10; /** As the vendor app reads it: 100 is full, below that the dock is charging it. */ export function am30DecodeSpareBattery(status: Uint8Array): { percent: number; state: "Charging" | "Full" } | null { const percent = status[AM30_STATUS_SPARE_BATTERY]; if (!percent || percent > 100) return null; return { percent, state: percent === 100 ? "Full" : "Charging" }; } /** Byte 1 is 0 on battery, 1 charging, 2 full, as the vendor app reads it. */ export function am30DecodeBattery(reply: Uint8Array): { percent: number | null; state: Am30ChargeState } { const percent = reply[2] !== 0 && reply[3] <= 100 ? reply[3] : null; const state = (["Discharging", "Charging", "Full"] as const)[reply[1]] ?? "Unknown"; return { percent, state }; } /** * Receiver light, sent to the receiver itself (never relayed). Read `88` / * write `08`: effect, speed, brightness, colour option, R, G, B. Read `87` / * write `07`: on/off. */ export const AM30_RECEIVER_GET_LIGHT = 0x88; export const AM30_RECEIVER_SET_LIGHT = 0x08; export const AM30_RECEIVER_GET_LIGHT_SWITCH = 0x87; export const AM30_RECEIVER_SET_LIGHT_SWITCH = 0x07; /** Effects 1..8: always on, breathing, neon, light wave, marquee, ring, buffer, chase. */ export const AM30_RECEIVER_EFFECT_COUNT = 8; export const AM30_RECEIVER_SPEEDS: readonly number[] = [1, 2, 3, 4, 5]; export const AM30_RECEIVER_BRIGHTNESS: readonly number[] = [0, 25, 50, 75, 100]; /** Colour option 7 = custom RGB; 0..6 pick these presets. */ const RECEIVER_CUSTOM_COLOR = 7; const RECEIVER_PRESETS = ["#ff0000", "#ff8800", "#ffff00", "#00ff00", "#00ffff", "#0000ff", "#8800ff"]; export interface Am30ReceiverLight { enabled: boolean; /** 1..8, see `AM30_RECEIVER_EFFECT_COUNT`. */ effect: number; /** The vendor slider, 1..5; the wire stores 4..0. */ speed: number; /** Percent in 25 % steps; the wire stores 0..4. */ brightness: number; color: string; } const hex = (bytes: ArrayLike) => `#${Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("")}`; export function am30DecodeReceiverLight(light: Uint8Array, power: Uint8Array): Am30ReceiverLight { return { enabled: power[1] === 1, effect: Math.max(1, Math.min(AM30_RECEIVER_EFFECT_COUNT, light[1])), speed: 5 - Math.min(4, light[2]), brightness: Math.min(4, light[3]) * 25, color: RECEIVER_PRESETS[light[4]] ?? hex(light.subarray(5, 8)), }; } /** The 64-byte `08` write. Its checksum is in byte 8, after the seven fields. */ export function am30EncodeReceiverLight(light: Omit): Uint8Array { const { effect, speed, brightness, color } = light; if (!Number.isInteger(effect) || effect < 1 || effect > AM30_RECEIVER_EFFECT_COUNT) { throw new Error(`Receiver light effect must be 1..${AM30_RECEIVER_EFFECT_COUNT}.`); } if (!AM30_RECEIVER_SPEEDS.includes(speed)) throw new Error("Receiver light speed must be 1..5."); if (!AM30_RECEIVER_BRIGHTNESS.includes(brightness)) throw new Error("Receiver light brightness must be 0..100 in 25 % steps."); const match = /^#?([0-9a-f]{6})$/i.exec(color); if (!match) throw new Error(`Invalid receiver light colour: ${color}.`); const rgb = parseInt(match[1], 16); // The vendor app writes speed 0 for always on. const fields = [ AM30_RECEIVER_SET_LIGHT, effect, effect === 1 ? 0 : 5 - speed, brightness / 25, RECEIVER_CUSTOM_COLOR, (rgb >> 16) & 0xff, (rgb >> 8) & 0xff, rgb & 0xff, ]; const out = new Uint8Array(64); out.set(fields); out[8] = (255 - (fields.reduce((sum, b) => sum + b, 0) & 0xff)) & 0xff; return out; } /** * Key matrix actions from the vendor app (`D0` / `50`), label to 4-byte value. * Scroll Up/Down are the factory values of slots 14 and 15. */ export const AM30_BUTTON_ACTIONS: ReadonlyArray = [ ["Left Click", [0x01, 0, 0xf0, 0]], ["Right Click", [0x01, 0, 0xf1, 0]], ["Middle Click", [0x01, 0, 0xf2, 0]], ["Back", [0x01, 0, 0xf3, 0]], ["Forward", [0x01, 0, 0xf4, 0]], ["Scroll Up", [0x01, 0, 0xf5, 0x01]], ["Scroll Down", [0x01, 0, 0xf5, 0xff]], ["DPI Loop", [0x14, 0, 0x00, 0]], ["DPI +", [0x14, 0, 0x01, 0]], ["DPI -", [0x14, 0, 0x02, 0]], ["Volume Up", [0x03, 0, 0xe9, 0]], ["Volume Down", [0x03, 0, 0xea, 0]], ["Mute", [0x03, 0, 0xe2, 0]], ["Play/Pause", [0x03, 0, 0xcd, 0]], ["Disabled", [0, 0, 0, 0]], ]; /** Action label for a 4-byte slot; "Custom" for keys, combinations and macros. */ export function am30DecodeButtonAction(bytes: ArrayLike): string { const match = AM30_BUTTON_ACTIONS.find(([, value]) => value.every((b, i) => b === bytes[i])); return match?.[0] ?? "Custom"; } export function am30EncodeButtonAction(label: string): readonly [number, number, number, number] | null { return AM30_BUTTON_ACTIONS.find(([l]) => l === label)?.[1] ?? null; } /** Factory assignments, from the first cable read of the key matrix. */ export const AM30_DEFAULT_BUTTON_MAPPINGS: Readonly> = { Left: "Left Click", Right: "Right Click", Middle: "Middle Click", Back: "Back", Forward: "Forward", "Scroll Up": "Scroll Up", "Scroll Down": "Scroll Down", }; /** A Yichip-protocol Angry Miao model, keyed by its `8F` device id. */ export interface AngryMiaoProfile { deviceId: number; brand: "Angry Miao"; model: string; sensor: string; maxPollingHz: number; minDpi: number; maxDpi: number; dpiStep: number; /** Key matrix slot per physical button. */ buttons: ReadonlyArray<{ name: string; slot: number }>; } /** * Device id 2222 (0x08AE) and firmware bytes 0x0318 were read over the USB * cable (0x3151:0x402A). PAW3950 and 50-30000 DPI are from the retail listing * and the AM30 manual; the top stage reads back 30000. */ export const ANGRY_MIAO_INFINITY_PROFILE: AngryMiaoProfile = { deviceId: 2222, brand: "Angry Miao", model: "Infinity .100", sensor: "PixArt PAW3950", maxPollingHz: 8000, minDpi: 50, maxDpi: 30000, dpiStep: 50, // Slot 3 is Forward and slot 4 is Back on this mouse. Slot 5 reads DPI Loop, // but the mouse has no DPI button, so it is not offered. Slots 12 and 13 // hold a second wheel pair the vendor app does not show. buttons: [ { name: "Left", slot: 0 }, { name: "Right", slot: 1 }, { name: "Middle", slot: 2 }, { name: "Back", slot: 4 }, { name: "Forward", slot: 3 }, { name: "Scroll Up", slot: 14 }, { name: "Scroll Down", slot: 15 }, ], }; export const ANGRY_MIAO_PROFILES: ReadonlyMap = new Map([ [ANGRY_MIAO_INFINITY_PROFILE.deviceId, ANGRY_MIAO_INFINITY_PROFILE], ]); export interface AngryMiaoTransport { transport: "direct" | "dongle" | "bluetooth"; /** The settings collection on this link. */ usagePage: number; usage: number; } /** Transport by product id. The receiver relays every mouse command; Bluetooth wraps them in report 6. */ export const ANGRY_MIAO_PRODUCTS: ReadonlyMap = new Map([ [0x402a, { transport: "direct", usagePage: AM30_USAGE_PAGE, usage: AM30_USAGE }], [0x5007, { transport: "dongle", usagePage: AM30_USAGE_PAGE, usage: AM30_USAGE }], [0x402c, { transport: "bluetooth", usagePage: AM30_BT_USAGE_PAGE, usage: AM30_BT_USAGE }], ]);