Skip to content

Commit 9860fe9

Browse files
committed
More review changes. Fix sphinx errors. Break api.rst into separate files.
1 parent a129217 commit 9860fe9

File tree

12 files changed

+176
-72
lines changed

12 files changed

+176
-72
lines changed

README.rst

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ Introduction
1010
:target: https://gitter.im/adafruit/circuitpython?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge
1111
:alt: Gitter
1212
13-
This driver simulates USB HID devices, such as keyboard, mouse, and joystick.
14-
15-
Currently keyboard and mouse are implemented.
13+
This driver simulates USB HID devices. Currently keyboard and mouse are implemented.
1614

1715
Dependencies
1816
=============
@@ -33,8 +31,8 @@ The ``Keycode`` class defines USB HID keycodes to send using ``Keyboard``.
3331

3432
.. code-block:: python
3533
36-
from adafruit_hid import Keyboard
37-
from adafruit_hid import Keycode
34+
from adafruit_hid.keyboard import Keyboard
35+
from adafruit_hid.keycode import Keycode
3836
3937
# Set up a keyboard device.
4038
kbd = Keyboard()
@@ -56,25 +54,25 @@ The ``Keycode`` class defines USB HID keycodes to send using ``Keyboard``.
5654
# Release all keys.
5755
kbd.release_all()
5856
59-
The ``USKeyboardLayout`` sends ASCII characters using keypresses. It assumes
57+
The ``KeyboardLayoutUS`` sends ASCII characters using keypresses. It assumes
6058
the host is set to accept keypresses from a US keyboard.
6159

6260
If the host is expecting a non-US keyboard, the character to key mapping provided by
63-
``USKeyboardLayout`` will not always be correct.
61+
``KeyboardLayoutUS`` will not always be correct.
6462
Different keypresses will be needed in some cases. For instance, to type an ``'A'`` on
6563
a French keyboard (AZERTY instead of QWERTY), ``Keycode.Q`` should be pressed.
6664

67-
Currently this package provides only ``USKeyboardLayout``. More ``KeyboardLayout``
65+
Currently this package provides only ``KeyboardLayoutUS``. More ``KeyboardLayout``
6866
classes could be added to handle non-US keyboards and the different input methods provided
6967
by various operating systems.
7068

