class of Buttons -isPressed -wasPressed -notPressed -LongPress (Timer)
button.cpp@0:8625b4741933, 2018-11-30 (annotated)
- Committer:
- x1dmoesc
- Date:
- Fri Nov 30 15:46:00 2018 +0000
- Revision:
- 0:8625b4741933
- Child:
- 1:e9d0d9b29acc
Stand_30.11.2018
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:8625b4741933 | 8 | button::button(PinName pBtn, PinMode pBtnMode) |
x1dmoesc | 0:8625b4741933 | 9 | { |
x1dmoesc | 0:8625b4741933 | 10 | pin = new DigitalIn(pBtn, pBtnMode); |
x1dmoesc | 0:8625b4741933 | 11 | init(); |
x1dmoesc | 0:8625b4741933 | 12 | } |
x1dmoesc | 0:8625b4741933 | 13 | |
x1dmoesc | 0:8625b4741933 | 14 | |
x1dmoesc | 0:8625b4741933 | 15 | /******************************************************************************/ |
x1dmoesc | 0:8625b4741933 | 16 | // initialisation |
x1dmoesc | 0:8625b4741933 | 17 | /******************************************************************************/ |
x1dmoesc | 0:8625b4741933 | 18 | void button::init() |
x1dmoesc | 0:8625b4741933 | 19 | { |
x1dmoesc | 0:8625b4741933 | 20 | bIsPressed = false; |
x1dmoesc | 0:8625b4741933 | 21 | bWasPressed = false; |
x1dmoesc | 0:8625b4741933 | 22 | bNewState = false; |
x1dmoesc | 0:8625b4741933 | 23 | bOldState = false; |
x1dmoesc | 0:8625b4741933 | 24 | |
x1dmoesc | 0:8625b4741933 | 25 | bBtnNewAction = false; |
x1dmoesc | 0:8625b4741933 | 26 | |
x1dmoesc | 0:8625b4741933 | 27 | iNumCycle = 0; |
x1dmoesc | 0:8625b4741933 | 28 | |
x1dmoesc | 0:8625b4741933 | 29 | return; |
x1dmoesc | 0:8625b4741933 | 30 | } |
x1dmoesc | 0:8625b4741933 | 31 | |
x1dmoesc | 0:8625b4741933 | 32 | |
x1dmoesc | 0:8625b4741933 | 33 | /******************************************************************************/ |
x1dmoesc | 0:8625b4741933 | 34 | // query the button with debouncing |
x1dmoesc | 0:8625b4741933 | 35 | // - bNewState: current state of button |
x1dmoesc | 0:8625b4741933 | 36 | // - bOldState: previous state of button |
x1dmoesc | 0:8625b4741933 | 37 | // - bBtnNewAction: flag, if an action is occur |
x1dmoesc | 0:8625b4741933 | 38 | // - iNumCycle: cycle counter of same state |
x1dmoesc | 0:8625b4741933 | 39 | // - NUNCYCLES: max. value of cycle counter |
x1dmoesc | 0:8625b4741933 | 40 | /******************************************************************************/ |
x1dmoesc | 0:8625b4741933 | 41 | void button::polling() |
x1dmoesc | 0:8625b4741933 | 42 | { |
x1dmoesc | 0:8625b4741933 | 43 | |
x1dmoesc | 0:8625b4741933 | 44 | |
x1dmoesc | 0:8625b4741933 | 45 | bOldState = bNewState; |
x1dmoesc | 0:8625b4741933 | 46 | bNewState = (bool)pin->read(); |
x1dmoesc | 0:8625b4741933 | 47 | bWasPressed = false; |
x1dmoesc | 0:8625b4741933 | 48 | |
x1dmoesc | 0:8625b4741933 | 49 | if( !bBtnNewAction and (bOldState != bNewState) ) { // if there is a action (changing signal) on the button (btn), then rise the flag bBtnNewAction |
x1dmoesc | 0:8625b4741933 | 50 | bBtnNewAction = true; |
x1dmoesc | 0:8625b4741933 | 51 | } |
x1dmoesc | 0:8625b4741933 | 52 | |
x1dmoesc | 0:8625b4741933 | 53 | |
x1dmoesc | 0:8625b4741933 | 54 | |
x1dmoesc | 0:8625b4741933 | 55 | if(bBtnNewAction) { // if flag ist true (new action on the button) |
x1dmoesc | 0:8625b4741933 | 56 | |
x1dmoesc | 0:8625b4741933 | 57 | if(bOldState == bNewState) { // if current state equals previous state |
x1dmoesc | 0:8625b4741933 | 58 | if(iNumCycle <= NUMCYCLE) { // count the number of cycle unless the max. value is reached |
x1dmoesc | 0:8625b4741933 | 59 | iNumCycle++; |
x1dmoesc | 0:8625b4741933 | 60 | |
x1dmoesc | 0:8625b4741933 | 61 | } else if(iNumCycle > NUMCYCLE) { // else if the max. value is reached |
x1dmoesc | 0:8625b4741933 | 62 | iNumCycle = 0; // reset the counter, there is no action any more expected |
x1dmoesc | 0:8625b4741933 | 63 | bBtnNewAction = false; |
x1dmoesc | 0:8625b4741933 | 64 | |
x1dmoesc | 0:8625b4741933 | 65 | if(bNewState == PRESSED) { |
x1dmoesc | 0:8625b4741933 | 66 | bIsPressed = true; |
x1dmoesc | 0:8625b4741933 | 67 | |
x1dmoesc | 0:8625b4741933 | 68 | } else if(bNewState == NOT_PRESSED) { |
x1dmoesc | 0:8625b4741933 | 69 | |
x1dmoesc | 0:8625b4741933 | 70 | if(bIsPressed == true) |
x1dmoesc | 0:8625b4741933 | 71 | bWasPressed = true; |
x1dmoesc | 0:8625b4741933 | 72 | |
x1dmoesc | 0:8625b4741933 | 73 | bIsPressed = false; |
x1dmoesc | 0:8625b4741933 | 74 | } |
x1dmoesc | 0:8625b4741933 | 75 | |
x1dmoesc | 0:8625b4741933 | 76 | } |
x1dmoesc | 0:8625b4741933 | 77 | } else { |
x1dmoesc | 0:8625b4741933 | 78 | iNumCycle = 0; |
x1dmoesc | 0:8625b4741933 | 79 | } |
x1dmoesc | 0:8625b4741933 | 80 | |
x1dmoesc | 0:8625b4741933 | 81 | } |
x1dmoesc | 0:8625b4741933 | 82 | |
x1dmoesc | 0:8625b4741933 | 83 | return; |
x1dmoesc | 0:8625b4741933 | 84 | } |
x1dmoesc | 0:8625b4741933 | 85 | |
x1dmoesc | 0:8625b4741933 | 86 | |
x1dmoesc | 0:8625b4741933 | 87 | |
x1dmoesc | 0:8625b4741933 | 88 | /******************************************************************************/ |
x1dmoesc | 0:8625b4741933 | 89 | // |
x1dmoesc | 0:8625b4741933 | 90 | /******************************************************************************/ |
x1dmoesc | 0:8625b4741933 | 91 | bool button::isPressed() |
x1dmoesc | 0:8625b4741933 | 92 | { |
x1dmoesc | 0:8625b4741933 | 93 | if(bIsPressed) return true; |
x1dmoesc | 0:8625b4741933 | 94 | else return false; |
x1dmoesc | 0:8625b4741933 | 95 | } |
x1dmoesc | 0:8625b4741933 | 96 | |
x1dmoesc | 0:8625b4741933 | 97 | |
x1dmoesc | 0:8625b4741933 | 98 | /******************************************************************************/ |
x1dmoesc | 0:8625b4741933 | 99 | // |
x1dmoesc | 0:8625b4741933 | 100 | /******************************************************************************/ |
x1dmoesc | 0:8625b4741933 | 101 | bool button::wasPressed() |
x1dmoesc | 0:8625b4741933 | 102 | { |
x1dmoesc | 0:8625b4741933 | 103 | if(bWasPressed) return true; |
x1dmoesc | 0:8625b4741933 | 104 | else return false; |
x1dmoesc | 0:8625b4741933 | 105 | |
x1dmoesc | 0:8625b4741933 | 106 | } |
x1dmoesc | 0:8625b4741933 | 107 | |
x1dmoesc | 0:8625b4741933 | 108 |