USB HID API Reference#
Overview#
The USB HID input interface in the RTOS SDK is provided by drv_input, which uniformly abstracts keyboards, mice, and USB touch class input devices.
Header file location:
#include "drv_input.h"
Corresponding source code:
src/rtsmart/libs/rtsmart_hal/drivers/input/drv_input.hsrc/rtsmart/libs/rtsmart_hal/drivers/input/drv_input.c
Device Types#
enum drv_input_device_type {
DRV_INPUT_DEV_UNKNOWN = 0,
DRV_INPUT_DEV_KEYBOARD,
DRV_INPUT_DEV_MOUSE,
DRV_INPUT_DEV_TOUCH,
};
Key Data Structures#
struct drv_input_info#
Device capability information:
kind: Device typeev_bits: Bitmap of supported eventskey_bits: Bitmap of supported keysrel_bits: Bitmap of supported relative coordinatesabs_bits: Bitmap of supported absolute coordinatesname: Device name
drv_input_inst_t#
The input device instance handle, containing:
id: Device numberfd: Device file descriptorbutton_state: Current button state bitspath: Device path
The application layer should only treat it as a handle and is not recommended to modify members directly.
struct drv_keyboard_frame#
Keyboard frame:
keycodes[]: Key code arrayvalues[]: Key value array, corresponding toKEY_PRESSED/KEY_RELEASED/KEY_REPEATcount: Number of events in this framecomplete: Whether a sync end event has been read
struct drv_pointer_frame#
Pointer frame, applicable to mouse and USB touch devices:
completehas_relhas_abstouch_seentouch_downrel_x/rel_ywheel/hwheelabs_x/abs_ypressurebuttonspressed_maskreleased_mask
Basic Instance API#
drv_input_inst_create#
int drv_input_inst_create(int id, drv_input_inst_t **inst);
Open an input device by event number, e.g., event0.
drv_input_inst_create_path#
int drv_input_inst_create_path(const char *path, drv_input_inst_t **inst);
Open an input device by path, e.g., /dev/hidk0.
drv_input_inst_destroy#
void drv_input_inst_destroy(drv_input_inst_t **inst);
Close and destroy the instance.
Read/Write API#
drv_input_poll#
int drv_input_poll(drv_input_inst_t *inst, int timeout_ms);
Wait for an input event.
Return value:
> 0: there is an event0: timeout< 0: error code
drv_input_read_event#
int drv_input_read_event(drv_input_inst_t *inst, struct input_event *event);
Read a single raw input_event.
drv_input_read_frame#
int drv_input_read_frame(drv_input_inst_t *inst, struct drv_input_frame *frame);
Read a frame of raw input events until a sync event is encountered or no more data is available.
drv_input_read_keyboard_frame#
int drv_input_read_keyboard_frame(drv_input_inst_t *inst, struct drv_keyboard_frame *frame);
Read a keyboard frame.
Return value:
> 0: number of key-value pairs in the frame0: no complete keyboard event currently< 0: error code
drv_input_read_pointer_frame#
int drv_input_read_pointer_frame(drv_input_inst_t *inst, struct drv_pointer_frame *frame);
Read a pointer frame, used for mouse and USB touch.
Information and Discovery API#
drv_input_get_info#
int drv_input_get_info(drv_input_inst_t *inst, struct drv_input_info *info);
Query device capability information.
drv_input_find_first_by_type#
int drv_input_find_first_by_type(uint32_t kind,
char *path,
size_t path_size,
struct drv_input_info *info);
Find the first input device of the specified type, and return the path and capability information.
Applicable to the following scenarios:
Automatically discover USB keyboard at startup
Re-bind the same type of device after hot-plug
Event Auxiliary Judgment API#
drv_input_is_key_event#
bool drv_input_is_key_event(const struct input_event *event);
drv_input_is_rel_event#
bool drv_input_is_rel_event(const struct input_event *event);
drv_input_is_abs_event#
bool drv_input_is_abs_event(const struct input_event *event);
drv_input_is_sync_event#
bool drv_input_is_sync_event(const struct input_event *event);
Used to determine the raw input_event type.
Recommended Call Flow#
Keyboard#
Use
drv_input_inst_create_path()ordrv_input_reconnect_by_type()to open the deviceCall
drv_input_poll()to wait for dataCall
drv_input_read_keyboard_frame()to read a frameIf an error is returned, use
drv_input_is_disconnect_error()to determine whether reconnection is neededCall
drv_input_inst_destroy()on exit
Mouse / USB Touch#
Open an input instance
Call
drv_input_poll()to wait for dataCall
drv_input_read_pointer_frame()to read a pointer frameUse fields such as
has_rel/has_abs/buttons/touch_downto handle inputPerform reconnection when disconnected
Example Code Snippet#
drv_input_inst_t *inst = NULL;
struct drv_input_info info;
if (drv_input_reconnect_by_type(&inst,
DRV_INPUT_DEV_KEYBOARD,
NULL,
0,
&info) == 0) {
printf("keyboard ready: %s\n", info.name);
}
struct drv_keyboard_frame frame;
int ret = drv_input_read_keyboard_frame(inst, &frame);
if (ret > 0) {
for (size_t i = 0; i < frame.count; i++) {
printf("key=%u value=%d\n", frame.keycodes[i], frame.values[i]);
}
}
Notes#
Do not treat Linux input keycodes as sequential character numbers, especially for letter keys.
For hot-plug devices, it is recommended to always combine
drv_input_is_disconnect_error()anddrv_input_reconnect_by_type().The reading interfaces for keyboard and pointer devices are different. Do not mix
drv_input_read_keyboard_frame()withdrv_input_read_pointer_frame().