7169
.. code-block:: python
7270
73-
from adafruit_hid import Keyboard
74-
from adafruit_hid import USKeyboardLayout
71+
from adafruit_hid.keyboard import Keyboard
72+
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
7573
7674
kbd = Keyboard()
77-
layout = USKeyboardLayout(kbd)
75+
layout = KeyboardLayoutUS(kbd)
7876
7977
# Type 'abc' followed by Enter (a newline).
8078
layout.write('abc\n')
@@ -83,11 +81,11 @@ by various operating systems.
8381
# The method will return (Keycode.SHIFT, Keycode.FOUR).
8482
keycodes = layout.keycodes('$')
8583
86-
The ``Mouse` class simulates a three-button mouse with a scroll wheel.
84+
The ``Mouse`` class simulates a three-button mouse with a scroll wheel.
8785

8886
.. code-block:: python
8987
90-
from adafruit_hid import Mouse
88+
from adafruit_hid.mouse import Mouse
9189
9290
m = Mouse()
9391
@@ -117,4 +115,7 @@ API Reference
117115
.. toctree::
118116
:maxdepth: 2
119117

120-
api
118+
keyboard
119+
keycode
120+
keyboard_layout_us
121+
mouse

adafruit_hid/__init__.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,7 @@
2424
`adafruit_hid`
2525
====================================================
2626
27-
TODO(description)
27+
This driver simulates USB HID devices. Currently keyboard and mouse are implemented.
2828
2929
* Author(s): Scott Shawcroft, Dan Halbert
3030
"""
31-
# Import these for convenience so that the user may import the packages classes
32-
# directly without needing to know the file names.
33-
# E.g.:
34-
# from adafruit_hid import Keyboard
35-
36-
from .keyboard import Keyboard
37-
from .keycode import Keycode
38-
from .mouse import Mouse
39-
from .us_keyboard_layout import USKeyboardLayout

adafruit_hid/keyboard.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# The MIT License (MIT)
22
#
3-
# Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
3+
# Copyright (c) 2017 Dan Halbert
44
#
55
# Permission is hereby granted, free of charge, to any person obtaining a copy
66
# of this software and associated documentation files (the "Software"), to deal
@@ -22,7 +22,7 @@
2222
#
2323

2424
"""
25-
:mod:`adafruit_hid.Keyboard`
25+
:mod:`adafruit_hid.keyboard.Keyboard`
2626
====================================================
2727
2828
* Author(s): Scott Shawcroft, Dan Halbert
@@ -31,7 +31,7 @@
3131
from micropython import const
3232
import usb_hid
3333

34-
from . import Keycode
34+
from .keycode import Keycode
3535

3636
class Keyboard:
3737
"""Send HID keyboard reports."""
@@ -66,10 +66,14 @@ def __init__(self):
6666
def press(self, *keycodes):
6767
"""Send a report indicating that the given keys have been pressed.
6868
69+
:param keycodes: Press these keycodes all at once.
70+
:raises ValueError: if more than six regular keys are pressed.
71+
6972
Keycodes may be modifiers or regular keys.
7073
No more than six regular keys may be pressed simultaneously.
7174
72-
Examples:
75+
Examples::
76+
7377
from adafruit_hid.keycode import Keycode
7478
7579
# Press ctrl-x.
@@ -88,7 +92,12 @@ def press(self, *keycodes):
8892
def release(self, *keycodes):
8993
"""Send a USB HID report indicating that the given keys have been released.
9094
91-
Examples:
95+
:param keycodes: Release these keycodes all at once.
96+
97+
If a keycode to be released was not pressed, it is ignored.
98+
99+
Example::
100+
92101
# release SHIFT key
93102
kbd.release(Keycode.SHIFT)
94103
"""

adafruit_hid/us_keyboard_layout.py renamed to adafruit_hid/keyboard_layout_us.py

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# The MIT License (MIT)
22
#
3-
# Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
3+
# Copyright (c) 2017 Dan Halbert
44
#
55
# Permission is hereby granted, free of charge, to any person obtaining a copy
66
# of this software and associated documentation files (the "Software"), to deal
@@ -22,15 +22,15 @@
2222
#
2323

2424
"""
25-
:mod:`adafruit_hid.USKeyboardLayout`
26-
====================================================
25+
:mod:`adafruit_hid.keyboard_layout_us.KeyboardLayoutUS`
26+
=======================================================
2727
2828
* Author(s): Dan Halbert
2929
"""
3030

31-
from . import Keycode
31+
from .keycode import Keycode
3232

33-
class USKeyboardLayout:
33+
class KeyboardLayoutUS:
3434
"""Map ASCII characters to appropriate keypresses on a standard US PC keyboard.
3535
3636
Non-ASCII characters and most control characters will raise an exception.
@@ -182,11 +182,29 @@ class USKeyboardLayout:
182182
)
183183

184184
def __init__(self, keyboard):
185-
"""Specify the layout for the given keyboard."""
185+
"""Specify the layout for the given keyboard.
186+
187+
:param keyboard: a Keyboard object. Write characters to this keyboard when requested.
188+
189+
Example::
190+
191+
kbd = Keyboard()
192+
layout = KeyboardLayoutUS(kbd)
193+
"""
194+
186195
self.keyboard = keyboard
187196

188197
def write(self, string):
189-
"""Type the string by pressing and releasing keys on my keyboard."""
198+
"""Type the string by pressing and releasing keys on my keyboard.
199+
200+
:param string: A string of ASCII characters.
201+
:raises ValueError: if any of the characters are not ASCII or have no keycode (such as some control characters).
202+
203+
Example::
204+
205+
# Write abc followed by Enter to the keyboard
206+
layout.write('abc\\n')
207+
"""
190208
for char in string:
191209
keycode = self._char_to_keycode(char)
192210
# If this is a shifted char, clear the SHIFT flag and press the SHIFT key.
@@ -197,7 +215,24 @@ def write(self, string):
197215
self.keyboard.release_all()
198216

199217
def keycodes(self, char):
200-
"""Return a tuple of keycodes needed to type the given character."""
218+
"""Return a tuple of keycodes needed to type the given character.
219+
220+
:param char: A single ASCII character in a string.
221+
:type char: str of length one.
222+
:returns: tuple of Keycode keycodes.
223+
:raises ValueError: if ``char`` is not ASCII or there is no keycode for it.
224+
225+
Examples::
226+
227+
# Returns (Keycode.TAB,)
228+
keycodes('\t')
229+
# Returns (Keycode.A,)
230+
keycode('a')
231+
# Returns (Keycode.SHIFT, Keycode.A)
232+
keycode('A')
233+
# Raises ValueError because it's a accented e and is not ASCII
234+
keycode('é')
235+
"""
201236
keycode = self._char_to_keycode(char)
202237
if keycode & self.SHIFT_FLAG:
203238
return (Keycode.SHIFT, keycode & ~self.SHIFT_FLAG)
@@ -208,7 +243,8 @@ def _char_to_keycode(self, char):
208243
"""Return the HID keycode for the given ASCII character, with the SHIFT_FLAG possibly set.
209244
210245
If the character requires pressing the Shift key, the SHIFT_FLAG bit is set.
211-
You must clear this bit before passing the keycode in a USB report."""
246+
You must clear this bit before passing the keycode in a USB report.
247+
"""
212248
char_val = ord(char)
213249
if char_val > 128:
214250
raise ValueError("Not an ASCII character.")

adafruit_hid/keycode.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#
2323

2424
"""
25-
:mod:`adafruit_hid.Keycode`
25+
:mod:`adafruit_hid.keycode.Keycode`
2626
====================================================
2727
2828
* Author(s): Scott Shawcroft, Dan Halbert

adafruit_hid/mouse.py

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#
2323

2424
"""
25-
:mod:`adafruit_hid.Mouse`
25+
:mod:`adafruit_hid.mouse.Mouse`
2626
====================================================
2727
2828
* Author(s): Dan Halbert
@@ -33,9 +33,11 @@ class Mouse:
3333
"""Send USB HID mouse reports."""
3434

3535
LEFT_BUTTON = 1
36+
"""Left mouse button."""
3637
RIGHT_BUTTON = 2
38+
"""Right mouse button."""
3739
MIDDLE_BUTTON = 4
38-
ALL_BUTTONS = LEFT_BUTTON | RIGHT_BUTTON | MIDDLE_BUTTON
40+
"""Middle mouse button."""
3941

4042
def __init__(self):
4143
"""Create a Mouse object that will send USB mouse HID reports."""
@@ -56,12 +58,26 @@ def __init__(self):
5658

5759

5860
def press(self, buttons):
59-
"""Press the given mouse buttons."""
61+
"""Press the given mouse buttons.
62+
63+
:param buttons: a bitwise-or'd combination of ``LEFT_BUTTON``, ``MIDDLE_BUTTON``, and ``RIGHT_BUTTON``.
64+
65+
Examples::
66+
67+
# Press the left button.
68+
m.press(Mouse.LEFT_BUTTON)
69+
70+
# Press the left and right buttons simultaneously.
71+
m.press(Mouse.LEFT_BUTTON | Mouse.RIGHT_BUTTON)
72+
"""
6073
self.report[0] |= buttons
6174
self.move(0, 0, 0)
6275

6376
def release(self, buttons):
64-
"""Release the given mouse buttons."""
77+
"""Release the given mouse buttons.
78+
79+
:param buttons: a bitwise-or'd combination of ``LEFT_BUTTON``, ``MIDDLE_BUTTON``, and ``RIGHT_BUTTON``.
80+
"""
6581
self.report[0] &= ~buttons
6682
self.move(0, 0, 0)
6783

@@ -71,19 +87,53 @@ def release_all(self):
7187
self.move(0, 0, 0)
7288

7389
def click(self, buttons):
74-
"""Press and release the given mouse buttons."""
90+
"""Press and release the given mouse buttons.
91+
92+
:param buttons: a bitwise-or'd combination of ``LEFT_BUTTON``, ``MIDDLE_BUTTON``, and ``RIGHT_BUTTON``.
93+
94+
Examples::
95+
96+
# Click the left button.
97+
m.click(Mouse.LEFT_BUTTON)
98+
99+
# Double-click the left button.
100+
m.click(Mouse.LEFT_BUTTON)
101+
m.click(Mouse.LEFT_BUTTON)
102+
"""
103+
75104
self.press(buttons)
76105
self.release(buttons)
77106

78107
def move(self, x_distance, y_distance, wheel_turn):
79108
"""Move the mouse and turn the wheel as directed.
80109
81-
All arguments should be in the range -127 to 127 inclusive.
82-
"""
83-
self.report[1] = x_distance
84-
self.report[2] = y_distance
85-
self.report[3] = wheel_turn
86-
self.hid_mouse.send_report(self.report)
110+
:param x_distance: Move the mouse along the x axis. Negative is to the left, positive is to the right.
111+
:param y_distance: Move the mouse along the y axis. Negative is toward the user, positive is away from the user.
112+
:param wheel turn: Rotate the wheel this amount. Negative is toward the user, positive is away from the user.
113+
:raises ValueError: if any argument is not in the range -127 to 127 inclusive.
114+
115+
Examples::
87116
117+
# Move 100 to the left.
118+
m.move(-100, 0, 0)
88119
120+
# Move diagonally to the upper right.
121+
m.move(50, 20, 0)
89122
123+
# Roll the mouse wheel away from the user.
124+
m.move(0, 0, 5)
125+
"""
126+
if (self._distance_ok(x_distance)
127+
and self._distance_ok(y_distance)
128+
and self._distance_ok(wheel_turn)):
129+
self.report[1] = x_distance
130+
self.report[2] = y_distance
131+
self.report[3] = wheel_turn
132+
self.hid_mouse.send_report(self.report)
133+
else:
134+
raise ValueError('All arguments must be >= -127 and <= 127')
135+
136+
@staticmethod
137+
def _distance_ok(dist):
138+
"""Return True if dist is in the range [-127,127]"""
139+
return -127 <= dist <= 127

api.rst

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)