elec350

Dependencies:   mbed

Fork of elec350 by Bob Merrison-Hort

Committer:
rmerrisonhort
Date:
Wed Oct 14 21:09:51 2015 +0000
Revision:
3:998b7d011f2a
Parent:
0:753cf4c2738f
Child:
4:549d1d8ca969
Updated button class to take button name in ctor.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rmerrisonhort 0:753cf4c2738f 1 #include "button.h"
rmerrisonhort 0:753cf4c2738f 2
rmerrisonhort 3:998b7d011f2a 3 Button::Button(string name)
rmerrisonhort 0:753cf4c2738f 4 {
rmerrisonhort 3:998b7d011f2a 5 if (name == "user") {
rmerrisonhort 3:998b7d011f2a 6 this->pin = new DigitalIn(PA_0);
rmerrisonhort 3:998b7d011f2a 7 }
rmerrisonhort 0:753cf4c2738f 8 }
rmerrisonhort 0:753cf4c2738f 9
rmerrisonhort 0:753cf4c2738f 10 bool Button::isPressed()
rmerrisonhort 0:753cf4c2738f 11 {
rmerrisonhort 0:753cf4c2738f 12 if (this->pin->read() == 1) {
rmerrisonhort 0:753cf4c2738f 13 return true;
rmerrisonhort 0:753cf4c2738f 14 } else {
rmerrisonhort 0:753cf4c2738f 15 return false;
rmerrisonhort 0:753cf4c2738f 16 }
rmerrisonhort 0:753cf4c2738f 17 }
rmerrisonhort 0:753cf4c2738f 18
rmerrisonhort 0:753cf4c2738f 19 float Button::waitWhileHeld()
rmerrisonhort 0:753cf4c2738f 20 {
rmerrisonhort 0:753cf4c2738f 21 const float waitInterval = 0.1f;
rmerrisonhort 0:753cf4c2738f 22 while(this->isPressed() == false) {
rmerrisonhort 0:753cf4c2738f 23 wait(waitInterval);
rmerrisonhort 0:753cf4c2738f 24 }
rmerrisonhort 0:753cf4c2738f 25
rmerrisonhort 0:753cf4c2738f 26 float heldTime = 0.0f;
rmerrisonhort 0:753cf4c2738f 27 while(this->isPressed() == true) {
rmerrisonhort 0:753cf4c2738f 28 heldTime += waitInterval;
rmerrisonhort 0:753cf4c2738f 29 wait(waitInterval);
rmerrisonhort 0:753cf4c2738f 30 }
rmerrisonhort 0:753cf4c2738f 31
rmerrisonhort 0:753cf4c2738f 32 return heldTime;
rmerrisonhort 0:753cf4c2738f 33 }