Reading through the Macropad hotkeys tutorial (https://learn.adafruit.com/macropad-hot ... ng-further), there's a note that says "CircuitPython can receive serial messages while also emulating a keyboard".
I've looked through the Adafruit HID API docs (https://circuitpython.readthedocs.io/pr ... index.html) and don't see anything that looks promising in this respect, so I assume this would be using serial through a UART directly. Can this be done over the same USB connection that the HID emulation is using?
Are there any examples of something like this available?
Macropad - reading serial while emulating a keyboard
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- mccollam
- Posts: 6
- Joined: Fri Mar 10, 2017 3:00 pm
- dastels
- Posts: 15608
- Joined: Tue Oct 20, 2015 3:22 pm
Re: Macropad - reading serial while emulating a keyboard
I take that to mean that it can read from the USB/serial connection while acting as a keyboard. A single physical device can enumerate as multiple logical devices over a single USB connection.
Dave
Dave
- mccollam
- Posts: 6
- Joined: Fri Mar 10, 2017 3:00 pm
Re: Macropad - reading serial while emulating a keyboard
Awesome, thanks for the pointer, Dave!
I found the usb_cdc module which seems to do the trick! (Or at least it will when I can work out how to get the readline method to return before hitting the timeout...)
For anyone else who is wondering, this needs to be enabled in boot.py:
You can then read and write from it in code.py:
My read is that readline() should terminate when it sees '\n' but I haven't gotten that working yet. I'll update here once I get something fully working!
I found the usb_cdc module which seems to do the trick! (Or at least it will when I can work out how to get the readline method to return before hitting the timeout...)
For anyone else who is wondering, this needs to be enabled in boot.py:
Code: Select all
import usb_cdc
usb_cdc.enable(console=True, data=True)
usb_cdc.data.timeout = 1
Code: Select all
import usb_cdc
[...]
uart = usb_cdc.data
if uart.in_waiting > 0:
s = uart.readline()
uart.write(s) # write back out what was read
s = bytearray('hello, world'.encode()) # encode a string as a bytearray
uart.write(s) # write out that string
- dastels
- Posts: 15608
- Joined: Tue Oct 20, 2015 3:22 pm
Re: Macropad - reading serial while emulating a keyboard
That's what I'd expect. Is there a way to set the line terminator. It might be using the MSDOS/Windows convention of \r\n.My read is that readline() should terminate when it sees '\n'
Dave
- mccollam
- Posts: 6
- Joined: Fri Mar 10, 2017 3:00 pm
Re: Macropad - reading serial while emulating a keyboard
It turned out to be something with what Minicom was doing. Once I switched to sending data from a Python script directly, everything worked.
Thanks again for the pointers!
Thanks again for the pointers!
Please be positive and constructive with your questions and comments.