
elec350
Fork of elec350 by
button.cpp@16:721e41936a07, 2015-11-19 (annotated)
- Committer:
- rmerrisonhort
- Date:
- Thu Nov 19 10:06:45 2015 +0000
- Revision:
- 16:721e41936a07
- Parent:
- 8:a82ea42026db
Added InternalTemperature class.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rmerrisonhort | 0:753cf4c2738f | 1 | #include "button.h" |
rmerrisonhort | 0:753cf4c2738f | 2 | |
rmerrisonhort | 4:549d1d8ca969 | 3 | // Button constructor. |
rmerrisonhort | 3:998b7d011f2a | 4 | Button::Button(string name) |
rmerrisonhort | 0:753cf4c2738f | 5 | { |
rmerrisonhort | 4:549d1d8ca969 | 6 | // Set the "pin" attribute to a DigitalIn object corresponding |
rmerrisonhort | 4:549d1d8ca969 | 7 | // to the pin for the selected button (only one button at the |
rmerrisonhort | 7:aa63d1e53be6 | 8 | // moment. |
rmerrisonhort | 3:998b7d011f2a | 9 | if (name == "user") { |
rmerrisonhort | 6:1eee8ba927fe | 10 | |
rmerrisonhort | 3:998b7d011f2a | 11 | this->pin = new DigitalIn(PA_0); |
rmerrisonhort | 3:998b7d011f2a | 12 | } |
rmerrisonhort | 0:753cf4c2738f | 13 | } |
rmerrisonhort | 0:753cf4c2738f | 14 | |
rmerrisonhort | 4:549d1d8ca969 | 15 | // isPressed method: Return true if the button is currently pressed |
rmerrisonhort | 4:549d1d8ca969 | 16 | // and false otherwise. |
rmerrisonhort | 0:753cf4c2738f | 17 | bool Button::isPressed() |
rmerrisonhort | 0:753cf4c2738f | 18 | { |
rmerrisonhort | 4:549d1d8ca969 | 19 | // Get current state of the pin. |
rmerrisonhort | 4:549d1d8ca969 | 20 | int pinValue = this->pin->read(); |
rmerrisonhort | 4:549d1d8ca969 | 21 | |
rmerrisonhort | 4:549d1d8ca969 | 22 | // Check the value returned from this->pin->read() and return true or false. |
rmerrisonhort | 4:549d1d8ca969 | 23 | if (pinValue == 1) { |
rmerrisonhort | 0:753cf4c2738f | 24 | return true; |
rmerrisonhort | 0:753cf4c2738f | 25 | } else { |
rmerrisonhort | 0:753cf4c2738f | 26 | return false; |
rmerrisonhort | 0:753cf4c2738f | 27 | } |
rmerrisonhort | 0:753cf4c2738f | 28 | } |
rmerrisonhort | 0:753cf4c2738f | 29 | |
rmerrisonhort | 8:a82ea42026db | 30 | |
rmerrisonhort | 8:a82ea42026db | 31 | float Button::getPulse(float timeout) |
rmerrisonhort | 0:753cf4c2738f | 32 | { |
rmerrisonhort | 8:a82ea42026db | 33 | // Create & start a timer. |
rmerrisonhort | 7:aa63d1e53be6 | 34 | Timer timer; |
rmerrisonhort | 7:aa63d1e53be6 | 35 | timer.start(); |
rmerrisonhort | 7:aa63d1e53be6 | 36 | |
rmerrisonhort | 8:a82ea42026db | 37 | // Wait for the button to be pressed. |
rmerrisonhort | 8:a82ea42026db | 38 | while(this->isPressed() == false) { |
rmerrisonhort | 8:a82ea42026db | 39 | // If a timeout was specified AND we've waited longer than |
rmerrisonhort | 8:a82ea42026db | 40 | // the specified value, return -1. |
rmerrisonhort | 8:a82ea42026db | 41 | if (timeout != -1.0f && timer.read() > timeout) { |
rmerrisonhort | 8:a82ea42026db | 42 | return -1.0f; |
rmerrisonhort | 8:a82ea42026db | 43 | } |
rmerrisonhort | 8:a82ea42026db | 44 | } |
rmerrisonhort | 8:a82ea42026db | 45 | |
rmerrisonhort | 8:a82ea42026db | 46 | // Reset timer to zero |
rmerrisonhort | 8:a82ea42026db | 47 | timer.reset(); |
rmerrisonhort | 8:a82ea42026db | 48 | |
rmerrisonhort | 7:aa63d1e53be6 | 49 | // Wait for the button to be released. |
rmerrisonhort | 0:753cf4c2738f | 50 | while(this->isPressed() == true) { |
rmerrisonhort | 7:aa63d1e53be6 | 51 | wait(0.01f); |
rmerrisonhort | 0:753cf4c2738f | 52 | } |
rmerrisonhort | 0:753cf4c2738f | 53 | |
rmerrisonhort | 7:aa63d1e53be6 | 54 | // Stop the timer and return number of seconds elapsed. |
rmerrisonhort | 7:aa63d1e53be6 | 55 | timer.stop(); |
rmerrisonhort | 7:aa63d1e53be6 | 56 | return timer.read(); |
rmerrisonhort | 0:753cf4c2738f | 57 | } |