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

Committer:
x1dmoesc
Date:
Wed Jan 09 16:02:43 2019 +0000
Revision:
3:68f58a34e627
Parent:
2:cf58f3863c07
Zaehler von int auf uint8_t geaendert

Who changed what in which revision?

UserRevisionLine numberNew contents of line
x1dmoesc 0:8625b4741933 1 #include "button.h"
x1dmoesc 0:8625b4741933 2 #include "mbed.h"
x1dmoesc 0:8625b4741933 3
x1dmoesc 0:8625b4741933 4
x1dmoesc 0:8625b4741933 5 /******************************************************************************/
x1dmoesc 0:8625b4741933 6 // constructor
x1dmoesc 0:8625b4741933 7 /******************************************************************************/
x1dmoesc 1:e9d0d9b29acc 8 button::button(PinName pBtn, PinMode pBtnMode, float time)
x1dmoesc 0:8625b4741933 9 {
x1dmoesc 1:e9d0d9b29acc 10 fTime = time;
x1dmoesc 0:8625b4741933 11 pin = new DigitalIn(pBtn, pBtnMode);
x1dmoesc 1:e9d0d9b29acc 12
x1dmoesc 1:e9d0d9b29acc 13 if(fTime > 0)
x1dmoesc 1:e9d0d9b29acc 14 timer = new Timeout;
x1dmoesc 1:e9d0d9b29acc 15
x1dmoesc 0:8625b4741933 16 init();
x1dmoesc 1:e9d0d9b29acc 17
x1dmoesc 0:8625b4741933 18 }
x1dmoesc 0:8625b4741933 19
x1dmoesc 0:8625b4741933 20
x1dmoesc 0:8625b4741933 21 /******************************************************************************/
x1dmoesc 0:8625b4741933 22 // initialisation
x1dmoesc 0:8625b4741933 23 /******************************************************************************/
x1dmoesc 0:8625b4741933 24 void button::init()
x1dmoesc 0:8625b4741933 25 {
x1dmoesc 1:e9d0d9b29acc 26 bIsPressed = false;
x1dmoesc 1:e9d0d9b29acc 27 bLongPress = false;
x1dmoesc 0:8625b4741933 28 bWasPressed = false;
x1dmoesc 0:8625b4741933 29 bNewState = false;
x1dmoesc 0:8625b4741933 30 bOldState = false;
x1dmoesc 0:8625b4741933 31
x1dmoesc 0:8625b4741933 32 bBtnNewAction = false;
x1dmoesc 0:8625b4741933 33
x1dmoesc 0:8625b4741933 34 iNumCycle = 0;
x1dmoesc 1:e9d0d9b29acc 35 if(fTime > 0)
x1dmoesc 1:e9d0d9b29acc 36 timer->detach();
x1dmoesc 1:e9d0d9b29acc 37
x1dmoesc 0:8625b4741933 38
x1dmoesc 0:8625b4741933 39 return;
x1dmoesc 0:8625b4741933 40 }
x1dmoesc 0:8625b4741933 41
x1dmoesc 0:8625b4741933 42
x1dmoesc 0:8625b4741933 43 /******************************************************************************/
x1dmoesc 0:8625b4741933 44 // query the button with debouncing
x1dmoesc 0:8625b4741933 45 // - bNewState: current state of button
x1dmoesc 0:8625b4741933 46 // - bOldState: previous state of button
x1dmoesc 0:8625b4741933 47 // - bBtnNewAction: flag, if an action is occur
x1dmoesc 0:8625b4741933 48 // - iNumCycle: cycle counter of same state
x1dmoesc 0:8625b4741933 49 // - NUNCYCLES: max. value of cycle counter
x1dmoesc 2:cf58f3863c07 50 // - *iNumBtn: pointer of variable, which counts pressed buttons
x1dmoesc 0:8625b4741933 51 /******************************************************************************/
x1dmoesc 3:68f58a34e627 52 void button::polling(uint8_t *iNumBtn)
x1dmoesc 0:8625b4741933 53 {
x1dmoesc 1:e9d0d9b29acc 54 bOldState = bNewState; // bNewState gets the becomes the bOldstate
x1dmoesc 1:e9d0d9b29acc 55 bNewState = (bool)pin->read(); // update bNewState
x1dmoesc 1:e9d0d9b29acc 56 bWasPressed = false; // reset the flag was pressed
x1dmoesc 0:8625b4741933 57
x1dmoesc 2:cf58f3863c07 58
x1dmoesc 1:e9d0d9b29acc 59 if(!bBtnNewAction and (bOldState != bNewState)) // if there is a action (changing signal) on the button (btn), then rise the flag bBtnNewAction
x1dmoesc 1:e9d0d9b29acc 60 bBtnNewAction = true;
x1dmoesc 1:e9d0d9b29acc 61
x1dmoesc 0:8625b4741933 62
x1dmoesc 0:8625b4741933 63
x1dmoesc 0:8625b4741933 64 if(bBtnNewAction) { // if flag ist true (new action on the button)
x1dmoesc 0:8625b4741933 65
x1dmoesc 0:8625b4741933 66 if(bOldState == bNewState) { // if current state equals previous state
x1dmoesc 0:8625b4741933 67 if(iNumCycle <= NUMCYCLE) { // count the number of cycle unless the max. value is reached
x1dmoesc 0:8625b4741933 68 iNumCycle++;
x1dmoesc 0:8625b4741933 69
x1dmoesc 0:8625b4741933 70 } else if(iNumCycle > NUMCYCLE) { // else if the max. value is reached
x1dmoesc 0:8625b4741933 71 iNumCycle = 0; // reset the counter, there is no action any more expected
x1dmoesc 1:e9d0d9b29acc 72 bBtnNewAction = false; // there is nomore new action.
x1dmoesc 0:8625b4741933 73
x1dmoesc 1:e9d0d9b29acc 74 if(bNewState == PRESSED) { // btn is pressed, then
x1dmoesc 1:e9d0d9b29acc 75 bIsPressed = true; // set this flag true
x1dmoesc 1:e9d0d9b29acc 76
x1dmoesc 2:cf58f3863c07 77 if(iNumBtn != NULL)
x1dmoesc 2:cf58f3863c07 78 (*iNumBtn)++;
x1dmoesc 2:cf58f3863c07 79
x1dmoesc 1:e9d0d9b29acc 80 if(fTime > 0) // if btn has a timer,
x1dmoesc 1:e9d0d9b29acc 81 timer->attach(this, &button::setLongPressFlag, fTime);
x1dmoesc 1:e9d0d9b29acc 82
x1dmoesc 0:8625b4741933 83
x1dmoesc 1:e9d0d9b29acc 84 } else if(bNewState == NOT_PRESSED) { // if btn is not pressed, then
x1dmoesc 1:e9d0d9b29acc 85 if(bIsPressed == true) // if btn was pressed, then
x1dmoesc 1:e9d0d9b29acc 86 bWasPressed = true; // set this flag true
x1dmoesc 0:8625b4741933 87
x1dmoesc 1:e9d0d9b29acc 88 bIsPressed = false; // set this flag false
x1dmoesc 1:e9d0d9b29acc 89
x1dmoesc 1:e9d0d9b29acc 90 if(fTime > 0) // if btn has a timer,
x1dmoesc 1:e9d0d9b29acc 91 timer->detach();
x1dmoesc 1:e9d0d9b29acc 92 bLongPress = false;
x1dmoesc 2:cf58f3863c07 93
x1dmoesc 2:cf58f3863c07 94 if((iNumBtn != NULL && *iNumBtn) > 0)
x1dmoesc 2:cf58f3863c07 95 (*iNumBtn)--;
x1dmoesc 0:8625b4741933 96 }
x1dmoesc 0:8625b4741933 97
x1dmoesc 0:8625b4741933 98 }
x1dmoesc 0:8625b4741933 99 } else {
x1dmoesc 0:8625b4741933 100 iNumCycle = 0;
x1dmoesc 0:8625b4741933 101 }
x1dmoesc 0:8625b4741933 102 }
x1dmoesc 0:8625b4741933 103
x1dmoesc 0:8625b4741933 104 return;
x1dmoesc 0:8625b4741933 105 }
x1dmoesc 0:8625b4741933 106
x1dmoesc 0:8625b4741933 107
x1dmoesc 0:8625b4741933 108
x1dmoesc 0:8625b4741933 109 /******************************************************************************/
x1dmoesc 0:8625b4741933 110 //
x1dmoesc 0:8625b4741933 111 /******************************************************************************/
x1dmoesc 0:8625b4741933 112 bool button::isPressed()
x1dmoesc 0:8625b4741933 113 {
x1dmoesc 0:8625b4741933 114 if(bIsPressed) return true;
x1dmoesc 0:8625b4741933 115 else return false;
x1dmoesc 0:8625b4741933 116 }
x1dmoesc 0:8625b4741933 117
x1dmoesc 0:8625b4741933 118
x1dmoesc 0:8625b4741933 119 /******************************************************************************/
x1dmoesc 0:8625b4741933 120 //
x1dmoesc 0:8625b4741933 121 /******************************************************************************/
x1dmoesc 1:e9d0d9b29acc 122 bool button::isLongPress()
x1dmoesc 1:e9d0d9b29acc 123 {
x1dmoesc 1:e9d0d9b29acc 124 if(bLongPress) return true;
x1dmoesc 0:8625b4741933 125 else return false;
x1dmoesc 1:e9d0d9b29acc 126
x1dmoesc 0:8625b4741933 127 }
x1dmoesc 0:8625b4741933 128
x1dmoesc 0:8625b4741933 129
x1dmoesc 1:e9d0d9b29acc 130 /******************************************************************************/
x1dmoesc 1:e9d0d9b29acc 131 //
x1dmoesc 1:e9d0d9b29acc 132 /******************************************************************************/
x1dmoesc 1:e9d0d9b29acc 133 bool button::wasPressed()
x1dmoesc 1:e9d0d9b29acc 134 {
x1dmoesc 1:e9d0d9b29acc 135 if(bWasPressed) return true;
x1dmoesc 1:e9d0d9b29acc 136 else return false;
x1dmoesc 1:e9d0d9b29acc 137
x1dmoesc 1:e9d0d9b29acc 138 }
x1dmoesc 1:e9d0d9b29acc 139
x1dmoesc 1:e9d0d9b29acc 140
x1dmoesc 1:e9d0d9b29acc 141 /******************************************************************************/
x1dmoesc 1:e9d0d9b29acc 142 //
x1dmoesc 1:e9d0d9b29acc 143 /******************************************************************************/
x1dmoesc 1:e9d0d9b29acc 144 void button::setLongPressFlag(){
x1dmoesc 1:e9d0d9b29acc 145 bLongPress = true;
x1dmoesc 1:e9d0d9b29acc 146 timer->detach();
x1dmoesc 1:e9d0d9b29acc 147 }
x1dmoesc 1:e9d0d9b29acc 148
x1dmoesc 1:e9d0d9b29acc 149