Skip to content

Commit

Permalink
Add static method button::process_all()
Browse files Browse the repository at this point in the history
This is a convenience method that simply calls process() for each of the
EV3 buttons. See #90.
  • Loading branch information
ddemidov committed Jul 17, 2015
1 parent 69f45d0 commit e3edfe5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
17 changes: 17 additions & 0 deletions cpp/ev3dev.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <fstream>
#include <list>
#include <map>
#include <array>
#include <algorithm>
#include <system_error>
#include <mutex>
Expand Down Expand Up @@ -916,6 +917,7 @@ bool button::process()
}

//-----------------------------------------------------------------------------

#ifndef NO_LINUX_HEADERS
button button::back (KEY_BACKSPACE);
button button::left (KEY_LEFT);
Expand All @@ -924,6 +926,21 @@ button button::up (KEY_UP);
button button::down (KEY_DOWN);
button button::enter(KEY_ENTER);
#endif

//-----------------------------------------------------------------------------

bool button::process_all() {
std::array<bool, 6> changed = {
back. process(),
left. process(),
right.process(),
up. process(),
down. process(),
enter.process()
};
return std::any_of(changed.begin(), changed.end(), [](bool c){ return c; });
}

//-----------------------------------------------------------------------------

void sound::beep()
Expand Down
6 changes: 5 additions & 1 deletion cpp/ev3dev.h
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,7 @@ class button

// Check if the button state has changed,
// call onclick function in case it has.
// Returns true if the state has changed, false otherwise.
// Returns true if the state has changed since the last call.
bool process();

static button back;
Expand All @@ -1318,6 +1318,10 @@ class button
static button down;
static button enter;

// Call process() for each of the EV3 buttons.
// Returns true if any of the states have changed since the last call.
static bool process_all();

private:
int _bit;
bool _state = false;
Expand Down

0 comments on commit e3edfe5

Please sign in to comment.