adafruit / Adafruit_Blinka

Add CircuitPython hardware API and libraries to MicroPython & CPython devices
https://learn.adafruit.com/circuitpython-on-raspberrypi-linux
MIT License
457 stars 346 forks source link

usb_hid mouse Raspberry Pi Zero 2 W #611

Open Med-JH opened 2 years ago

Med-JH commented 2 years ago

I cant get mouse emulation working,

It is either becoming stuck or gives an error. If I follow this example I can send keyboard and mouse input. So the hardware and OS should be ok. I am also looking for a absolute mouse solution.

Thank you

Error variant:

import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.mouse import Mouse
from adafruit_hid.keycode import Keycode
usb_hid.enable((usb_hid.Device.MOUSE,))
usb_hid.enable((usb_hid.Device.MOUSE,))

Error:

Traceback (most recent call last): File "/usr/local/lib/python3.9/dist-packages/adafruit_hid/mouse.py", line 51, in init self._send_no_move() File "/usr/local/lib/python3.9/dist-packages/adafruit_hid/mouse.py", line 148, in _send_no_move self._mouse_device.send_report(self.report) File "/usr/local/lib/python3.9/dist-packages/usb_hid.py", line 66, in send_report fd.write(report) BlockingIOError: [Errno 11] write could not complete without blocking

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python3.9/dist-packages/adafruit_hid/mouse.py", line 54, in init self._send_no_move() File "/usr/local/lib/python3.9/dist-packages/adafruit_hid/mouse.py", line 148, in _send_no_move self._mouse_device.send_report(self.report) File "/usr/local/lib/python3.9/dist-packages/usb_hid.py", line 66, in send_report fd.write(report) BlockingIOError: [Errno 11] write could not complete without blocking

stuck variant:

import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.mouse import Mouse
from adafruit_hid.keycode import Keycode
usb_hid.enable((usb_hid.Device.KEYBOARD, usb_hid.Device.MOUSE),)
mouse = Mouse(usb_hid.devices)

Spawns a keyboard on a connected Windows PC but no mouse. Instead of the mouse I get an failed HID device with Error:

This device cannot start. (Code 10) The I/O request was canceled.

mouse = Mouse(usb_hid.devices) never finishes

if I use

usb_hid.enable((usb_hid.Device.KEYBOARD, usb_hid.Device.MOUSE),boot_device=2) I get a mouse but no keyboard

my preparation

lsb_release -a

No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux 11 (bullseye)
Release:        11
Codename:       bullseye

boot/config.txt

...
dtoverlay=dwc2
enable_uart=1
dtoverlay=tc358743
...

pip list

Adafruit-Blinka                  8.2.1
adafruit-circuitpython-busdevice 5.2.3
adafruit-circuitpython-hid       5.3.2
adafruit-circuitpython-requests  1.12.8
adafruit-circuitpython-typing    1.8.1
Adafruit-PlatformDetect          3.27.2
Adafruit-PureIO                  1.1.9
adafruit-python-shell            1.3.3
makermelissa commented 2 years ago

I noticed the example you linked to was for the original Raspberry Pi Zero W and you have the Pi Zero 2 W. Is it possible some functionality may have been removed?

Med-JH commented 2 years ago

I dont think that there is anything missing on the Pi Zero 2 becausse the example works, a "boot" mouse works and I also tested pikvm which has similar functionality. My guess is, that it is something with the reports, but I cant figure it out.

Med-JH commented 2 years ago

I skipped the Mouse and added Digitizer code to usb_hid.py

I used code from here https://gist.github.com/bitboy85/cdcd0e7e04082db414b5f1d23ab09005 and added it to usb_hid.py

Device.DIGI = Device.DIGI = Device(
       descriptor=bytes(
        # Absolute mouse
        (0x05, 0x01)    # Usage Page (Generic Desktop)
        ...

    ),
    usage_page=0x1,
    usage=0x02,
    report_ids=[11],
    in_report_lengths=[6], # Number of bytes in the send report = 1 byte for buttons, 2 bytes for x, 2 bytes for y, 1 byte for wheel
    out_report_lengths=[0],
)

I then build mu code on based on mouse_abs.py

but mouse appears to be still broken in the current usb_hid.py

makermelissa commented 2 years ago

Good find. Like 99.9% of this was written by a community contributor, so I'm not really familiar with this module at all without really diving in. If you'd like to try and get it working, I'd be happy to review a PR.