pybricks / support

Pybricks support and general discussion
MIT License
107 stars 6 forks source link

[Feature] Input option #88

Closed JorgePe closed 4 years ago

JorgePe commented 4 years ago

Thanks for 'input' - it works great and I can now use it from ev3dev to control hubs. I know there are better communication options on your roadmap but if there is some time slot I would love a few extra options:

example: i = input("Prompt", echo = False, end='\r\n')

While echo is useful for debugging, disabling it would reduce a bit the number of messages. And choosing the proper termination characters would allow apps like Nordic nRF UART or Nordic nRF Toolbox to work out of the box with Pybricks (don't know what is expected by the first but the second only allows those 3 options and none of them seem to work - not even CR that works with my python code, I'm using 0x0D)

laurensvalk commented 4 years ago

:+1: for not echo-ing at all. If there isn't an option already I think we can just live without it.

input is a standard MicroPython function. As an exercise, do you want to have a look at its code to see what it does and how it might be altered/configured? :smile:

JorgePe commented 4 years ago

OK. I thought you had implemented it from scratch.

laurensvalk commented 4 years ago

In general, everything you import from the pybricks package is part of this project, and we try to use other existing MicroPython functionality as-is without modification.

laurensvalk commented 4 years ago

@JorgePe since you closed it -- any insights you want to share?

JorgePe commented 4 years ago

not quite... I didn't find the code itself (supposed to be in builtins module.. where is that?) but documentation only states the prompt option. I did find input for the unix port but it is a different thing.

laurensvalk commented 4 years ago

There are quite a few ways to search through MicroPython and it can arguably be a bit daunting at first.

Here's a trick I use sometimes if I can't guess where it might be:

All MicroPython string literals (like function names) are saved only once to save space, prefixed by MP_QSTR_. So you can find where input is defined by searching for MP_QSTR_input. In this particular case, you can see it is guarded by MICROPY_PY_BUILTINS_INPUT (just like many optional MicroPython features), which helps you search further. This leads to mp_builtin_input in micropython/py/modbuiltins.c.

JorgePe commented 4 years ago

so it call readline and if we want to change the behavior we can implement our own mp_hal_readline... or perhaps just mp_hal_stdin_rx_chr and i'm lost again. Sorry, I am not a programmer at all :)

laurensvalk commented 4 years ago

so it call readline and if we want to change the behavior we can implement our own mp_hal_readline...

Exactly - and that's a good find, and in principle that's even possible within the scope of the project since it does not require modifying MicroPython itself.

What is the current behavior for input in pybricks-micropython in ev3dev? You can run pybricks-repl as a shortcut to get the REPL. (Honest question - I've never tried myself.)

Sorry, I am not a programmer at all :)

I wasn't a programmer when I started this either, I'm probably still not :smile:

JorgePe commented 4 years ago

What is the current behavior for input in pybricks-micropython in ev3dev? You can run pybricks-repl as a shortcut to get the REPL. (Honest question - I've never tried myself.)

looks the same - it blocks while waiting for ENTER, accepts just one argument (prompt) or none.

laurensvalk commented 4 years ago

I meant, does it echo too?

JorgePe commented 4 years ago

gosh :O no it doesn't echo (sorry, long day)