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

Using the buttons #233

Closed
ZPhilo opened this issue Jan 26, 2021 · 14 comments
Closed

Using the buttons #233

ZPhilo opened this issue Jan 26, 2021 · 14 comments
Labels
documentation Improvements or additions to documentation platform: Powered Up Issues related to LEGO Powered Up

Comments

@ZPhilo
Copy link

ZPhilo commented Jan 26, 2021

Describe the issue
On prime/inventor hub, Button.CENTER is listed, but pressing it aborts the program

Suggested improvement
Link to a program snippet that allows usage of Button.CENTER in custom user interface.

@ZPhilo ZPhilo added the triage Issues that have not been triaged yet label Jan 26, 2021
@laurensvalk laurensvalk added the hub: primehub/inventorhub Issues related to the LEGO SPIKE Prime hub and LEGO MINDSTORMS Robot Invetor hub label Jan 26, 2021
@dlech dlech added documentation Improvements or additions to documentation platform: Powered Up Issues related to LEGO Powered Up and removed hub: primehub/inventorhub Issues related to the LEGO SPIKE Prime hub and LEGO MINDSTORMS Robot Invetor hub triage Issues that have not been triaged yet labels Jan 26, 2021
@dlech
Copy link
Member

dlech commented Jan 26, 2021

On Prime/Inventor hubs, the center button is treated the same as the green button on other hubs. So if you really want to use it, you can catch the KeyboardInterrupt to use the button for input, but you have to be careful to make sure there is still a way to stop your program.

We can make an example, but we don't really want to widely encourage using it.

@ZPhilo
Copy link
Author

ZPhilo commented Jan 26, 2021

Makes sense... still think that something needs to be done to clarify this in Button documentation.

@laurensvalk
Copy link
Member

We could enable something like this. What do you think @ZPhilo?

hub = PrimeHub()

hub.buttons.set_keyboard_interrupt(False)

# Now you can read the Button.CENTER too like the others!

# But of course now you have to take care of stopping yourself, or use a long press to shut down entirely.

hub.buttons.set_keyboard_interrupt(True)

# Now it works as usual again.

Until now, we had something more complicated in mind (e.g. a double click) that stopped us from doing it so far, but this would be quite simple.

For now, this would mean that the stop button in Pybricks Code would not work either, but we're fixing that in other ways.

@dlech
Copy link
Member

dlech commented Jan 26, 2021

On the one hand, I would rather not add anything to keep things super simple. On the other hand, it would be cool if it took a button (or None) as the argument, then you could use the Bluetooth button as a stop button instead if you wanted to.

@ZPhilo
Copy link
Author

ZPhilo commented Jan 26, 2021

The simple scheme as proposed by Laurens seems fine to me. I was indeed a bit worried by the potential problem of lack of stop, but if a long press allows to take back control anyway, it's fine!

@laurensvalk
Copy link
Member

We could put this functionality in ‘’pybricks.tools.enable_keyboard_interrupt()’’ to keep the button interface simple.

Or better yet, check which micropython module already has this function and enable that.

@laurensvalk
Copy link
Member

Or better yet, check which micropython module already has this function and enable that.

Guess what, it is already enabled 😄

from pybricks.tools import wait

from micropython import kbd_intr

kbd_intr(-1)

print("Can't cancel program in the next 5 seconds!")
wait(5000)

print("Now you can stop again")
kbd_intr(3)
wait(5000)

So we just need to make sure we return Button.CENTER as well.

@laurensvalk
Copy link
Member

laurensvalk commented Jan 26, 2021

@ZPhilo: I know you like the online GitHub interface, so if you want to, you can add &pb_Button_CENTER_obj here.

Screenshot from 2021-01-26 21-14-27

And then you can click propose changes (this is just an example screenshot)

image

@laurensvalk
Copy link
Member

@JanderII is probably interested in this as well. Would this suit your application?

@ZPhilo
Copy link
Author

ZPhilo commented Jan 27, 2021

@ZPhilo: I know you like the online GitHub interface, so if you want to, you can add &pb_Button_CENTER_obj here.

Done!
BTW, I just noticed an error in buttons program snippet: it uses Button.BT instead of Button.BLUETOOTH

@laurensvalk
Copy link
Member

Thank you! I think now you just need to go to your branch and click on the pull request button.

@laurensvalk
Copy link
Member

BTW, I just noticed an error in buttons program snippet: it uses Button.BT instead of Button.BLUETOOTH

You're right, thanks! It would be great if you want to to change that also!

@ZPhilo
Copy link
Author

ZPhilo commented Jan 27, 2021

Done for both (hopefully right ;) )

@laurensvalk
Copy link
Member

This has been added so we can close it. Thanks @ZPhilo!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation platform: Powered Up Issues related to LEGO Powered Up
Projects
None yet
Development

No branches or pull requests

3 participants