-
Notifications
You must be signed in to change notification settings - Fork 39
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
improve the button api #108
Comments
How about this? ev3_keys = [
'back' : ev3dev.button.back,
'left' : ev3dev.button.left,
'right' : ev3dev.button.right,
'up' : ev3dev.button.up,
'down' : ev3dev.button.down,
'enter' : ev3dev.button.enter
]
def key_pressed(k):
return ev3_keys[k].pressed()
def keys_pressed()
return [k for k in ev3_keys if ev3_keys[k].pressed()] |
+1 for
maybe instead of |
May be
Would not you miss an opportunity to do something like if 'left' in buttons.keys_pressed(): do_something_great() or even if set(['left', 'up']).issubset(buttons.keys_pressed()): do_something_even_greater() ? |
I don't think I'd miss them. For those I would probably rather write:
Given that there are just 6 keys, changes that two will be used for the same thing are probably quite low. We can also add both :) |
Well, you could do if set(['left', 'up']).issubset(buttons.keys_pressed()): go_north_west() :) |
Ok, I'll try to add this into python bindings. |
Should be fixed by ddemidov/ev3dev-lang-python@f535ed6 and ddemidov/ev3dev-lang-python@3e1a70a. |
Thanks! |
@ensonic / @ddemidov - I am about to merge in the helper functions for the
@ensonic - can you send along some sample code where you are using the
|
Probably not. The properties (
I think @ensonic and I agreed that
I am not @ensonic, but this looks like what you are looking for: https://mp-devel.iais.fraunhofer.de/code/projects/ORA/repos/robertalab/browse/EV3MenuEv3dev/roberta/ev3.py#187 |
If Looking at @ensonic's code, the important bits for the
And it's pretty clear that he's looking for an "any" operation with Boolean result. I'm going to add the helpers to the |
I agree that could be helpful. |
Just confirming, I was indeed looking for something that returns true if any key is pressed. That is useful when e.g. showing an error. |
That's |
I've read through the updates here but I'm unsure about how the current Button class works. What is the current notation for checking for a button press? I read the documentation on readthedocs but am not sure if that is updated. So if I want to check for whether the left button is pressed or not, should I write |
Currently you have to create an instance of the btn = ev3.Button()
# Is 'Left' button pressed?
print('yes' if btn.left else 'no')
# Check if any buttons are pressed:
print('yes' if btn.any() else 'no')
# Do something when state of 'Left' button changes:
def report(state):
print('pressed' if state else 'not pressed')
btn.on_left = report
while True: # This loop checks buttons state continuously, calls appropriate event handlers
btn.process() |
@ddemidov - Thank you. This is exactly what I was looking for. The button class makes much more sense now. |
I am missing two feature from the button api.
This is not very discoverable.
2.) how would I check for any key pressed?
If there would be an enum of available keys, I could check if any of them are pressed. We could also just have some convenience api for this.
The text was updated successfully, but these errors were encountered: