1、引言
USB-HID是Human Interface Device的缩写,属于人机交互操作的设备,如USB鼠标,USB键盘,USB游戏操纵杆,USB触摸板,USB轨迹球、电话拨号设备、VCR遥控等等设备。 TPYBoard开发板除了具有usb host功能以外,还可作为USB-HID设备来应用,这里重点讲述如果作为鼠标和键盘使用。[链接内容](http://www.micropython.net.cn)
2、TPYBV101作为鼠标应用
TPYBV101作为鼠标的应用可以参考micropython官方实例中的usb-mouse应用。这里以官方实例为例。
(1)编辑 boot.py 文件以更改 USB—mouse 的确认方式。具体如下:
<span style="font-family: 宋体, SimSun; font-size: 12px;"># boot.py -- run on boot-
up
# can run arbitrary Python, but best to keep it minimal
import pyb
#pyb.main('main.py')
# main script to run after this one#pyb.usb_mode('CDC+MSC')
# act as a serial and a storage device
pyb.usb_mode('CDC+HID')
# act as a serial device and a mouse
</span>
其实就是去掉了pyb.usb_mode('CDC+HID')前的注释符。这里pyb.usb_mode(),定义了HID的设备,默认为mouse,也可以用pyb.usb_mode('CDC+HID',hid=pyb.hid_mouse)。如果是键盘,应改为pyb.usb_mode('CDC+HID',hid=pyb.hid_keyboard)。
(2)REPL调试鼠标事件
pyb.hid((0,10,0,0)) #注意这里两层括号
回车后,会发现鼠标向右移动了10个像素。pyb.hid()的具体用法:
pyb.hid((buttons, x, y, z))
这里buttons取0,1,2,3分别表示0移动,1按下左键,2按下中键,3按下右键。这句也可以用pyb.USB_HID().send((buttons, x, y, z)),效果是一样的。
(3)鼠标左右摇晃,代码如下:
>>> import math
>>> def osc(n, d):
... for i in range(n):
... pyb.hid((0, int(20 * math.sin(i / 10)), 0, 0))
... pyb.delay(d)
...
>>> osc(100, 50)
这段代码也可以写到main.py中,这时大家可能会问,u盘没了,main.py怎么编辑啊。这里需要进入TPYBV101的安全模式。按住usr键,按一下reset,此时led2与led3交替亮,当led3亮起,led2没亮时,松开usr,此时led3快闪后,可以发现u盘挂载出来了,这时可以修改main.py文件。
#main.py
import math
import pyb
def osc(n, d):
for i in range(n):
pyb.hid((0, int(20 * math.sin(i / 10)), 0, 0))
pyb.delay(d)
osc(100, 50)
保存后,按reset重启后,就可以看到效果了。
(4)加上加速度传感器的鼠标(官方例程)
用加速度传感器制作一个能根据 tpyboard 板角度移动而移动的鼠标。相关的代码可以直接在交互式解释器里边完成,或者放入 main.py 文件中。在这里我们选择放入 main.py 文件。
在 main.py 中代码:
import pyb
switch = pyb.Switch()
accel = pyb.Accel()
while not switch():
pyb.hid((0, accel.x(), accel.y(), 0))
pyb.delay(20)
保存文件,退出或卸载 tpyboard 设备。通过复位按键重置,其将再次成为鼠标,且根据板的倾斜角度移动鼠标。你应该能注意到 y轴方向是颠倒的。这个问题很容易解决:在上述的 pyb.hid( )行里,y 轴方向前边加上负号即可。
3、TPYBV101作为键盘应用
(1) 编辑 boot.py 文件,定义usb-keyboard
# boot.py -- run on boot-up
# can run arbitrary Python, but best to keep it minimal
import machine
import pyb
#pyb.main('main.py') # main script to run after this one
#pyb.usb_mode('CDC+MSC') # act as a serial and a storage device
pyb.usb_mode('CDC+HID',hid=pyb.hid_keyboard) # act as a serial device and a keyboard
(2)按键测试,这里为了便于查看,我们修改main.py文件:
# main.py -- put your code here!
hid=pyb.USB_HID()
def release_key_once():
buf = bytearray(8) # report is 8 bytes long
buf[2] = 0
hid.send(buf) # key released
pyb.delay(10)
def press_key_once(key):
buf = bytearray(8) # report is 8 bytes long
buf[2] = key
hid.send(buf) # key released
pyb.delay(10)
def press_2key(key1,key2):
buf = bytearray(8) # report is 8 bytes long
buf[2] = key1
buf[3] = key2
hid.send(buf) # key released
pyb.delay(10)
def release_2key():
buf = bytearray(8) # report is 8 bytes long
buf[2] = 0
buf[3] = 0
hid.send(buf) # key released
pyb.delay(10)
pyb.delay(1000)
press_key_once(0x04)
release_key_once()
pyb.delay(1000)
press_key_once(0x05)
release_key_once()
pyb.delay(1000)
press_key_once(0x2B)
release_key_once()
pyb.delay(1000)
press_key_once(0x28)
release_key_once()
pyb.delay(1000)
press_key_once(0x06)
release_key_once()
pyb.delay(1000)
press_key_once(0x07)
release_key_once()
pyb.delay(1000)
press_2key(0x08,0x09)
release_2key()
pyb.delay(1000)
这个程序定义了按下一个键press_key_once(key),抬起一个键 release_key_once(),按下两个键press_2key(key1,key2),抬起两个键release_2key()的方法。具体运行效果,可先打开一个记事本,然后按一下reset键,或者插拔一次usb口,最后可以看到在记事本里,先打入ab,接着是tab,回车,接着cdef,除了ef几乎同时出现,前面的输入间都间隔了1秒。