forked from Logitech/logiPy
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathled.py
More file actions
577 lines (483 loc) · 18.6 KB
/
led.py
File metadata and controls
577 lines (483 loc) · 18.6 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
import ctypes
import os
import platform
import time
from enum import Enum, auto
from pathlib import Path
from typing import Optional, Union
from .color import Color
from .keys import *
LOGI_LED_BITMAP_WIDTH = 21
LOGI_LED_BITMAP_HEIGHT = 6
LOGI_LED_BITMAP_BYTES_PER_KEY = 4
LOGI_LED_BITMAP_SIZE = (
LOGI_LED_BITMAP_WIDTH * LOGI_LED_BITMAP_HEIGHT * LOGI_LED_BITMAP_BYTES_PER_KEY
)
# Required Globals
_LOGI_SHARED_SDK_LED = ctypes.c_int(1)
class SDKNotFoundException(Exception):
pass
class KeyType(Enum):
scan = auto()
hid = auto()
quartz = auto()
name = auto()
LOGI_DEVICETYPE_MONOCHROME_ORD = 0
LOGI_DEVICETYPE_RGB_ORD = 1
LOGI_DEVICETYPE_PERKEY_RGB_ORD = 2
class DeviceType(Enum):
monochrome = 1 << LOGI_DEVICETYPE_MONOCHROME_ORD
rgb = 1 << LOGI_DEVICETYPE_RGB_ORD
perkey_rgb = 1 << LOGI_DEVICETYPE_PERKEY_RGB_ORD
all = monochrome | rgb | perkey_rgb
class LEDService:
"""Service implementation for the LED API."""
dll: ctypes.CDLL
use_legacy_dll: bool = True
wait_for_sdk_initialization: bool = True
def __init__(
self,
path_dll: Union[str, Path, None] = None,
use_legacy_dll: bool = True,
wait_for_sdk_initialization: bool = True,
) -> None:
"""Initializes the LED service and loads the necessary DLL.
Parameters
----------
path_dll : Union[str, Path], optional
The path to the DLL, if None will try to find the DLL automatically.
use_legacy_dll : bool, optional
Use the legacy DLL included in the Logitech G Hub.
wait_for_sdk_initialization : bool, optional
Wait for the SDK to initialize.
Raises
------
SDKNotFoundException
If the SDK DLL is not found.
"""
self.use_legacy_dll = use_legacy_dll
self.wait_for_sdk_initialization = wait_for_sdk_initialization
self.dll = self.load_dll(path_dll=path_dll)
def start(self) -> bool:
"""Initialize the LED API. This is a necessary step if you want to work with the API."""
init_result = bool(self.dll.LogiLedInit())
if init_result and self.wait_for_sdk_initialization:
time.sleep(1)
return init_result
def shutdown(self):
"""Shutdown the SDK for the thread."""
return bool(self.dll.LogiLedShutdown())
def __enter__(self) -> "LEDService":
"""Context manager adaption."""
self.start()
return self
def __exit__(self, type, value, traceback) -> None:
"""Context manager adaption."""
self.shutdown()
def load_dll(self, path_dll: Optional[Union[str, Path]] = None) -> ctypes.CDLL:
"""Load the DLL."""
if not path_dll:
bitness = "x86" if platform.architecture()[0] == "32bit" else "x64"
if self.use_legacy_dll:
subpath_dll = f"LGHUB/sdks/sdk_legacy_led_{bitness}.dll"
else:
subpath_dll = Path(f"/Logitech Gaming Software/SDK/LED/{bitness}/LogitechLed.dll")
# It is best to use ProgramW6432: https://stackoverflow.com/a/51305013
try:
subpath_lgs = os.environ["ProgramW6432"]
except KeyError:
subpath_lgs = os.environ["ProgramFiles"]
path_dll = Path(subpath_lgs) / subpath_dll
else:
path_dll = Path(path_dll)
if path_dll.exists():
return ctypes.cdll.LoadLibrary(str(path_dll))
else:
raise SDKNotFoundException(f"The SDK DLL was not found at {path_dll}")
def set_target_device(self, device: DeviceType) -> bool:
"""Set the target device or device group that is affected by subsequent lighting calls.
Parameters
----------
device : DeviceType
The target device or group.
Returns
-------
bool
Whether or not the device action worked.
"""
return bool(self.dll.LogiLedSetTargetDevice(ctypes.c_int(device.value)))
def save_current_lighting(self) -> bool:
"""Save the current lighting that can be restored later."""
return bool(self.dll.LogiLedSaveCurrentLighting())
def restore_lighting(self) -> bool:
"""Restore the last saved lighting."""
return bool(self.dll.LogiLedRestoreLighting())
def flash_lighting(
self, red: int, green: int, blue: int, duration: int, interval: int
) -> bool:
"""Flashes the lighing color of the combined RGB percentages over
the specified millisecond duration and millisecond interval.
Note that RGB ranges from 0-255, but this function ranges from 0-100.
Parameters
----------
red : int
The red percentage, values from 0-100.
green : int
The green percentage, values from 0-100.
blue : int
The blue percentages, values from 0-100.
duration : int
The duration for the effect in ms, if 0 will go on until reset.
interval : int
The interval for the effect in ms.
Returns
-------
bool
Whether or not the flash instruction succeeded.
"""
red = ctypes.c_int(red)
green = ctypes.c_int(green)
blue = ctypes.c_int(blue)
duration = ctypes.c_int(duration)
interval = ctypes.c_int(interval)
return bool(self.dll.LogiLedFlashLighting(red, green, blue, duration, interval))
def pulse_lighting(
self, red: int, green: int, blue: int, duration: int, interval: int
) -> bool:
"""Pulses the lighting of the combined RGB percentages over the specified
millisecond duration and interval.
Note that RGB ranges from 0-255, but this function ranges from 0-100.
Parameters
----------
red : int
The red percentages, values from 0-100.
green : int
The green percentages, values from 0-100.
blue : int
The blue percentages, values from 0-100.
duration : int
The duration for the effect in ms, if 0 will go on until reset.
interval : int
The interval for the effect in ms.
Returns
-------
bool
Whether or not the pulse instruction succeeded.
"""
red = ctypes.c_int(red)
green = ctypes.c_int(green)
blue = ctypes.c_int(blue)
duration = ctypes.c_int(duration)
interval = ctypes.c_int(interval)
return bool(self.dll.LogiLedPulseLighting(red, green, blue, duration, interval))
def stop_effects(self) -> bool:
"""Stop all effects like pulse and flash."""
return bool(self.dll.LogiLedStopEffects())
def set_lighting_from_bitmap(self, bitmap: bytes) -> bool:
"""Sets the color of each key in a 21x6 rectangular area specified by the BGRA byte array bitmap.
Each element corresponds to the physical location of each location of each key.
Note that this function only applies to LOGI_DEVICETYPE_PERKEY_RGB devices.
Parameters
----------
bitmap : bytes
The bitmap with the colors.
Returns
-------
bool
Whether or not the lighting action worked.
"""
bitmap = ctypes.c_char_p(bitmap)
return bool(self.dll.LogiLedSetLightingFromBitmap(bitmap))
def set_lighting_for_key(
self, key: int, key_type: KeyType, red: int, green: int, blue: int
) -> bool:
"""Sets the lighting to the color of the combined RGB percentages for the specified key code.
Note that this function only applies to LOGI_DEVICETYPE_PERKEY_RGB devices.
Note that RGB ranges from 0-255, but this function ranges from 0-100.
Parameters
----------
key : int
The key.
key_type : KeyType
The type of the given key.
red : int
The red percentages, values from 0-100.
green : int
The green percentages, values from 0-100.
blue : int
The blue percentages, values from 0-100.
Returns
-------
bool
Whether or not the lighting action worked.
"""
key = ctypes.c_int(key)
red = ctypes.c_int(red)
green = ctypes.c_int(green)
blue = ctypes.c_int(blue)
type_to_key = {
KeyType.scan: self.dll.LogiLedSetLightingForKeyWithScanCode,
KeyType.hid: self.dll.LogiLedSetLightingForKeyWithHidCode,
KeyType.quartz: self.dll.LogiLedSetLightingForKeyWithQuartzCode,
KeyType.name: self.dll.LogiLedSetLightingForKeyWithKeyName,
}
try:
set_lighting = type_to_key[key_type]
except KeyError:
return False
return bool(set_lighting(key, red, green, blue))
def save_lighting_for_key(self, key_name: int) -> bool:
"""Saves the current lighting for the specified key.
Note that this function only applies to LOGI_DEVICETYPE_PERKEY_RGB devices.
Parameters
----------
key_name : int
The name of the key.
Returns
-------
bool
Whether or not the save succeeded.
"""
key_name = ctypes.c_int(key_name)
return bool(self.dll.LogiLedSaveLightingForKey(key_name))
def restore_lighting_for_key(self, key_name: int) -> bool:
"""Restores the last saved lighting for the given key.
Note that this function only applies to LOGI_DEVICETYPE_PERKEY_RGB devices.
Parameters
----------
key_name : int
The name of the key.
Returns
-------
bool
Whether or not the restoring succeeded.
"""
key_name = ctypes.c_int(key_name)
return bool(self.dll.LogiLedRestoreLightingForKey(key_name))
def flash_single_key(
self, key_name: int, red: int, green: int, blue: int, duration: int, interval: int
) -> bool:
"""Flashes the lighting color of the combined RGB percentages over the specified millisecond
duration and interval for the key with the given name.
Note that this function only applies to LOGI_DEVICETYPE_PERKEY_RGB devices.
Note that RGB ranges from 0-255, but this function ranges from 0-100.
Parameters
----------
key_name : int
The name of the key.
red : int
The red percentages, values from 0-100.
green : int
The green percentages, values from 0-100.
blue : int
The blue percentages, values from 0-100.
duration : int
The duration for the effect in ms, if 0 will go on until reset.
interval : int
The interval for the effect in ms.
Returns
-------
bool
Whether or not the flash action succeeded.
"""
key_name = ctypes.c_int(key_name)
red = ctypes.c_int(red)
green = ctypes.c_int(green)
blue = ctypes.c_int(blue)
duration = ctypes.c_int(duration)
interval = ctypes.c_int(interval)
return bool(self.dll.LogiLedFlashSingleKey(key_name, red, green, blue, duration, interval))
def pulse_single_key(
self,
key_name: int,
red_start: int,
green_start: int,
blue_start: int,
duration: int,
is_infinite: bool = False,
red_end: int = 0,
green_end: int = 0,
blue_end: int = 0,
) -> bool:
"""Pulses the lighting color of the combined RGB percentages over the specified millisecond
duration for the key with the given name. The color will gradually change from the starting color
to the ending color. If no ending color is specified, the ending color will be black.
The effect will stop after one interval unless is_infinite is set to True.
Note that this function only applies to LOGI_DEVICETYPE_PERKEY_RGB devices.
Note that RGB ranges from 0-255, but this function ranges from 0-100.
Parameters
----------
key_name : int
The name of the key.
red_start : int
The starting red percentage, values from 0-100.
green_start : int
The starting green percentage, values from 0-100.
blue_start : int
The starting blue percentage, values from 0-100.
duration : int
The duration for the effect in ms, if 0 will go on until reset.
is_infinite : bool, optional
Set this to True if you want the pulse to go on. By default False.
red_end : int
The ending red percentage, values from 0-100. By default 0.
green_end : int
The ending green percentage, values from 0-100. By default 0.
blue_end : int
The ending blue percentage, values from 0-100. By default 0.
Returns
-------
bool
Whether or not the pulse action succeeded.
"""
key_name = ctypes.c_int(key_name)
red_start = ctypes.c_int(red_start)
green_start = ctypes.c_int(green_start)
blue_start = ctypes.c_int(blue_start)
red_end = ctypes.c_int(red_end)
green_end = ctypes.c_int(green_end)
blue_end = ctypes.c_int(blue_end)
duration = ctypes.c_int(duration)
is_infinite = ctypes.c_bool(is_infinite)
return bool(
self.dll.LogiLedPulseSingleKey(
key_name,
red_start,
green_start,
blue_start,
red_end,
green_end,
blue_end,
duration,
is_infinite,
)
)
def stop_effects_on_key(self, key_name: int) -> bool:
"""Stop all effects on the given key.
Parameters
----------
key_name : int
The key name.
Returns
-------
bool
Whether or not the stop action succeeded.
"""
key_name = ctypes.c_int(key_name)
return bool(self.dll.LogiLedStopEffectsOnKey(key_name))
def get_config_option_number(self, key: str, default: int = 0) -> Optional[float]:
"""Get the default value for the configuration key as a float.
If the call fails, the return value is None.
Parameters
----------
key : str
The configuration key, like 'health/low_health_threshold'.
default : int, optional
The default value for the configuration, by default 0.
Returns
-------
Optional[float]
The value for the configuration key or if the call fails None.
"""
key = ctypes.c_wchar_p(key)
default = ctypes.c_couble(default)
if self.dll.LogiGetConfigOptionNumber(key, ctypes.pointer(default), _LOGI_SHARED_SDK_LED):
return default.value
return None
def get_config_option_bool(self, key: str, default: bool = False) -> Optional[bool]:
"""Get the default value for the configuration key as a bool.
If the call fails, the return value is None.
Parameters
----------
key : str
The configuration key, like 'health/pulse_on_low'.
default : bool, optional
The default value for the configuration, by default False
Returns
-------
Optional[bool]
The value for the configuration key or if the call fails None.
"""
key = ctypes.c_wchar_p(key)
default = ctypes.c_bool(default)
if self.dll.LogiGetConfigOptionBool(key, ctypes.pointer(default), _LOGI_SHARED_SDK_LED):
return default.value
return None
def get_config_option_color(self, key: str, *args) -> Optional[Color]:
"""Get the default value for the configuration key as a color.
If the call fails, the return value is None.
Note: you can either call this function with rgb percentage values or with a Color object.
Parameters
----------
key : str
The configuration key.
Returns
-------
Optional[Color]
The color for the configuration key or if the call fails None.
"""
key = ctypes.c_wchar_p(key)
default = None
red, green, blue = 0, 0, 0
if isinstance(args[0], Color):
default = args[0]
red = ctypes.c_int(default.red)
green = ctypes.c_int(default.green)
blue = ctypes.c_int(default.blue)
else:
red_pct, green_pct, blue_pct = args[0], args[1], args[2]
red = ctypes.c_int(int((red_pct / 100) * 255))
green = ctypes.c_int(int((green_pct / 100) * 255))
blue = ctypes.c_int(int((blue_pct / 100) * 255))
if self.dll.LogiGetConfigOptionColor(
key,
ctypes.pointer(red),
ctypes.pointer(green),
ctypes.pointer(blue),
_LOGI_SHARED_SDK_LED,
):
return Color(red.value, green.value, blue.value)
return None
def get_config_option_key_input(self, key: str, default: str = "") -> Optional[str]:
"""Get the default value for the key as a input key.
If the call fails, the return value is None.
Parameters
----------
key : str
The configuration key, e.g. 'abilities/primary'.
default : str, optional
The default value for this configuration key, by default "".
Returns
-------
Optional[str]
The value for the configuration key or if the call fails None.
Examples
--------
self.get_config_option_key_input('abilities/primary', 'A')
"""
key = ctypes.c_wchar_p(key)
default_key = ctypes.create_string_buffer(256)
default_key.value = default
if self.dll.LogiGetConfigOptionKeyInput(key, default_key, _LOGI_SHARED_SDK_LED):
return str(default_key.value)
return None
def set_config_option_label(self, key: str, label: str) -> bool:
"""Set the label for a configuration key.
Parameters
----------
key : str
The configuration key.
label : str
The configuration label.
Returns
-------
bool
Whether or not the configuration setting succeeded.
Examples
--------
self.set_config_option_label('health', 'Health')
self.set_config_option_label('health/pulse_on_low', 'Pulse on Low')
"""
key = ctypes.c_wchar_p(key)
label = ctypes.c_wchar_p(label)
return bool(self.dll.LogiSetConfigOptionLabel(key, label, _LOGI_SHARED_SDK_LED))