usb_mouse.rst 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. Making the pyboard act as a USB mouse
  2. =====================================
  3. The pyboard is a USB device, and can configured to act as a mouse instead
  4. of the default USB flash drive.
  5. To do this we must first edit the ``boot.py`` file to change the USB
  6. configuration. If you have not yet touched your ``boot.py`` file then it
  7. will look something like this::
  8. # boot.py -- run on boot-up
  9. # can run arbitrary Python, but best to keep it minimal
  10. import pyb
  11. #pyb.main('main.py') # main script to run after this one
  12. #pyb.usb_mode('VCP+MSC') # act as a serial and a storage device
  13. #pyb.usb_mode('VCP+HID') # act as a serial device and a mouse
  14. To enable the mouse mode, uncomment the last line of the file, to
  15. make it look like::
  16. pyb.usb_mode('VCP+HID') # act as a serial device and a mouse
  17. If you already changed your ``boot.py`` file, then the minimum code it
  18. needs to work is::
  19. import pyb
  20. pyb.usb_mode('VCP+HID')
  21. This tells the pyboard to configure itself as a VCP (Virtual COM Port,
  22. ie serial port) and HID (human interface device, in our case a mouse)
  23. USB device when it boots up.
  24. Eject/unmount the pyboard drive and reset it using the RST switch.
  25. Your PC should now detect the pyboard as a mouse!
  26. Sending mouse events by hand
  27. ----------------------------
  28. To get the py-mouse to do anything we need to send mouse events to the PC.
  29. We will first do this manually using the REPL prompt. Connect to your
  30. pyboard using your serial program and type the following::
  31. >>> hid = pyb.USB_HID()
  32. >>> hid.send((0, 10, 0, 0))
  33. Your mouse should move 10 pixels to the right! In the command above you
  34. are sending 4 pieces of information: button status, x, y and scroll. The
  35. number 10 is telling the PC that the mouse moved 10 pixels in the x direction.
  36. Let's make the mouse oscillate left and right::
  37. >>> import math
  38. >>> def osc(n, d):
  39. ... for i in range(n):
  40. ... hid.send((0, int(20 * math.sin(i / 10)), 0, 0))
  41. ... pyb.delay(d)
  42. ...
  43. >>> osc(100, 50)
  44. The first argument to the function ``osc`` is the number of mouse events to send,
  45. and the second argument is the delay (in milliseconds) between events. Try
  46. playing around with different numbers.
  47. **Exercise: make the mouse go around in a circle.**
  48. Making a mouse with the accelerometer
  49. -------------------------------------
  50. Now lets make the mouse move based on the angle of the pyboard, using the
  51. accelerometer. The following code can be typed directly at the REPL prompt,
  52. or put in the ``main.py`` file. Here, we'll put in in ``main.py`` because to do
  53. that we will learn how to go into safe mode.
  54. At the moment the pyboard is acting as a serial USB device and an HID (a mouse).
  55. So you cannot access the filesystem to edit your ``main.py`` file.
  56. You also can't edit your ``boot.py`` to get out of HID-mode and back to normal
  57. mode with a USB drive...
  58. To get around this we need to go into *safe mode*. This was described in
  59. the [safe mode tutorial](tut-reset), but we repeat the instructions here:
  60. 1. Hold down the USR switch.
  61. 2. While still holding down USR, press and release the RST switch.
  62. 3. The LEDs will then cycle green to orange to green+orange and back again.
  63. 4. Keep holding down USR until *only the orange LED is lit*, and then let
  64. go of the USR switch.
  65. 5. The orange LED should flash quickly 4 times, and then turn off.
  66. 6. You are now in safe mode.
  67. In safe mode, the ``boot.py`` and ``main.py`` files are not executed, and so
  68. the pyboard boots up with default settings. This means you now have access
  69. to the filesystem (the USB drive should appear), and you can edit ``main.py``.
  70. (Leave ``boot.py`` as-is, because we still want to go back to HID-mode after
  71. we finish editing ``main.py``.)
  72. In ``main.py`` put the following code::
  73. import pyb
  74. switch = pyb.Switch()
  75. accel = pyb.Accel()
  76. hid = pyb.USB_HID()
  77. while not switch():
  78. hid.send((0, accel.x(), accel.y(), 0))
  79. pyb.delay(20)
  80. Save your file, eject/unmount your pyboard drive, and reset it using the RST
  81. switch. It should now act as a mouse, and the angle of the board will move
  82. the mouse around. Try it out, and see if you can make the mouse stand still!
  83. Press the USR switch to stop the mouse motion.
  84. You'll note that the y-axis is inverted. That's easy to fix: just put a
  85. minus sign in front of the y-coordinate in the ``hid.send()`` line above.
  86. Restoring your pyboard to normal
  87. --------------------------------
  88. If you leave your pyboard as-is, it'll behave as a mouse everytime you plug
  89. it in. You probably want to change it back to normal. To do this you need
  90. to first enter safe mode (see above), and then edit the ``boot.py`` file.
  91. In the ``boot.py`` file, comment out (put a # in front of) the line with the
  92. ``VCP+HID`` setting, so it looks like::
  93. #pyb.usb_mode('VCP+HID') # act as a serial device and a mouse
  94. Save your file, eject/unmount the drive, and reset the pyboard. It is now
  95. back to normal operating mode.