elec350

Dependencies:   mbed

Fork of elec350 by Bob Merrison-Hort

Committer:
rmerrisonhort
Date:
Thu Oct 15 16:55:27 2015 +0000
Revision:
6:1eee8ba927fe
Parent:
4:549d1d8ca969
Child:
7:aa63d1e53be6
.

Who changed what in which revision?

UserRevisionLine numberNew 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 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 0:753cf4c2738f 30 float Button::waitWhileHeld()
rmerrisonhort 0:753cf4c2738f 31 {
rmerrisonhort 0:753cf4c2738f 32 const float waitInterval = 0.1f;
rmerrisonhort 0:753cf4c2738f 33 while(this->isPressed() == false) {
rmerrisonhort 0:753cf4c2738f 34 wait(waitInterval);
rmerrisonhort 0:753cf4c2738f 35 }
rmerrisonhort 0:753cf4c2738f 36
rmerrisonhort 0:753cf4c2738f 37 float heldTime = 0.0f;
rmerrisonhort 0:753cf4c2738f 38 while(this->isPressed() == true) {
rmerrisonhort 0:753cf4c2738f 39 heldTime += waitInterval;
rmerrisonhort 0:753cf4c2738f 40 wait(waitInterval);
rmerrisonhort 0:753cf4c2738f 41 }
rmerrisonhort 0:753cf4c2738f 42
rmerrisonhort 0:753cf4c2738f 43 return heldTime;
rmerrisonhort 0:753cf4c2738f 44 }