@openmouse/protocol 0.49.0 → 0.51.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (175) hide show
  1. package/README.md +2 -0
  2. package/dist/amkette/index.d.ts +2 -0
  3. package/dist/amkette/index.d.ts.map +1 -0
  4. package/dist/amkette/index.js +2 -0
  5. package/dist/amkette/index.js.map +1 -0
  6. package/dist/amkette/xs.d.ts +63 -0
  7. package/dist/amkette/xs.d.ts.map +1 -0
  8. package/dist/amkette/xs.js +79 -0
  9. package/dist/amkette/xs.js.map +1 -0
  10. package/dist/corsair/bragi.d.ts +70 -12
  11. package/dist/corsair/bragi.d.ts.map +1 -1
  12. package/dist/corsair/bragi.js +85 -16
  13. package/dist/corsair/bragi.js.map +1 -1
  14. package/dist/drivers/amkette/xs-hid.d.ts +142 -0
  15. package/dist/drivers/amkette/xs-hid.d.ts.map +1 -0
  16. package/dist/drivers/amkette/xs-hid.js +683 -0
  17. package/dist/drivers/amkette/xs-hid.js.map +1 -0
  18. package/dist/drivers/atk/hid.d.ts.map +1 -1
  19. package/dist/drivers/atk/hid.js +2 -2
  20. package/dist/drivers/atk/hid.js.map +1 -1
  21. package/dist/drivers/atk/products.d.ts +7 -0
  22. package/dist/drivers/atk/products.d.ts.map +1 -1
  23. package/dist/drivers/atk/products.js +19 -2
  24. package/dist/drivers/atk/products.js.map +1 -1
  25. package/dist/drivers/corsair/bragi-hid.d.ts +17 -12
  26. package/dist/drivers/corsair/bragi-hid.d.ts.map +1 -1
  27. package/dist/drivers/corsair/bragi-hid.js +35 -15
  28. package/dist/drivers/corsair/bragi-hid.js.map +1 -1
  29. package/dist/drivers/gwolves/products.d.ts +5 -0
  30. package/dist/drivers/gwolves/products.d.ts.map +1 -1
  31. package/dist/drivers/gwolves/products.js +17 -10
  32. package/dist/drivers/gwolves/products.js.map +1 -1
  33. package/dist/drivers/gwolves/xvi-hid.d.ts +1 -0
  34. package/dist/drivers/gwolves/xvi-hid.d.ts.map +1 -1
  35. package/dist/drivers/gwolves/xvi-hid.js +14 -7
  36. package/dist/drivers/gwolves/xvi-hid.js.map +1 -1
  37. package/dist/drivers/hitscan/xvi-hid.d.ts +110 -0
  38. package/dist/drivers/hitscan/xvi-hid.d.ts.map +1 -0
  39. package/dist/drivers/hitscan/xvi-hid.js +803 -0
  40. package/dist/drivers/hitscan/xvi-hid.js.map +1 -0
  41. package/dist/drivers/incott/hid.d.ts +5 -0
  42. package/dist/drivers/incott/hid.d.ts.map +1 -1
  43. package/dist/drivers/incott/hid.js +18 -7
  44. package/dist/drivers/incott/hid.js.map +1 -1
  45. package/dist/drivers/lamzu/hid.d.ts +4 -0
  46. package/dist/drivers/lamzu/hid.d.ts.map +1 -1
  47. package/dist/drivers/lamzu/hid.js +41 -2
  48. package/dist/drivers/lamzu/hid.js.map +1 -1
  49. package/dist/drivers/lamzu-atlantis/hid.d.ts.map +1 -1
  50. package/dist/drivers/lamzu-atlantis/hid.js +2 -3
  51. package/dist/drivers/lamzu-atlantis/hid.js.map +1 -1
  52. package/dist/drivers/logitech/hidpp.d.ts +2 -0
  53. package/dist/drivers/logitech/hidpp.d.ts.map +1 -1
  54. package/dist/drivers/logitech/hidpp.js +28 -13
  55. package/dist/drivers/logitech/hidpp.js.map +1 -1
  56. package/dist/drivers/mchose/hid.d.ts +9 -0
  57. package/dist/drivers/mchose/hid.d.ts.map +1 -1
  58. package/dist/drivers/mchose/hid.js +36 -5
  59. package/dist/drivers/mchose/hid.js.map +1 -1
  60. package/dist/drivers/motospeed/hid.js +1 -1
  61. package/dist/drivers/motospeed/hid.js.map +1 -1
  62. package/dist/drivers/mouse-types.d.ts +17 -1
  63. package/dist/drivers/mouse-types.d.ts.map +1 -1
  64. package/dist/drivers/ninjutso/hid.d.ts +1 -0
  65. package/dist/drivers/ninjutso/hid.d.ts.map +1 -1
  66. package/dist/drivers/ninjutso/hid.js +18 -11
  67. package/dist/drivers/ninjutso/hid.js.map +1 -1
  68. package/dist/drivers/pulsar/pulsar-hid.js +1 -1
  69. package/dist/drivers/pulsar/pulsar-hid.js.map +1 -1
  70. package/dist/drivers/razer/hid.d.ts +0 -1
  71. package/dist/drivers/razer/hid.d.ts.map +1 -1
  72. package/dist/drivers/razer/hid.js +14 -9
  73. package/dist/drivers/razer/hid.js.map +1 -1
  74. package/dist/drivers/redragon/hid.d.ts +44 -63
  75. package/dist/drivers/redragon/hid.d.ts.map +1 -1
  76. package/dist/drivers/redragon/hid.js +207 -270
  77. package/dist/drivers/redragon/hid.js.map +1 -1
  78. package/dist/drivers/registry.d.ts +2 -1
  79. package/dist/drivers/registry.d.ts.map +1 -1
  80. package/dist/drivers/registry.js +12 -1
  81. package/dist/drivers/registry.js.map +1 -1
  82. package/dist/drivers/ryunix/kyu-pro-mx1-hid.d.ts.map +1 -1
  83. package/dist/drivers/ryunix/kyu-pro-mx1-hid.js +4 -1
  84. package/dist/drivers/ryunix/kyu-pro-mx1-hid.js.map +1 -1
  85. package/dist/drivers/scyrox/hid.d.ts +5 -0
  86. package/dist/drivers/scyrox/hid.d.ts.map +1 -1
  87. package/dist/drivers/scyrox/hid.js +5 -0
  88. package/dist/drivers/scyrox/hid.js.map +1 -1
  89. package/dist/drivers/vendors.d.ts +8 -3
  90. package/dist/drivers/vendors.d.ts.map +1 -1
  91. package/dist/drivers/vendors.js +41 -13
  92. package/dist/drivers/vendors.js.map +1 -1
  93. package/dist/gearhub/index.d.ts +8 -0
  94. package/dist/gearhub/index.d.ts.map +1 -1
  95. package/dist/gearhub/index.js +18 -0
  96. package/dist/gearhub/index.js.map +1 -1
  97. package/dist/hitscan/index.d.ts +2 -0
  98. package/dist/hitscan/index.d.ts.map +1 -1
  99. package/dist/hitscan/index.js +2 -0
  100. package/dist/hitscan/index.js.map +1 -1
  101. package/dist/hitscan/xvi-inputs.d.ts +81 -0
  102. package/dist/hitscan/xvi-inputs.d.ts.map +1 -0
  103. package/dist/hitscan/xvi-inputs.js +393 -0
  104. package/dist/hitscan/xvi-inputs.js.map +1 -0
  105. package/dist/hitscan/xvi.d.ts +278 -0
  106. package/dist/hitscan/xvi.d.ts.map +1 -0
  107. package/dist/hitscan/xvi.js +773 -0
  108. package/dist/hitscan/xvi.js.map +1 -0
  109. package/dist/incott/index.d.ts +33 -8
  110. package/dist/incott/index.d.ts.map +1 -1
  111. package/dist/incott/index.js +44 -11
  112. package/dist/incott/index.js.map +1 -1
  113. package/dist/index.d.ts +1 -0
  114. package/dist/index.d.ts.map +1 -1
  115. package/dist/index.js +1 -0
  116. package/dist/index.js.map +1 -1
  117. package/dist/keychron/index.d.ts.map +1 -1
  118. package/dist/keychron/index.js +4 -0
  119. package/dist/keychron/index.js.map +1 -1
  120. package/dist/lamzu/atlantis.d.ts +18 -9
  121. package/dist/lamzu/atlantis.d.ts.map +1 -1
  122. package/dist/lamzu/atlantis.js +21 -6
  123. package/dist/lamzu/atlantis.js.map +1 -1
  124. package/dist/lamzu/index.d.ts +8 -5
  125. package/dist/lamzu/index.d.ts.map +1 -1
  126. package/dist/lamzu/index.js +10 -5
  127. package/dist/lamzu/index.js.map +1 -1
  128. package/dist/mchose/index.d.ts +14 -0
  129. package/dist/mchose/index.d.ts.map +1 -1
  130. package/dist/mchose/index.js +25 -2
  131. package/dist/mchose/index.js.map +1 -1
  132. package/dist/ninjutso/index.d.ts.map +1 -1
  133. package/dist/ninjutso/index.js +5 -2
  134. package/dist/ninjutso/index.js.map +1 -1
  135. package/dist/razer/codec.d.ts +3 -2
  136. package/dist/razer/codec.d.ts.map +1 -1
  137. package/dist/razer/codec.js +5 -4
  138. package/dist/razer/codec.js.map +1 -1
  139. package/dist/redragon/deprecated.d.ts +24 -0
  140. package/dist/redragon/deprecated.d.ts.map +1 -0
  141. package/dist/redragon/deprecated.js +54 -0
  142. package/dist/redragon/deprecated.js.map +1 -0
  143. package/dist/redragon/holtek.d.ts +127 -0
  144. package/dist/redragon/holtek.d.ts.map +1 -0
  145. package/dist/redragon/holtek.js +232 -0
  146. package/dist/redragon/holtek.js.map +1 -0
  147. package/dist/redragon/index.d.ts +9 -203
  148. package/dist/redragon/index.d.ts.map +1 -1
  149. package/dist/redragon/index.js +11 -430
  150. package/dist/redragon/index.js.map +1 -1
  151. package/dist/redragon/m612.d.ts +82 -0
  152. package/dist/redragon/m612.d.ts.map +1 -0
  153. package/dist/redragon/m612.js +166 -0
  154. package/dist/redragon/m612.js.map +1 -0
  155. package/dist/redragon/m724.d.ts +47 -0
  156. package/dist/redragon/m724.d.ts.map +1 -0
  157. package/dist/redragon/m724.js +107 -0
  158. package/dist/redragon/m724.js.map +1 -0
  159. package/dist/ryunix/index.d.ts +1 -0
  160. package/dist/ryunix/index.d.ts.map +1 -1
  161. package/dist/ryunix/index.js +1 -0
  162. package/dist/ryunix/index.js.map +1 -1
  163. package/dist/ryunix/kyu-pro-mx1.d.ts +10 -1
  164. package/dist/ryunix/kyu-pro-mx1.d.ts.map +1 -1
  165. package/dist/ryunix/kyu-pro-mx1.js +10 -12
  166. package/dist/ryunix/kyu-pro-mx1.js.map +1 -1
  167. package/dist/ryunix/protocol.d.ts +204 -0
  168. package/dist/ryunix/protocol.d.ts.map +1 -0
  169. package/dist/ryunix/protocol.js +366 -0
  170. package/dist/ryunix/protocol.js.map +1 -0
  171. package/dist/scyrox/index.d.ts +5 -0
  172. package/dist/scyrox/index.d.ts.map +1 -1
  173. package/dist/scyrox/index.js +37 -1
  174. package/dist/scyrox/index.js.map +1 -1
  175. package/package.json +6 -1
@@ -1,42 +1,55 @@
1
- import { REDRAGON_COMMIT_CODES, REDRAGON_CONFIG_USAGE, REDRAGON_CONFIG_USAGE_PAGE, REDRAGON_M612_ACTIVE_PROFILE_ADDRESS, REDRAGON_M612_BUTTON_LENGTH, REDRAGON_M612_BUTTON_OFFSETS, REDRAGON_M612_BUTTON_OPTIONS, REDRAGON_M612_DPI_LABELS, REDRAGON_M612_EFFECTS, REDRAGON_M612_LIGHTING_BLOCK_LENGTH, REDRAGON_M612_LIGHTING_EFFECT_ADDRESS, REDRAGON_M612_LIGHTING_SPEEDS, REDRAGON_M612_POLL_ADDRESS, REDRAGON_M612_POLL_LENGTH, REDRAGON_M612_PRODUCT_ID, REDRAGON_M612_PROFILE_COUNT, REDRAGON_M612_PROFILE_SELECT_CODE, REDRAGON_M612_PROFILE_BASES, REDRAGON_M612_SLOT_LENGTH, REDRAGON_M612_STAGE_COUNT, REDRAGON_POLLING_CODES, REDRAGON_POLLING_RATES, REDRAGON_PRODUCTS, REDRAGON_PRODUCT_IDS, REDRAGON_PROFILE0, REDRAGON_PROFILE0_DPI_SUBCMDS, REDRAGON_REPORT_ID, REDRAGON_VENDOR_ID, redragonDecodeRead, redragonEncodeCommit, redragonEncodeDpiSlot, redragonEncodePollingRate, redragonEncodeRead, redragonEncodeWrite, redragonHello, redragonM612ButtonAddress, redragonM612DecodeButtonAction, redragonM612DecodeDpi, redragonM612EffectForBit, redragonM612EncodeButtonAction, redragonM612EncodeDpi, redragonM612SlotAddress, redragonSession, } from "@openmouse/protocol/redragon";
1
+ import { REDRAGON_ACTIVE_PROFILE_ADDRESS, REDRAGON_BUTTON_BASES, REDRAGON_BUTTON_LENGTH, REDRAGON_COMMIT_CODES, REDRAGON_CONFIG_USAGE, REDRAGON_CONFIG_USAGE_PAGE, REDRAGON_M612_EFFECTS, REDRAGON_M612_LIGHTING_BLOCK_LENGTH, REDRAGON_M612_LIGHTING_EFFECT_ADDRESS, REDRAGON_M724_EFFECTS, REDRAGON_M724_LIGHTING_BLOCK_LENGTH, REDRAGON_MAX_TRANSFER, REDRAGON_MODELS, REDRAGON_POLLING_CODES, REDRAGON_POLLING_RATES, REDRAGON_PRODUCTS, REDRAGON_PRODUCT_IDS, REDRAGON_PROFILE_BASES, REDRAGON_PROFILE_COUNT, REDRAGON_PROFILE_SELECT_CODE, REDRAGON_REPORT_ID, REDRAGON_SLOT_LENGTH, REDRAGON_STAGE_COUNT, REDRAGON_VENDOR_ID, redragonDecodeRead, redragonEncodeCommit, redragonEncodeRead, redragonEncodeWrite, redragonM612EffectForBit, redragonM724EffectForBlock, redragonM724LightingAddress, redragonPollAddress, redragonSession, redragonSlotAddress, } from "@openmouse/protocol/redragon";
2
2
  /** RDCfg gaps consecutive writes ~8-18 ms; stay well above that. */
