non-blocking reads from serial?
I'd like to write an app that talks to a program on my PC bidirectionally (kind of as a remote and utility display: send key presses and sensor values from the card10 to the PC, allow the PC to ask the card10 to blink LEDs/update display contents). The obvious way seems to be the USB-serial.
print() I can send text to the PC, but I would like a non-blocking way to check for data being sent from the PC, so that in an app main loop that reads sensors, controls LED blinking, ... this is just another step that checks for commands coming in.
I'm flexible in what this specifically looks like, some ideas:
- micropython seems to have a streams abstraction that maybe could wrap the serial input and seems to support this: http://docs.micropython.org/en/latest/library/uio.html (referenced from https://firmware.card10.badge.events.ccc.de/pycardium/stdlib.html#uio)
- it could be a function specific to the "standard output" matching
print(), e.g. a
try_input()that returns whatever is in the input buffer without waiting or a function to check if there is anything in the input buffer.
Possible workaround: I assume with
input() I can do a blocking read of a line of text (haven't tested this yet), but that would rely on the PC-side constantly sending newlines to not block the app in that call, which doesn't seem ideal.