
elec350
Fork of elec350 by
Diff: button.cpp
- Revision:
- 8:a82ea42026db
- Parent:
- 7:aa63d1e53be6
--- a/button.cpp Tue Oct 20 14:06:30 2015 +0000 +++ b/button.cpp Tue Oct 20 15:01:59 2015 +0000 @@ -27,17 +27,25 @@ } } -float Button::getPulse() + +float Button::getPulse(float timeout) { - // Wait for the button to be pressed. - while(this->isPressed() == false) { - // Do nothing. - } - - // Start a timer. + // Create & start a timer. Timer timer; timer.start(); + // Wait for the button to be pressed. + while(this->isPressed() == false) { + // If a timeout was specified AND we've waited longer than + // the specified value, return -1. + if (timeout != -1.0f && timer.read() > timeout) { + return -1.0f; + } + } + + // Reset timer to zero + timer.reset(); + // Wait for the button to be released. while(this->isPressed() == true) { wait(0.01f);