-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcorsair-status-writer.py
More file actions
57 lines (47 loc) · 1.38 KB
/
Copy pathcorsair-status-writer.py
File metadata and controls
57 lines (47 loc) · 1.38 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
#!/usr/bin/env python3
"""
Reads Corsair AI Workstation 300 ERAX MMIO region and writes status JSON
to /run/corsair-status every second. Run as root via systemd service.
"""
import mmap, os, time, json, signal, sys
ERAX_BASE = 0xFEC40400
STATUS_FILE = "/run/corsair-status"
MODES = {0: "Balanced", 1: "Max", 2: "Quiet", 3: "Super"}
page_size = 4096
page_addr = ERAX_BASE & ~(page_size - 1)
base = ERAX_BASE - page_addr
fd = os.open("/dev/mem", os.O_RDONLY | os.O_SYNC)
m = mmap.mmap(fd, page_size, mmap.MAP_SHARED, mmap.PROT_READ, offset=page_addr)
def d(off):
return m[base + off]
def read_status():
mode_raw = d(0x31)
fan1 = (d(0x35) << 8) | d(0x36)
fan2 = (d(0x37) << 8) | d(0x38)
return {
"mode": MODES.get(mode_raw, f"Unknown"),
"mode_raw": mode_raw,
"fan1": fan1,
"fan2": fan2,
"cput": d(0x70),
"gput": d(0x71),
}
def cleanup(sig, frame):
m.close()
os.close(fd)
try:
os.unlink(STATUS_FILE)
except FileNotFoundError:
pass
sys.exit(0)
signal.signal(signal.SIGTERM, cleanup)
signal.signal(signal.SIGINT, cleanup)
tmp = STATUS_FILE + ".tmp"
while True:
status = read_status()
data = json.dumps(status) + "\n"
with open(tmp, "w") as f:
f.write(data)
os.replace(tmp, STATUS_FILE)
os.chmod(STATUS_FILE, 0o644)
time.sleep(1)