elec350

Dependencies:   mbed

Fork of elec350 by Bob Merrison-Hort

Committer:
rmerrisonhort
Date:
Tue Oct 20 14:06:30 2015 +0000
Revision:
7:aa63d1e53be6
Parent:
6:1eee8ba927fe
Child:
8:a82ea42026db
Changed Led methods to On and Off.

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 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 7:aa63d1e53be6 30 float Button::getPulse()
rmerrisonhort 0:753cf4c2738f 31 {
rmerrisonhort 7:aa63d1e53be6 32 // Wait for the button to be pressed.
rmerrisonhort 0:753cf4c2738f 33 while(this->isPressed() == false) {
rmerrisonhort 7:aa63d1e53be6 34 // Do nothing.
rmerrisonhort 0:753cf4c2738f 35 }
rmerrisonhort 0:753cf4c2738f 36
rmerrisonhort 7:aa63d1e53be6 37 // Start a timer.
rmerrisonhort 7:aa63d1e53be6 38 Timer timer;
rmerrisonhort 7:aa63d1e53be6 39 timer.start();
rmerrisonhort 7:aa63d1e53be6 40
rmerrisonhort 7:aa63d1e53be6 41 // Wait for the button to be released.
rmerrisonhort 0:753cf4c2738f 42 while(this->isPressed() == true) {
rmerrisonhort 7:aa63d1e53be6 43 wait(0.01f);
rmerrisonhort 0:753cf4c2738f 44 }
rmerrisonhort 0:753cf4c2738f 45
rmerrisonhort 7:aa63d1e53be6 46 // Stop the timer and return number of seconds elapsed.
rmerrisonhort 7:aa63d1e53be6 47 timer.stop();
rmerrisonhort 7:aa63d1e53be6 48 return timer.read();
rmerrisonhort 0:753cf4c2738f 49 }