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

Provide an event based API for EV3 buttons #90

Closed
ddemidov opened this issue Jul 5, 2015 · 4 comments
Closed

Provide an event based API for EV3 buttons #90

ddemidov opened this issue Jul 5, 2015 · 4 comments

Comments

@ddemidov
Copy link
Member

ddemidov commented Jul 5, 2015

Similar to how it is implemented in the remote_control class.

@ddemidov
Copy link
Member Author

Done for C++ and exposed in Python. For now each button should be handled individually:

def say_hello(pressed): if pressed: print('hello')
button.enter.onclick( say_hello )
while True: button.enter.process()

What I don't like about this is the processing part. I think I would prefer a single process() function that would iterate over all buttons. What is stopping me from implementing this is mainly where to put it (as a static method in a button class, or as a standalone helper class?) and how to name it.

Of course, it may be easily done on the user side of the code as well.

ddemidov added a commit that referenced this issue Jul 17, 2015
This is a convenience method that simply calls process() for each of the
EV3 buttons. See #90.
@ddemidov
Copy link
Member Author

I've added a static button::process_all() method. It simply calls button::process() for each of the EV3 buttons.

@cho934
Copy link
Contributor

cho934 commented Mar 25, 2016

Done for C++ and exposed in Python. For now each button should be handled individually:

def say_hello(pressed): if pressed: print('hello')
button.enter.onclick( say_hello )
while True: button.enter.process()

I don't understand how to use this onclick and process function.
Could you create a small example using the onclick function in C++ ?

@ddemidov
Copy link
Member Author

Something like this should work:

// Set a functor (here created as a lambda function) as the button event handler:
ev3dev::button::enter.onclick = [](bool state) {
    std::cout << state ? "pressed" : "not pressed" << std::endl;
};

// Do event processing in a loop. Whenever the enter button changes state,
// the onclick functor is called with the appropriate argument:
while (!ev3dev::button::back.pressed())
  ev3dev::button::enter.process();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants