Skip to content

Commit fd1fdee

Browse files
committed
move set_user_keymap to usb_host module
1 parent 6174e58 commit fd1fdee

File tree

2 files changed

+33
-31
lines changed

2 files changed

+33
-31
lines changed

shared-bindings/usb/__init__.c

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -37,41 +37,10 @@
3737
//| The `usb` is a subset of PyUSB that allows you to communicate to USB devices.
3838
//| """
3939
//|
40-
//| def set_user_keymap(keymap: ReadableBuffer, /) -> None:
41-
//| """Set the keymap used by a USB HID keyboard in kernel mode
42-
//|
43-
//| The keymap consists of 256 or 384 1-byte entries that map from USB keycodes
44-
//| to ASCII codes. The first 128 entries are for unmodified keys,
45-
//| the next 128 entries are for shifted keys,and the next optional 128 entries are
46-
//| for altgr-modified keys.
47-
//|
48-
//| The values must all be ASCII (32 through 126 inclusive); other values are not valid.
49-
//|
50-
//| The values at index 0, 128, and 256 (if the keymap has 384 entries) must be
51-
//| 0; other values are reserved for future expansion to indicate alternate
52-
//| keymap formats.
53-
//|
54-
//| At other indices, the value 0 is used to indicate that the normal
55-
//| definition is still used. For instance, the entry for HID_KEY_ARROW_UP
56-
//| (0x52) is usually 0 so that the default behavior of sending an escape code
57-
//| is preserved.
58-
//|
59-
//| This function is a CircuitPython extension not present in PyUSB
60-
//| """
61-
//|
62-
STATIC mp_obj_t usb_set_user_keymap(mp_obj_t buf_in) {
63-
mp_buffer_info_t bufinfo;
64-
mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ);
65-
usb_keymap_set(bufinfo.buf, bufinfo.len);
66-
return mp_const_none;
67-
}
68-
69-
MP_DEFINE_CONST_FUN_OBJ_1(usb_set_user_keymap_obj, usb_set_user_keymap);
7040

7141
STATIC mp_rom_map_elem_t usb_module_globals_table[] = {
7242
{ MP_ROM_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_usb) },
7343
{ MP_ROM_QSTR(MP_QSTR_core), MP_OBJ_FROM_PTR(&usb_core_module) },
74-
{ MP_ROM_QSTR(MP_QSTR_set_user_keymap), MP_OBJ_FROM_PTR(&usb_set_user_keymap_obj) },
7544
};
7645

7746
STATIC MP_DEFINE_CONST_DICT(usb_module_globals, usb_module_globals_table);

shared-bindings/usb_host/__init__.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,49 @@
3030

3131
#include "shared-bindings/usb_host/__init__.h"
3232
#include "shared-bindings/usb_host/Port.h"
33+
#include "supervisor/usb.h"
3334

3435
//| """USB Host
3536
//|
3637
//| The `usb_host` module allows you to manage USB host ports. To communicate
3738
//| with devices use the `usb` module that is a subset of PyUSB's API.
3839
//| """
40+
//|
41+
//| def set_user_keymap(keymap: ReadableBuffer, /) -> None:
42+
//| """Set the keymap used by a USB HID keyboard in kernel mode
43+
//|
44+
//| The keymap consists of 256 or 384 1-byte entries that map from USB keycodes
45+
//| to ASCII codes. The first 128 entries are for unmodified keys,
46+
//| the next 128 entries are for shifted keys,and the next optional 128 entries are
47+
//| for altgr-modified keys.
48+
//|
49+
//| The values must all be ASCII (32 through 126 inclusive); other values are not valid.
50+
//|
51+
//| The values at index 0, 128, and 256 (if the keymap has 384 entries) must be
52+
//| 0; other values are reserved for future expansion to indicate alternate
53+
//| keymap formats.
54+
//|
55+
//| At other indices, the value 0 is used to indicate that the normal
56+
//| definition is still used. For instance, the entry for HID_KEY_ARROW_UP
57+
//| (0x52) is usually 0 so that the default behavior of sending an escape code
58+
//| is preserved.
59+
//|
60+
//| This function is a CircuitPython extension not present in PyUSB
61+
//| """
62+
//|
63+
STATIC mp_obj_t usb_set_user_keymap(mp_obj_t buf_in) {
64+
mp_buffer_info_t bufinfo;
65+
mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ);
66+
usb_keymap_set(bufinfo.buf, bufinfo.len);
67+
return mp_const_none;
68+
}
69+
70+
MP_DEFINE_CONST_FUN_OBJ_1(usb_set_user_keymap_obj, usb_set_user_keymap);
3971

4072
STATIC mp_map_elem_t usb_host_module_globals_table[] = {
4173
{ MP_ROM_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_usb_host) },
4274
{ MP_ROM_QSTR(MP_QSTR_Port), MP_OBJ_FROM_PTR(&usb_host_port_type) },
75+
{ MP_ROM_QSTR(MP_QSTR_set_user_keymap), MP_OBJ_FROM_PTR(&usb_set_user_keymap_obj) },
4376
};
4477

4578
STATIC MP_DEFINE_CONST_DICT(usb_host_module_globals, usb_host_module_globals_table);

0 commit comments

Comments
 (0)