-
Notifications
You must be signed in to change notification settings - Fork 1.3k
'getch()' #231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Try Reference: https://docs.python.org/3/library/sys.html#sys.stdin |
not really - its basically input() but it only gives you a char at a time. i wanted something where it was timeout-not-blocking AND you didnt have to hit return |
We'll have to add an new API for this. Its not well covered in CPython. More background is here: https://stackoverflow.com/questions/2408560/python-nonblocking-console-input |
Shouldn't |
Just tried this: |
Yes, but that's not consistent with other stream-like interfaces. |
I think we want to make the behavior consistent with CPython. Do you mean other stream-like interfaces in Python or in general? It's a file-like interface, where the reads might be really slow, because the device (the human) is slow to supply characters. I tried to find some |
Looking at the documentation at https://docs.python.org/3/library/io.html I can see there is a whole zoo of different behaviors, depending on what the object on which you call It seems that there is a way to switch the |
there's nothing in 'standard' python that works on all OS's - windows uses a DLL and mscvt or whatnot - we have to do something custom :) |
IMHO the canonical python way to handle non-blocking io is with select: https://docs.python.org/3/library/select.html Like Radomir mentions though it quickly gets into platform specific optimizations for desktop though (poll, epoll, kpoll, etc.), but at the end of the day they're all the same idea of polling for availability of data from an arbitrary file descriptor with a select-like API. Adding a select API and ability to use it to poll for new chars from stdin, etc. would be handy, and it would be a natural progression to add polling for other descriptors like sockets (same way python likes to handle sockets on the desktop). With a light wrapper on top it could become a general purpose async IO engine too (i.e. your main loop is just a big poll of awaiting descriptors with select). |
It seems to me that being able to |
Another way to achieve this is to provide a second "raw" serial connection to the computer in addition to the input. This would allow for binary data transmission in addition to text. It could work just like a UART connection. We'd want dynamic USB descriptors for this (mentioned in #190) so that its only on as needed. |
good idea! that's probably the 'right' way to do it, but yeah would have to wait until you can select the USB interfaces cauz there'd be juggling between webusb, cdc, HID. |
I have a situation where I need to have a Raspberry Pi 3 interact with a board running Circuit Python. I need to use Firmata between the two for two-way communication. I would need to transfer both ASCII and binary data. Being able to do this is highly desirable for some applications, like robotics. |
PS I am geekguy on the Adafruit forums. |
member hybotics/geekguy mentioned the same use case that I have for which this feature is highly desirable. At my company we use Raspberry Pi boards to host our internal test and production machines and sometimes have Arduino boards hanging on them over USB. CircuitPython boards are preferred compared to Arduino because we can stay with one language and also the instrument is self-documenting because it hold's its own code and readme files. The hardware is relatively elegant because it only requires that a USB cable be run to each remote device or sensor. A problem is that we don't have a great way to send serial data instructions to the boards. as a work-around is there a way to query the input buffer so that I can know if sys.stdin.read(1) would pass through or block? |
Hello folks! I was directed to this thread by Scott (@tannewt ) over at the Adafruit forums, in reply to my query about "Arduino-like 'serial' with Circuit Python". For what I'm trying to achieve, being able to send and receive serial data in this way through the USB connection would be really great. Thanks all! -Casey |
Ditto to @protothesis comments above... and I was similarly directed by @tannewt from the Arduino Discord... My goal would be to have something similar to the Arduino CmdMessenger. Right now the best I can come up with is using two boards: 1 for communication via the Serial Console, and 1 for Device Control with a UART between them. If anybody has already been down that road, I'd love to hear about it... |
an intermediate solution for now is to use the hardware UART + a usb/uart cable. elegant? not as much but it does work now :) |
I created a workaround for this and am sharing it via this pull request Not sure it's the right solution, but it does solve our issues. |
Has anything changed with respect to the original request, i.e. a non-blocking input() or sys.stdin.read() function? Or is there another way for bi-directional communication via USB? |
@dhalbert yes, that is very useful. It returns a boolean, though, not the number of bytes. So a non-blocking read func has to look like
or is there a better way? |
Yes, that's the best way for now. Sorry, it would be nicer if |
Related to 2nd USB-serial channel wanted, Perhaps we could have static descriptor with shared Of course usb-serial and microphone cannot work but I was thinking - if in the OS the microphone driver is blacklisted, |
We want to provide run-time choice of the USB devices that CircuitPython presents. That is #1015. Blacklisting might work on Linux, but we want an easier mechanism and one that is OS-independent. |
The secondary serial channel idea mentioned in this thread now is covered in issue #3853. |
kk - we'd still need a way to read individual characters from the secondary USB-serial connection |
Yeah, Python is still bad at that. |
I think we should have the second CDC available as a UART compatible (aka stream). It'd be similar to how we have objects for MIDI stuff. |
yes! think about interfacing with processing, unity, cnc etc. just need bidirectional byte stream (e.g. like |
I'm using that method to interface with a desktop program on my DynOSSAT-EDU. That program opens the COM port and writes bytes to it, and the CircuitPython board should receive them and act in consequence in a non-blocking way. It mostly works, but I found a problem: the moment I get a Ctrl-C or Ctrl-D byte (0x03 or 0x04) in a frame and read it using sys.stdin.read(), the program closes throwing a KeyboardInterrupt. Just leaving this here as a warning for someone wanting to do this; if you only send ASCII-printable bytes you won't find any problem. |
yesss! oldest issue closed :D |
Since the usb_cdc module is on hold indefinitely, will this issue be reopened? I'm stuck on a nightly until, fingers crossed, this is available as an option in version 7. Thanks |
its not on hold indefinitely - we turned it off a few PR's ago because it was confusing some IDEs - you can build yourself and enable, or i think use nighties from over a week ago :) |
This Guide page describes how to configure a build to turn it on: https://learn.adafruit.com/building-circuitpython/customizing-usb-devices |
I've spent the evening playing with my Neo Trinkey and have had a not-fun time tracking down issues like the ones mentioned here. It's a cute little board, and I was doing some fun things like touch pads to simulate keys through the hid. Then I wanted to change colors by sending DC4F character command through the serial port. Using supervisor.runtime.serial_bytes_available was non-intuitive at best, but I found it. It would be nice if serial_bytes_available returned a count instead of a boolean (efficiency at higher baud rates or when chars are buffered), and I worry about the Ctrl-C and Ctrl-D issue mentioned with sys.stdin. I'm not crazy about rebuilding a custom circuit python if I don't need to. I'll be back. |
you can subscribe to #4689 - when that's merged in (no ETA!) it will allow turning on/off the second CDC channel |
Is or will usb_cdc be supported on nRF52840 devices (i.e. Particle Xenon)? |
yep! |
OK its odd but hear me out :)
python used to allow raw REPL keygrabs (e.g. 'press any key to continue') but deprecated it in 3 for just input() which is blocking and requires a return key
it would be useful for interactive-at-the repl scripts to have a non-input() way of getting raw characters. i mean, you can send individual chars with print("x", end='') so why not have the reverse :D
also, using input() wonks with the REPL history: if you use input() it puts that entry into the history which i think is a bit odd:

The text was updated successfully, but these errors were encountered: