[Micropython]TPYBoardV10X教程9 USB-HID应用 | 系列教程 | 技术支持 | MicroPython


当前位置:首页>技术支持>系列教程>[Micropython]TPYBoardV10X教程9 USB-HID应用

[Micropython]TPYBoardV10X教程9 USB-HID应用

原创版权归山东萝卜科技有限公司所有,转载必须以链接形式注明作者和原始出处。

本部分重点讲述如果将TPYBV101作为USB-HID来应用。

1、引言

USB-HID是Human Interface Device的缩写,属于人机交互操作的设备,如USB鼠标,USB键盘,USB游戏操纵杆,USB触摸板,USB轨迹球、电话拨号设备、VCR遥控等等设备。 TPYBV101除了具有usb host功能以外,还可作为USB-HID设备来应用,这里重点讲述如果作为鼠标和键盘使用。

2、TPYBV101作为鼠标应用

TPYBV101作为鼠标的应用可以参考micropython官方实例中的usb-mouse应用。这里以官方实例为例。

(1)编辑 boot.py 文件以更改 USB—mouse 的确认方式。具体如下:

# 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('VCP+MSC') # act as a serial and a storage device
pyb.usb_mode('VCP+HID') # act as a serial device and a mouse


其实就是去掉了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调试鼠标事件

这里依然用putty进行REPL调试。当进行完(1)再次启动时,会发现原本会出现的u盘没有了,此时设备的串口也可能发生了改变,因此在连接Putty前要先确认一下串口。在putty中,输入:


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秒。

(3)按键值的定义

