David Möschwitzer / button
Committer:
x1dmoesc
Date:
Fri Apr 17 14:05:36 2020 +0000
Revision:
8:6c8a00baae91
Parent:
7:09fb38cf8526
add new coments and debug the condition of: if(iNumBtn != NULL && *iNumBtn > 0)

Who changed what in which revision?

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