Introduction to Using MaixCAM MaixPy USB HID (as device)
1. 简介#
MaixPy currently supports simulate as USB HID device like keyboards, mice, and touchscreens, and the following is a guide on how to use maixPy to control your PC via HID.
If you want to use USB as host to connect HID devices into MaixCAM, set USB mode to HOST mode in
settings
.
2. Preparation#
MaixPy firmware version should be >= 4.5.1.
You must enable the HID device before operating HID, there are two ways:
- Open the
Settings
application that comes with MaixCAM, clickUSB Settings
in turn -> tick the required HID devices, such asKeyboard
,Mouse
,Touchscreen
, and then clickConfirm
, then restart MaixCAM. - Through the
Examples/tools/maixcam_switch_usb_mode.py
in MaixVision, modify the HID devices that need to be switched on in thedevice_list
, run it and restart MaixCAM.
Note: Since only 4 USB devices are supported, only 4 devices can be started at the same time among ncm
, rndis
, keyboard
, mouse
, touchpad
, choose according to the actual demand, among them, ncm
and rndis
are the USB network protocol devices, you can turn them off if you don't need them, by default, they are turned on.
3. Write a keyboard in MaixPy.#
You need to enable HID Keyboard
to run it.
The following example sends keyboard event to pc.
After creating the hid
object, key events can be sent using the write
method. A key event is represented by an 8-byte array, where:
- Byte 1: Indicates the status of modifier keys like
ctrl
,shift
,alt
, etc. Each bit represents a specific modifier key:bit0: left ctrl
,bit1: left shift
,bit2: left alt
,bit3: left GUI (e.g., Windows key)
,bit4: right ctrl
,bit5: right shift
,bit6: right alt
,bit7: right GUI
- Byte 2: Reserved byte.
- Byte 3: Primary key value. A value of 0 means the key is released. Key codes are referenced from the "Universal Serial Bus HID Usage Tables" section of the USB HID documentation.
- Bytes 4~8: Additional keys, used to press multiple keys at once. A value of 0 means the key is released.
For specific usage, refer to the example code above.
4. Write a mouse in MaixPy.#
You need to enable HID Mouse
to run it.
The following example moves the mouse 5 pixels every 100ms.
5. Write a touchpad in MaixPy.#
The HID Touchpad
needs to be enabled to run.
In the following example, move the touchscreen 150 units every 100ms. Note that the coordinate system of the touchscreen is absolute, not relative, and that you need to map the actual size of the screen to the interval [1, 0x7FFF], the coordinates (1,1) means the upper left corner, the coordinates (0x7FFF,0x7FFF) means the lower right corner.