Basic USB HID test failing...

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Basic USB HID test failing...

Post by pythoncoder » Tue Sep 13, 2016 9:21 am

Are you not asking the USB interface to do two contradictory things at once? From the point of view of the host PC, the device connecting to its USB port will appear either as a keyboard device or as a serial device. The fact that you are getting a REPL implies it is using it as a serial device. I'd question whether the host PC would also be able to use it as a keyboard.

I've only actually done this on Arduino but the aim was to produce a device which, when plugged into a PC, appeared as a keyboard. This precluded getting debug information out of the same port.
Peter Hinch
Index to my micropython libraries.

fpp
Posts: 64
Joined: Wed Jul 20, 2016 12:08 pm

Re: Basic USB HID test failing...

Post by fpp » Tue Sep 13, 2016 11:20 am

Thanks for the suggestion ! This is related to my vague musings at the end of post #5 on page 1...
However, the docs for pyb_usb.mode don't seem to reflect that :

Code: Select all

 pyb.usb_mode([modestr, ]vid=0xf055, pid=0x9801, hid=pyb.hid_mouse)
    If called with no arguments, return the current USB mode as a string.
    If called with modestr provided, attempts to set USB mode. This can only be done when called from boot.py before pyb.main() has been called. The following values of modestr are understood:
        None: disables USB
        'VCP': enable with VCP (Virtual COM Port) interface
        'VCP+MSC': enable with VCP and MSC (mass storage device class)
        'VCP+HID': enable with VCP and HID (human interface device)
Unless the wording is really ambiguous, it does look like VCP is a given in all cases, and you can't ask for HID only ?...

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Basic USB HID test failing...

Post by pythoncoder » Wed Sep 14, 2016 6:09 am

I think you can call it with HID only. As you'll lose the REPL I suggest trying it on an SD card so you can get the REPL back by booting from flash or altering the SD card's boot.py from a PC.
Peter Hinch
Index to my micropython libraries.

fpp
Posts: 64
Joined: Wed Jul 20, 2016 12:08 pm

Re: Basic USB HID test failing...

Post by fpp » Wed Sep 14, 2016 8:26 am

Well, you're clearly part of the handful of experienced and helpful members of this board, so I'll give it a go tonight... after all, it's not like there's anything to lose :-)

I do wonder however why you think that... Either you've found information that eludes me, or you have uncanny intuition, or have actually tried it... But nothing I've read on the subject even hints at this (I even quickly perused the C code for pyb, until my eyes glazed over :-) ).

Even the very few available (python) code samples about USB HID (all mouse, no keyboard) use the dual mode.
Come to think of it, maybe I should also try those, just to see if we're barking up the right tree ? :-)

fpp
Posts: 64
Joined: Wed Jul 20, 2016 12:08 pm

Re: Basic USB HID test failing...

Post by fpp » Wed Sep 14, 2016 6:55 pm

As promised, here are two more data points for tonight on this subject :

One :
Running the same code as quoted above, but with only pyb.usb_mode('HID', hid=pyb.hid_keyboard)" in boot.py (instead of "VCP+HID"), and without the "prints"
... does not make it run any better (nothing sent to the PC active window).
In fact, I suspect (can't be sure without the console) that it doesn't run at all, as the boot sequence is unusual, with all LEDs flashing quickly in sequence.

Two :
Setting the board in "mouse mode" with pyb.usb_mode('CDC+HID') in boot.py,
and running the example code below in main.py :
http://docs.micropython.org/en/latest/p ... ts-by-hand
... works perfectly, including with serial console connected.

So I guess there are only two reasonable explanations left now :
- the implementation of HID for keyboard is buggy (although at least one person seems to have it working, but did not provide code)
or, more probably,
- my guesswork of how it should work, put together from hints (lacking documentation and examples), is totally wrong.

Either way, I'm at total loss :-)

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Basic USB HID test failing...

Post by dhylands » Wed Sep 14, 2016 9:01 pm

I got the following to work. In boot.py, use:

Code: Select all

pyb.usb_mode('VCP+HID', hid=pyb.hid_keyboard) # act as a serial device and a keyboard
and I used the following test code:

Code: Select all

import pyb

repl = pyb.USB_VCP()

kb = pyb.USB_HID()
led = pyb.LED(1)
buf = bytearray(9)
sw = pyb.Switch()

buf[0] = 0x01   # Keyboard

LEFT_SHIFT = 1 << 1

print('Press USR switch')
while 1 :
    if sw() :
        print('Sending T')
        led.toggle()
        # Do the key down
        buf[0] = LEFT_SHIFT
        buf[2] = 0x17 # keycode for 'T'
        kb.send(buf)
        # Do the key up
        buf[0] = 0x00
        buf[2] = 0x00
        kb.send(buf)
        # Wait for switch to be released
        while sw():
            pass
        led.toggle()
    if repl.any():
        data = repl.recv(1)
        ch = data[0]
        if ch < ord(' ') or ch > ord('~'):
            ch = '.'
        print("Rcvd: 0x%x '%c'" % (data[0], ch))
and this is what I get:

Code: Select all

MicroPython v1.8.3-24-g095e43a-dirty on 2016-09-06; PYBv1.0 with STM32F405RG
Type "help()" for more information.
>>> 
>>> import hid_test
Press USR switch
Sending T
Rcvd: 0x54 'T'
Sending T
Rcvd: 0x54 'T'
Sending T
Rcvd: 0x54 'T'
Sending T
Rcvd: 0x54 'T'
If I switch to a different terminal window then I see a T pressed each time I push the user button.

Chapter 10 of this document: http://www.usb.org/developers/hidpage/Hut1_12v2.pdf has the key code to key mapping (which is how I figured out 0x17 mapped to T)

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Basic USB HID test failing...

Post by pythoncoder » Thu Sep 15, 2016 7:52 am

OK, I stand corrected - my knowledge of Linux is evidently lacking. :oops: I didn't realise that the host PC can manage to recognise the single USB device both as a keyboard and as a serial device. Can Windows and OSX manage this trick too?
Peter Hinch
Index to my micropython libraries.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Basic USB HID test failing... thanks

Post by dhylands » Thu Sep 15, 2016 8:40 am

pythoncoder wrote:OK, I stand corrected - my knowledge of Linux is evidently lacking. :oops: I didn't realise that the host PC can manage to recognise the single USB device both as a keyboard and as a serial device. Can Windows and OSX manage this trick too?
I believe so. When a device had more than one set of functionality they're referred to as composite devices.
Being a serial port and mass storage is just one example of a composite device and that works on Windows, Linux and OSX.

fpp
Posts: 64
Joined: Wed Jul 20, 2016 12:08 pm

Re: Basic USB HID test failing...

Post by fpp » Thu Sep 15, 2016 8:43 am

Dunno about OSX (but willing to bet), on Windows certainly yes : all the above testing was done on a Windows7 host, as that is the non-negociable target for what I'm tyring to do here...

fpp
Posts: 64
Joined: Wed Jul 20, 2016 12:08 pm

Re: Basic USB HID test failing...

Post by fpp » Thu Sep 15, 2016 8:45 am

...and quick but heartfelt thanks to Dave (again) for digging deeper !

Saw the post this morning but too late for even a quick test before leaving for work.

Will do this evening and report...

Post Reply