Skip to content
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

Alternate to input and usys.stdin on Move hub #1102

Closed
dlech opened this issue Jun 6, 2023 · 3 comments · Fixed by pybricks/pybricks-micropython#177
Closed

Alternate to input and usys.stdin on Move hub #1102

dlech opened this issue Jun 6, 2023 · 3 comments · Fixed by pybricks/pybricks-micropython#177
Labels
enhancement New feature or request hub: movehub Issues related to the LEGO BOOST Move hub software: pybricks-micropython Issues with Pybricks MicroPython firmware (or EV3 runtime) topic: multitasking Issues releated to parallel threads, tasks, coroutines, etc.

Comments

@dlech
Copy link
Member

dlech commented Jun 6, 2023

Is your feature request related to a problem? Please describe.
This has come up several times recently (#1099, #1085, #529). There is no easy way to get input from a computer to a BOOST Move hub.

Describe the solution you'd like
A function similar to usys.stdin.read() that plays nice with the new async run loop with minimal implementation size.

Describe alternatives you've considered

  • The new broadcast/observe functionality is not a complete solution since it can't be used on macOS and requires experimental BlueZ features to be manually enabled on Linux (or elevated privileges to directly poke HCI).
  • We have also proposed an API for wrapping the Nordic UART service, but the Move hub currently doesn't have anywhere near enough free flash memory to implement this.
  • We could implement a very minimal mp_hal_readline() (e.g. backspace wouldn't work) and use the existing builtin input() function. However, since this prints in addition to getting input, it would not be ideal for use in a program with an async run loop where we need it to be awaitable rather than blocking.
@dlech dlech added enhancement New feature or request hub: movehub Issues related to the LEGO BOOST Move hub topic: multitasking Issues releated to parallel threads, tasks, coroutines, etc. software: pybricks-micropython Issues with Pybricks MicroPython firmware (or EV3 runtime) labels Jun 6, 2023
@laurensvalk
Copy link
Member

laurensvalk commented Jun 6, 2023

We've previously had a non-blocking getchar that seemed to work well for most people.

See #154

@dlech
Copy link
Member Author

dlech commented Jun 6, 2023

👍 That is probably as simple as it gets.

@dlech
Copy link
Member Author

dlech commented Jun 6, 2023

The only downside I see is that to be non-blocking it could not support multi-byte UTF-8 characters (and would probably have to return bytes or int instead of str to avoid exception in case we ever enable MICROPY_PY_BUILTINS_STR_UNICODE). That is, unless we implemented a way to peek at the stdin buffer without removing bytes.

dlech added a commit to dlech/pybricks-micropython that referenced this issue Jun 6, 2023
This adds a new non-blocking function to read one byte from stdin. This
is needed since there is no other way to read from stdin on the Move
hub. It is also useful on other hubs as it eliminates the need to
use poll() to avoid blocking reads.

Fixes: pybricks/support#1102
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request hub: movehub Issues related to the LEGO BOOST Move hub software: pybricks-micropython Issues with Pybricks MicroPython firmware (or EV3 runtime) topic: multitasking Issues releated to parallel threads, tasks, coroutines, etc.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants