Pure MoonBit HID report descriptor parsing, layout compilation and offline report decoding.
Dependencies
git clone https://github.com/We1chan/moonhid.git
cd moonhid
moon run cmd/mainmoon run cmd/moonhid --target native -- inspect descriptor.hex
moon run cmd/moonhid --target native -- decode --kind input descriptor.hex report.hex
moon run cmd/moonhid --target js -- inspect descriptor.hexnode scripts/build-web.mjs
python3 scripts/serve-web.py///|
test "README: decode one relative axis" {
let descriptor = b"\x05\x01\x09\x30\x15\x81\x25\x7f\x75\x08\x95\x01\x81\x06"
let layout = match @moonhid.compile_descriptor(descriptor) {
Ok(layout) => layout
Err(_) => fail("descriptor rejected")
}
let report = match @moonhid.decode_report(layout, @moonhid.Input, b"\xff") {
Ok(report) => report
Err(_) => fail("report rejected")
}
assert_eq(report.values[0].value, -1L)
assert_eq(report.values[0].usage, Some({ page: 1, id: 0x30, }))
}| 接口 | 用途 |
|---|---|
| parse_hex | 严格解析连续或 ASCII 空白分隔的十六进制字节对 |
| parse_items | 保留 item 类型、tag、原始数据和字节位置 |
| unsigned_value / signed_value | 小端短项数值,完整保留 32 位范围 |
| field_usage | 按序查询 Usage;Variable 越界重复最后一项,Array 越界返回 None |
| compile_descriptor | 计算 Main 字段、集合树、元数据、报告 ID 与位偏移 |
| decode_unit / effective_physical_range | 展开单位维度,并应用 HID 物理范围缺省规则 |
| descriptor_to_json / layout_to_json / report_to_json | 导出版本化描述符与布局、解码组件 |
| report_length | 查询指定方向和 ID 的完整报告字节长度 |
| extract_bytes | 按 LSB 位序提取原始字节,支持非字节对齐 |
| extract_bits | 提取跨字节、带符号或无符号的位字段 |
| decode_report | 校验长度并返回变量值、数组 Usage 与 Null State 标记 |
moon check --target all --deny-warn
moon build
moon test
moon test --target native
moon test --target js # 需要 Node.js
node scripts/build-web.mjs
node scripts/test-web.mjs
moon info
moon fmt --checkpub(all) struct DecodedReport {
kind : ReportKind
report_id : Int
values : Array[DecodedValue]
opaque_values : Array[OpaqueValue]
} derive(Eq, Debug)pub(all) struct Field {
descriptor_offset : Int
kind : ReportKind
report_id : Int
bit_offset : Int
bit_size : Int
count : Int
flags : Int
logical_min : Int64
logical_max : Int64
physical_min : Int64?
physical_max : Int64?
unit : Int64?
unit_exponent : Int?
collection_index : Int?
usage_spans : Array[UsageSpan]
string_spans : Array[IndexSpan]
designator_spans : Array[IndexSpan]
alternate_usages : Array[Array[UsageSpan]]
} derive(Eq, Debug)fn decode_report(layout : Layout, kind : ReportKind, bytes : Bytes) -> Result[DecodedReport, Diagnostic]fn extract_bits(bytes : Bytes, bit_offset : Int, bit_size : Int, signed? : Bool) -> Result[Int64, Diagnostic]fn format_hex(bytes : Bytes) -> StringInstall
Download zipPure MoonBit HID report descriptor parsing, layout compilation and offline report decoding.
Dependencies