ev3dev / ev3dev-lang

(deprecated) language bindings for ev3dev sensors, motors, LEDs, etc.
GNU General Public License v2.0
56 stars 39 forks source link

Provide an event based API for EV3 buttons #90

Closed ddemidov closed 9 years ago

ddemidov commented 9 years ago

Similar to how it is implemented in the remote_control class.

ddemidov commented 9 years ago

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 commented 9 years ago

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

cho934 commented 8 years ago

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 commented 8 years ago

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();