Skip to content

Commit 6896564

Browse files
committed
* update hid doc and example
1 parent 8fda06b commit 6896564

File tree

7 files changed

+273
-0
lines changed

7 files changed

+273
-0
lines changed

docs/doc/en/peripheral/hid.md

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+

docs/doc/en/sidebar.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ items:
148148
label: WDT watchdog
149149
- file: peripheral/adc.md
150150
label: ADC
151+
- file: peripheral/hid.md
152+
label: HID
151153

152154
- label: Off-chip modules
153155
items:

docs/doc/zh/peripheral/hid.md

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
title: MaixCAM MaixPy 使用 HID 设备
3+
---
4+
5+
## 简介
6+
7+
HID(Human Interface Device)设备是一类计算机外围设备,用于向计算机传输输入数据,或从计算机接收输出数据。HID 设备最常见的例子包括键盘、鼠标、游戏控制器、触摸屏、和手写板等。HID 协议是一种用于人机交互设备的通信协议,它允许这些设备通过 USB、蓝牙或其他连接方式与主机进行数据交换。MaixPy目前支持作为键盘、鼠标和触摸屏来使用,下面将会介入如何使用MaixPy通过HID来控制你的个人电脑~
8+
9+
## 一定要操作的前期准备
10+
11+
> MaixPy 固件版本应该 > 4.1.22(不包含)
12+
13+
在操作HID前一定要先使能HID设备,有两种方法:
14+
1. 打开MaixCAM自带的`Settings`应用,依次点击`USB Settings`->勾选需要的HID设备,如`Keyboard``Mouse``Touchscreen`,然后点击`Confirm`后重启MaixCAM
15+
2. 通过MaixVision中的`Examples/tools/maixcam_switch_usb_mode.py`示例,修改代码`device_list`中需要开启的HID设备,运行后重启MaixCAM
16+
注意:由于最多只支持4个USB设备,因此在`ncm``rndis``keyboard``mouse``touchpad`之中只能同时启动4个设备,根据实际需求选择,其中`ncm``rndis`是USB网络协议设备,如果不需要可以关掉,默认是打开的。
17+
18+
## 用MaixPy编写一个键盘
19+
20+
需要使能了`HID Keyboard`后才能运行。
21+
22+
下面示例中,通过键盘发送`rstuv`四个字符,然后松开按键。
23+
24+
```python
25+
from maix import hid, time
26+
27+
keyboard = hid.Hid(hid.DeviceType.DEVICE_KEYBOARD)
28+
29+
# 按键编号参考[USB HID文档](https://www.usb.org))的"Universal Serial Bus HID Usage Tables"部分
30+
keys = [21, 22, 23, 24, 25, 0] # 表示[r, s, t, u, v, 0], 0表示松开按键
31+
32+
for key in keys:
33+
keyboard.write([0, 0, key, 0, 0, 0, 0, 0])
34+
35+
```
36+
37+
## 用MaixPy编写一个鼠标
38+
39+
需要使能了`HID Mouse`后才能运行。
40+
41+
下面示例中,每隔100ms移动鼠标5个像素。
42+
43+
```python
44+
from maix import hid, time
45+
46+
mouse = hid.Hid(hid.DeviceType.DEVICE_MOUSE)
47+
48+
button = 0 # 按键状态,0表示松开,1表示按下左键,2表示按下右键,4表示按下滚轮键
49+
x_oft = 0 # 相对当前位置的偏移量,数值范围是-127~127
50+
y_oft = 0 # 相对当前位置的偏移量,数值范围是-127~127
51+
wheel_move = 0 # 滚轮移动距离,数值范围是-127~127
52+
53+
count = 0
54+
while True:
55+
x_oft += 5
56+
y_oft += 5
57+
mouse.write([button, x_oft, y_oft, wheel_move])
58+
time.sleep_ms(100)
59+
count += 1
60+
if count > 50:
61+
break
62+
```
63+
64+
## 用MaixPy编写一个触摸屏
65+
66+
需要使能了`HID Touchpad`后才能运行。
67+
68+
下面示例中,每隔100ms移动触摸屏150个单位,注意触摸屏的坐标系是绝对坐标,而不是相对坐标,另外需要将屏幕实际尺寸映射到[1, 0x7FFF]区间,坐标(1,1)表示左上角,坐标(0x7FFF,0x7FFF)表示右下角。
69+
70+
```python
71+
from maix import hid, time
72+
73+
touchpad = hid.Hid(hid.DeviceType.DEVICE_TOUCHPAD)
74+
75+
def touchpad_set(button, x_oft, y_oft, wheel_move):
76+
touchpad.write([button, # 按键状态,0表示松开,1表示按下左键,2表示按下右键,4表示按下滚轮键
77+
x_oft & 0xff, (x_oft >> 8) & 0xff, # 绝对位置,最左为1, 最右为0x7fff,0表示不操作,数值范围是0~0x7fff
78+
y_oft & 0xff, (y_oft >> 8) & 0xff, # 绝对位置,最上为1, 最下为0x7fff,0表示不操作,数值范围是0~0x7fff
79+
wheel_move]) # 滚轮移动距离,数值范围是-127~127
80+
button = 0
81+
x_oft = 0
82+
y_oft = 0
83+
wheel_move = 0
84+
count = 0
85+
while True:
86+
x_oft += 150
87+
y_oft += 150
88+
touchpad_set(button, x_oft, y_oft, wheel_move)
89+
time.sleep_ms(100)
90+
count += 1
91+
if count > 50:
92+
break
93+
```

