Skip to content

Commit 2cf03fc

Browse files
committed
Create libmk_get_device_ident function and remove IntEnum
1 parent 69b99aa commit 2cf03fc

File tree

7 files changed

+95
-82
lines changed

7 files changed

+95
-82
lines changed

.gitignore

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ CMakeCache.txt
7676
*CMakeTmp*
7777
*CMakeFiles*
7878
*CMakeCache.txt
79+
install_manifest.txt
7980

8081
# Python packages
8182
/*.egg-info
@@ -84,5 +85,6 @@ CMakeCache.txt
8485
*.pyc
8586

8687
# Executables
87-
record
88-
main
88+
/record
89+
/main
90+
/ambilight

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ addons:
1515
- python-dev
1616
- python3-dev
1717
install:
18-
- sudo python -m pip install -U pip scikit-build enum
18+
- sudo python -m pip install -U pip scikit-build
1919
before_script:
2020
- mkdir cmake-build
2121
script:

libmk/libmk.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
* The variables are required so that if the library is loaded into a
3333
* different language (like Python), interaction is easier.
3434
*/
35-
static LibMK_Handle* DeviceHandle;
3635
static libusb_context* Context;
36+
static LibMK_Handle* DeviceHandle;
3737

3838

3939
typedef enum LibMK_Model LibMK_Model;
@@ -410,6 +410,16 @@ LibMK_Model libmk_ident_model(char* product) {
410410
}
411411

412412

