-
Notifications
You must be signed in to change notification settings - Fork 2
add find_boot_keyboard function and example #7
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# SPDX-FileCopyrightText: Copyright (c) 2025 Tim Cocks for Adafruit Industries | ||
# | ||
# SPDX-License-Identifier: MIT | ||
import array | ||
|
||
import usb | ||
|
||
import adafruit_usb_host_descriptors | ||
|
||
# lists for mouse interface indexes, endpoint addresses, and USB Device instances | ||
# each of these will end up with length 2 once we find both mice | ||
mouse_interface_indexes = None | ||
mouse_endpoint_addresses = None | ||
keyboard = None | ||
|
||
# scan for connected USB devices | ||
for device in usb.core.find(find_all=True): | ||
# check for boot mouse endpoints on this device | ||
kbd_interface_index, kbd_endpoint_address = ( | ||
adafruit_usb_host_descriptors.find_boot_keyboard_endpoint(device) | ||
) | ||
# if a boot mouse interface index and endpoint address were found | ||
tannewt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if kbd_interface_index is not None and kbd_interface_index is not None: | ||
keyboard = device | ||
|
||
# detach device from kernel if needed | ||
if keyboard.is_kernel_driver_active(0): | ||
keyboard.detach_kernel_driver(0) | ||
|
||
# set the mouse configuration so it can be used | ||
keyboard.set_configuration() | ||
|
||
buf = array.array("b", [0] * 8) | ||
|
||
while True: | ||
# try to read data from the mouse | ||
try: | ||
count = keyboard.read(mouse_endpoint_addresses, buf, timeout=10) | ||
|
||
# if there is no data it will raise USBTimeoutError | ||
except usb.core.USBTimeoutError: | ||
# Nothing to do if there is no data for this mouse | ||
continue | ||
|
||
for b in buf: | ||
print(hex(b), end=" ") | ||
print() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.