docs/doc/zh/sidebar.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ items:
148148
label: WDT 看门狗使用
149149
- file: peripheral/adc.md
150150
label: ADC 使用
151+
- file: peripheral/hid.md
152+
label: HID Device 使用
151153

152154
- label: 片外模块
153155
items:
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from maix import hid, time
2+
3+
keyboard = None
4+
try:
5+
keyboard = hid.Hid(hid.DeviceType.DEVICE_KEYBOARD)
6+
except Exception as e:
7+
print(e)
8+
print('The HID evice is not enabled')
9+
print('Try: Use the application Settings -> USB Settings -> HID Keyboard, then click Confirm, and restart the system.')
10+
print('You can also use examples/tools/maixcam_switch_usb_mode.py to enable the HID Keyboard, then restart the system.')
11+
exit(0)
12+
13+
# Refer to the "Universal Serial Bus HID Usage Tables" section of the official documentation(https://www.usb.org).
14+
keys = [21, 22, 23, 24, 25, 0] # [r, s, t, u, v, 0], 0 means release.
15+
16+
for key in keys:
17+
keyboard.write([0, 0, key, 0, 0, 0, 0, 0])
18+
19+
print('OK')

examples/peripheral/usb/hid_mouse.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from maix import hid, time
2+
3+
mouse = None
4+
try:
5+
mouse = hid.Hid(hid.DeviceType.DEVICE_MOUSE)
6+
except Exception as e:
7+
print(e)
8+
print('The HID evice is not enabled')
9+
print('Try: Use the application Settings -> USB Settings -> HID Mouse, then click Confirm, and restart the system.')
10+
print('You can also use examples/tools/maixcam_switch_usb_mode.py to enable the HID Mouse, then restart the system.')
11+
exit(0)
12+
13+
button = 0
14+
x_oft = 0
15+
y_oft = 0
16+
wheel_move = 0
17+
18+
count = 0
19+
while True:
20+
x_oft += 5
21+
y_oft += 5
22+
mouse.write([button, x_oft, y_oft, wheel_move])
23+
time.sleep_ms(100)
24+
count += 1
25+
if count > 50:
26+
break
27+
28+
print('OK')
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from maix import hid, time
2+
3+
touchpad = None
4+
try:
5+
touchpad = hid.Hid(hid.DeviceType.DEVICE_TOUCHPAD)
6+
except Exception as e:
7+
print(e)
8+
print('The HID evice is not enabled')
9+
print('Try: Use the application Settings -> USB Settings -> HID Touchpad, then click Confirm, and restart the system.')
10+
print('You can also use examples/tools/maixcam_switch_usb_mode.py to enable the HID Touchpad, then restart the system.')
11+
exit(0)
12+
13+
def touchpad_set(button, x_oft, y_oft, wheel_move):
14+
touchpad.write([button,
15+
x_oft & 0xff, (x_oft >> 8) & 0xff,
16+
y_oft & 0xff, (y_oft >> 8) & 0xff,
17+
wheel_move])
18+
button = 0
19+
x_oft = 0
20+
y_oft = 0
21+
wheel_move = 0
22+
23+
count = 0
24+
while True:
25+
x_oft += 150
26+
y_oft += 150
27+
touchpad_set(button, x_oft, y_oft, wheel_move)
28+
time.sleep_ms(100)
29+
count += 1
30+
if count > 50:
31+
break
32+
33+
print('OK')

0 commit comments

Comments
 (0)