# 诊断

> 做一个自己的手柄测试工具：原始轴值、72 扇区摇杆圆度、静止漂移和按键回弹检测。

Source: https://gamepadly.com/sdk/zh/guides/diagnostics

`@gamepadly/sdk/diagnostics` 是 [gamepadly.com](https://gamepadly.com) 手柄测试工具的无界面核心。它负责测量，你负责绘制。

```ts
import { createGamepadly } from '@gamepadly/sdk';
import { buildSectorPath, createControllerTelemetry } from '@gamepadly/sdk/diagnostics';

const hub = createGamepadly();
const telemetry = createControllerTelemetry({ source: hub.hid });

function frame(now: number) {
  const f = telemetry.update(now);

  const ring = f.diagnostics.circularity.left;
  leftRing.setAttribute('d', buildSectorPath(ring.sectors, 80, 80, 58));
  coverage.textContent = `已转过 ${ring.coveragePct.toFixed(0)}%`;
  error.textContent = ring.avgErrorPct === null ? '—' : `误差 ${ring.avgErrorPct.toFixed(1)}%`;

  const drift = f.diagnostics.drift.getSnapshot();
  leftDrift.textContent = drift.left.verdict; // 'waiting' | 'normal' | 'mild' | 'drift'

  bounced.textContent = f.diagnostics.bounce.getBouncedButtons().join(', ');
  tested.textContent = `已测试 ${f.diagnostics.tested.size} 个按键`;

  requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
```

## 每项测量的含义

| 测量 | 原理 |
| :--- | :--- |
| **圆度** | 摇杆行程分成 72 个扇区。转动摇杆时，每个扇区记录到达的最大半径。`avgErrorPct` 是与正圆的平均偏差，`coveragePct` 是已经转过的比例。 |
| **漂移** | 摇杆静止时，记录偏离中心的峰值和抖动。结论从 `'waiting'`（还没稳定）到 `'normal'`、`'mild'` 或 `'drift'`。阈值可以用 `drift` 选项配置。 |
| **回弹** | 松开后几毫秒内又被按下，是磨损的微动开关造成的触点回弹。用 `bounce` 选项调整。 |
| **已测试** | 至少按过一次的按键集合，用于“把每个键都按一遍”的检查清单。 |

`f.input` 里的轴值是原始值，没有死区，所以数字反映的是硬件本身。

## 多个手柄

遥测会切换到收到有意输入的那个手柄，并为每个手柄分别保存测量结果。`f.slots` 列出所有手柄，`f.activeSlotId` 是当前显示的那个。调用 `telemetry.selectSlot(id, now)` 手动选择，`telemetry.resetActive()` 清空它的测量结果。传入 `brandFilter: 'sony'` 可以做一个只针对某个品牌的测试工具。

完整页面见[诊断示例](https://gamepadly.com/sdk/zh/examples#手柄诊断)。
