elec350

Dependencies:   mbed

Fork of elec350 by Bob Merrison-Hort

button.cpp

Committer:
rmerrisonhort
Date:
2015-10-15
Revision:
4:549d1d8ca969
Parent:
3:998b7d011f2a
Child:
6:1eee8ba927fe

File content as of revision 4:549d1d8ca969:

#include "button.h"

// Button constructor.
Button::Button(string name)
{
    // Set the "pin" attribute to a DigitalIn object corresponding
    // to the pin for the selected button (only one button at the
    // moment.
    if (name == "user") {
        this->pin = new DigitalIn(PA_0);
    }
}

// isPressed method: Return true if the button is currently pressed
// and false otherwise.
bool Button::isPressed()
{
    // Get current state of the pin.
    int pinValue = this->pin->read();
    
    // Check the value returned from this->pin->read() and return true or false.
    if (pinValue == 1) {
        return true;
    } else {
        return false;
    }
}

float Button::waitWhileHeld()
{
    const float waitInterval = 0.1f;
    while(this->isPressed() == false) {
        wait(waitInterval);
    }
    
    float heldTime = 0.0f;
    while(this->isPressed() == true) {
        heldTime += waitInterval;
        wait(waitInterval);
    }
    
    return heldTime;
}