Micropython Development Board Tpyboard application of Usb-hid

Source: Internet
Author: User
Tags sin

Usb-hid is the abbreviation of human Interface device, which belongs to the Human-computer interaction operation device, such as USB mouse, USB keyboard, USB game joystick, USB touchpad, USB trackball, telephone dialing device, VCR remote control and so On. Tpyboard with Micropython In addition to the USB host feature, It can also be applied as a Usb-hid device, which focuses on if used as a mouse and Keyboard.

first, as a mouse application

(1) Edit the boot.py file to change how the Usb-mouse is Confirmed. Specific as Follows:

?
1234 <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>

In fact, the Pyb.usb_mode (' cdc+hid ') is removed before the Comment. Here Pyb.usb_mode (), defines the device of HID, the default is mouse, also can use Pyb.usb_mode (' cdc+hid ', hid=pyb.hid_mouse). If it is a keyboard, it should be changed to Pyb.usb_mode (' Cdc+hid ', hid=pyb.hid_keyboard).

(2) Repl Debugging Mouse events

This is still used putty for REPL Debugging. When finished (1) start again, you will find that the original will appear in the U disk is not, at this time the Device's serial port may also have changed, so before connecting the putty to confirm the serial Port. In putty, enter:

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

When you enter, you will notice that the mouse moves 10 pixels to the Right. Specific usage of Pyb.hid ():

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

Here buttons take 0,1,2,3 respectively represents 0 move, 1 Press the left button, 2 press the Middle key, 3 press the right Button. This sentence can also be used Pyb. Usb_hid (). send ((buttons, x, y, z)), The effect is the Same.

(3) the mouse shaking left and right, the code is as Follows:

?
1234567     >>> importmath    >>> def osc(n, d):    ...   forinrange(n):    ...     pyb.hid((0int(20* math.sin(i /10)), 00))    ...     pyb.delay(d)    ...    >>> osc(10050)

This code can also be written to main.py, when you may ask, u disk is not, main.py how to edit ah. There is a need to enter TPYBV101 safe mode. Hold down the USR key, click reset, at this time led2 and led3 alternating light, when the led3 on, led2 not bright, release usr, at this time led3 flash, you can find the U disk mounted, then you can modify the main.py File.

?
12345678     #main.py    importmath    importpyb    def osc(n, d):    forinrange(n):    pyb.hid((0int(20*math.sin(i /10)), 00))    pyb.delay(d)    osc(10050)

After saving, press Reset to restart, you can see the Effect.

second, as a keyboard application

(1) edit boot.py file, define Usb-keyboard

?
1234567  # boot.py -- run on boot-up    # can run arbitrary Python, but best to keep it minimal    importmachine    importpyb    #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) key test, here for easy viewing, We modify the main.py file: Yue de wealth: https://yuedecaifu.com

?
12345678910111213141516171819202122232425262728293031323334353637383940414243444546     # main.py -- put your code here!    hid=pyb.USB_HID()    defrelease_key_once():    buf = bytearray(8# report is 8 bytes long    buf[2=0    hid.send(buf) # key released    pyb.delay(10)    defpress_key_once(key):    buf =bytearray(8# report is 8 bytes long    buf[2= key    hid.send(buf) # key released    pyb.delay(10)    defpress_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)    defrelease_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)

This program defines the method of pressing a key press_key_once (key), lifting a key release_key_once (), pressing two keys Press_2key (key1,key2), lifting two keys Release_2key (). Specific running effect, You can open a notepad, and then click the Reset key, or Plug and unplug a USB port, and finally you can see in notepad, first into ab, then tab, enter, and then cdef, in addition to EF almost at the same time, the front input between the interval of 1 seconds.

Micropython Development Board Tpyboard application of Usb-hid

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.