3
3
  const WRITE_DELAY_MS = 30;
4
- /** Factory DPI table a default RDCfg pushes (level 1 is 1200 out of the box). */
5
- const FACTORY_STAGES = [1200, 2400, 3500, 5500, 12400];
6
- const DEFAULT_POLLING_HZ = 1000;
4
+ const LIFT_OFF_LEVELS = ["Low", "Medium", "High"];
5
+ /** RDCfg's three brightness positions as percentages. */
6
+ const BRIGHTNESS = [33, 67, 100];
7
+ /** Lighting speed 1 (slowest) to 8; the wire byte is `9 - speed`. */
8
+ const SPEEDS = [1, 2, 3, 4, 5, 6, 7, 8];
9
+ /** M612 effects OpenMouse can show; FLASH has no matching lighting mode. */
10
+ const M612_MODES = [
11
+ ["static", "Static"],
12
+ ["breathing", "Breathing single"],
13
+ ["spectrumBreathing", "Breathing random"],
14
+ ["wave", "Wave"],
15
+ ["off", "Off"],
16
+ ];
17
+ /** True when the `0xFFA0:0x01` collection with feature report 2 is present at any depth. */
7
18
  function hasConfigCollection(collections) {
8
19
  return collections.some((collection) => (collection.usagePage === REDRAGON_CONFIG_USAGE_PAGE &&
9
20
  collection.usage === REDRAGON_CONFIG_USAGE &&
10
21
  (collection.featureReports ?? []).some((report) => report.reportId === REDRAGON_REPORT_ID)) ||
11
22
  hasConfigCollection(collection.children ?? []));
12
23
  }
24
+ const hex = (bytes) => Array.from(bytes).map((byte) => byte.toString(16).padStart(2, "0")).join(" ");
13
25
  /**
14
- * Redragon K1NG 1K (M724, `04d9:fc7a`) WebHID control.
26
+ * Redragon Holtek mice (K1NG 1K M724 `04d9:fc7a`, Predator M612 `04d9:fc61`)
27
+ * over WebHID.
15
28
  *
16
- * Transport: Holtek vendor collection `0xFFA0:0x01` on USB interface 2,
17
- * 16-byte numbered feature report 2, `SET_FEATURE` writes of the form
18
- * `[F3 sub profile section ...]` (see `@openmouse/protocol/redragon`).
19
- * The device STALLs malformed writes and never answers reads: RDCfg pushes
20
- * its whole profile table at startup and issues no `GET_REPORT` at all, so
21
- * this driver is write-only like the SteelSeries Rival 3. `readStatus`
22
- * therefore reports this session's last-written stages (or the factory
23
- * table before any write) with `valuesVerified: false`, and the only
24
- * hardware probe is the `FA FA` marker every `GET_FEATURE` echo carries.
29
+ * Both keep their settings in one flat memory on the `0xFFA0:0x01`
30
+ * collection (see `@openmouse/protocol/redragon`): every value shown is
31
+ * read from the mouse with `F2`, and every write runs inside the vendor
32
+ * bracket, is followed by the commit block, and is read back before it is
33
+ * reported as done. DPI stages, polling, buttons, and (on the M724)
34
+ * lighting live per onboard profile, so they follow the active profile.
25
35
  */
26
36
  export class RedragonHidClient {
27
37
  device;
38
+ model;
28
39
  queue = Promise.resolve();
29
- lastStages = null;
30
- lastPollingHz = null;
40
+ /** Throws for a PID without a model descriptor; `isSupported` filters those out first. */
31
41
  constructor(device) {
42
+ const model = REDRAGON_MODELS.get(device.productId);
43
+ if (!model)
44
+ throw new Error(`Redragon product 0x${device.productId.toString(16)} is not a supported Holtek mouse.`);
32
45
  this.device = device;
33
- // The driver registry builds this class for every Redragon PID. The M612
34
- // answers reads, so it gets a subclass with read-back and active-stage
35
- // control; the app gates those controls on which methods exist.
36
- if (new.target === RedragonHidClient && device.productId === REDRAGON_M612_PRODUCT_ID) {
46
+ this.model = model;
47
+ // Compatibility: before the shared client, M612 devices built through
48
+ // this constructor were `RedragonM612HidClient` instances.
49
+ if (new.target === RedragonHidClient && model.model === "M612")
37
50
  return new RedragonM612HidClient(device);
38
- }
39
51
  }
52
+ /** Holtek VID, a catalogued PID, and the config collection (the mouse and keyboard interfaces lack it). */
40
53
  static isSupported(device) {
41
54
  if (device.vendorId !== REDRAGON_VENDOR_ID)
42
55
  return false;
@@ -48,7 +61,7 @@ export class RedragonHidClient {
48
61
  return [...REDRAGON_POLLING_RATES];
49
62
  }
50
63
  getDpiOptions() {
51
- return [200, 400, 800, 1200, 1600, 2000, 2400, 3200, 3500, 4000, 5500, 6400, 8000, 10000, 12400];
64
+ return [...this.model.dpi.labels];
52
65
  }
53
66
  async open() {
54
67
  if (!this.device.opened)
@@ -61,172 +74,10 @@ export class RedragonHidClient {
61
74
  async readStatus() {
62
75
  return await this.run(async () => {
63
76
  await this.open();
64
- await this.probeConfigChannel();
65
- const product = REDRAGON_PRODUCTS.get(this.device.productId);
66
- // The firmware reports a generic "USB Gaming Mouse" product string,
67
- // so prefer the catalog name whenever the PID is known.
68
- const name = product ? `Redragon ${product.name}` : (this.device.productName?.trim() || "Redragon Mouse");
69
- const stages = this.lastStages ?? [...FACTORY_STAGES];
70
- return {
71
- brand: "Redragon",
72
- name,
73
- ui: {
74
- family: "redragon",
75
- settingsReady: true,
76
- valuesVerified: false,
77
- hideUnsupportedPollingRates: true,
78
- hideProcessingCard: true,
79
- pollingNote: "The M724 never reports its polling rate; the value shown is this session's last write, or 1000 Hz before any write.",
80
- statusNote: "The K1NG 1K never reports settings back; values shown are this session's last writes, or the factory table before any write.",
81
- dpiStageEditor: {
82
- maxStages: REDRAGON_PROFILE0_DPI_SUBCMDS.length,
83
- countEditable: false,
84
- minDpi: 50,
85
- maxDpi: product?.maxDpi ?? 12400,
86
- stepDpi: 50,
87
- },
88
- defaultDisplayName: `Redragon ${product?.name ?? "Mouse"}`,
89
- },
90
- batteryPercent: null,
91
- batteryState: "Unknown",
92
- dpi: stages[0] ?? FACTORY_STAGES[0],
93
- dpiStages: stages,
94
- pollingRateHz: this.lastPollingHz ?? DEFAULT_POLLING_HZ,
95
- supportedPollingRates: this.supportedPollingRates,
96
- activeProfile: null,
97
- connectionType: "Wired",
98
- liftOffDistance: null,
99
- firmware: [],
100
- };
101
- });
102
- }
103
- /**
104
- * Writes the whole profile-1 DPI table in one vendor session bracket,
105
- * closed by the commit block. Bisected live: RDCfg re-pushes the table on
106
- * every Apply and the change is felt immediately; a lone slot write is
107
- * stored but only takes effect at boot, and the trailing `F1` block is
108
- * what activates it. Sibling stages come from this session's cache
109
- * (factory table before any write): editing in RDCfg meanwhile makes the
110
- * cache stale, exactly like any write-only driver.
111
- */
112
- async setDpiStageValue(stage, dpi) {
113
- if (!Number.isInteger(stage) || stage < 0 || stage >= REDRAGON_PROFILE0_DPI_SUBCMDS.length) {
114
- throw new Error(`Redragon DPI stage ${stage} is out of range 0-4.`);
115
- }
116
- // Validates the DPI before anything is sent.
117
- redragonEncodeDpiSlot(REDRAGON_PROFILE0, stage, dpi);
118
- await this.run(async () => {
119
- await this.open();
120
- const next = [...(this.lastStages ?? FACTORY_STAGES)];
121
- next[stage] = dpi;
122
- await this.writeSession(async () => {
123
- for (let level = 0; level < REDRAGON_PROFILE0_DPI_SUBCMDS.length; level++) {
124
- await this.sendFrame(redragonEncodeDpiSlot(REDRAGON_PROFILE0, level, next[level]));
125
- }
126
- for (const code of REDRAGON_COMMIT_CODES) {
127
- await this.sendFrame(redragonEncodeCommit(code));
128
- }
129
- });
130
- this.lastStages = next;
131
- });
132
- return dpi;
133
- }
134
- /**
135
- * Writes the polling rate inside the same session bracket + commit block
136
- * as DPI writes (the vendor sends it mid-push; the trailing `F1` block is
137
- * what activates writes, verified live for DPI).
138
- */
139
- async setPollingRate(hz) {
140
- const frame = redragonEncodePollingRate(hz);
141
- await this.run(async () => {
142
- await this.open();
143
- await this.writeSession(async () => {
144
- await this.sendFrame(frame);
145
- for (const code of REDRAGON_COMMIT_CODES) {
146
- await this.sendFrame(redragonEncodeCommit(code));
147
- }
148
- });
149
- this.lastPollingHz = hz;
150
- });
151
- return hz;
152
- }
153
- /**
154
- * The `FA FA` echo every `GET_FEATURE` carries doubles as the proof that
155
- * the granted interface is the config channel: the mouse and keyboard
156
- * interfaces expose no feature report 2 at all. The marker is matched as
157
- * the last two payload bytes so both WebHID framings (report id stripped
158
- * or kept) validate.
159
- */
160
- async probeConfigChannel() {
161
- const view = await this.device.receiveFeatureReport(REDRAGON_REPORT_ID);
162
- const echo = new Uint8Array(view.buffer.slice(view.byteOffset, view.byteOffset + view.byteLength));
163
- console.debug("[redragon] feature2 echo", [...echo].map((b) => b.toString(16).padStart(2, "0")).join(" "));
164
- const marker = echo.length >= 2 && echo[echo.length - 2] === 0xfa && echo[echo.length - 1] === 0xfa;
165
- if (!marker) {
166
- throw new Error(`The Redragon config channel answered ${echo.length} bytes without its FA FA marker (see console "[redragon] feature2 echo"). Add the device again and choose the entry backed by the vendor interface.`);
167
- }
168
- }
169
- async sendFrame(frame) {
170
- await this.device.sendFeatureReport(REDRAGON_REPORT_ID, frame.slice(1).buffer);
171
- await new Promise((resolve) => setTimeout(resolve, WRITE_DELAY_MS));
172
- }
173
- async sendSessionFrame(begin) {
174
- await this.sendFrame(begin ? redragonHello() : redragonSession(false));
175
- }
176
- async writeSession(operation) {
177
- await this.sendSessionFrame(true);
178
- try {
179
- await operation();
180
- }
181
- finally {
182
- await this.sendSessionFrame(false);
183
- }
184
- }
185
- async run(operation) {
186
- const result = this.queue.then(operation, operation);
187
- this.queue = result.then(() => undefined, () => undefined);
188
- return await result;
189
- }
190
- }
191
- /** RDCfg's button numbers, named for what each one is on the mouse. */
192
- const M612_BUTTON_NAMES = [
193
- "Left (1)", "Right (2)", "Wheel click (3)", "Fire (4)", "Button 5", "Button 6",
194
- "Button 7", "Button 8", "Button 9", "Wheel up", "Wheel down",
195
- ];
196
- /** RDCfg effects OpenMouse can show; FLASH has no matching lighting mode. */
197
- const M612_MODES = [
198
- ["static", "Static"],
199
- ["breathing", "Breathing single"],
200
- ["spectrumBreathing", "Breathing random"],
201
- ["wave", "Wave"],
202
- ["off", "Off"],
203
- ];
204
- /** RDCfg's three brightness positions as percentages. */
205
- const M612_BRIGHTNESS = [33, 67, 100];
206
- const M612_SPEEDS = Array.from({ length: REDRAGON_M612_LIGHTING_SPEEDS }, (_, index) => index + 1);
207
- /**
208
- * Redragon Predator M612 (`04d9:fc61`) WebHID control.
209
- *
210
- * Same Holtek transport as the M724, but the M612 answers `F2` reads (see
211
- * `redragonEncodeRead`), so every value shown is read from the mouse and
212
- * every write is read back before it is reported as done. Reads need no
213
- * session bracket; writes use the vendor bracket and commit block.
214
- *
215
- * DPI stages live per onboard profile, so writes target whichever profile
216
- * the mouse reports as active. DPI values snap to the positions RDCfg's
217
- * slider offers and report its label for them.
218
- */
219
- export class RedragonM612HidClient extends RedragonHidClient {
220
- getDpiOptions() {
221
- return [...REDRAGON_M612_DPI_LABELS];
222
- }
223
- async readStatus() {
224
- return await this.run(async () => {
225
- await this.open();
226
- await this.probeConfigChannel();
227
77
  const settings = await this.readSettings();
228
- const product = REDRAGON_PRODUCTS.get(REDRAGON_M612_PRODUCT_ID);
229
- const labels = REDRAGON_M612_DPI_LABELS;
78
+ const product = REDRAGON_PRODUCTS.get(this.model.productId);
79
+ const labels = this.model.dpi.labels;
80
+ const liftOff = await this.readLiftOff();
230
81
  return {
231
82
  brand: "Redragon",
232
83
  name: `Redragon ${product.name}`,
@@ -237,11 +88,11 @@ export class RedragonM612HidClient extends RedragonHidClient {
237
88
  hideUnsupportedPollingRates: true,
238
89
  hideProcessingCard: true,
239
90
  dpiStageEditor: {
240
- maxStages: REDRAGON_M612_STAGE_COUNT,
91
+ maxStages: REDRAGON_STAGE_COUNT,
241
92
  countEditable: false,
242
93
  minDpi: labels[0],
243
94
  maxDpi: labels[labels.length - 1],
244
- stepDpi: 10,
95
+ stepDpi: this.model.dpi.step,
245
96
  },
246
97
  defaultDisplayName: `Redragon ${product.name}`,
247
98
  showAdvancedSection: true,
@@ -254,12 +105,13 @@ export class RedragonM612HidClient extends RedragonHidClient {
254
105
  pollingRateHz: settings.pollingHz,
255
106
  supportedPollingRates: this.supportedPollingRates,
256
107
  activeProfile: settings.profile + 1,
257
- profileCount: REDRAGON_M612_PROFILE_COUNT,
108
+ profileCount: REDRAGON_PROFILE_COUNT,
258
109
  buttonMappings: await this.readButtonMappings(settings.profile),
259
- buttonOptions: [...REDRAGON_M612_BUTTON_OPTIONS],
260
- lighting: await this.readLighting(),
110
+ buttonOptions: [...this.model.buttons.options],
111
+ lighting: this.model.lighting === "profile" ? await this.readProfileLighting(settings.profile) : await this.readEffectLighting(),
261
112
  connectionType: "Wired",
262
- liftOffDistance: null,
113
+ liftOffDistance: liftOff,
114
+ ...(this.model.liftOffAddress === undefined ? {} : { supportedLiftOffDistances: [...LIFT_OFF_LEVELS] }),
263
115
  firmware: [],
264
116
  };
265
117
  });
@@ -271,29 +123,28 @@ export class RedragonM612HidClient extends RedragonHidClient {
271
123
  */
272
124
  async setDpiStageValue(stage, dpi) {
273
125
  // Validate both before anything is sent.
274
- const code = redragonM612EncodeDpi(dpi);
275
- redragonM612SlotAddress(0, stage);
126
+ const code = this.model.dpi.encode(dpi);
127
+ redragonSlotAddress(0, stage);
276
128
  return await this.run(async () => {
277
129
  await this.open();
278
- const address = redragonM612SlotAddress(await this.readActiveProfile(), stage);
279
- const slot = await this.readBytes(address, REDRAGON_M612_SLOT_LENGTH);
130
+ const address = redragonSlotAddress(await this.readActiveProfile(), stage);
131
+ const slot = await this.readBytes(address, REDRAGON_SLOT_LENGTH);
280
132
  await this.writeAndConfirm(address, [slot[0], code.value, code.range, code.value, code.range]);
281
133
  return code.dpi;
282
134
  });
283
135
  }
284
136
  /** Selects the active DPI stage of the active profile, as the DPI button does. */
285
137
  async setActiveDpiStage(stage) {
286
- if (!Number.isInteger(stage) || stage < 0 || stage >= REDRAGON_M612_STAGE_COUNT) {
287
- throw new Error(`Redragon M612 DPI stage ${stage} is outside 0-4.`);
138
+ if (!Number.isInteger(stage) || stage < 0 || stage >= REDRAGON_STAGE_COUNT) {
139
+ throw new Error(`Redragon DPI stage ${stage} is outside 0-${REDRAGON_STAGE_COUNT - 1}.`);
288
140
  }
289
141
  return await this.run(async () => {
290
142
  await this.open();
291
- const base = REDRAGON_M612_PROFILE_BASES[await this.readActiveProfile()];
292
- await this.writeAndConfirm(base, [stage]);
143
+ await this.writeAndConfirm(REDRAGON_PROFILE_BASES[await this.readActiveProfile()], [stage]);
293
144
  return stage;
294
145
  });
295
146
  }
296
- /** Rewrites the polling code, keeping the block's other bytes. */
147
+ /** Rewrites the active profile's polling code, keeping the byte after it. */
297
148
  async setPollingRate(hz) {
298
149
  const code = REDRAGON_POLLING_CODES[hz];
299
150
  if (code === undefined) {
@@ -301,26 +152,30 @@ export class RedragonM612HidClient extends RedragonHidClient {
301
152
  }
302
153
  return await this.run(async () => {
303
154
  await this.open();
304
- const block = await this.readBytes(REDRAGON_M612_POLL_ADDRESS, REDRAGON_M612_POLL_LENGTH);
305
- block[0] = code;
306
- await this.writeAndConfirm(REDRAGON_M612_POLL_ADDRESS, [...block]);
155
+ const address = redragonPollAddress(await this.readActiveProfile());
156
+ const [, next] = await this.readBytes(address, 2);
157
+ await this.writeAndConfirm(address, [code, next]);
307
158
  return hz;
308
159
  });
309
160
  }
310
- /** Switches the onboard profile (1-5), exactly as RDCfg's MODE menu does. */
161
+ /**
162
+ * Switches the onboard profile (1-5) as RDCfg's MODE menu does: the profile
163
+ * byte outside any bracket, `F1 02 01`, then the commit block. Without the
164
+ * block the M724 stores the byte but keeps the old profile running.
165
+ */
311
166
  async setProfile(profile) {
312
- if (!Number.isInteger(profile) || profile < 1 || profile > REDRAGON_M612_PROFILE_COUNT) {
313
- throw new Error(`Redragon M612 profile ${profile} is outside 1-${REDRAGON_M612_PROFILE_COUNT}.`);
167
+ if (!Number.isInteger(profile) || profile < 1 || profile > REDRAGON_PROFILE_COUNT) {
168
+ throw new Error(`Redragon profile ${profile} is outside 1-${REDRAGON_PROFILE_COUNT}.`);
314
169
  }
315
170
  await this.run(async () => {
316
171
  await this.open();
317
- const current = await this.readBytes(REDRAGON_M612_ACTIVE_PROFILE_ADDRESS, 2);
172
+ const current = await this.readBytes(REDRAGON_ACTIVE_PROFILE_ADDRESS, 2);
318
173
  const next = [profile - 1, current[1]];
319
- await this.sendFrame(redragonEncodeWrite(REDRAGON_M612_ACTIVE_PROFILE_ADDRESS, next));
320
- await this.sendFrame(redragonEncodeCommit(REDRAGON_M612_PROFILE_SELECT_CODE));
174
+ await this.sendFrame(redragonEncodeWrite(REDRAGON_ACTIVE_PROFILE_ADDRESS, next));
175
+ await this.sendFrame(redragonEncodeCommit(REDRAGON_PROFILE_SELECT_CODE));
321
176
  for (const code of REDRAGON_COMMIT_CODES)
322
177
  await this.sendFrame(redragonEncodeCommit(code));
323
- await this.confirm(REDRAGON_M612_ACTIVE_PROFILE_ADDRESS, next);
178
+ await this.confirm(REDRAGON_ACTIVE_PROFILE_ADDRESS, next);
324
179
  });
325
180
  }
326
181
  /**
@@ -329,10 +184,10 @@ export class RedragonM612HidClient extends RedragonHidClient {
329
184
  * leave the mouse unable to click.
330
185
  */
331
186
  async setButtonMapping(button, action) {
332
- const slot = M612_BUTTON_NAMES.indexOf(button);
187
+ const slot = this.model.buttons.names.indexOf(button);
333
188
  if (slot < 0)
334
- throw new Error(`The Redragon M612 has no button named "${button}".`);
335
- const value = redragonM612EncodeButtonAction(action);
189
+ throw new Error(`The Redragon ${this.model.model} has no button named "${button}".`);
190
+ const value = this.model.buttons.encode(action);
336
191
  await this.run(async () => {
337
192
  await this.open();
338
193
  const profile = await this.readActiveProfile();
@@ -343,30 +198,67 @@ export class RedragonM612HidClient extends RedragonHidClient {
343
198
  throw new Error("Keep Left click on at least one button, or the mouse cannot click.");
344
199
  }
345
200
  }
346
- await this.writeAndConfirm(redragonM612ButtonAddress(profile, slot), value);
201
+ await this.writeAndConfirm(REDRAGON_BUTTON_BASES[profile] + this.model.buttons.offsets[slot], value);
202
+ });
203
+ }
204
+ /** Sets the lift-off height (M724 only): Low, Medium, or High. */
205
+ async setLiftOffDistance(value) {
206
+ const address = this.model.liftOffAddress;
207
+ if (address === undefined)
208
+ throw new Error(`The Redragon ${this.model.model} has no lift-off setting.`);
209
+ const level = LIFT_OFF_LEVELS.indexOf(value);
210
+ if (level < 0)
211
+ throw new Error(`Redragon lift-off "${value}" is not Low, Medium, or High.`);
212
+ await this.run(async () => {
213
+ await this.open();
214
+ const [, next] = await this.readBytes(address, 2);
215
+ await this.writeAndConfirm(address, [level, next]);
347
216
  });
217
+ return value;
348
218
  }
349
219
  /**
350
- * Writes the effect selector and that effect's parameter block. Lighting is
351
- * global, not per profile. A colour, speed, or brightness equal to what
352
- * the current effect shows is treated as unchanged, so switching effects
353
- * keeps the target effect's own stored values.
220
+ * Writes the lighting. On the M612 that is the global effect selector and
221
+ * that effect's parameter block, where a colour, speed, or brightness equal
222
+ * to what the current effect shows is treated as unchanged, so switching
223
+ * effects keeps the target effect's own stored values. On the M724 it is
224
+ * the active profile's single block.
354
225
  */
355
226
  async setLighting(lighting) {
227
+ const model = this.model.model;
228
+ if (lighting.speed != null && !SPEEDS.includes(lighting.speed)) {
229
+ throw new Error(`Redragon ${model} lighting speed ${lighting.speed} is outside 1-${SPEEDS.length}.`);
230
+ }
231
+ if (lighting.brightness != null && !BRIGHTNESS.includes(lighting.brightness)) {
232
+ throw new Error(`Redragon ${model} brightness ${lighting.brightness}% is not one of ${BRIGHTNESS.join(", ")}.`);
233
+ }
234
+ const rgb = lighting.color == null ? null : parseColor(lighting.color, model);
235
+ if (this.model.lighting === "profile") {
236
+ const info = REDRAGON_M724_EFFECTS.find((candidate) => candidate.name === lighting.mode);
237
+ if (!info)
238
+ throw new Error(`The Redragon ${model} has no "${lighting.mode}" lighting effect.`);
239
+ await this.run(async () => {
240
+ await this.open();
241
+ const address = redragonM724LightingAddress(await this.readActiveProfile());
242
+ const block = [...await this.readBytes(address, REDRAGON_M724_LIGHTING_BLOCK_LENGTH)];
243
+ block[4] = info.mode;
244
+ block[6] = info.kind;
245
+ if (info.color && rgb)
246
+ block.splice(1, 3, ...rgb);
247
+ if (info.speed && lighting.speed != null)
248
+ block[5] = SPEEDS.length + 1 - lighting.speed;
249
+ if (lighting.brightness != null)
250
+ block[7] = BRIGHTNESS.indexOf(lighting.brightness) + 1;
251
+ await this.writeAndConfirm(address, block);
252
+ });
253
+ return;
254
+ }
356
255
  const effect = M612_MODES.find(([, mode]) => mode === lighting.mode)?.[0];
357
256
  if (!effect)
358
- throw new Error(`The Redragon M612 has no "${lighting.mode}" lighting effect.`);
257
+ throw new Error(`The Redragon ${model} has no "${lighting.mode}" lighting effect.`);
359
258
  const info = REDRAGON_M612_EFFECTS.find((candidate) => candidate.effect === effect);
360
- if (lighting.speed != null && !M612_SPEEDS.includes(lighting.speed)) {
361
- throw new Error(`Redragon M612 lighting speed ${lighting.speed} is outside 1-${REDRAGON_M612_LIGHTING_SPEEDS}.`);
362
- }
363
- if (lighting.brightness != null && !M612_BRIGHTNESS.includes(lighting.brightness)) {
364
- throw new Error(`Redragon M612 brightness ${lighting.brightness}% is not one of ${M612_BRIGHTNESS.join(", ")}.`);
365
- }
366
- const rgb = lighting.color == null ? null : parseColor(lighting.color);
367
259
  await this.run(async () => {
368
260
  await this.open();
369
- const shown = await this.readLighting();
261
+ const shown = await this.readEffectLighting();
370
262
  const selector = await this.readBytes(REDRAGON_M612_LIGHTING_EFFECT_ADDRESS, 2);
371
263
  const writes = [];
372
264
  if (info.block !== null) {
@@ -374,10 +266,10 @@ export class RedragonM612HidClient extends RedragonHidClient {
374
266
  if (info.color && rgb && lighting.color !== shown.color)
375
267
  block.splice(1, 3, ...rgb);
376
268
  if (info.speed && lighting.speed != null && lighting.speed !== shown.speed) {
377
- block[5] = REDRAGON_M612_LIGHTING_SPEEDS + 1 - lighting.speed;
269
+ block[5] = SPEEDS.length + 1 - lighting.speed;
378
270
  }
379
271
  if (lighting.brightness != null && lighting.brightness !== shown.brightness) {
380
- block[7] = M612_BRIGHTNESS.indexOf(lighting.brightness) + 1;
272
+ block[7] = BRIGHTNESS.indexOf(lighting.brightness) + 1;
381
273
  }
382
274
  writes.push([info.block, block]);
383
275
  }
@@ -392,70 +284,94 @@ export class RedragonM612HidClient extends RedragonHidClient {
392
284
  await this.confirm(address, data);
393
285
  });
394
286
  }
287
+ /** Names every button of `profile` with its decoded action, from one ranged read. */
395
288
  async readButtonMappings(profile) {
396
- const first = redragonM612ButtonAddress(profile, 0);
397
- const span = REDRAGON_M612_BUTTON_OFFSETS.at(-1) + REDRAGON_M612_BUTTON_LENGTH;
398
- const bytes = await this.readRange(first, span);
399
- return Object.fromEntries(M612_BUTTON_NAMES.map((name, slot) => {
400
- const offset = REDRAGON_M612_BUTTON_OFFSETS[slot];
401
- return [name, redragonM612DecodeButtonAction(bytes.subarray(offset, offset + REDRAGON_M612_BUTTON_LENGTH))];
402
- }));
289
+ const { names, offsets, decode } = this.model.buttons;
290
+ const bytes = await this.readRange(REDRAGON_BUTTON_BASES[profile], offsets.at(-1) + REDRAGON_BUTTON_LENGTH);
291
+ return Object.fromEntries(names.map((name, slot) => [name, decode(bytes.subarray(offsets[slot], offsets[slot] + REDRAGON_BUTTON_LENGTH))]));
403
292
  }
404
- async readLighting() {
293
+ /** M724: the profile's block; brightness is hidden when the effect is Off. */
294
+ async readProfileLighting(profile) {
295
+ const block = await this.readBytes(redragonM724LightingAddress(profile), REDRAGON_M724_LIGHTING_BLOCK_LENGTH);
296
+ const info = redragonM724EffectForBlock(block);
297
+ const lit = info !== undefined && info.name !== "Off";
298
+ return {
299
+ zone: "Mouse",
300
+ modes: REDRAGON_M724_EFFECTS.map((effect) => effect.name),
301
+ mode: info?.name ?? null,
302
+ color: `#${hex(block.subarray(1, 4)).replaceAll(" ", "")}`,
303
+ color2: null,
304
+ colorModes: REDRAGON_M724_EFFECTS.filter((effect) => effect.color).map((effect) => effect.name),
305
+ dualColorModes: [],
306
+ reactiveModes: REDRAGON_M724_EFFECTS.filter((effect) => effect.speed).map((effect) => effect.name),
307
+ speeds: SPEEDS,
308
+ speed: info?.speed && block[5] >= 1 && block[5] <= SPEEDS.length ? SPEEDS.length + 1 - block[5] : null,
309
+ brightness: lit ? (BRIGHTNESS[block[7] - 1] ?? null) : null,
310
+ brightnessLevels: lit ? [...BRIGHTNESS] : undefined,
311
+ };
312
+ }
313
+ /** M612: the global effect bit and that effect's parameter block (none for Off). */
314
+ async readEffectLighting() {
405
315
  const [bit] = await this.readBytes(REDRAGON_M612_LIGHTING_EFFECT_ADDRESS, 1);
406
316
  const info = redragonM612EffectForBit(bit ?? -1);
407
317
  const block = info?.block != null ? await this.readBytes(info.block, REDRAGON_M612_LIGHTING_BLOCK_LENGTH) : null;
408
318
  const mode = M612_MODES.find(([effect]) => effect === info?.effect)?.[1] ?? null;
409
- const colorModes = M612_MODES.filter(([effect]) => REDRAGON_M612_EFFECTS.find((e) => e.effect === effect).color).map(([, m]) => m);
410
- const speedModes = M612_MODES.filter(([effect]) => REDRAGON_M612_EFFECTS.find((e) => e.effect === effect).speed).map(([, m]) => m);
411
- const lit = block !== null;
319
+ const modesWhere = (key) => M612_MODES.filter(([effect]) => REDRAGON_M612_EFFECTS.find((e) => e.effect === effect)[key]).map(([, m]) => m);
412
320
  return {
413
321
  zone: "Mouse",
414
322
  modes: M612_MODES.map(([, m]) => m),
415
323
  mode,
416
- color: block ? `#${[...block.subarray(1, 4)].map((byte) => byte.toString(16).padStart(2, "0")).join("")}` : null,
324
+ color: block ? `#${hex(block.subarray(1, 4)).replaceAll(" ", "")}` : null,
417
325
  color2: null,
418
- colorModes,
326
+ colorModes: modesWhere("color"),
419
327
  dualColorModes: [],
420
- reactiveModes: speedModes,
421
- speeds: M612_SPEEDS,
422
- speed: block && info?.speed && block[5] >= 1 && block[5] <= REDRAGON_M612_LIGHTING_SPEEDS
423
- ? REDRAGON_M612_LIGHTING_SPEEDS + 1 - block[5] : null,
424
- brightness: block ? (M612_BRIGHTNESS[block[7] - 1] ?? null) : null,
328
+ reactiveModes: modesWhere("speed"),
329
+ speeds: SPEEDS,
330
+ speed: block && info?.speed && block[5] >= 1 && block[5] <= SPEEDS.length ? SPEEDS.length + 1 - block[5] : null,
331
+ brightness: block ? (BRIGHTNESS[block[7] - 1] ?? null) : null,
425
332
  // Off has no parameter block, so there is no brightness to set.
426
- brightnessLevels: lit ? [...M612_BRIGHTNESS] : undefined,
333
+ brightnessLevels: block !== null ? [...BRIGHTNESS] : undefined,
427
334
  };
428
335
  }
336
+ /** Null on models without lift-off, or for a level byte outside 0-2. */
337
+ async readLiftOff() {
338
+ if (this.model.liftOffAddress === undefined)
339
+ return null;
340
+ const [level] = await this.readBytes(this.model.liftOffAddress, 1);
341
+ return LIFT_OFF_LEVELS[level] ?? null;
342
+ }
343
+ /** Reads any length in transfers of at most 8 bytes. */
429
344
  async readRange(address, length) {
430
345
  const out = new Uint8Array(length);
431
- for (let offset = 0; offset < length; offset += 8) {
432
- out.set(await this.readBytes(address + offset, Math.min(8, length - offset)), offset);
346
+ for (let offset = 0; offset < length; offset += REDRAGON_MAX_TRANSFER) {
347
+ out.set(await this.readBytes(address + offset, Math.min(REDRAGON_MAX_TRANSFER, length - offset)), offset);
433
348
  }
434
349
  return out;
435
350
  }
351
+ /** Active profile, its stage table and active stage, and its polling rate; rejects values the firmware never holds. */
436
352
  async readSettings() {
437
353
  const profile = await this.readActiveProfile();
438
- const base = REDRAGON_M612_PROFILE_BASES[profile];
439
- const [activeStage] = await this.readBytes(base, 1);
440
- if (activeStage === undefined || activeStage >= REDRAGON_M612_STAGE_COUNT) {
441
- throw new Error(`The Redragon M612 reported DPI stage ${activeStage}; expected 0-4.`);
354
+ const [activeStage] = await this.readBytes(REDRAGON_PROFILE_BASES[profile], 1);
355
+ if (activeStage === undefined || activeStage >= REDRAGON_STAGE_COUNT) {
356
+ throw new Error(`The Redragon ${this.model.model} reported DPI stage ${activeStage}; expected 0-4.`);
442
357
  }
443
358
  const stages = [];
444
- for (let stage = 0; stage < REDRAGON_M612_STAGE_COUNT; stage++) {
445
- const slot = await this.readBytes(redragonM612SlotAddress(profile, stage), REDRAGON_M612_SLOT_LENGTH);
446
- stages.push(redragonM612DecodeDpi(slot[1], slot[2]));
359
+ for (let stage = 0; stage < REDRAGON_STAGE_COUNT; stage++) {
360
+ const slot = await this.readBytes(redragonSlotAddress(profile, stage), REDRAGON_SLOT_LENGTH);
361
+ stages.push(this.model.dpi.decode(slot[1], slot[2]));
447
362
  }
448
- const [pollCode] = await this.readBytes(REDRAGON_M612_POLL_ADDRESS, 1);
363
+ const [pollCode] = await this.readBytes(redragonPollAddress(profile), 1);
449
364
  const pollingHz = REDRAGON_POLLING_RATES.find((hz) => REDRAGON_POLLING_CODES[hz] === pollCode);
450
365
  if (pollingHz === undefined) {
451
- throw new Error(`The Redragon M612 reported polling code ${pollCode}; expected 1, 2, 4, or 8.`);
366
+ throw new Error(`The Redragon ${this.model.model} reported polling code ${pollCode}; expected 1, 2, 4, or 8.`);
452
367
  }
453
368
  return { profile, activeStage, stages, pollingHz };
454
369
  }
370
+ /** The active profile index 0-4, rejecting anything else. */
455
371
  async readActiveProfile() {
456
- const [profile] = await this.readBytes(REDRAGON_M612_ACTIVE_PROFILE_ADDRESS, 1);
457
- if (profile === undefined || profile >= REDRAGON_M612_PROFILE_BASES.length) {
458
- throw new Error(`The Redragon M612 reported profile ${profile}; expected 0-4.`);
372
+ const [profile] = await this.readBytes(REDRAGON_ACTIVE_PROFILE_ADDRESS, 1);
373
+ if (profile === undefined || profile >= REDRAGON_PROFILE_COUNT) {
374
+ throw new Error(`The Redragon ${this.model.model} reported profile ${profile}; expected 0-4.`);
459
375
  }
460
376
  return profile;
461
377
  }
@@ -469,9 +385,8 @@ export class RedragonM612HidClient extends RedragonHidClient {
469
385
  async writeAndConfirm(address, data) {
470
386
  await this.writeSession(async () => {
471
387
  await this.sendFrame(redragonEncodeWrite(address, data));
472
- for (const code of REDRAGON_COMMIT_CODES) {
388
+ for (const code of REDRAGON_COMMIT_CODES)
473
389
  await this.sendFrame(redragonEncodeCommit(code));
474
- }
475
390
  });
476
391
  await this.confirm(address, data);
477
392
  }
@@ -479,15 +394,37 @@ export class RedragonM612HidClient extends RedragonHidClient {
479
394
  async confirm(address, data) {
480
395
  const confirmed = await this.readBytes(address, data.length);
481
396
  if (confirmed.some((byte, index) => byte !== data[index])) {
482
- const hex = (bytes) => Array.from(bytes).map((byte) => byte.toString(16).padStart(2, "0")).join(" ");
483
- throw new Error(`The Redragon M612 kept ${hex(confirmed)} at 0x${address.toString(16)} instead of ${hex(data)}.`);
397
+ throw new Error(`The Redragon ${this.model.model} kept ${hex(confirmed)} at 0x${address.toString(16)} instead of ${hex(data)}.`);
484
398
  }
485
399
  }
400
+ async sendFrame(frame) {
401
+ await this.device.sendFeatureReport(REDRAGON_REPORT_ID, frame.slice(1).buffer);
402
+ await new Promise((resolve) => setTimeout(resolve, WRITE_DELAY_MS));
403
+ }
404
+ /** The bracket is always closed: a session left open hangs the interface. */
405
+ async writeSession(operation) {
406
+ await this.sendFrame(redragonSession(true));
407
+ try {
408
+ await operation();
409
+ }
410
+ finally {
411
+ await this.sendFrame(redragonSession(false));
412
+ }
413
+ }
414
+ async run(operation) {
415
+ const result = this.queue.then(operation, operation);
416
+ this.queue = result.then(() => undefined, () => undefined);
417
+ return await result;
418
+ }
419
+ }
420
+ /** @deprecated `RedragonHidClient` serves the M612 too; the model follows the PID. */
421
+ export class RedragonM612HidClient extends RedragonHidClient {
486
422
  }
487
- function parseColor(color) {
423
+ /** `#rrggbb` to RGB bytes; anything else throws. */
424
+ function parseColor(color, model) {
488
425
  const match = /^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i.exec(color);
489
426
  if (!match)
490
- throw new Error(`Redragon M612 colour "${color}" is not #rrggbb.`);
427
+ throw new Error(`Redragon ${model} colour "${color}" is not #rrggbb.`);
491
428
  return [parseInt(match[1], 16), parseInt(match[2], 16), parseInt(match[3], 16)];
492
429
  }
493
430
  //# sourceMappingURL=hid.js.map