Skip to content

usb_hid: "USB Busy" errors trying to use the mouse device #114

@dhalbert

Description

@dhalbert

I added mouse support to adafruit_hid, but when I tested the code I got OSError: USB Busy errors in the REPL when I tried to send a report.

I found a device on usage page 1 with usage 2. I'm not sure what the USB report should be. I looked through the C++ code and it was hard to find the right descriptor or even interpret the ones I found. I was assuming it was four bytes: one for buttons, and three for x, y, and wheel. But that may be wrong. Could you point me to the right place to look for it?

Is the USB Busy error due to the mouse conflicting with the keyboard device? I did some websearching and it appears that one might want to create a a "multiple Top-Level Collection (TLC)" HID device or perhaps some kind of composite device. But I haven't studied this in detail.

I have an important use case for instantiating the mouse and keyboard devices simultaneously, because I want to be able to simulate "Shift-leftclick", etc.

Here's the error and the code.

>>> from adafruit_hid import Mouse
>>> m = Mouse()
>>> m.move(0,0,0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "adafruit_hid/mouse.py", line 83, in move
OSError: USB Busy
>>>
import usb_hid

class Mouse:
    """Send USB HID mouse reports."""

    LEFT_BUTTON = 1
    RIGHT_BUTTON = 2
    MIDDLE_BUTTON = 4
    ALL_BUTTONS = LEFT_BUTTON | RIGHT_BUTTON | MIDDLE_BUTTON

    def __init__(self):
        """Create a Mouse object that will send USB mouse HID reports."""
        self.hid_mouse = None
        for device in usb_hid.devices:
            if device.usage_page is 0x1 and device.usage is 0x02:
                self.hid_mouse = device
                break
        if not self.hid_mouse:
            raise IOError("Could not find an HID mouse device.")

        # Reuse this bytearray to send mouse reports.
        # report[0] buttons pressed (LEFT, MIDDLE, RIGHT)
        # report[1] x movement
        # report[2] y movement
        # report[3] wheel movement
        self.report = bytearray(4)


    def press(self, buttons):
        "Press the given mouse buttons."
        self.report[0] |= buttons
        self.move(0, 0, 0)

    def release(self, buttons):
        "Release the given mouse buttons."
        self.report[0] &= ~buttons
        self.move(0, 0, 0)

    def release_all(self):
        "Release all the mouse buttons."
        self.report[0] = 0
        self.move(0, 0, 0)

    def click(self, buttons):
        "Press and release the given mouse buttons."
        self.press(buttons)
        self.release(buttons)

    def move(self, x_distance, y_distance, wheel_turn):
        "Move the mouse and turn the wheel as directed. Any or all arguments may be zero."
        self.report[1] = x_distance
        self.report[2] = y_distance
        self.report[3] = wheel_turn
        self.hid_mouse.send_report(self.report)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy