Skip to content

Commit

Permalink
MAJOR API change, all of your shit is broken now.
Browse files Browse the repository at this point in the history
- Moved all defines to their own Enum, see issue #5 for more details
- Abstracted away structures and pointers, now you get easy-to-use namedtuples!
- Updated examples to reflect new API.
  • Loading branch information
10se1ucgo committed Jun 19, 2016
1 parent e8ccb3c commit eb99b58
Show file tree
Hide file tree
Showing 9 changed files with 406 additions and 288 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,7 @@ target/

# CUE DLLs
CUESDK_2013.dll
CUESDK.x64_2013.dll
CUESDK.x64_2013.dll

# a dumb test
test.py
38 changes: 14 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,30 @@ Use pip
# Load CUE DLL. Provide path to DLL yourself.
>>> Corsair = CUE("CUESDK.x64_2013.dll")
# Gives us exclusive access to controling the lighting and turns everything off.
>>> Corsair.RequestControl(CAM_ExclusiveLightingControl)
>>> Corsair.RequestControl(CAM.ExclusiveLightingControl)
True
# Sets the color of the H key to white.
>>> Corsair.SetLedsColors(1, CorsairLedColor(CLK_H, 255, 255, 255))
>>> Corsair.SetLedsColors(CorsairLedColor(CLK.H, 255, 255, 255))
True
# Sets the color of the A and B key to green
Corsair.SetLedsColors([CorsairLedColor(CLK.A, 0, 255, 0), CorsairLedColor(CLK.B, 0, 255, 0)])
# Returns number of Corsair devices.
>>> Corsair.GetDeviceCount()
1
# Takes zero-based index of device and returns a pointer to the struct with device info.
>>> Corsair.GetDeviceInfo(0)
<cue_sdk.cue_api.LP_CorsairDeviceInfo object at 0x000000000294DA48>
# Takes zero-based index of device and returns a namedtuple with the device info.
>>> device_info = Corsair.GetDeviceInfo(0)
# Returns the model of the device. Look at cue_structs.py for the other fields. Index with the same device index.
>>> device_info[0].model
'K70 RGB'
# Returns a pointer to the struct with all the LED positions.
>>> Corsair.GetLedPositions()
<cue_sdk.cue_api.LP_CorsairLedPositions object at 0x0000000002A976C8>
>>> led_positions = Corsair.GetLedPositions()
# Returns number of LEDs on the device. Index with the device index. Look at cue_structs.py for the other fields.
>>> led_positions[0].numberOfLed
111
# Returns the led id for the key name. Relative to logical layout (e.g. on an AZERTY keyboard it will return Q, not A)
>>> print(device_info)
CorsairDeviceInfo(type=<CDT.Keyboard: 2>, model='K70 RGB', physicalLayout=<CPL.US: 1>, logicalLayout=<CLL.NA: 2>, capsMask=<CDC.Lighting: 1>)
# Takes zero-based index of device and returns a namedtuple with the led positions + led count.
>>> Corsair.GetLedPositions(0)
CorsairLedPositions(numberOfLed=111, pLedPosition=[CorsairLedPosition(ledId=13, top=50.0, left=7.0, height=13.0, width=13.0), ...])
# Returns the led id (CLK enum) for the key name. Relative to logical layout (e.g. on an AZERTY keyboard it will return Q, not A)
>>> Corsair.CorsairGetLedIdForKeyName('a')
38
>>> 38 is CLK_A
True
<CLK.A: 38>
# Performs protocol handshake and returns details. Already called when the CUE class is initialized, no need to call for it yourself.
>>> Corsair.PerformProtocolHandshake()
<cue_sdk.cue_struct.CorsairProtocolDetails object at 0x0000000002A98210>
CorsairProtocolDetails(sdkVersion='1.15.28', serverVersion='1.16.42', sdkProtocolVersion=2, serverProtocolVersion=2, breakingChanges=False)
# Protocol details are stored here when called handshake is performed on init.
>>> Corsair.ProtocolDetails
<cue_sdk.cue_struct.CorsairProtocolDetails object at 0x0000000002A980E0>
# Returns the SDK version number. Look at cue_structs.py for the other fields.
>>> Corsair.ProtocolDetails.sdkVersion
'1.10.73'
CorsairProtocolDetails(sdkVersion='1.15.28', serverVersion='1.16.42', sdkProtocolVersion=2, serverProtocolVersion=2, breakingChanges=False)
```
68 changes: 46 additions & 22 deletions cue_sdk/cue_api.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
from ctypes import CDLL, c_bool, c_int, c_void_p, c_char, POINTER, CFUNCTYPE
from ctypes import CDLL, c_bool, c_int, c_void_p, c_char, POINTER, CFUNCTYPE, Structure
from .cue_struct import *
from .cue_defines import CorsairError, CE_Success
from .cue_struct import _CorsairDeviceInfo, _CorsairLedPositions, _CorsairProtocolDetails
from .cue_defines import *
from .cue_exceptions import *
import platform


class CUE(object):

def __init__(self, dll_path):
def __init__(self, dll_path, silence_errors=False):
if platform.system() == "Windows":
self._libcue = CDLL(dll_path)
else:
raise RuntimeError("CUE is not supported {platform} as of right now.".format(platform=platform.system()))

self.silence_errors = silence_errors
# Function prototypes
self._libcue.CorsairSetLedsColors.restype = c_bool
self._libcue.CorsairSetLedsColors.argtypes = [c_int, POINTER(CorsairLedColor)]
Expand All @@ -24,10 +26,10 @@ def __init__(self, dll_path):

self._libcue.CorsairGetDeviceCount.restype = c_int

self._libcue.CorsairGetDeviceInfo.restype = POINTER(CorsairDeviceInfo)
self._libcue.CorsairGetDeviceInfo.restype = POINTER(_CorsairDeviceInfo)
self._libcue.CorsairGetDeviceInfo.argtypes = [c_int]

self._libcue.CorsairGetLedPositions.restype = POINTER(CorsairLedPositions)
self._libcue.CorsairGetLedPositions.restype = POINTER(_CorsairLedPositions)

self._libcue.CorsairGetLedIdForKeyName.restype = c_int
self._libcue.CorsairGetLedIdForKeyName.argtypes = [c_char]
Expand All @@ -38,56 +40,78 @@ def __init__(self, dll_path):
self._libcue.CorsairReleaseControl.restype = c_bool
self._libcue.CorsairReleaseControl.argtypes = [c_int]

self._libcue.CorsairPerformProtocolHandshake.restype = CorsairProtocolDetails
self._libcue.CorsairPerformProtocolHandshake.restype = _CorsairProtocolDetails

self._libcue.CorsairGetLastError.restype = c_int

self._callbacks = {}
self.ProtocolDetails = self.PerformProtocolHandshake()
self.ErrorCheck()

def SetLedsColors(self, size, led_color):
if hasattr(led_color, "__getitem__"):
def _error_check(func):
def wrapper(self, *args, **kwargs):
res = func(self, *args, **kwargs)
if not self.silence_errors:
self.ErrorCheck()
return res
return wrapper

@_error_check
def SetLedsColors(self, led_colors):
if hasattr(led_colors, "__getitem__"):
size = len(led_colors)
led_array = CorsairLedColor * size
return self._libcue.CorsairSetLedsColors(size, led_array(*led_color))
return self._libcue.CorsairSetLedsColors(size, led_color)
return self._libcue.CorsairSetLedsColors(size, led_array(*led_colors))
return self._libcue.CorsairSetLedsColors(1, led_colors)

def SetLedsColorsAsync(self, size, led_color, callback=None, context=None):
@_error_check
def SetLedsColorsAsync(self, led_colors, callback=None, context=None):
if callback:
if context is None:
context = id(callback)
self._callbacks[context] = callback
if hasattr(led_color, "__getitem__"):
if hasattr(led_colors, "__getitem__"):
size = len(led_colors)
led_array = CorsairLedColor * size
return self._libcue.CorsairSetLedsColorsAsync(size, led_array(*led_color), self._callback, context)
return self._libcue.CorsairSetLedsColorsAsync(size, led_color, self._callback, context)
return self._libcue.CorsairSetLedsColorsAsync(size, led_array(*led_colors), self._callback, context)
return self._libcue.CorsairSetLedsColorsAsync(1, _CorsairLedColor(*led_colors), self._callback, context)

@_error_check
def GetDeviceCount(self):
return self._libcue.CorsairGetDeviceCount()

@_error_check
def GetDeviceInfo(self, device_id):
return self._libcue.CorsairGetDeviceInfo(device_id)
return CorsairDeviceInfo(self._libcue.CorsairGetDeviceInfo(device_id)[device_id])

def GetLedPositions(self):
return self._libcue.CorsairGetLedPositions()
@_error_check
def GetLedPositions(self, device_id):
leds = self._libcue.CorsairGetLedPositions()[device_id]
led_positions = []
for x in range(leds.numberOfLed):
led_positions.append(CorsairLedPosition(*leds.pLedPosition[x]))
return CorsairLedPositions(leds.numberOfLed, led_positions)

@_error_check
def GetLedIdForKeyName(self, key_name):
return self._libcue.CorsairGetLedIdForKeyName(key_name)
return CLK(self._libcue.CorsairGetLedIdForKeyName(key_name.encode('ascii')))

@_error_check
def RequestControl(self, access_mode):
return self._libcue.CorsairRequestControl(access_mode)

@_error_check
def ReleaseControl(self, access_mode):
return self._libcue.CorsairReleaseControl(access_mode)

@_error_check
def PerformProtocolHandshake(self):
return self._libcue.CorsairPerformProtocolHandshake()
return CorsairProtocolDetails(self._libcue.CorsairPerformProtocolHandshake())

def GetLastError(self):
return self._libcue.CorsairGetLastError()
return CE(self._libcue.CorsairGetLastError())

def ErrorCheck(self):
if self.GetLastError() != CE_Success:
if self.GetLastError() != CE.Success:
raise CorsairError[self.GetLastError()]

def _callback_handler(self, context, result, error):
Expand Down
Loading

0 comments on commit eb99b58

Please sign in to comment.