please anyone can help with this?
I'm writing a non blocking timed input like this sample code:
The first input (start) gets out ok but the second passes through the timed_input without allowing to insert anything from the keyboard.
Code: Select all
import sys, select, utime def timed_input(): i,_, _ = select.select( [sys.stdin], [sys.stdout], [sys.stderr], 5 ) if (i): answer = sys.stdin.readline() else: answer = "*" for x in answer: print(x, ord(x)) return answer.replace('\n', '') print("Start from:") start= timed_input() print("End to:") end = timed_input() print(start, len(start), end, len(end))
I printed the details of the output from the second request because I suspected there was something left in the stdin buffer which caused the exit from the sys.stdin.readline() and indeed there's a LF (chr(10)) left which i guess is what causes the exit from the input.
I found there's sys.stdin.flush() to flush the buffer but it doesn't work in microPython (AttributeError: 'FileIO' object has no attribute 'flush').
This is confirmed by the fact that if I don't input anything, letting the timeout exit, the second call doesn't exit but waits for an input.
How can I flush the buffer or use a workaround to get a working sequence of timed inputs?
Thanks for any help.