Skip to content

Commit 8ad20f1

Browse files
committed
Update Python wrapper PyMethodDef
1 parent 684cbdb commit 8ad20f1

File tree

5 files changed

+148
-52
lines changed

5 files changed

+148
-52
lines changed

clean.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
rm -rf CMakeFiles CMakeCache.txt _skbuild
2+
rm record main ambilight record ambilight *.cmake *.so*

libmk/libmk.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
/**
2-
* Author: RedFantom
1+
/** Author: RedFantom
32
* License: GNU GPLv3
43
* Copyright (c) 2018-2019 RedFantom
54
*/
@@ -58,6 +57,17 @@ const unsigned int ISO[] =
5857
const unsigned char LAYOUT_ANSI = 0;
5958
const unsigned char LAYOUT_ISO = 1;
6059

60+
const char* LIBMK_MODEL_STRINGS[] = {
61+
"MasterKeys Pro L RGB",
62+
"MasterKeys Pro S RGB",
63+
"MasterKeys Pro L White",
64+
"MasterKeys Pro M White",
65+
"Unknown Model",
66+
"MasterKeys Pro M RGB",
67+
"Unknown Model",
68+
"MasterKeys Pro S White",
69+
};
70+
6171
/** Layout matrices
6272
*
6373
* These matrices describe the internal addresses of keys within the
@@ -168,6 +178,18 @@ const unsigned char LIBMK_LAYOUT[2][3][LIBMK_MAX_ROWS][LIBMK_MAX_COLS] = {
168178
};
169179

170180

181+
const char* LIBMK_MODEL_STRINGS[] = {
182+
"MasterKeys Pro L RGB", // 0
183+
"MasterKeys Pro S RGB", // 1
184+
"MasterKeys Pro L White", // 2
185+
"MasterKeys Pro M White", // 3
186+
"Unknown Model", // 4
187+
"MasterKeys Pro M RGB", // 5
188+
"Unknown Model", // 6
189+
"MasterKeys Pro S White",
190+
};
191+
192+
171193
bool libmk_init(void) {
172194
int result = libusb_init(&Context);
173195
#ifdef LIBMK_USB_DEBUG

libmk/libmk.h

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ typedef enum LibMK_Model {
127127
} LibMK_Model;
128128

129129

130+
<<<<<<< HEAD
130131
/** @brief Struct describing a supported USB device
131132
*
132133
* This struct may be used as a linked list. Holds information required
@@ -144,17 +145,7 @@ typedef struct LibMK_Device {
144145

145146

146147
/** @brief Array of strings representing the supported models */
147-
const char* LIBMK_MODEL_STRINGS[] = {
148-
"MasterKeys Pro L RGB", // 0
149-
"MasterKeys Pro S RGB", // 1
150-
"MasterKeys Pro L White", // 2
151-
"MasterKeys Pro M White", // 3
152-
"Unknown Model", // 4
153-
"MasterKeys Pro M RGB", // 5
154-
"Unknown Model", // 6
155-
"MasterKeys Pro S White",
156-
};
157-
148+
const char* LIBMK_MODEL_STRINGS[];
158149

159150
/** @brief Struct describing an opened supported device
160151
*
@@ -319,14 +310,6 @@ int libmk_reset(LibMK_Handle* handle);
319310

320311
/** @brief Internal function. Return the bDevice USB descriptor property */
321312
int libmk_get_device_ident(LibMK_Handle* handle);
322-
<<<<<<< HEAD
323-
=======
324-
int libmk_get_firmware_version(LibMK_Handle* handle, LibMK_Firmware** fw);
325-
int libmk_set_active_profile(LibMK_Handle* handle, char profile);
326-
int libmk_get_active_profile(LibMK_Handle* handle, char* profile);
327-
int libmk_save_profile(LibMK_Handle* handle);
328-
<<<<<<< HEAD
329-
>>>>>>> ce36524... Fix libmk_create_handle
330313

331314
/** @brief Retrieve details on the firmware version of the device
332315
*
@@ -337,9 +320,9 @@ int libmk_save_profile(LibMK_Handle* handle);
337320
* @returns LibMK_Result result code, NULL or LibMK_Firmware* in fw
338321
*/
339322
int libmk_get_firmware_version(LibMK_Handle* handle, LibMK_Firmware** fw);
340-
=======
323+
324+
341325
int libmk_set_control_mode(LibMK_Handle* handle, LibMK_ControlMode mode);
342-
>>>>>>> 748c903... Fix errors during profile control
343326

344327
/** @brief Send a single packet and verify the response
345328
*
@@ -355,11 +338,7 @@ int libmk_set_control_mode(LibMK_Handle* handle, LibMK_ControlMode mode);
355338
* pointer.
356339
*/
357340
int libmk_send_packet(LibMK_Handle* handle, unsigned char* packet);
358-
<<<<<<< HEAD
359-
=======
360-
int libmk_send_recv_packet(LibMK_Handle* handle, unsigned char* packet, bool r);
361-
unsigned char* libmk_build_packet(unsigned char predef, ...);
362-
>>>>>>> 748c903... Fix errors during profile control
341+
363342

364343
/** @brief Exchange a single packet with the keyboard
365344
*
@@ -374,14 +353,6 @@ unsigned char* libmk_build_packet(unsigned char predef, ...);
374353
*/
375354
int libmk_exch_packet(LibMK_Handle* handle, unsigned char* packet);
376355

377-
/** @brief Build a packet using the given data
378-
*
379-
* @param predef: Amount of predefined bytes in the packet
380-
* @param ...: bytes that are predefined, starting from index 0
381-
* @returns Pointer to the allocated array of bytes
382-
*/
383-
unsigned char* libmk_build_packet(unsigned char predef, ...);
384-
385356
/** @brief Set effect to be active on keyboard
386357
*
387358
* @param handle: LibMK_Handle for device to set effect on. If NULL uses
@@ -489,5 +460,29 @@ int libmk_get_active_profile(LibMK_Handle* handle, char* profile);
489460
*/
490461
int libmk_save_profile(LibMK_Handle* handle);
491462

463+
/** @brief Send a packet specifying whether to expect a response
464+
*
465+
* @param handle: LibMK_Handle of the device to send the packet to. If
466+
* NULL the global device handle is used.
467+
* @param packet: Pointer to array of packet to send.
468+
* @param r: Whether to expect a response. If ``false``, the function
469+
* still attempts to read a response to make sure that the buffer is
470+
* empty. If ``true``, performs protocol error checks and yields an
471+
* error if no response was received.
472+
* @returns LibMK_Result result code
473+
*/
474+
int libmk_send_recv_packet(LibMK_Handle* handle, unsigned char* packet, bool r);
475+
476+
/** @brief Build a new packet of data that can be sent to keyboard
477+
*
478+
* @param predef: Amount of bytes given in the variable arguments
479+
* @param ...: Bytes to from index zero of the packet. The amount of
480+
* bytes given must be equal to the amount specified by ``predef``,
481+
* otherwise this function will segfault.
482+
* @returns Pointer to the allocated packet with the set bytes. NULL if
483+
* no memory could be allocated.
484+
*/
485+
unsigned char* libmk_build_packet(unsigned char predef, ...);
486+
492487
/** Debugging purposes */
493488
void libmk_print_packet(unsigned char* packet, char* label);

masterkeys/__init__.py

Lines changed: 72 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ class Model:
7878
MODEL_UNKNOWN = -3
7979

8080

