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

Committer:
x1dmoesc
Date:
Tue Dec 04 10:40:33 2018 +0000
Revision:
1:e9d0d9b29acc
Parent:
0:8625b4741933
Child:
2:cf58f3863c07
add timer and bWasPressed;

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 0:8625b4741933 50 /******************************************************************************/
x1dmoesc 0:8625b4741933 51 void button::polling()
x1dmoesc 0:8625b4741933 52 {
x1dmoesc 1:e9d0d9b29acc 53 bOldState = bNewState; // bNewState gets the becomes the bOldstate
x1dmoesc 1:e9d0d9b29acc 54 bNewState = (bool)pin->read(); // update bNewState
x1dmoesc 1:e9d0d9b29acc 55 bWasPressed = false; // reset the flag was pressed
x1dmoesc 0:8625b4741933 56
x1dmoesc 0:8625b4741933 57
x1dmoesc 1:e9d0d9b29acc 58 if(!bBtnNewAction and (bOldState != bNewState)) // if there is a action (changing signal) on the button (btn), then rise the flag bBtnNewAction
x1dmoesc 1:e9d0d9b29acc 59 bBtnNewAction = true;
x1dmoesc 1:e9d0d9b29acc 60
x1dmoesc 0:8625b4741933 61
x1dmoesc 0:8625b4741933 62
x1dmoesc 0:8625b4741933 63 if(bBtnNewAction) { // if flag ist true (new action on the button)
x1dmoesc 0:8625b4741933 64
x1dmoesc 0:8625b4741933 65 if(bOldState == bNewState) { // if current state equals previous state
x1dmoesc 0:8625b4741933 66 if(iNumCycle <= NUMCYCLE) { // count the number of cycle unless the max. value is reached
x1dmoesc 0:8625b4741933 67 iNumCycle++;
x1dmoesc 0:8625b4741933 68
x1dmoesc 0:8625b4741933 69 } else if(iNumCycle > NUMCYCLE) { // else if the max. value is reached
x1dmoesc 0:8625b4741933 70 iNumCycle = 0; // reset the counter, there is no action any more expected
x1dmoesc 1:e9d0d9b29acc 71 bBtnNewAction = false; // there is nomore new action.
x1dmoesc 0:8625b4741933 72
x1dmoesc 1:e9d0d9b29acc 73 if(bNewState == PRESSED) { // btn is pressed, then
x1dmoesc 1:e9d0d9b29acc 74 bIsPressed = true; // set this flag true
x1dmoesc 1:e9d0d9b29acc 75
x1dmoesc 1:e9d0d9b29acc 76 if(fTime > 0) // if btn has a timer,
x1dmoesc 1:e9d0d9b29acc 77 timer->attach(this, &button::setLongPressFlag, fTime);
x1dmoesc 1:e9d0d9b29acc 78
x1dmoesc 0:8625b4741933 79
x1dmoesc 1:e9d0d9b29acc 80 } else if(bNewState == NOT_PRESSED) { // if btn is not pressed, then
x1dmoesc 1:e9d0d9b29acc 81 if(bIsPressed == true) // if btn was pressed, then
x1dmoesc 1:e9d0d9b29acc 82 bWasPressed = true; // set this flag true
x1dmoesc 0:8625b4741933 83
x1dmoesc 1:e9d0d9b29acc 84 bIsPressed = false; // set this flag false
x1dmoesc 1:e9d0d9b29acc 85
x1dmoesc 1:e9d0d9b29acc 86 if(fTime > 0) // if btn has a timer,
x1dmoesc 1:e9d0d9b29acc 87 timer->detach();
x1dmoesc 1:e9d0d9b29acc 88 bLongPress = false;
x1dmoesc 0:8625b4741933 89 }
x1dmoesc 0:8625b4741933 90
x1dmoesc 0:8625b4741933 91 }
x1dmoesc 0:8625b4741933 92 } else {
x1dmoesc 0:8625b4741933 93 iNumCycle = 0;
x1dmoesc 0:8625b4741933 94 }
x1dmoesc 0:8625b4741933 95 }
x1dmoesc 0:8625b4741933 96
x1dmoesc 0:8625b4741933 97 return;
x1dmoesc 0:8625b4741933 98 }
x1dmoesc 0:8625b4741933 99
x1dmoesc 0:8625b4741933 100
x1dmoesc 0:8625b4741933 101
x1dmoesc 0:8625b4741933 102 /******************************************************************************/
x1dmoesc 0:8625b4741933 103 //
x1dmoesc 0:8625b4741933 104 /******************************************************************************/
x1dmoesc 0:8625b4741933 105 bool button::isPressed()
x1dmoesc 0:8625b4741933 106 {
x1dmoesc 0:8625b4741933 107 if(bIsPressed) return true;
x1dmoesc 0:8625b4741933 108 else return false;
x1dmoesc 0:8625b4741933 109 }
x1dmoesc 0:8625b4741933 110
x1dmoesc 0:8625b4741933 111
x1dmoesc 0:8625b4741933 112 /******************************************************************************/
x1dmoesc 0:8625b4741933 113 //
x1dmoesc 0:8625b4741933 114 /******************************************************************************/
x1dmoesc 1:e9d0d9b29acc 115 bool button::isLongPress()
x1dmoesc 1:e9d0d9b29acc 116 {
x1dmoesc 1:e9d0d9b29acc 117 if(bLongPress) return true;
x1dmoesc 0:8625b4741933 118 else return false;
x1dmoesc 1:e9d0d9b29acc 119
x1dmoesc 0:8625b4741933 120 }
x1dmoesc 0:8625b4741933 121
x1dmoesc 0:8625b4741933 122
x1dmoesc 1:e9d0d9b29acc 123 /******************************************************************************/
x1dmoesc 1:e9d0d9b29acc 124 //
x1dmoesc 1:e9d0d9b29acc 125 /******************************************************************************/
x1dmoesc 1:e9d0d9b29acc 126 bool button::wasPressed()
x1dmoesc 1:e9d0d9b29acc 127 {
x1dmoesc 1:e9d0d9b29acc 128 if(bWasPressed) return true;
x1dmoesc 1:e9d0d9b29acc 129 else return false;
x1dmoesc 1:e9d0d9b29acc 130
x1dmoesc 1:e9d0d9b29acc 131 }
x1dmoesc 1:e9d0d9b29acc 132
x1dmoesc 1:e9d0d9b29acc 133
x1dmoesc 1:e9d0d9b29acc 134 /******************************************************************************/
x1dmoesc 1:e9d0d9b29acc 135 //
x1dmoesc 1:e9d0d9b29acc 136 /******************************************************************************/
x1dmoesc 1:e9d0d9b29acc 137 void button::setLongPressFlag(){
x1dmoesc 1:e9d0d9b29acc 138 bLongPress = true;
x1dmoesc 1:e9d0d9b29acc 139 timer->detach();
x1dmoesc 1:e9d0d9b29acc 140 }
x1dmoesc 1:e9d0d9b29acc 141
x1dmoesc 1:e9d0d9b29acc 142