From e3edfe5cce159ad9ff3c8515d6dae170e8b4aa8d Mon Sep 17 00:00:00 2001 From: Denis Demidov Date: Fri, 17 Jul 2015 22:23:32 +0300 Subject: [PATCH] Add static method button::process_all() This is a convenience method that simply calls process() for each of the EV3 buttons. See #90. --- cpp/ev3dev.cpp | 17 +++++++++++++++++ cpp/ev3dev.h | 6 +++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/cpp/ev3dev.cpp b/cpp/ev3dev.cpp index 05ef629..5186909 100644 --- a/cpp/ev3dev.cpp +++ b/cpp/ev3dev.cpp @@ -35,6 +35,7 @@ #include #include #include +#include #include #include #include @@ -916,6 +917,7 @@ bool button::process() } //----------------------------------------------------------------------------- + #ifndef NO_LINUX_HEADERS button button::back (KEY_BACKSPACE); button button::left (KEY_LEFT); @@ -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 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() diff --git a/cpp/ev3dev.h b/cpp/ev3dev.h index 7df47d9..8d2750f 100644 --- a/cpp/ev3dev.h +++ b/cpp/ev3dev.h @@ -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; @@ -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;