Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of frdm_echo by
PushButtons.cpp@15:a32db434af65, 2014-04-25 (annotated)
- Committer:
- silverpanda
- Date:
- Fri Apr 25 16:22:54 2014 +0000
- Revision:
- 15:a32db434af65
- Parent:
- 13:019e24491f32
debugged
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
silverpanda | 13:019e24491f32 | 1 | #include "mbed.h" |
silverpanda | 13:019e24491f32 | 2 | #include "PushButtons.h" |
silverpanda | 13:019e24491f32 | 3 | |
silverpanda | 13:019e24491f32 | 4 | PushButtons::PushButtons() |
silverpanda | 13:019e24491f32 | 5 | { |
silverpanda | 13:019e24491f32 | 6 | // construct instances for push buttons |
silverpanda | 13:019e24491f32 | 7 | sw2 = new DigitalIn(SW2); |
silverpanda | 13:019e24491f32 | 8 | sw3 = new DigitalIn(SW3); |
silverpanda | 13:019e24491f32 | 9 | |
silverpanda | 13:019e24491f32 | 10 | // reset push buttons |
silverpanda | 13:019e24491f32 | 11 | reset(pb2); |
silverpanda | 13:019e24491f32 | 12 | reset(pb3); |
silverpanda | 13:019e24491f32 | 13 | } |
silverpanda | 13:019e24491f32 | 14 | //----------------------------------------------------------------------------- |
silverpanda | 13:019e24491f32 | 15 | |
silverpanda | 13:019e24491f32 | 16 | bool PushButtons::tick10ms() |
silverpanda | 13:019e24491f32 | 17 | { |
silverpanda | 13:019e24491f32 | 18 | // process push button metadata |
silverpanda | 13:019e24491f32 | 19 | process(pb2); |
silverpanda | 13:019e24491f32 | 20 | process(pb3); |
silverpanda | 13:019e24491f32 | 21 | |
silverpanda | 13:019e24491f32 | 22 | return !*sw2 || !*sw3; |
silverpanda | 13:019e24491f32 | 23 | } |
silverpanda | 13:019e24491f32 | 24 | //----------------------------------------------------------------------------- |
silverpanda | 13:019e24491f32 | 25 | |
silverpanda | 13:019e24491f32 | 26 | void PushButtons::reset(ePushButton thisPushButton) |
silverpanda | 13:019e24491f32 | 27 | { |
silverpanda | 13:019e24491f32 | 28 | // initiate push button |
silverpanda | 13:019e24491f32 | 29 | timer[thisPushButton] = 0; |
silverpanda | 13:019e24491f32 | 30 | justPressed[thisPushButton] = false; |
silverpanda | 13:019e24491f32 | 31 | oldPressed[thisPushButton] = false; |
silverpanda | 13:019e24491f32 | 32 | } |
silverpanda | 13:019e24491f32 | 33 | //----------------------------------------------------------------------------- |
silverpanda | 13:019e24491f32 | 34 | |
silverpanda | 13:019e24491f32 | 35 | void PushButtons::process(ePushButton thisPushButton) |
silverpanda | 13:019e24491f32 | 36 | { |
silverpanda | 13:019e24491f32 | 37 | // check if the push button is pressed |
silverpanda | 13:019e24491f32 | 38 | bool isPressed = !*getPointer(thisPushButton); |
silverpanda | 13:019e24491f32 | 39 | |
silverpanda | 13:019e24491f32 | 40 | // update debounce timer |
silverpanda | 13:019e24491f32 | 41 | if(timer[thisPushButton]) timer[thisPushButton]--; |
silverpanda | 13:019e24491f32 | 42 | |
silverpanda | 13:019e24491f32 | 43 | // if not in debounce mode |
silverpanda | 13:019e24491f32 | 44 | else if(isPressed) { |
silverpanda | 13:019e24491f32 | 45 | |
silverpanda | 13:019e24491f32 | 46 | // "just pressed" is set only once |
silverpanda | 13:019e24491f32 | 47 | if(!oldPressed[thisPushButton]) justPressed[thisPushButton] = true; |
silverpanda | 13:019e24491f32 | 48 | timer[thisPushButton] = 5; |
silverpanda | 13:019e24491f32 | 49 | } |
silverpanda | 13:019e24491f32 | 50 | |
silverpanda | 13:019e24491f32 | 51 | // remember old state |
silverpanda | 13:019e24491f32 | 52 | oldPressed[thisPushButton] = isPressed; |
silverpanda | 13:019e24491f32 | 53 | } |
silverpanda | 13:019e24491f32 | 54 | //----------------------------------------------------------------------------- |
silverpanda | 13:019e24491f32 | 55 | |
silverpanda | 13:019e24491f32 | 56 | DigitalIn *PushButtons::getPointer(ePushButton thisPushButton) |
silverpanda | 13:019e24491f32 | 57 | { |
silverpanda | 13:019e24491f32 | 58 | DigitalIn *thisPointer = 0; |
silverpanda | 13:019e24491f32 | 59 | switch(thisPushButton) { |
silverpanda | 13:019e24491f32 | 60 | case pb2: thisPointer = sw2; break; |
silverpanda | 13:019e24491f32 | 61 | case pb3: thisPointer = sw3; break; |
silverpanda | 13:019e24491f32 | 62 | } |
silverpanda | 13:019e24491f32 | 63 | |
silverpanda | 13:019e24491f32 | 64 | // return pointer to requested push button |
silverpanda | 13:019e24491f32 | 65 | return thisPointer; |
silverpanda | 13:019e24491f32 | 66 | } |
silverpanda | 13:019e24491f32 | 67 | //----------------------------------------------------------------------------- |
silverpanda | 13:019e24491f32 | 68 | |
silverpanda | 13:019e24491f32 | 69 | bool PushButtons::getJustPressed(ePushButton thisPushButton) |
silverpanda | 13:019e24491f32 | 70 | { |
silverpanda | 13:019e24491f32 | 71 | // return state, and then reset |
silverpanda | 13:019e24491f32 | 72 | bool isJustPressed = justPressed[thisPushButton]; |
silverpanda | 13:019e24491f32 | 73 | justPressed[thisPushButton] = false; |
silverpanda | 13:019e24491f32 | 74 | return isJustPressed; |
silverpanda | 13:019e24491f32 | 75 | } |
silverpanda | 13:019e24491f32 | 76 | //----------------------------------------------------------------------------- |
silverpanda | 13:019e24491f32 | 77 | |
silverpanda | 13:019e24491f32 | 78 | bool PushButtons::getPressed(ePushButton thisPushButton) |
silverpanda | 13:019e24491f32 | 79 | { |
silverpanda | 13:019e24491f32 | 80 | // see if push button is momentary pressed |
silverpanda | 13:019e24491f32 | 81 | return *getPointer(thisPushButton); |
silverpanda | 13:019e24491f32 | 82 | } |
silverpanda | 13:019e24491f32 | 83 | //----------------------------------------------------------------------------- |