class of Buttons -isPressed -wasPressed -notPressed -LongPress (Timer)
button.cpp
- Committer:
- x1dmoesc
- Date:
- 2019-01-09
- Revision:
- 3:68f58a34e627
- Parent:
- 2:cf58f3863c07
File content as of revision 3:68f58a34e627:
#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(); }