0

Macropad - reading serial while emulating a keyboard
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Macropad - reading serial while emulating a keyboard

by mccollam on Fri Jul 30, 2021 1:05 pm

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?

mccollam
 
Posts: 6
Joined: Fri Mar 10, 2017 3:00 pm

Re: Macropad - reading serial while emulating a keyboard

by dastels on Fri Jul 30, 2021 3:13 pm

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

dastels
 
Posts: 7278
Joined: Tue Oct 20, 2015 3:22 pm

Re: Macropad - reading serial while emulating a keyboard

by mccollam on Sat Jul 31, 2021 9:32 pm

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:

Code: Select all | TOGGLE FULL SIZE
import usb_cdc
usb_cdc.enable(console=True, data=True)
usb_cdc.data.timeout = 1


You can then read and write from it in code.py:
Code: Select all | TOGGLE FULL SIZE
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


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!

mccollam
 
Posts: 6
Joined: Fri Mar 10, 2017 3:00 pm

Re: Macropad - reading serial while emulating a keyboard

by dastels on Sat Jul 31, 2021 11:25 pm

My read is that readline() should terminate when it sees '\n'


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.

Dave

dastels
 
Posts: 7278
Joined: Tue Oct 20, 2015 3:22 pm

Re: Macropad - reading serial while emulating a keyboard

by mccollam on Sun Aug 01, 2021 9:18 pm

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!

mccollam
 
Posts: 6
Joined: Fri Mar 10, 2017 3:00 pm

Please be positive and constructive with your questions and comments.


cron