adafruit / circuitpython

CircuitPython - a Python implementation for teaching coding with microcontrollers
https://circuitpython.org
Other
4.04k stars 1.2k forks source link

adafruit_hid.mouse doesn't run when board is plugged into USB #1668

Closed uhrheber closed 5 years ago

uhrheber commented 5 years ago

I tried the following code:

from time import sleep
from adafruit_hid.mouse import Mouse

mouse = Mouse()

while True:
    mouse.move(x=15)
    sleep(0.07)
    mouse.move(y=15)
    sleep(0.07)
    mouse.move(x=-15)
    sleep(0.07)
    mouse.move(y=-15)
    sleep(0.07)

Just a simple test, to move the mouse cursor around in squares. The code runs perfectly when:

But: When the board is freshly connected to USB, the code doesn't start. Tested on ItsyBisty M4 Express and pca10059. After plugging in, the M4 Express shows a green LED, followed by an orange LED, followed by four blue flashes.

HID keyboard does work, though.

OS was Windows 10.

deshipu commented 5 years ago

Do you get any errors or other message in the console?

uhrheber commented 5 years ago

Should there be any errors, they're long gone when I connect a terminal. I only get a REPL prompt after pressing enter. But the LED blink code should give a hint, I guess?

deshipu commented 5 years ago

I don't suppose you have a PyPortal or Hallowing to try and get the messages displayed on the screen? I tried with a Hallowing just now, but I couldn't reproduce your problem — it works for me.

deshipu commented 5 years ago

By the way, which version of the firmware are you using?

uhrheber commented 5 years ago

4.0.0 beta 5, latest master from today, on both boards.

deshipu commented 5 years ago

That's the same I tried. I suppose this is something specific to Windows.

deshipu commented 5 years ago

You could try adding some delay at the beginning of the program...

uhrheber commented 5 years ago

Maybe. I'll try it with Linux when I'm home.

uhrheber commented 5 years ago

I already tried a 5 seconds delay at the beginning. No dice.

uhrheber commented 5 years ago

I now added a 0.5 seconds delay before and after instantiating mouse, and now it works.

This should maybe be added to the documentation/examples.

The working code is now:

from time import sleep

import analogio
import board
import digitalio
from adafruit_hid.mouse import Mouse

sleep(0.5)
mouse = Mouse()
sleep(0.5) 

while True:
    mouse.move(x=15)
    sleep(0.07)
    mouse.move(y=15)
    sleep(0.07)
    mouse.move(x=-15)
    sleep(0.07)
    mouse.move(y=-15)
    sleep(0.07)

I'm closing this issue.

dhalbert commented 5 years ago

The Mouse code actually does this already, but it sounds like you need a bit more: https://github.com/adafruit/Adafruit_CircuitPython_HID/blob/master/adafruit_hid/mouse.py#L60