Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion micropython/usb/usb-device-mouse/manifest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
metadata(version="0.1.0")
metadata(version="0.2.0")
require("usb-device-hid")
package("usb")
11 changes: 10 additions & 1 deletion micropython/usb/usb-device-mouse/usb/device/mouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from micropython import const
import struct
import machine
import time

from usb.device.hid import HIDInterface

Expand All @@ -23,7 +24,7 @@ def __init__(self, interface_str="MicroPython Mouse"):
self._r = False # Right button
self._buf = bytearray(3)

def send_report(self, dx=0, dy=0):
def send_report(self, dx=0, dy=0, timeout_ms=100):
b = 0
if self._l:
b |= 1 << 0
Expand All @@ -35,11 +36,19 @@ def send_report(self, dx=0, dy=0):
# Wait for any pending report to be sent to the host
# before updating contents of _buf.
#
# Give up after timeout_ms like HIDInterface.send_report(), otherwise a
# host that never reads the endpoint (a boot keyboard-only environment,
# for instance) would keep the first report pending and block every
# later call. Returns False without touching _buf in that case.
#
# This loop can be removed if you don't care about possibly missing a
# transient report, the final report buffer contents will always be the
# last one sent to the host (it just might lose one of the ones in the
# middle).
deadline = time.ticks_add(time.ticks_ms(), timeout_ms)
while self.busy():
if time.ticks_diff(deadline, time.ticks_ms()) <= 0:
return False
machine.idle()

struct.pack_into("Bbb", self._buf, 0, b, dx, dy)
Expand Down
Loading