class of Buttons -isPressed -wasPressed -notPressed -LongPress (Timer)
button.cpp
- Committer:
- x1dmoesc
- Date:
- 2018-11-30
- Revision:
- 0:8625b4741933
- Child:
- 1:e9d0d9b29acc
File content as of revision 0:8625b4741933:
#include "button.h" #include "mbed.h" /******************************************************************************/ // constructor /******************************************************************************/ button::button(PinName pBtn, PinMode pBtnMode) { pin = new DigitalIn(pBtn, pBtnMode); init(); } /******************************************************************************/ // initialisation /******************************************************************************/ void button::init() { bIsPressed = false; bWasPressed = false; bNewState = false; bOldState = false; bBtnNewAction = false; iNumCycle = 0; 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 /******************************************************************************/ void button::polling() { bOldState = bNewState; bNewState = (bool)pin->read(); bWasPressed = false; 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; if(bNewState == PRESSED) { bIsPressed = true; } else if(bNewState == NOT_PRESSED) { if(bIsPressed == true) bWasPressed = true; bIsPressed = false; } } } else { iNumCycle = 0; } } return; } /******************************************************************************/ // /******************************************************************************/ bool button::isPressed() { if(bIsPressed) return true; else return false; } /******************************************************************************/ // /******************************************************************************/ bool button::wasPressed() { if(bWasPressed) return true; else return false; }