Basic USB HID test failing...
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Basic USB HID test failing...
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.
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.
Index to my micropython libraries.
Re: Basic USB HID test failing...
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 :
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 ?...
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)
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Basic USB HID test failing...
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.
Index to my micropython libraries.
Re: Basic USB HID test failing...
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 ?
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 ?
Re: Basic USB HID test failing...
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
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
Re: Basic USB HID test failing...
I got the following to work. In boot.py, use: and I used the following test code: and this is what I get:
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)
Code: Select all
pyb.usb_mode('VCP+HID', hid=pyb.hid_keyboard) # act as a serial device and a keyboard
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))
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'
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)
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Basic USB HID test failing...
OK, I stand corrected - my knowledge of Linux is evidently lacking. 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.
Index to my micropython libraries.
Re: Basic USB HID test failing... thanks
I believe so. When a device had more than one set of functionality they're referred to as composite devices.pythoncoder wrote:OK, I stand corrected - my knowledge of Linux is evidently lacking. 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?
Being a serial port and mass storage is just one example of a composite device and that works on Windows, Linux and OSX.
Re: Basic USB HID test failing...
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...
Re: Basic USB HID test failing...
...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...
Saw the post this morning but too late for even a quick test before leaving for work.
Will do this evening and report...