The Kurgan 1.0.0: one process for the LCD, fans, lighting, local LLM servers and the web UI
This commit is contained in:
commit
f631386618
71 files changed
+14616
No files matched your search
@@ -0,0 +1,146 @@
|
||||
"""Corsair Lighting Node CORE (1B1C:0C1A) driven directly over Windows HID.
|
||||
|
||||
Each command is a 64-byte output report answered by a 16-byte input report whose first
|
||||
byte is 0 on success. LED colours go 50 at a time, one packet per colour channel, and only
|
||||
appear after a commit. The node returns to its stored hardware effect if it hears nothing
|
||||
for about 20 s in software mode, so `keepalive()` re-commits every few seconds.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import ctypes
|
||||
import time
|
||||
|
||||
from kurgan.telemetry.sensors import corsair_software_running
|
||||
from kurgan.hardware.hid import HidChannel, hid_device_paths
|
||||
|
||||
VID, PID = 0x1B1C, 0x0C1A
|
||||
OP_FIRMWARE, OP_DIRECT, OP_COMMIT, OP_PORT_STATE = 0x02, 0x32, 0x33, 0x38
|
||||
PORT_HARDWARE, PORT_SOFTWARE = 0x01, 0x02
|
||||
LEDS_PER_PACKET = 50
|
||||
MAX_LEDS = 204
|
||||
KEEPALIVE_SECONDS = 5.0
|
||||
|
||||
_k32 = ctypes.WinDLL("kernel32", use_last_error=True)
|
||||
_k32.CreateMutexW.argtypes = [ctypes.c_void_p, ctypes.c_bool, ctypes.c_wchar_p]
|
||||
_k32.CreateMutexW.restype = ctypes.c_void_p
|
||||
_k32.CloseHandle.argtypes = [ctypes.c_void_p]
|
||||
_ERROR_ALREADY_EXISTS = 183
|
||||
|
||||
|
||||
class LightingNodeBusy(OSError):
|
||||
pass
|
||||
|
||||
|
||||
def direct_packets(channel: int, colors: list[tuple[int, int, int]],
|
||||
previous: list[tuple[int, int, int]] | None = None) -> list[bytes]:
|
||||
"""Direct-colour packets for `colors`, skipping chunks whose colour channel is unchanged."""
|
||||
packets = []
|
||||
for start in range(0, len(colors), LEDS_PER_PACKET):
|
||||
chunk = colors[start:start + LEDS_PER_PACKET]
|
||||
old = None if previous is None else previous[start:start + LEDS_PER_PACKET]
|
||||
for component in range(3):
|
||||
values = bytes(color[component] for color in chunk)
|
||||
if old is not None and values == bytes(color[component] for color in old):
|
||||
continue
|
||||
packets.append(bytes((OP_DIRECT, channel, start, len(chunk), component)) + values)
|
||||
return packets
|
||||
|
||||
|
||||
class LightingNodeCore:
|
||||
"""Open with `with LightingNodeCore() as node:` and call `show()` or `keepalive()` regularly."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.ch: HidChannel | None = None
|
||||
self.firmware = ""
|
||||
self._mutex: int | None = None
|
||||
self._software: set[int] = set()
|
||||
self._shown: dict[int, list[tuple[int, int, int]]] = {}
|
||||
self._last_commit = 0.0
|
||||
|
||||
def __enter__(self) -> "LightingNodeCore":
|
||||
self.open()
|
||||
return self
|
||||
|
||||
def __exit__(self, *exc) -> None:
|
||||
self.close()
|
||||
|
||||
def open(self) -> None:
|
||||
if self.ch is not None:
|
||||
return
|
||||
if corsair_software_running("Corsair Lighting Node Core", fan_tools=False):
|
||||
raise LightingNodeBusy("Lighting Node CORE busy: exit iCUE, or disable OpenRGB's "
|
||||
"Corsair Lighting Node Core detector")
|
||||
ctypes.set_last_error(0)
|
||||
mutex = _k32.CreateMutexW(None, False, "Local\\TTLcdLightingNode")
|
||||
if not mutex or ctypes.get_last_error() == _ERROR_ALREADY_EXISTS:
|
||||
if mutex:
|
||||
_k32.CloseHandle(mutex)
|
||||
raise LightingNodeBusy("Lighting Node CORE is in use by another dashboard process")
|
||||
self._mutex = mutex
|
||||
try:
|
||||
paths = hid_device_paths(VID, PID)
|
||||
if len(paths) != 1:
|
||||
raise OSError(f"Expected one Lighting Node CORE, found {len(paths)}")
|
||||
self.ch = HidChannel(paths[0], 64)
|
||||
reply = self._cmd(OP_FIRMWARE)
|
||||
self.firmware = ".".join(str(b) for b in reply[2:5])
|
||||
except Exception:
|
||||
self.close()
|
||||
raise
|
||||
|
||||
def close(self) -> None:
|
||||
if self.ch is not None:
|
||||
try:
|
||||
self.release()
|
||||
except OSError:
|
||||
pass
|
||||
self.ch.close()
|
||||
self.ch = None
|
||||
if self._mutex is not None:
|
||||
_k32.CloseHandle(self._mutex)
|
||||
self._mutex = None
|
||||
|
||||
def show(self, channel: int, colors: list[tuple[int, int, int]]) -> None:
|
||||
"""Display `colors` on header `channel` (0 or 1), sending only what changed."""
|
||||
if channel not in (0, 1) or not 0 < len(colors) <= MAX_LEDS:
|
||||
raise ValueError("Channel must be 0 or 1 with 1-204 LEDs")
|
||||
if channel not in self._software:
|
||||
self._cmd(OP_PORT_STATE, channel, PORT_SOFTWARE)
|
||||
self._software.add(channel)
|
||||
self._shown.pop(channel, None)
|
||||
previous = self._shown.get(channel)
|
||||
if previous is not None and len(previous) != len(colors):
|
||||
previous = None
|
||||
packets = direct_packets(channel, colors, previous)
|
||||
for packet in packets:
|
||||
self._cmd(*packet)
|
||||
if packets:
|
||||
self._commit()
|
||||
self._shown[channel] = list(colors)
|
||||
|
||||
def keepalive(self) -> None:
|
||||
if self._software and time.monotonic() - self._last_commit >= KEEPALIVE_SECONDS:
|
||||
self._commit()
|
||||
|
||||
def release(self) -> None:
|
||||
"""Hand every header back to the node's stored hardware effect."""
|
||||
for channel in sorted(self._software):
|
||||
self._cmd(OP_PORT_STATE, channel, PORT_HARDWARE)
|
||||
self._software.clear()
|
||||
self._shown.clear()
|
||||
|
||||
def _commit(self) -> None:
|
||||
self._cmd(OP_COMMIT, 0xFF)
|
||||
self._last_commit = time.monotonic()
|
||||
|
||||
def _cmd(self, *payload: int) -> bytes:
|
||||
ch = self.ch
|
||||
if ch is None:
|
||||
raise OSError("Lighting Node CORE is not open")
|
||||
ch.drain()
|
||||
ch.send(bytes(payload))
|
||||
reply = ch.read(300)
|
||||
if not reply or len(reply) < 2 or reply[1] != 0: # [report id, status, ...]
|
||||
raise OSError(f"Lighting Node CORE didn't accept command 0x{payload[0]:02X}")
|
||||
return reply
|
||||
Reference in new issue
Block a user