81+
class ControlMode:
82+
FIRMWARE_CTRL = 0x00
83+
EFFECT_CTRL = 0x01
84+
CUSTOM_CTRL = 0x02
85+
PROFILE_CTRL = 0x03
86+
87+
8188
MODEL_STRINGS = {
8289
0: "MasterKeys Pro L RGB",
8390
5: "MasterKeys Pro M RGB",
@@ -111,9 +118,9 @@ def set_device(model):
111118
is supported in the Python library. Only the first found device
112119
of the specified model is controlled.
113120
:type model: int
114-
:return: Result code
121+
:return: Result code (:class:`.ResultCode`)
115122
:rtype: int
116-
:raises: TypeError upon invalid argument type
123+
:raises: ``TypeError`` upon invalid argument type
117124
"""
118125
return _mk.set_device(model)
119126

@@ -123,7 +130,7 @@ def enable_control():
123130
"""
124131
Enable control on the device that has been set
125132
126-
:return: Result code
133+
:return: Result code (:class:`.ResultCode`)
127134
:rtype: int
128135
"""
129136
return _mk.enable_control()
@@ -134,7 +141,7 @@ def disable_control():
134141
"""
135142
Disable control on the device that has been set and is controlled
136143
137-
:return: Result code
144+
:return: Result code (:class:`.ResultCode`)
138145
:rtype: int
139146
"""
140147
return _mk.disable_control()
@@ -145,11 +152,11 @@ def set_effect(effect):
145152
"""
146153
Set the effect to be active on the controlled keyboard
147154
148-
:param effect: Effect number to set to be active
155+
:param effect: Effect number to set to be active (:class:`.Effect`)
149156
:type effect: int
150-
:return: Result code
157+
:return: Result code (:class:`.ResultCode`)
151158
:rtype: int
152-
:raises: TypeError upon invalid argument type
159+
:raises: ``TypeError`` upon invalid argument type
153160
"""
154161
return _mk.set_effect(effect)
155162

@@ -162,10 +169,11 @@ def set_all_led_color(layout):
162169
:param layout: List of lists of color tuples such as created by
163170
build_layout_list()
164171
:type layout: List[List[Tuple[int, int, int], ...], ...]
165-
:return: Result code
172+
:return: Result code (:class:`.ResultCode`)
166173
:rtype: int
167-
:raises: ValueError if the wrong amount of elements is in the list
168-
:raises: TypeError if invalid argument type (any element)
174+
:raises: ``ValueError`` if the wrong amount of elements is in the
175+
list
176+
:raises: ``TypeError`` if invalid argument type (any element)
169177
"""
170178
return _mk.set_all_led_color(layout)
171179

@@ -181,7 +189,7 @@ def set_full_led_color(r, g, b):
181189
:type g: int
182190
:param b: blue color byte
183191
:type b: int
184-
:return: Result code
192+
:return: Result code (:class:`.ResultCode`)
185193
:rtype: int
186194
"""
187195
return _mk.set_full_led_color(r, g, b)
@@ -202,9 +210,9 @@ def set_ind_led_color(row, col, r, g, b):
202210
:type g: int
203211
:param b: blue color byte
204212
:type b: int
205-
:return: Result code
213+
:return: Result code (:class:`.ResultCode`)
206214
:rtype: int
207-
:raises: TypeError upon invalid argument type
215+
:raises: ``TypeError`` upon invalid argument type
208216
"""
209217
return _mk.set_ind_led_color(row, col, r, g, b)
210218

@@ -255,7 +263,7 @@ def set_effect_details(effect, direction=0, speed=0x60, amount=0x00,
255263
For more details about the parameters, please see the libmk
256264
documentation.
257265
258-
:param effect: Effect number
266+
:param effect: Effect number (:class:`.Effect`)
259267
:type effect: int
260268
:param direction: Direction of the animation of the effect
261269
:type direction: int
@@ -267,8 +275,57 @@ def set_effect_details(effect, direction=0, speed=0x60, amount=0x00,
267275
:type foreground: Tuple[int, int, int]
268276
:param background: Background color of the effect
269277
:type background: Tuple[int, int, int]
270-
:return: Result code
278+
:return: Result code (:class:`.ResultCode`)
271279
:rtype: int
272280
"""
273281
return _mk.set_effect_details(
274282
effect, direction, speed, amount, foreground, background)
283+
284+
285+
def get_active_profile():
286+
# type: () -> int
287+
"""
288+
Return the number of the profile active on the keyboard
289+
290+
:return: Result code (:class:`.ResultCode`) or profile number
291+
:rtype: int
292+
"""
293+
return _mk.get_active_profile()
294+
295+
296+
def set_active_profile(profile):
297+
# type: (int) -> int
298+
"""
299+
Activate a profile on the keyboard
300+
301+
:param profile: Number of profile to activate
302+
:type profile: int
303+
:return: Result code (:class:`.ResultCode`)
304+
:rtype: int
305+
"""
306+
return _mk.set_active_profile(profile)
307+
308+
309+
def save_profile():
310+
# type: () -> int
311+
"""
312+
Save the changes made to the lighting to the active profile
313+
314+
:return: Result code (:class:`.ResultCode`)
315+
:rtype: int
316+
"""
317+
return _mk.save_profile()
318+
319+
320+
def set_control_mode(mode):
321+
# type: (int) -> int
322+
"""
323+
Change the control mode of the keyboard manually
324+
325+
:param mode: New control mode to set (:class:`.ControlMode`)
326+
:type mode: int
327+
:return: Result code (:class:`.ResultCode`)
328+
:rtype: int
329+
"""
330+
return _mk.set_control_mode(mode)
331+

masterkeys/masterkeys.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,27 @@ static struct PyMethodDef masterkeys_funcs[] = {
323323
masterkeys_get_device_ident,
324324
METH_NOARGS,
325325
"Return the bDevice USB descriptor value"
326-
}, {NULL, NULL, 0, NULL}
326+
}, {
327+
"get_active_profile",
328+
masterkeys_get_active_profile,
329+
METH_VARARGS,
330+
"Return the number of the active profile"
331+
}, {
332+
"set_active_profile",
333+
masterkeys_set_active_profile,
334+
METH_VARARGS,
335+
"Set the active profile on the keyboard"
336+
}, {
337+
"save_profile",
338+
masterkeys_save_profile,
339+
METH_VARARGS,
340+
"Save the changes made to the active profile"
341+
}, {
342+
"set_control_mode",
343+
masterkeys_set_control_mode,
344+
METH_VARARGS,
345+
"Set the control mode of the keyboard"
346+
},{NULL, NULL, 0, NULL}
327347
};
328348

329349

0 commit comments

Comments
 (0)