Created
October 21, 2023 02:20
-
-
Save sunfkny/390117f4d849db091bc4e2b5ac53ca76 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://github.com/douweschulte/opencontrol | |
import math | |
from loguru import logger | |
import logging | |
import sys | |
import ctypes | |
import enum | |
import typing | |
logger.remove() | |
logger.add(sys.stdout, level=logging.INFO) | |
InsydeDCHU = ctypes.cdll.LoadLibrary("./InsydeDCHU.dll") | |
@typing.overload | |
def ReadAppSettings(page: int, offset: int) -> int: | |
... | |
@typing.overload | |
def ReadAppSettings(page: int, offset: int, length: int) -> list[int]: | |
... | |
def ReadAppSettings(page: int, offset: int, length: int = 1) -> list[int] | int: | |
InsydeDCHU.ReadAppSettings.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.POINTER(ctypes.c_ubyte)] | |
InsydeDCHU.ReadAppSettings.restype = ctypes.c_int | |
if length == 1 or length is None: | |
buffer = ctypes.c_ubyte() | |
res = InsydeDCHU.ReadAppSettings(page, offset, length, ctypes.byref(buffer)) | |
logger.debug(f"ReadAppSettings returned {res}") | |
return buffer.value | |
else: | |
buffer = (ctypes.c_ubyte * length)() | |
res = InsydeDCHU.ReadAppSettings(page, offset, length, buffer) | |
logger.debug(f"ReadAppSettings returned {res}") | |
return list(buffer) | |
def GetDCHU_Data_Buffer(command: int, length: int = 256) -> list[int]: | |
InsydeDCHU.GetDCHU_Data_Buffer.argtypes = [ctypes.c_int, ctypes.POINTER(ctypes.c_ubyte)] | |
InsydeDCHU.GetDCHU_Data_Buffer.restype = ctypes.c_int | |
buffer = (ctypes.c_ubyte * length)() | |
res = InsydeDCHU.GetDCHU_Data_Buffer(command, buffer) | |
logger.debug(f"GetDCHU_Data_Buffer returned {res}") | |
return list(buffer) | |
class KeyboardMode(enum.Enum): | |
Random = 0 | |
Custom = 1 | |
Breath = 2 | |
Cycle = 3 | |
Wave = 4 | |
Dance = 5 | |
Tempo = 6 | |
Flash = 7 | |
AllColor = 8 | |
def KeyboardGetStatus(): | |
brightness = ReadAppSettings(2, 35) | |
color = ReadAppSettings(2, 81, 3) | |
mode = ReadAppSettings(2, 32) | |
status = ReadAppSettings(2, 84) | |
state = "On" if status else "Off" | |
booteffect = ReadAppSettings(2, 7) | |
boot = "Overridden" if booteffect else "Default" | |
sleep = ReadAppSettings(2, 37, 3) | |
sleepsec = sleep[0] * 3600 + sleep[1] * 60 + sleep[2] | |
sleepstatus = ReadAppSettings(2, 36) | |
sleepstate = "On" if sleepstatus else "Off" | |
return f"""RGBA {color[0]} {color[1]} {color[2]} {brightness} | |
MODE {KeyboardMode(mode).name} | |
LEDSTATUS {state} | |
BOOTEFFECT {boot} | |
SLEEP {sleepsec} SEC | |
SLEEPSTATUS {sleepstate}""" | |
class PowerMode(enum.Enum): | |
Quiet = 0 | |
Powersaving = 1 | |
Performance = 2 | |
Entertainment = 3 | |
def PowerModeGetStatus(): | |
mode = ReadAppSettings(1, 1) | |
return f"POWERMODE {PowerMode(mode).name}" | |
class FanMode(enum.Enum): | |
Auto = 0 | |
Max = 1 | |
MaxQ = 5 | |
Custom = 6 | |
def FanCurve(): | |
numArray2 = ReadAppSettings(4, 0, 256) | |
D1 = numArray2[16] | |
D2 = numArray2[17] | |
D3 = numArray2[18] | |
T1 = numArray2[22] | |
T2 = numArray2[23] | |
T3 = numArray2[24] | |
return f"{T1}°C {D1}% {T2}°C {D2}% {T3}°C {D3}% 100°C 100% " | |
def FanInfo(): | |
numArray = GetDCHU_Data_Buffer(12) | |
rpm = int(numArray[3]) + (int(numArray[2]) << 8) | |
if rpm != 0: | |
rpm = int(60.0 / (5.56521739130435e-05 * rpm) * 2) | |
percent = int(float(numArray[16]) / 255 * 100) | |
cpu_temp = int(numArray[18]) | |
return f"""CPU {rpm}RPM {percent}% {cpu_temp}°C""" | |
def FanGetStatus(): | |
offset = ReadAppSettings(4, 7, 4) | |
mode = ReadAppSettings(4, 5, 4) | |
return f"""FANMODE {FanMode(mode[0]).name} | |
FANOFFSET {offset[0]} | |
FANCURVE {FanCurve()} | |
FANINFO {FanInfo()}""" | |
if __name__ == "__main__": | |
print(KeyboardGetStatus()) | |
print(PowerModeGetStatus()) | |
print(FanGetStatus()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment