Skip to content

Updated README & Examples For Startup Race Condition #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
26 changes: 26 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,26 @@ This is easily achieved by downloading
Usage Example
=============

.. note::

A "startup race" condition can occur when using HID on some systems. The use of
a pause (``time.sleep()``) before creating a HID instance in your code will allow
the host system to finish setting up the device on the USB bus.

The ``Keyboard`` class sends keypress reports for a USB keyboard device to the host.

The ``Keycode`` class defines USB HID keycodes to send using ``Keyboard``.

.. code-block:: python

import time
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode

# Sleep for a bit to avoid a race condition on some systems
# when creating a HID instance
time.sleep(1)

# Set up a keyboard device.
kbd = Keyboard()

Expand Down Expand Up @@ -78,9 +89,14 @@ by various operating systems.

.. code-block:: python

import time
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS

# Sleep for a bit to avoid a race condition on some systems
# when creating a HID instance
time.sleep(1)

kbd = Keyboard()
layout = KeyboardLayoutUS(kbd)

Expand All @@ -95,8 +111,13 @@ The ``Mouse`` class simulates a three-button mouse with a scroll wheel.

.. code-block:: python

import time
from adafruit_hid.mouse import Mouse

# Sleep for a bit to avoid a race condition on some systems
# when creating a HID instance
time.sleep(1)

m = Mouse()

# Click the left mouse button.
Expand Down Expand Up @@ -125,9 +146,14 @@ remote controls, or the multimedia keys on certain keyboards.

.. code-block:: python

import time
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode

# Sleep for a bit to avoid a race condition on some systems
# when creating a HID instance
time.sleep(1)

cc = ConsumerControl()

# Raise volume.
Expand Down
4 changes: 4 additions & 0 deletions examples/keyboard_shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import board
import digitalio

# Sleep for a bit to avoid a race condition on some systems
# when creating a HID instance
time.sleep(1)

kbd = Keyboard()

# define buttons. these can be any physical switches/buttons, but the values
Expand Down
4 changes: 4 additions & 0 deletions examples/scroll.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import board
import digitalio

# Sleep for a bit to avoid a race condition on some systems
# when creating a HID instance
time.sleep(1)

mouse = Mouse()

# define buttons. these can be any physical switches/buttons, but the values
Expand Down