Skip to content

Commit 167e101

Browse files
authored
Merge pull request #15 from dhalbert/wait_for_ready
wait for HID device ready; allow longer mouse movements
2 parents 524a8b5 + ae96b33 commit 167e101

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed

adafruit_hid/consumer_control.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* Author(s): Dan Halbert
2929
"""
3030

31+
import time
3132
import usb_hid
3233

3334
class ConsumerControl:
@@ -52,6 +53,14 @@ def __init__(self):
5253
# View bytes as a single 16-bit number.
5354
self.usage_id = memoryview(self.report)[0:2]
5455

56+
# Do a no-op to test if HID device is ready.
57+
# If not, wait a bit and try once more.
58+
try:
59+
self.send(0x0)
60+
except OSError:
61+
time.sleep(1)
62+
self.send(0x0)
63+
5564
def send(self, consumer_code):
5665
"""Send a report to do the specified consumer control action,
5766
and then stop the action (so it will not repeat).

adafruit_hid/keyboard.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@
2828
* Author(s): Scott Shawcroft, Dan Halbert
2929
"""
3030

31-
from micropython import const
31+
import time
3232
import usb_hid
3333

34+
from micropython import const
35+
3436
from .keycode import Keycode
3537

3638
class Keyboard:
@@ -63,6 +65,15 @@ def __init__(self):
6365
# View onto bytes 2-7 in report.
6466
self.report_keys = memoryview(self.report)[2:]
6567

68+
# Do a no-op to test if HID device is ready.
69+
# If not, wait a bit and try once more.
70+
try:
71+
self.release_all()
72+
except OSError:
73+
time.sleep(1)
74+
self.release_all()
75+
76+
6677
def press(self, *keycodes):
6778
"""Send a report indicating that the given keys have been pressed.
6879

adafruit_hid/mouse.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
2828
* Author(s): Dan Halbert
2929
"""
30+
import time
3031
import usb_hid
3132

3233
class Mouse:
@@ -56,6 +57,13 @@ def __init__(self):
5657
# report[3] wheel movement
5758
self.report = bytearray(4)
5859

60+
# Do a no-op to test if HID device is ready.
61+
# If not, wait a bit and try once more.
62+
try:
63+
self.move(0, 0, 0)
64+
except OSError:
65+
time.sleep(1)
66+
self.move(0, 0, 0)
5967

6068
def press(self, buttons):
6169
"""Press the given mouse buttons.
@@ -116,7 +124,6 @@ def move(self, x=0, y=0, wheel=0):
116124
positive is downwards.
117125
:param wheel: Rotate the wheel this amount. Negative is toward the user, positive
118126
is away from the user. The scrolling effect depends on the host.
119-
:raises ValueError: if any argument is not in the range -127 to 127 inclusive.
120127
121128
Examples::
122129
@@ -133,17 +140,20 @@ def move(self, x=0, y=0, wheel=0):
133140
# Roll the mouse wheel away from the user.
134141
m.move(wheel=1)
135142
"""
136-
if (self._distance_ok(x)
137-
and self._distance_ok(y)
138-
and self._distance_ok(wheel)):
139-
self.report[1] = x
140-
self.report[2] = y
141-
self.report[3] = wheel
143+
144+
# Send multiple reports if necessary to move or scroll requested amounts.
145+
while x != 0 or y != 0 or wheel != 0:
146+
partial_x = self._limit(x)
147+
partial_y = self._limit(y)
148+
partial_wheel = self._limit(wheel)
149+
self.report[1] = partial_x
150+
self.report[2] = partial_y
151+
self.report[3] = partial_wheel
142152
self.hid_mouse.send_report(self.report)
143-
else:
144-
raise ValueError('All arguments must be >= -127 and <= 127')
153+
x -= partial_x
154+
y -= partial_y
155+
wheel -= partial_wheel
145156

146157
@staticmethod
147-
def _distance_ok(dist):
148-
"""Return True if dist is in the range [-127,127]"""
149-
return -127 <= dist <= 127
158+
def _limit(dist):
159+
return min(127, max(-127, dist))

0 commit comments

Comments
 (0)