为什么上面的0x04是a,0x05是b呢,这里的定义具体如下:

         #define KEY_NONE                               0x00
         #define KEY_ERRORROLLOVER                      0x01
         #define KEY_POSTFAIL                           0x02
         #define KEY_ERRORUNDEFINED                     0x03
         #define KEY_A                                  0x04
         #define KEY_B                                  0x05
         #define KEY_C                                  0x06
         #define KEY_D                                  0x07
         #define KEY_E                                  0x08
         #define KEY_F                                  0x09
         #define KEY_G                                  0x0A
         #define KEY_H                                  0x0B
         #define KEY_I                                  0x0C
         #define KEY_J                                  0x0D
         #define KEY_K                                  0x0E
         #define KEY_L                                  0x0F
         #define KEY_M                                  0x10
         #define KEY_N                                  0x11
         #define KEY_O                                  0x12
         #define KEY_P                                  0x13
         #define KEY_Q                                  0x14
         #define KEY_R                                  0x15
         #define KEY_S                                  0x16
         #define KEY_T                                  0x17
         #define KEY_U                                  0x18
         #define KEY_V                                  0x19
         #define KEY_W                                  0x1A
         #define KEY_X                                  0x1B
         #define KEY_Y                                  0x1C
         #define KEY_Z                                  0x1D
         #define KEY_1_EXCLAMATION_MARK                 0x1E
         #define KEY_2_AT                               0x1F
         #define KEY_3_NUMBER_SIGN                      0x20
         #define KEY_4_DOLLAR                           0x21
         #define KEY_5_PERCENT                          0x22
         #define KEY_6_CARET                            0x23
         #define KEY_7_AMPERSAND                        0x24
         #define KEY_8_ASTERISK                         0x25
         #define KEY_9_OPARENTHESIS                     0x26
         #define KEY_0_CPARENTHESIS                     0x27
         #define KEY_ENTER                              0x28
         #define KEY_ESCAPE                             0x29
         #define KEY_BACKSPACE                          0x2A
         #define KEY_TAB                                0x2B
         #define KEY_SPACEBAR                           0x2C
         #define KEY_MINUS_UNDERSCORE                   0x2D
         #define KEY_EQUAL_PLUS                         0x2E
         #define KEY_OBRACKET_AND_OBRACE                0x2F
         #define KEY_CBRACKET_AND_CBRACE                0x30
         #define KEY_BACKSLASH_VERTICAL_BAR             0x31
         #define KEY_NONUS_NUMBER_SIGN_TILDE            0x32
         #define KEY_SEMICOLON_COLON                    0x33
         #define KEY_SINGLE_AND_DOUBLE_QUOTE            0x34
         #define KEY_GRAVE ACCENT AND TILDE             0x35
         #define KEY_COMMA_AND_LESS                     0x36
         #define KEY_DOT_GREATER                        0x37
         #define KEY_SLASH_QUESTION                     0x38
         #define KEY_CAPS LOCK                          0x39
         #define KEY_F1                                 0x3A
         #define KEY_F2                                 0x3B
         #define KEY_F3                                 0x3C
         #define KEY_F4                                 0x3D
         #define KEY_F5                                 0x3E
         #define KEY_F6                                 0x3F
         #define KEY_F7                                 0x40
         #define KEY_F8                                 0x41
         #define KEY_F9                                 0x42
         #define KEY_F10                                0x43
         #define KEY_F11                                0x44
         #define KEY_F12                                0x45
         #define KEY_PRINTSCREEN                        0x46
         #define KEY_SCROLL LOCK                        0x47
         #define KEY_PAUSE                              0x48
         #define KEY_INSERT                             0x49
         #define KEY_HOME                               0x4A
         #define KEY_PAGEUP                             0x4B
         #define KEY_DELETE                             0x4C
         #define KEY_END1                               0x4D
         #define KEY_PAGEDOWN                           0x4E
         #define KEY_RIGHTARROW                         0x4F
         #define KEY_LEFTARROW                          0x50
         #define KEY_DOWNARROW                          0x51
         #define KEY_UPARROW                            0x52
         #define KEY_KEYPAD_NUM_LOCK_AND_CLEAR          0x53
         #define KEY_KEYPAD_SLASH                       0x54
         #define KEY_KEYPAD_ASTERIKS                    0x55
         #define KEY_KEYPAD_MINUS                       0x56
         #define KEY_KEYPAD_PLUS                        0x57
         #define KEY_KEYPAD_ENTER                       0x58
         #define KEY_KEYPAD_1_END                       0x59
         #define KEY_KEYPAD_2_DOWN_ARROW                0x5A
         #define KEY_KEYPAD_3_PAGEDN                    0x5B
         #define KEY_KEYPAD_4_LEFT_ARROW                0x5C
         #define KEY_KEYPAD_5                           0x5D
         #define KEY_KEYPAD_6_RIGHT_ARROW               0x5E
         #define KEY_KEYPAD_7_HOME                      0x5F
         #define KEY_KEYPAD_8_UP_ARROW                  0x60
         #define KEY_KEYPAD_9_PAGEUP                    0x61
         #define KEY_KEYPAD_0_INSERT                    0x62
         #define KEY_KEYPAD_DECIMAL_SEPARATOR_DELETE    0x63
         #define KEY_NONUS_BACK_SLASH_VERTICAL_BAR      0x64
         #define KEY_APPLICATION                        0x65
         #define KEY_POWER                              0x66
         #define KEY_KEYPAD_EQUAL                       0x67
         #define KEY_F13                                0x68
         #define KEY_F14                                0x69
         #define KEY_F15                                0x6A
         #define KEY_F16                                0x6B
         #define KEY_F17                                0x6C
         #define KEY_F18                                0x6D
         #define KEY_F19                                0x6E
         #define KEY_F20                                0x6F
         #define KEY_F21                                0x70
         #define KEY_F22                                0x71
         #define KEY_F23                                0x72
         #define KEY_F24                                0x73
         #define KEY_EXECUTE                            0x74
         #define KEY_HELP                               0x75
         #define KEY_MENU                               0x76
         #define KEY_SELECT                             0x77
         #define KEY_STOP                               0x78
         #define KEY_AGAIN                              0x79
         #define KEY_UNDO                               0x7A
         #define KEY_CUT                                0x7B
         #define KEY_COPY                               0x7C
         #define KEY_PASTE                              0x7D
         #define KEY_FIND                               0x7E
         #define KEY_MUTE                               0x7F
         #define KEY_VOLUME_UP                          0x80
         #define KEY_VOLUME_DOWN                        0x81
         #define KEY_LOCKING_CAPS_LOCK                  0x82
         #define KEY_LOCKING_NUM_LOCK                   0x83
         #define KEY_LOCKING_SCROLL_LOCK                0x84
         #define KEY_KEYPAD_COMMA                       0x85
         #define KEY_KEYPAD_EQUAL_SIGN                  0x86
         #define KEY_INTERNATIONAL1                     0x87
         #define KEY_INTERNATIONAL2                     0x88
         #define KEY_INTERNATIONAL3                     0x89
         #define KEY_INTERNATIONAL4                     0x8A
         #define KEY_INTERNATIONAL5                     0x8B
         #define KEY_INTERNATIONAL6                     0x8C
         #define KEY_INTERNATIONAL7                     0x8D
         #define KEY_INTERNATIONAL8                     0x8E
         #define KEY_INTERNATIONAL9                     0x8F
         #define KEY_LANG1                              0x90
         #define KEY_LANG2                              0x91
         #define KEY_LANG3                              0x92
         #define KEY_LANG4                              0x93
         #define KEY_LANG5                              0x94
         #define KEY_LANG6                              0x95
         #define KEY_LANG7                              0x96
         #define KEY_LANG8                              0x97
         #define KEY_LANG9                              0x98
         #define KEY_ALTERNATE_ERASE                    0x99
         #define KEY_SYSREQ                             0x9A
         #define KEY_CANCEL                             0x9B
         #define KEY_CLEAR                              0x9C
         #define KEY_PRIOR                              0x9D
         #define KEY_RETURN                             0x9E
         #define KEY_SEPARATOR                          0x9F
         #define KEY_OUT                                0xA0
         #define KEY_OPER                               0xA1
         #define KEY_CLEAR_AGAIN                        0xA2
         #define KEY_CRSEL                              0xA3
         #define KEY_EXSEL                              0xA4
         #define KEY_KEYPAD_00                          0xB0
         #define KEY_KEYPAD_000                         0xB1
         #define KEY_THOUSANDS_SEPARATOR                0xB2
         #define KEY_DECIMAL_SEPARATOR                  0xB3
         #define KEY_CURRENCY_UNIT                      0xB4
         #define KEY_CURRENCY_SUB_UNIT                  0xB5
         #define KEY_KEYPAD_OPARENTHESIS                0xB6
         #define KEY_KEYPAD_CPARENTHESIS                0xB7
         #define KEY_KEYPAD_OBRACE                      0xB8
         #define KEY_KEYPAD_CBRACE                      0xB9
         #define KEY_KEYPAD_TAB                         0xBA
         #define KEY_KEYPAD_BACKSPACE                   0xBB
         #define KEY_KEYPAD_A                           0xBC
         #define KEY_KEYPAD_B                           0xBD
         #define KEY_KEYPAD_C                           0xBE
         #define KEY_KEYPAD_D                           0xBF
         #define KEY_KEYPAD_E                           0xC0
         #define KEY_KEYPAD_F                           0xC1
         #define KEY_KEYPAD_XOR                         0xC2
         #define KEY_KEYPAD_CARET                       0xC3
         #define KEY_KEYPAD_PERCENT                     0xC4
         #define KEY_KEYPAD_LESS                        0xC5
         #define KEY_KEYPAD_GREATER                     0xC6
         #define KEY_KEYPAD_AMPERSAND                   0xC7
         #define KEY_KEYPAD_LOGICAL_AND                 0xC8
         #define KEY_KEYPAD_VERTICAL_BAR                0xC9
         #define KEY_KEYPAD_LOGIACL_OR                  0xCA
         #define KEY_KEYPAD_COLON                       0xCB
         #define KEY_KEYPAD_NUMBER_SIGN                 0xCC
         #define KEY_KEYPAD_SPACE                       0xCD
         #define KEY_KEYPAD_AT                          0xCE
         #define KEY_KEYPAD_EXCLAMATION_MARK            0xCF
         #define KEY_KEYPAD_MEMORY_STORE                0xD0
         #define KEY_KEYPAD_MEMORY_RECALL               0xD1
         #define KEY_KEYPAD_MEMORY_CLEAR                0xD2
         #define KEY_KEYPAD_MEMORY_ADD                  0xD3
         #define KEY_KEYPAD_MEMORY_SUBTRACT             0xD4
         #define KEY_KEYPAD_MEMORY_MULTIPLY             0xD5
         #define KEY_KEYPAD_MEMORY_DIVIDE               0xD6
         #define KEY_KEYPAD_PLUSMINUS                   0xD7
         #define KEY_KEYPAD_CLEAR                       0xD8
         #define KEY_KEYPAD_CLEAR_ENTRY                 0xD9
         #define KEY_KEYPAD_BINARY                      0xDA
         #define KEY_KEYPAD_OCTAL                       0xDB
         #define KEY_KEYPAD_DECIMAL                     0xDC
         #define KEY_KEYPAD_HEXADECIMAL                 0xDD
         #define KEY_LEFTCONTROL                        0xE0
         #define KEY_LEFTSHIFT                          0xE1
         #define KEY_LEFTALT                            0xE2
         #define KEY_LEFT_GUI                           0xE3
         #define KEY_RIGHTCONTROL                       0xE4
         #define KEY_RIGHTSHIFT                         0xE5
         #define KEY_RIGHTALT                           0xE6
         #define KEY_RIGHT_GUI                          0xE7

4、恢复正常模式

TPYBV101退出CDC+HID模式的方法有两个,一个是进入安全模式,将boot.py文件的pyb.usb_mode('CDC+HID')注释掉,另一种是恢复出厂设置,这种方法是按住usr键,按一下reset,然后led2和led3交替亮,当两个灯交替亮到三次,且均亮起时,松开usr,两个灯会快闪多次,然后TPYBV101恢复到出厂设置,此时main.py里的内容也都清空了。