
elec350
Fork of elec350 by
button.cpp@4:549d1d8ca969, 2015-10-15 (annotated)
- Committer:
- rmerrisonhort
- Date:
- Thu Oct 15 16:34:35 2015 +0000
- Revision:
- 4:549d1d8ca969
- Parent:
- 3:998b7d011f2a
- Child:
- 6:1eee8ba927fe
.
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 | 4:549d1d8ca969 | 8 | // moment. |
rmerrisonhort | 3:998b7d011f2a | 9 | if (name == "user") { |
rmerrisonhort | 3:998b7d011f2a | 10 | this->pin = new DigitalIn(PA_0); |
rmerrisonhort | 3:998b7d011f2a | 11 | } |
rmerrisonhort | 0:753cf4c2738f | 12 | } |
rmerrisonhort | 0:753cf4c2738f | 13 | |
rmerrisonhort | 4:549d1d8ca969 | 14 | // isPressed method: Return true if the button is currently pressed |
rmerrisonhort | 4:549d1d8ca969 | 15 | // and false otherwise. |
rmerrisonhort | 0:753cf4c2738f | 16 | bool Button::isPressed() |
rmerrisonhort | 0:753cf4c2738f | 17 | { |
rmerrisonhort | 4:549d1d8ca969 | 18 | // Get current state of the pin. |
rmerrisonhort | 4:549d1d8ca969 | 19 | int pinValue = this->pin->read(); |
rmerrisonhort | 4:549d1d8ca969 | 20 | |
rmerrisonhort | 4:549d1d8ca969 | 21 | // Check the value returned from this->pin->read() and return true or false. |
rmerrisonhort | 4:549d1d8ca969 | 22 | if (pinValue == 1) { |
rmerrisonhort | 0:753cf4c2738f | 23 | return true; |
rmerrisonhort | 0:753cf4c2738f | 24 | } else { |
rmerrisonhort | 0:753cf4c2738f | 25 | return false; |
rmerrisonhort | 0:753cf4c2738f | 26 | } |
rmerrisonhort | 0:753cf4c2738f | 27 | } |
rmerrisonhort | 0:753cf4c2738f | 28 | |
rmerrisonhort | 0:753cf4c2738f | 29 | float Button::waitWhileHeld() |
rmerrisonhort | 0:753cf4c2738f | 30 | { |
rmerrisonhort | 0:753cf4c2738f | 31 | const float waitInterval = 0.1f; |
rmerrisonhort | 0:753cf4c2738f | 32 | while(this->isPressed() == false) { |
rmerrisonhort | 0:753cf4c2738f | 33 | wait(waitInterval); |
rmerrisonhort | 0:753cf4c2738f | 34 | } |
rmerrisonhort | 0:753cf4c2738f | 35 | |
rmerrisonhort | 0:753cf4c2738f | 36 | float heldTime = 0.0f; |
rmerrisonhort | 0:753cf4c2738f | 37 | while(this->isPressed() == true) { |
rmerrisonhort | 0:753cf4c2738f | 38 | heldTime += waitInterval; |
rmerrisonhort | 0:753cf4c2738f | 39 | wait(waitInterval); |
rmerrisonhort | 0:753cf4c2738f | 40 | } |
rmerrisonhort | 0:753cf4c2738f | 41 | |
rmerrisonhort | 0:753cf4c2738f | 42 | return heldTime; |
rmerrisonhort | 0:753cf4c2738f | 43 | } |