|
| 1 | +--- |
| 2 | +title: Introduction to Using MaixCAM MaixPy HID Device |
| 3 | +--- |
| 4 | + |
| 5 | + |
| 6 | +## 简介 |
| 7 | + |
| 8 | +MaixPy currently supports the use of keyboards, mice, and touchscreens, and the following is a guide on how to use maixPy to control your PC via HID. |
| 9 | + |
| 10 | +## Preparation |
| 11 | + |
| 12 | +> MaixPy firmware version should be > 4.1.22 (not included). |
| 13 | +
|
| 14 | +You must enable the HID device before operating HID, there are two ways: |
| 15 | +1. Open the `Settings` application that comes with MaixCAM, click `USB Settings` in turn -> tick the required HID devices, such as `Keyboard`, `Mouse`, `Touchscreen`, and then click `Confirm` , then restart MaixCAM. |
| 16 | +2. Through the `Examples/tools/maixcam_switch_usb_mode.py` in MaixVision, modify the HID devices that need to be switched on in the `device_list`, run it and restart MaixCAM. |
| 17 | +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. |
| 18 | + |
| 19 | +## Write a keyboard in MaixPy. |
| 20 | + |
| 21 | +You need to enable `HID Keyboard` to run it. |
| 22 | + |
| 23 | +The following example sends `rstuv` four characters through the keyboard and then releases the key. |
| 24 | + |
| 25 | +```python |
| 26 | +from maix import hid, time |
| 27 | + |
| 28 | +keyboard = hid.Hid(hid.DeviceType.DEVICE_KEYBOARD) |
| 29 | + |
| 30 | +# Refer to the `Universal Serial Bus HID Usage Tables` section of the [USB HID Documentation](https://www.usb.org) for key numbers. |
| 31 | +keys = [21, 22, 23, 24, 25, 0] # means [r, s, t, u, v, 0], 0 means release key. |
| 32 | + |
| 33 | +for key in keys: |
| 34 | + keyboard.write([0, 0, key, 0, 0, 0, 0, 0]) |
| 35 | + |
| 36 | +``` |
| 37 | + |
| 38 | +## Write a mouse in MaixPy. |
| 39 | + |
| 40 | +You need to enable `HID Mouse` to run it. |
| 41 | + |
| 42 | +The following example moves the mouse 5 pixels every 100ms. |
| 43 | + |
| 44 | +```python |
| 45 | +from maix import hid, time |
| 46 | + |
| 47 | +mouse = hid.Hid(hid.DeviceType.DEVICE_MOUSE) |
| 48 | + |
| 49 | +button = 0 # button state, 0 means release, 1 means left button pressed, 2 means right button pressed, 4 means wheel button pressed |
| 50 | +x_oft = 0 # offset relative to current position, value range is -127~127 |
| 51 | +y_oft = 0 # offset relative to current position, value range is -127~127 |
| 52 | +wheel_move = 0 # The distance the wheel has moved, the range of values is -127~127 |
| 53 | + |
| 54 | +count = 0 |
| 55 | +while True: |
| 56 | + x_oft += 5 |
| 57 | + y_oft += 5 |
| 58 | + mouse.write([button, x_oft, y_oft, wheel_move]) |
| 59 | + time.sleep_ms(100) |
| 60 | + count += 1 |
| 61 | + if count > 50: |
| 62 | + break |
| 63 | +``` |
| 64 | + |
| 65 | +## Write a touchpad in MaixPy. |
| 66 | + |
| 67 | +The `HID Touchpad` needs to be enabled to run. |
| 68 | + |
| 69 | +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. |
| 70 | + |
| 71 | +```python |
| 72 | +from maix import hid, time |
| 73 | + |
| 74 | +touchpad = hid.Hid(hid.DeviceType.DEVICE_TOUCHPAD) |
| 75 | + |
| 76 | +def touchpad_set(button, x_oft, y_oft, wheel_move): |
| 77 | + touchpad.write([button, # button state, 0 means release, 1 means left button pressed, 2 means right button pressed, 4 means wheel button pressed |
| 78 | + x_oft & 0xff, (x_oft >> 8) & 0xff, # Absolute position, the leftmost is 1, the rightmost is 0x7fff, 0 means no operation, the value range is 0 to 0x7fff. |
| 79 | + y_oft & 0xff, (y_oft >> 8) & 0xff, # Absolute position, the topmost is 1, the bottom is 0x7fff, 0 means no operation, the value range is 0 to 0x7fff |
| 80 | + wheel_move]) # wheel move distance, value range is -127~127 |
| 81 | +button = 0 |
| 82 | +x_oft = 0 |
| 83 | +y_oft = 0 |
| 84 | +wheel_move = 0 |
| 85 | +count = 0 |
| 86 | +while True: |
| 87 | + x_oft += 150 |
| 88 | + y_oft += 150 |
| 89 | + touchpad_set(button, x_oft, y_oft, wheel_move) |
| 90 | + time.sleep_ms(100) |
| 91 | + count += 1 |
| 92 | + if count > 50: |
| 93 | + break |
| 94 | +``` |
| 95 | + |
| 96 | + |
0 commit comments