413+
int libmk_get_device_ident(LibMK_Handle* handle) {
414+
/** Return the bDevice attribute of a LibMK_Handle struct */
415+
if (handle == NULL)
416+
handle = DeviceHandle;
417+
if (handle == NULL)
418+
return 0x0000;
419+
return DeviceHandle->bDevice;
420+
}
421+
422+
413423
int libmk_enable_control(LibMK_Handle* handle) {
414424
/** Enable LED control for a keyboard by sending the enable packet
415425
*

libmk/libmk.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,7 @@ typedef struct LibMK_Device {
117117

118118

119119
typedef struct LibMK_Handle {
120-
/** Struct describing an opened device
121-
*
122-
*
123-
*/
120+
/** Struct describing an opened device */
124121
LibMK_Model model;
125122
int bVendor;
126123
int bDevice;
@@ -139,7 +136,6 @@ typedef struct LibMK_Effect_Details {
139136
unsigned char background[3];
140137
} LibMK_Effect_Details;
141138

142-
143139
/** Library management and initialization */
144140
bool libmk_init();
145141
int libmk_exit();
@@ -169,6 +165,7 @@ int libmk_disable_control(LibMK_Handle* handle);
169165
int libmk_claim_interface(LibMK_Handle* handle);
170166
int libmk_send_control_packet(LibMK_Handle* handle);
171167
int libmk_reset(LibMK_Handle* handle);
168+
int libmk_get_device_ident(LibMK_Handle* handle);
172169

173170

174171
/** Communication */

masterkeys/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Author: RedFantom
22
# License: GNU GPLv3
33
# Copyright (c) 2018 RedFantom
4+
project(masterkeys VERSION 0.1.0 DESCRIPTION "Wrapper around libmk")
45
find_package(PythonInterp REQUIRED)
56
find_package(PythonLibs REQUIRED)
67
find_package(PythonExtensions)

masterkeys/__init__.py

Lines changed: 65 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,36 @@
2121
Set the device to use with the library. Only a single device is
2222
supported. If multiple devices of the same model are connected, the
2323
first encountered is set to control.
24-
:param model: enum Model of device to set
25-
:return: enum Result code
24+
:param model: Model number of device to set
25+
:return: result code (SUCCESS or ERR*)
2626
:raises: TypeError upon invalid argument type
2727
2828
int enable_control()
2929
Send the control packet to the keyboard set to control.
30-
:return: enum Result code
30+
:return: result code (SUCCESS or ERR*)
3131
3232
int disable_control()
3333
Send the packet to release control to the keyboard set to control.
34-
:return: enum Result code
34+
:return: result code (SUCCESS or ERR*)
3535
3636
int set_effect(effect)
3737
Set one of the built-in LED effects on the keyboard.
38-
:param effect: enum Effect to enable
39-
:return: enum Result code
38+
:param effect: Effect number (EFF*)
39+
:return: result code (SUCCESS or ERR*)
4040
:raises: TypeError upon invalid argument type
4141
4242
int set_full_led_color(r, g, b)
4343
Set the color of all the LEDs on the keyboard to a single color.
4444
:param r: Red color value, 0-255 (two symbol HEX)
4545
:param g: Green color value
4646
:param b: Blue color value
47-
:return: enum Result code
47+
:return: result code (SUCCESS or ERR*)
4848
:raises: TypeError upon invalid argument type or count
4949
5050
int set_all_led_color(layout)
5151
Set the color of all the LEDs on the keyboard from a list of lists
5252
:param layout: List of lists of tuples ([row][col][index])
53-
:return: enum Result code
53+
:return: result code (SUCCESS or ERR*)
5454
:raises: TypeError if invalid type in the argument
5555
:raises: ValueError if invalid amount of elements in the argument
5656
@@ -59,79 +59,71 @@
5959
conjunction with the set_full_led_color function.
6060
:param row, col: Coordinates of LED to set color of
6161
:param r, g, b: Color values
62-
:return: enum Result code
62+
:return: result code (SUCCESS or ERR*)
6363
:raises: TypeError upon invalid argument type
6464
"""
65-
from enum import Enum
6665
from .masterkeys import *
6766

6867

6968
MAX_ROWS = 7
7069
MAX_COLS = 24
7170

72-
73-
class Result(Enum):
74-
"""Describes error codes that are used in libmk"""
75-
# Success codes
76-
SUCCESS = 0
77-
78-
# Device Errors
79-
ERR_INVALID_DEV = 1
80-
ERR_DEV_NOT_CONNECTED = 2
81-
ERR_DEV_NOT_SET = 3
82-
ERR_UNKNOWN_LAYOUT = 14
83-
ERR_DEV_NOT_CLOSED = 15
84-
ERR_DEV_RESET_FAILED = 16
85-
86-
# Interface Errors
87-
ERR_IFACE_CLAIM_FAILED = 4
88-
ERR_IFACE_RELEASE_FAILED = 5
89-
ERR_DEV_CLOSE_FAILED = 6
90-
ERR_DEV_OPEN_FAILED = 7
91-
92-
# Kernel Driver Errors
93-
ERR_KERNEL_DRIVER = 8
94-
95-
# Communication Errors
96-
ERR_DEV_LIST = 9
97-
ERR_TRANSFER = 10
98-
ERR_DESCR = 11 # Descriptor
99-
ERR_SEND = 12
100-
101-
# Protocol Errors
102-
ERR_PROTOCOL = 13
103-
104-
105-
class Effect(Enum):
106-
"""Describes built-in LED Effect numbers"""
107-
EFF_FULL_ON = 0
108-
EFF_BREATH = 1
109-
EFF_BREATH_CYCLE = 2
110-
EFF_SINGLE = 3
111-
EFF_WAVE = 4
112-
EFF_RIPPLE = 5
113-
EFF_CROSS = 6
114-
EFF_RAIN = 7
115-
EFF_STAR = 8
116-
EFF_SNAKE = 9
117-
EFF_REC = 10
118-
EFF_SPECTRUM = 11
119-
EFF_RAPID_FIRE = 12
120-
121-
122-
class Model(Enum):
123-
"""Describes models supported by this library"""
124-
RGB_L = 0
125-
RGB_M = 5
126-
RGB_S = 1
127-
128-
WHITE_L = 2
129-
WHITE_M = 3
130-
WHITE_S = 7
131-
132-
NOT_SET = -1
133-
ANY = -2
134-
UNKNOWN = -3
71+
# Success codes
72+
SUCCESS = 0
73+
74+
# Device Errors
75+
ERR_INVALID_DEV = 1
76+
ERR_DEV_NOT_CONNECTED = 2
77+
ERR_DEV_NOT_SET = 3
78+
ERR_UNKNOWN_LAYOUT = 14
79+
ERR_DEV_NOT_CLOSED = 15
80+
ERR_DEV_RESET_FAILED = 16
81+
82+
# Interface Errors
83+
ERR_IFACE_CLAIM_FAILED = 4
84+
ERR_IFACE_RELEASE_FAILED = 5
85+
ERR_DEV_CLOSE_FAILED = 6
86+
ERR_DEV_OPEN_FAILED = 7
87+
88+
# Kernel Driver Errors
89+
ERR_KERNEL_DRIVER = 8
90+
91+
# Communication Errors
92+
ERR_DEV_LIST = 9
93+
ERR_TRANSFER = 10
94+
ERR_DESCR = 11 # Descriptor
95+
ERR_SEND = 12
96+
97+
# Protocol Errors
98+
ERR_PROTOCOL = 13
99+
100+
# Effects
101+
EFF_FULL_ON = 0
102+
EFF_BREATH = 1
103+
EFF_BREATH_CYCLE = 2
104+
EFF_SINGLE = 3
105+
EFF_WAVE = 4
106+
EFF_RIPPLE = 5
107+
EFF_CROSS = 6
108+
EFF_RAIN = 7
109+
EFF_STAR = 8
110+
EFF_SNAKE = 9
111+
EFF_REC = 10
112+
EFF_SPECTRUM = 11
113+
EFF_RAPID_FIRE = 12
114+
115+
# Models
116+
MODEL_RGB_L = 0
117+
MODEL_RGB_M = 5
118+
MODEL_RGB_S = 1
119+
120+
MODEL_WHITE_L = 2
121+
MODEL_WHITE_M = 3
122+
MODEL_WHITE_S = 7
123+
124+
MODEL_NOT_SET = -1
125+
MODEL_ANY = -2
126+
MODEL_UNKNOWN = -3
135127

136128

137129
def set_all_led_color_dict(keys):

masterkeys/masterkeys.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,12 @@ static PyObject* masterkeys_set_effect_details(PyObject* self, PyObject* args) {
228228
}
229229

230230

231+
static PyObject* masterkeys_get_device_ident(PyObject* self, PyObject* args) {
232+
/** Return the bDevice value for the controlled keyboard */
233+
return PyInt_FromLong(libmk_get_device_ident(NULL));
234+
}
235+
236+
231237
static struct PyMethodDef masterkeys_funcs[] = {
232238
{
233239
"detect_devices",
@@ -276,6 +282,11 @@ static struct PyMethodDef masterkeys_funcs[] = {
276282
masterkeys_set_effect_details,
277283
METH_VARARGS,
278284
"Set the effect on the keyboard with specific arguments"
285+
}, {
286+
"get_device_ident",
287+
masterkeys_get_device_ident,
288+
METH_NOARGS,
289+
"Return the bDevice USB descriptor value"
279290
}, {NULL, NULL, 0, NULL}
280291
};
281292

0 commit comments

Comments
 (0)