Skip to content

Commit

Permalink
Merge pull request #15 from dhalbert/wait_for_ready
Browse files Browse the repository at this point in the history
wait for HID device ready; allow longer mouse movements
  • Loading branch information
tannewt authored Apr 9, 2018
2 parents 524a8b5 + ae96b33 commit 167e101
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
9 changes: 9 additions & 0 deletions adafruit_hid/consumer_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* Author(s): Dan Halbert
"""

import time
import usb_hid

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

# Do a no-op to test if HID device is ready.
# If not, wait a bit and try once more.
try:
self.send(0x0)
except OSError:
time.sleep(1)
self.send(0x0)

def send(self, consumer_code):
"""Send a report to do the specified consumer control action,
and then stop the action (so it will not repeat).
Expand Down
13 changes: 12 additions & 1 deletion adafruit_hid/keyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
* Author(s): Scott Shawcroft, Dan Halbert
"""

from micropython import const
import time
import usb_hid

from micropython import const

from .keycode import Keycode

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

# Do a no-op to test if HID device is ready.
# If not, wait a bit and try once more.
try:
self.release_all()
except OSError:
time.sleep(1)
self.release_all()


def press(self, *keycodes):
"""Send a report indicating that the given keys have been pressed.
Expand Down
34 changes: 22 additions & 12 deletions adafruit_hid/mouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* Author(s): Dan Halbert
"""
import time
import usb_hid

class Mouse:
Expand Down Expand Up @@ -56,6 +57,13 @@ def __init__(self):
# report[3] wheel movement
self.report = bytearray(4)

# Do a no-op to test if HID device is ready.
# If not, wait a bit and try once more.
try:
self.move(0, 0, 0)
except OSError:
time.sleep(1)
self.move(0, 0, 0)

def press(self, buttons):
"""Press the given mouse buttons.
Expand Down Expand Up @@ -116,7 +124,6 @@ def move(self, x=0, y=0, wheel=0):
positive is downwards.
:param wheel: Rotate the wheel this amount. Negative is toward the user, positive
is away from the user. The scrolling effect depends on the host.
:raises ValueError: if any argument is not in the range -127 to 127 inclusive.
Examples::
Expand All @@ -133,17 +140,20 @@ def move(self, x=0, y=0, wheel=0):
# Roll the mouse wheel away from the user.
m.move(wheel=1)
"""
if (self._distance_ok(x)
and self._distance_ok(y)
and self._distance_ok(wheel)):
self.report[1] = x
self.report[2] = y
self.report[3] = wheel

# Send multiple reports if necessary to move or scroll requested amounts.
while x != 0 or y != 0 or wheel != 0:
partial_x = self._limit(x)
partial_y = self._limit(y)
partial_wheel = self._limit(wheel)
self.report[1] = partial_x
self.report[2] = partial_y
self.report[3] = partial_wheel
self.hid_mouse.send_report(self.report)
else:
raise ValueError('All arguments must be >= -127 and <= 127')
x -= partial_x
y -= partial_y
wheel -= partial_wheel

@staticmethod
def _distance_ok(dist):
"""Return True if dist is in the range [-127,127]"""
return -127 <= dist <= 127
def _limit(dist):
return min(127, max(-127, dist))

0 comments on commit 167e101

Please sign in to comment.