Skip to content

Commit 2a83657

Browse files
authored
Merge pull request #8161 from jepler/usb_host_keyboard
usb host: add keyboard map control in usb workflow
2 parents e2e8b16 + fd1fdee commit 2a83657

File tree

5 files changed

+210
-93
lines changed

5 files changed

+210
-93
lines changed

shared-bindings/usb/__init__.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@
3030

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

3435
//| """PyUSB-compatible USB host API
3536
//|
3637
//| The `usb` is a subset of PyUSB that allows you to communicate to USB devices.
3738
//| """
39+
//|
3840

3941
STATIC mp_rom_map_elem_t usb_module_globals_table[] = {
4042
{ MP_ROM_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_usb) },

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);

shared-module/usb/utf16le.c

Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -26,60 +26,49 @@
2626

2727
#include "shared-module/usb/utf16le.h"
2828

29-
STATIC void _convert_utf16le_to_utf8(const uint16_t *utf16, size_t utf16_len, uint8_t *utf8, size_t utf8_len) {
30-
// TODO: Check for runover.
31-
(void)utf8_len;
29+
typedef struct {
30+
const uint16_t *buf;
31+
size_t len;
32+
} utf16_str;
3233

33-
for (size_t i = 0; i < utf16_len; i++) {
34-
uint16_t chr = utf16[i];
35-
if (chr < 0x80) {
36-
*utf8++ = chr & 0xff;
37-
} else if (chr < 0x800) {
38-
*utf8++ = (uint8_t)(0xC0 | (chr >> 6 & 0x1F));
39-
*utf8++ = (uint8_t)(0x80 | (chr >> 0 & 0x3F));
40-
} else if (chr < 0x10000) {
41-
// TODO: Verify surrogate.
42-
*utf8++ = (uint8_t)(0xE0 | (chr >> 12 & 0x0F));
43-
*utf8++ = (uint8_t)(0x80 | (chr >> 6 & 0x3F));
44-
*utf8++ = (uint8_t)(0x80 | (chr >> 0 & 0x3F));
45-
} else {
46-
// TODO: Handle UTF-16 code points that take two entries.
47-
uint32_t hc = ((chr & 0xFFFF0000) - 0xD8000000) >> 6; /* Get high 10 bits */
48-
chr = (chr & 0xFFFF) - 0xDC00; /* Get low 10 bits */
49-
chr = (hc | chr) + 0x10000;
50-
*utf8++ = (uint8_t)(0xF0 | (chr >> 18 & 0x07));
51-
*utf8++ = (uint8_t)(0x80 | (chr >> 12 & 0x3F));
52-
*utf8++ = (uint8_t)(0x80 | (chr >> 6 & 0x3F));
53-
*utf8++ = (uint8_t)(0x80 | (chr >> 0 & 0x3F));
54-
}
34+
STATIC uint32_t utf16str_peek_unit(utf16_str *utf) {
35+
if (!utf->len) {
36+
return 0;
5537
}
38+
return *utf->buf;
5639
}
5740

58-
// Count how many bytes a utf-16-le encoded string will take in utf-8.
59-
STATIC mp_int_t _count_utf8_bytes(const uint16_t *buf, size_t len) {
60-
size_t total_bytes = 0;
61-
for (size_t i = 0; i < len; i++) {
62-
uint16_t chr = buf[i];
63-
if (chr < 0x80) {
64-
total_bytes += 1;
65-
} else if (chr < 0x800) {
66-
total_bytes += 2;
67-
} else if (chr < 0x10000) {
68-
total_bytes += 3;
69-
} else {
70-
total_bytes += 4;
41+
STATIC uint32_t utf16str_next_unit(utf16_str *utf) {
42+
uint32_t result = utf16str_peek_unit(utf);
43+
if (utf->len) {
44+
utf->len--;
45+
utf->buf++;
46+
}
47+
return result;
48+
}
49+
STATIC uint32_t utf16str_next_codepoint(utf16_str *utf) {
50+
uint32_t unichr = utf16str_next_unit(utf);
51+
if (unichr >= 0xd800 && unichr < 0xdc00) {
52+
uint32_t low_surrogate = utf16str_peek_unit(utf);
53+
if (low_surrogate >= 0xdc00 && low_surrogate < 0xe000) {
54+
(void)utf16str_next_unit(utf);
55+
unichr = (unichr - 0xd800) * 0x400 + low_surrogate - 0xdc00 + 0x10000;
7156
}
7257
}
73-
return total_bytes;
58+
return unichr;
59+
}
60+
61+
STATIC void _convert_utf16le_to_utf8(vstr_t *vstr, utf16_str *utf) {
62+
while (utf->len) {
63+
vstr_add_char(vstr, utf16str_next_codepoint(utf));
64+
}
7465
}
7566

7667
mp_obj_t utf16le_to_string(const uint16_t *buf, size_t utf16_len) {
77-
size_t size = _count_utf8_bytes(buf, utf16_len);
68+
// will grow if necessary, but will never grow for an all-ASCII descriptor
7869
vstr_t vstr;
79-
vstr_init_len(&vstr, size + 1);
80-
byte *p = (byte *)vstr.buf;
81-
// Null terminate.
82-
p[size] = '\0';
83-
_convert_utf16le_to_utf8(buf, utf16_len, p, size);
70+
vstr_init(&vstr, utf16_len);
71+
utf16_str utf = {buf, utf16_len};
72+
_convert_utf16le_to_utf8(&vstr, &utf);
8473
return mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
8574
}

0 commit comments

Comments
 (0)