zoukankan      html  css  js  c++  java
  • MicroPython开发板TPYBoard关于USB-HID的应用

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

     一、作为鼠标应用

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

    ?
    1
    2
    3
    4
    <span style="font-family: 宋体, SimSun; font-size: 12px;"># boot.py -- run on boot-
    up<br># can run arbitrary Python, but best to keep it minimal<br>import pyb<br>#pyb.main('main.py')
    <br># main script to run after this one#pyb.usb_mode('CDC+MSC')
    <br># act as a serial and a storage device<br>pyb.usb_mode('CDC+HID')<br># act as a serial device and a mouse<br></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调试鼠标事件

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

    ?
    1
        pyb.hid((0,10,0,0))  #注意这里两层括号

    回车后,会发现鼠标向右移动了10个像素。pyb.hid()的具体用法:

    ?
    1
        pyb.hid((buttons, x, y, z))

    这里buttons取0,1,2,3分别表示0移动,1按下左键,2按下中键,3按下右键。这句也可以用pyb.USB_HID().send((buttons, x, y, z)),效果是一样的。

    (3)鼠标左右摇晃,代码如下:

    ?
    1
    2
    3
    4
    5
    6
    7
        >>> import math
        >>> def osc(n, d):
        ...   for in range(n):
        ...     pyb.hid((0int(20 * math.sin(i / 10)), 00))
        ...     pyb.delay(d)
        ...
        >>> osc(10050)

    这段代码也可以写到main.py中,这时大家可能会问,u盘没了,main.py怎么编辑啊。这里需要进入TPYBV101的安全模式。按住usr键,按一下reset,此时led2与led3交替亮,当led3亮起,led2没亮时,松开usr,此时led3快闪后,可以发现u盘挂载出来了,这时可以修改main.py文件。

    ?
    1
    2
    3
    4
    5
    6
    7
    8
        #main.py
        import math
        import pyb
        def osc(n, d):
        for in range(n):
        pyb.hid((0int(20 * math.sin(i / 10)), 00))
        pyb.delay(d)
        osc(10050)

    保存后,按reset重启后,就可以看到效果了。

    二、作为键盘应用

    (1) 编辑 boot.py 文件,定义usb-keyboard

    ?
    1
    2
    3
    4
    5
    6
    7
     # 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文件:悦德财富:https://yuedecaifu.com

    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
        # 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秒。

  • 相关阅读:
    团队沟通利器之UML——活动图
    Ninject对Web Api的支持问题
    关于分布式系统的数据一致性问题
    ASP.NET Web开发框架 查询
    用泛型的IEqualityComparer<T> 去除去重复项
    数据库连接监控组件,避免日常开发中因为数据库连接长时间占用或业务完成后忘记关闭连接所带来的数据库问题
    认识项目经理
    状态模式(State Pattern)
    Django框架学习通用视图
    MS CRM 2011 Schedule Service Activities
  • 原文地址:https://www.cnblogs.com/oceansea/p/5939553.html
Copyright © 2011-2022 走看看