
elec350
Fork of elec350 by
button.cpp@0:753cf4c2738f, 2015-10-13 (annotated)
- Committer:
- rmerrisonhort
- Date:
- Tue Oct 13 01:10:14 2015 +0000
- Revision:
- 0:753cf4c2738f
- Child:
- 3:998b7d011f2a
Created ELEC350 library.
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 | 0:753cf4c2738f | 3 | Button::Button() |
rmerrisonhort | 0:753cf4c2738f | 4 | { |
rmerrisonhort | 0:753cf4c2738f | 5 | this->pin = new DigitalIn(PA_0); |
rmerrisonhort | 0:753cf4c2738f | 6 | } |
rmerrisonhort | 0:753cf4c2738f | 7 | |
rmerrisonhort | 0:753cf4c2738f | 8 | bool Button::isPressed() |
rmerrisonhort | 0:753cf4c2738f | 9 | { |
rmerrisonhort | 0:753cf4c2738f | 10 | if (this->pin->read() == 1) { |
rmerrisonhort | 0:753cf4c2738f | 11 | return true; |
rmerrisonhort | 0:753cf4c2738f | 12 | } else { |
rmerrisonhort | 0:753cf4c2738f | 13 | return false; |
rmerrisonhort | 0:753cf4c2738f | 14 | } |
rmerrisonhort | 0:753cf4c2738f | 15 | } |
rmerrisonhort | 0:753cf4c2738f | 16 | |
rmerrisonhort | 0:753cf4c2738f | 17 | float Button::waitWhileHeld() |
rmerrisonhort | 0:753cf4c2738f | 18 | { |
rmerrisonhort | 0:753cf4c2738f | 19 | const float waitInterval = 0.1f; |
rmerrisonhort | 0:753cf4c2738f | 20 | while(this->isPressed() == false) { |
rmerrisonhort | 0:753cf4c2738f | 21 | wait(waitInterval); |
rmerrisonhort | 0:753cf4c2738f | 22 | } |
rmerrisonhort | 0:753cf4c2738f | 23 | |
rmerrisonhort | 0:753cf4c2738f | 24 | float heldTime = 0.0f; |
rmerrisonhort | 0:753cf4c2738f | 25 | while(this->isPressed() == true) { |
rmerrisonhort | 0:753cf4c2738f | 26 | heldTime += waitInterval; |
rmerrisonhort | 0:753cf4c2738f | 27 | wait(waitInterval); |
rmerrisonhort | 0:753cf4c2738f | 28 | } |
rmerrisonhort | 0:753cf4c2738f | 29 | |
rmerrisonhort | 0:753cf4c2738f | 30 | return heldTime; |
rmerrisonhort | 0:753cf4c2738f | 31 | } |