class of Buttons -isPressed -wasPressed -notPressed -LongPress (Timer)

Button.cpp

Committer:
x1dmoesc
Date:
2019-01-15
Revision:
4:3b94400f2894
Parent:
button.cpp@ 3:68f58a34e627
Child:
6:38624a50a610

File content as of revision 4:3b94400f2894:

#include "Button.h"
#include "mbed.h"


/******************************************************************************/
// constructor
/******************************************************************************/
Button::Button(PinName pBtn, PinMode  pBtnMode, float time)
{
    fTime = time;
    pin = new DigitalIn(pBtn, pBtnMode);

    if(fTime > 0)
        timer = new Timeout;

    init();

}


/******************************************************************************/
// initialisation
/******************************************************************************/
void Button::init()
{
    bIsPressed  = false;
    bLongPress  = false;
    bWasPressed = false;
    bNewState   = false;
    bOldState   = false;

    bBtnNewAction = false;

    iNumCycle = 0;
    if(fTime > 0)
        timer->detach();


    return;
}


/******************************************************************************/
// query the Button with debouncing
//      - bNewState:        current state of button
//      - bOldState:        previous state of button
//      - bBtnNewAction:    flag, if an action is occur
//      - iNumCycle:        cycle counter of same state
//      - NUNCYCLES:        max. value of cycle counter
//      - *iNumBtn:         pointer of variable, which counts pressed buttons
/******************************************************************************/
void Button::polling(uint8_t *iNumBtn)
{
    bOldState   = bNewState;                                                    // bNewState gets the becomes the bOldstate
    bNewState   = (bool)pin->read();                                            // update bNewState
    bWasPressed = false;                                                        // reset the flag was pressed

    
    if(!bBtnNewAction and (bOldState != bNewState))                             // if there is a action (changing signal) on the button (btn), then rise the flag bBtnNewAction
        bBtnNewAction = true;
    


    if(bBtnNewAction) {                                                         // if flag ist true (new action on the button)

        if(bOldState == bNewState) {                                            // if current state equals previous state
            if(iNumCycle <= NUMCYCLE) {                                         // count the number of cycle unless the max. value is reached
                iNumCycle++;

            } else if(iNumCycle > NUMCYCLE) {                                   // else if the max. value is reached
                iNumCycle = 0;                                                  // reset the counter, there is no action any more expected
                bBtnNewAction = false;                                          // there is nomore new action.

                if(bNewState == PRESSED) {                                      // btn is pressed, then
                    bIsPressed = true;                                          // set this flag true
                    
                    if(iNumBtn != NULL)
                        (*iNumBtn)++;
                    
                    if(fTime > 0)                                                // if btn has a timer, 
                        timer->attach(this, &Button::setLongPressFlag, fTime);
                        

                } else if(bNewState == NOT_PRESSED) {                           // if btn is not pressed, then
                    if(bIsPressed == true)                                      // if btn was pressed, then
                        bWasPressed = true;                                     // set this flag true
                    
                    bIsPressed = false;                                         // set this flag false
                    
                    if(fTime > 0)                                               // if btn has a timer, 
                        timer->detach();
                        bLongPress = false;
                        
                    if((iNumBtn != NULL && *iNumBtn) > 0)
                        (*iNumBtn)--;
                }

            }
        } else {
            iNumCycle = 0;
        }
    }

    return;
}



/******************************************************************************/
//
/******************************************************************************/
bool Button::isPressed()
{
    if(bIsPressed)  return true;
    else            return false;
}


/******************************************************************************/
//
/******************************************************************************/
bool Button::isLongPress()
{
    if(bLongPress)  return true;
    else            return false;

}


/******************************************************************************/
//
/******************************************************************************/
bool Button::wasPressed()
{
    if(bWasPressed) return true;
    else            return false;

}


/******************************************************************************/
//
/******************************************************************************/
void Button::setLongPressFlag(){
    bLongPress = true;
    timer->detach();
}