Repository navigation
Expand file tree
/
Copy pathdevice.go
More file actions
91 lines (76 loc) · 2.49 KB
/
Copy pathdevice.go
File metadata and controls
91 lines (76 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package atk
import (
"fmt"
"time"
"github.com/sstallion/go-hid"
)
// BatteryInfo holds the parsed battery percentage, voltage, and charging state.
type BatteryInfo struct {
Percentage uint8 `json:"percentage"`
Voltage float32 `json:"voltage"`
Charging bool `json:"charging"`
}
// DeviceInfo represents details of a discovered ATK peripheral.
type DeviceInfo struct {
Path string `json:"path"`
VendorID uint16 `json:"vendor_id"`
ProductID uint16 `json:"product_id"`
Interface int `json:"interface"`
UsagePage uint16 `json:"usage_page"`
Usage uint16 `json:"usage"`
ProductName string `json:"product_name"` // Retreived from device
ModelName string `json:"model_name"` // Matched from registry
ReportID byte `json:"report_id"` // Matched from registry
}
// Device represents an open connection to an ATK device.
type Device struct {
info *DeviceInfo
dev *hid.Device
}
// Info returns metadata about the connected device.
func (d *Device) Info() *DeviceInfo {
return d.info
}
// Close terminates the HID connection to the device.
func (d *Device) Close() error {
if d.dev != nil {
return d.dev.Close()
}
return nil
}
// QueryBattery queries the device for battery percentage and voltage,
// then parses the response.
func (d *Device) QueryBattery() (*BatteryInfo, error) {
// Construct raw payload frame (16 bytes)
payload := FinalizePayload(CmdQueryBattery, [14]byte{})
// Prepend Report ID to create the transfer packet
packet := make([]byte, 1+len(payload))
packet[0] = d.info.ReportID
copy(packet[1:], payload)
// Write packet to device
_, err := d.dev.Write(packet)
if err != nil {
return nil, fmt.Errorf("failed to write query packet: %w", err)
}
// Read response (expected layout: header/opcode, metadata, data bytes)
inBuf := make([]byte, 64)
n, err := d.dev.ReadWithTimeout(inBuf, 200*time.Millisecond)
if err != nil {
return nil, fmt.Errorf("read timeout or error: %w", err)
}
if n < 10 {
return nil, fmt.Errorf("response frame too short (got %d bytes, expected >= 10)", n)
}
// Parse out battery percentage (index 6)
batteryPercent := inBuf[6]
// Parse out charging state (index 7). 0x01 means charging, 0x00 means discharging.
charging := inBuf[7] == 0x01
// Parse out battery voltage in millivolts (indices 8 & 9)
millivolts := (uint16(inBuf[8]) << 8) | uint16(inBuf[9])
voltage := float32(millivolts) / 1000.0
return &BatteryInfo{
Percentage: batteryPercent,
Voltage: voltage,
Charging: charging,
}, nil
}