Macropad - reading serial while emulating a keyboard

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
mccollam
 
Posts: 6
Joined: Fri Mar 10, 2017 3:00 pm

Macropad - reading serial while emulating a keyboard

Post by mccollam »

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?

User avatar
dastels
 
Posts: 15608
Joined: Tue Oct 20, 2015 3:22 pm

Re: Macropad - reading serial while emulating a keyboard

Post by dastels »

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

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

Re: Macropad - reading serial while emulating a keyboard

Post by mccollam »

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

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

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!

User avatar
dastels
 
Posts: 15608
Joined: Tue Oct 20, 2015 3:22 pm

Re: Macropad - reading serial while emulating a keyboard

Post by dastels »

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

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

Re: Macropad - reading serial while emulating a keyboard

Post by mccollam »

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!

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”