Lucas Borges
/
MenuLCD_Copy
this locks like shit
Fork of MenuLCD_copy by
menbed/menbedButtonHandler.cpp@0:92357d1220f3, 2017-05-19 (annotated)
- Committer:
- ViniR
- Date:
- Fri May 19 13:07:52 2017 +0000
- Revision:
- 0:92357d1220f3
Ent?o PARA...
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ViniR | 0:92357d1220f3 | 1 | #include "mbed.h" |
ViniR | 0:92357d1220f3 | 2 | #include "include/menbedNavigator.h" |
ViniR | 0:92357d1220f3 | 3 | #include "include/menbedButtonHandlerTimespec.h" |
ViniR | 0:92357d1220f3 | 4 | #include "include/menbedButtonHandler.h" |
ViniR | 0:92357d1220f3 | 5 | |
ViniR | 0:92357d1220f3 | 6 | MenbedButtonHandler::MenbedButtonHandler (PinName selectPin, PinName downPin, |
ViniR | 0:92357d1220f3 | 7 | PinName upPin, PinName cancelPin, MenbedButtonHandlerTimespec *timespec, |
ViniR | 0:92357d1220f3 | 8 | MenbedNavigator *navigator) : |
ViniR | 0:92357d1220f3 | 9 | //select(select), down(down), up(up), cancel(cancel), |
ViniR | 0:92357d1220f3 | 10 | timespec(timespec), navigator(navigator) |
ViniR | 0:92357d1220f3 | 11 | { |
ViniR | 0:92357d1220f3 | 12 | select = new DigitalIn(selectPin); |
ViniR | 0:92357d1220f3 | 13 | down = new DigitalIn(downPin); |
ViniR | 0:92357d1220f3 | 14 | up = new DigitalIn(upPin); |
ViniR | 0:92357d1220f3 | 15 | cancel = new DigitalIn(cancelPin); |
ViniR | 0:92357d1220f3 | 16 | |
ViniR | 0:92357d1220f3 | 17 | numButtons = 4; |
ViniR | 0:92357d1220f3 | 18 | |
ViniR | 0:92357d1220f3 | 19 | select->mode (PullDown); |
ViniR | 0:92357d1220f3 | 20 | down->mode (PullDown); |
ViniR | 0:92357d1220f3 | 21 | up->mode (PullDown); |
ViniR | 0:92357d1220f3 | 22 | cancel->mode (PullDown); |
ViniR | 0:92357d1220f3 | 23 | |
ViniR | 0:92357d1220f3 | 24 | init(); |
ViniR | 0:92357d1220f3 | 25 | } |
ViniR | 0:92357d1220f3 | 26 | |
ViniR | 0:92357d1220f3 | 27 | |
ViniR | 0:92357d1220f3 | 28 | MenbedButtonHandler::MenbedButtonHandler (PinName selectPin, PinName downPin, |
ViniR | 0:92357d1220f3 | 29 | PinName upPin, MenbedButtonHandlerTimespec *timespec, |
ViniR | 0:92357d1220f3 | 30 | MenbedNavigator *navigator) : |
ViniR | 0:92357d1220f3 | 31 | timespec(timespec), navigator(navigator) |
ViniR | 0:92357d1220f3 | 32 | { |
ViniR | 0:92357d1220f3 | 33 | select = new DigitalIn(selectPin); |
ViniR | 0:92357d1220f3 | 34 | down = new DigitalIn(downPin); |
ViniR | 0:92357d1220f3 | 35 | up = new DigitalIn(upPin); |
ViniR | 0:92357d1220f3 | 36 | |
ViniR | 0:92357d1220f3 | 37 | numButtons = 3; |
ViniR | 0:92357d1220f3 | 38 | |
ViniR | 0:92357d1220f3 | 39 | select->mode (PullDown); |
ViniR | 0:92357d1220f3 | 40 | down->mode (PullDown); |
ViniR | 0:92357d1220f3 | 41 | up->mode (PullDown); |
ViniR | 0:92357d1220f3 | 42 | |
ViniR | 0:92357d1220f3 | 43 | init(); |
ViniR | 0:92357d1220f3 | 44 | } |
ViniR | 0:92357d1220f3 | 45 | |
ViniR | 0:92357d1220f3 | 46 | |
ViniR | 0:92357d1220f3 | 47 | MenbedButtonHandler::MenbedButtonHandler (PinName selectPin, PinName downPin, |
ViniR | 0:92357d1220f3 | 48 | MenbedButtonHandlerTimespec *timespec, |
ViniR | 0:92357d1220f3 | 49 | MenbedNavigator *navigator) : |
ViniR | 0:92357d1220f3 | 50 | timespec(timespec), navigator(navigator) |
ViniR | 0:92357d1220f3 | 51 | { |
ViniR | 0:92357d1220f3 | 52 | select = new DigitalIn(selectPin); |
ViniR | 0:92357d1220f3 | 53 | down = new DigitalIn(downPin); |
ViniR | 0:92357d1220f3 | 54 | |
ViniR | 0:92357d1220f3 | 55 | numButtons = 2; |
ViniR | 0:92357d1220f3 | 56 | |
ViniR | 0:92357d1220f3 | 57 | select->mode (PullDown); |
ViniR | 0:92357d1220f3 | 58 | down->mode (PullDown); |
ViniR | 0:92357d1220f3 | 59 | |
ViniR | 0:92357d1220f3 | 60 | init(); |
ViniR | 0:92357d1220f3 | 61 | } |
ViniR | 0:92357d1220f3 | 62 | |
ViniR | 0:92357d1220f3 | 63 | |
ViniR | 0:92357d1220f3 | 64 | void MenbedButtonHandler::init() |
ViniR | 0:92357d1220f3 | 65 | { |
ViniR | 0:92357d1220f3 | 66 | for (int i=0; i<4; i++) |
ViniR | 0:92357d1220f3 | 67 | { |
ViniR | 0:92357d1220f3 | 68 | buttonDebounced[i] = false; |
ViniR | 0:92357d1220f3 | 69 | buttonAlreadyDepressed[i] = false; |
ViniR | 0:92357d1220f3 | 70 | } |
ViniR | 0:92357d1220f3 | 71 | |
ViniR | 0:92357d1220f3 | 72 | currentTime_us = 0; |
ViniR | 0:92357d1220f3 | 73 | ticker.attach_us (this, &MenbedButtonHandler::tick, |
ViniR | 0:92357d1220f3 | 74 | timespec->scanPeriod_us); |
ViniR | 0:92357d1220f3 | 75 | } |
ViniR | 0:92357d1220f3 | 76 | |
ViniR | 0:92357d1220f3 | 77 | |
ViniR | 0:92357d1220f3 | 78 | void MenbedButtonHandler::tick (void) |
ViniR | 0:92357d1220f3 | 79 | { |
ViniR | 0:92357d1220f3 | 80 | // Accumulated amount of time that buttons have been continuously depressed |
ViniR | 0:92357d1220f3 | 81 | uint32_t buttonDepressedTime_us; |
ViniR | 0:92357d1220f3 | 82 | bool buttonCurrentlyDepressed; |
ViniR | 0:92357d1220f3 | 83 | |
ViniR | 0:92357d1220f3 | 84 | MenbedButtonEvent buttonEvent; |
ViniR | 0:92357d1220f3 | 85 | buttonEvent.numButtons = numButtons; |
ViniR | 0:92357d1220f3 | 86 | |
ViniR | 0:92357d1220f3 | 87 | currentTime_us += timespec->scanPeriod_us; |
ViniR | 0:92357d1220f3 | 88 | |
ViniR | 0:92357d1220f3 | 89 | // Cycle through each of the buttons |
ViniR | 0:92357d1220f3 | 90 | for (int i=MenbedButtonEvent::ButtonSelect; |
ViniR | 0:92357d1220f3 | 91 | (i<=MenbedButtonEvent::ButtonCancel) && (i<numButtons); |
ViniR | 0:92357d1220f3 | 92 | i++) |
ViniR | 0:92357d1220f3 | 93 | { |
ViniR | 0:92357d1220f3 | 94 | buttonEvent.name = (MenbedButtonEvent::ButtonName)i; |
ViniR | 0:92357d1220f3 | 95 | buttonCurrentlyDepressed = |
ViniR | 0:92357d1220f3 | 96 | isButtonDepressed((MenbedButtonEvent::ButtonName)i); |
ViniR | 0:92357d1220f3 | 97 | |
ViniR | 0:92357d1220f3 | 98 | // The amount of time the current button has been depressed is |
ViniR | 0:92357d1220f3 | 99 | // the current time minus the time which the button was first |
ViniR | 0:92357d1220f3 | 100 | // depressed. |
ViniR | 0:92357d1220f3 | 101 | buttonDepressedTime_us = currentTime_us - buttonPushedTime_us[i]; |
ViniR | 0:92357d1220f3 | 102 | |
ViniR | 0:92357d1220f3 | 103 | if (buttonCurrentlyDepressed && buttonAlreadyDepressed[i]) |
ViniR | 0:92357d1220f3 | 104 | { |
ViniR | 0:92357d1220f3 | 105 | if ((buttonDepressedTime_us >= timespec->typematicX10Delay_us) && |
ViniR | 0:92357d1220f3 | 106 | (currentTime_us - buttonLastTypematicTime_us[i] >= |
ViniR | 0:92357d1220f3 | 107 | timespec->typematicX10Period_us )) |
ViniR | 0:92357d1220f3 | 108 | { |
ViniR | 0:92357d1220f3 | 109 | buttonLastTypematicTime_us[i] = currentTime_us; |
ViniR | 0:92357d1220f3 | 110 | buttonEvent.action = MenbedButtonEvent::BUTTON_ACTION_PUSHED; |
ViniR | 0:92357d1220f3 | 111 | navigator->handleButtonEvent (buttonEvent); |
ViniR | 0:92357d1220f3 | 112 | } |
ViniR | 0:92357d1220f3 | 113 | else if ((buttonDepressedTime_us >= |
ViniR | 0:92357d1220f3 | 114 | timespec->typematicPeriod_us) |
ViniR | 0:92357d1220f3 | 115 | && (currentTime_us - buttonLastTypematicTime_us[i] >= |
ViniR | 0:92357d1220f3 | 116 | timespec->typematicPeriod_us)) |
ViniR | 0:92357d1220f3 | 117 | { |
ViniR | 0:92357d1220f3 | 118 | buttonLastTypematicTime_us[i] = currentTime_us; |
ViniR | 0:92357d1220f3 | 119 | buttonEvent.action = MenbedButtonEvent::BUTTON_ACTION_PUSHED; |
ViniR | 0:92357d1220f3 | 120 | navigator->handleButtonEvent (buttonEvent); |
ViniR | 0:92357d1220f3 | 121 | } |
ViniR | 0:92357d1220f3 | 122 | else if ((buttonDepressedTime_us >= timespec->debounceDelay_us) |
ViniR | 0:92357d1220f3 | 123 | && (!buttonDebounced[i])) |
ViniR | 0:92357d1220f3 | 124 | { |
ViniR | 0:92357d1220f3 | 125 | buttonLastTypematicTime_us[i] = currentTime_us; |
ViniR | 0:92357d1220f3 | 126 | buttonDebounced[i] = true; |
ViniR | 0:92357d1220f3 | 127 | buttonEvent.action = MenbedButtonEvent::BUTTON_ACTION_PUSHED; |
ViniR | 0:92357d1220f3 | 128 | navigator->handleButtonEvent (buttonEvent); |
ViniR | 0:92357d1220f3 | 129 | } |
ViniR | 0:92357d1220f3 | 130 | |
ViniR | 0:92357d1220f3 | 131 | if (buttonDebounced[i] && |
ViniR | 0:92357d1220f3 | 132 | (buttonDepressedTime_us >= |
ViniR | 0:92357d1220f3 | 133 | timespec->longReleaseDelay_us)) |
ViniR | 0:92357d1220f3 | 134 | { |
ViniR | 0:92357d1220f3 | 135 | buttonEvent.action = MenbedButtonEvent::BUTTON_ACTION_HELD_LONG; |
ViniR | 0:92357d1220f3 | 136 | navigator->handleButtonEvent (buttonEvent); |
ViniR | 0:92357d1220f3 | 137 | } |
ViniR | 0:92357d1220f3 | 138 | |
ViniR | 0:92357d1220f3 | 139 | buttonAlreadyDepressed[i] = true; |
ViniR | 0:92357d1220f3 | 140 | } |
ViniR | 0:92357d1220f3 | 141 | // Otherwise, if the button was just depressed, we indicate that it |
ViniR | 0:92357d1220f3 | 142 | // has not yet debounced and record the time so that we know when it |
ViniR | 0:92357d1220f3 | 143 | // was first pressed. |
ViniR | 0:92357d1220f3 | 144 | else if (buttonCurrentlyDepressed && !buttonAlreadyDepressed[i]) |
ViniR | 0:92357d1220f3 | 145 | { |
ViniR | 0:92357d1220f3 | 146 | buttonDebounced[i] = false; |
ViniR | 0:92357d1220f3 | 147 | buttonPushedTime_us[i] = currentTime_us; |
ViniR | 0:92357d1220f3 | 148 | |
ViniR | 0:92357d1220f3 | 149 | buttonAlreadyDepressed[i] = true; |
ViniR | 0:92357d1220f3 | 150 | } |
ViniR | 0:92357d1220f3 | 151 | // Otherwise, if the button is not depressed but it was previously |
ViniR | 0:92357d1220f3 | 152 | // depressed and the amount of time it was depressed exceeds the |
ViniR | 0:92357d1220f3 | 153 | // debounce time, then we say the button has been released. |
ViniR | 0:92357d1220f3 | 154 | else if (!buttonCurrentlyDepressed && buttonAlreadyDepressed[i]) |
ViniR | 0:92357d1220f3 | 155 | { |
ViniR | 0:92357d1220f3 | 156 | if (buttonDebounced[i]) |
ViniR | 0:92357d1220f3 | 157 | { |
ViniR | 0:92357d1220f3 | 158 | if (buttonDepressedTime_us >= |
ViniR | 0:92357d1220f3 | 159 | timespec->longReleaseDelay_us) |
ViniR | 0:92357d1220f3 | 160 | { |
ViniR | 0:92357d1220f3 | 161 | buttonEvent.action = MenbedButtonEvent::BUTTON_ACTION_RELEASED_LONG; |
ViniR | 0:92357d1220f3 | 162 | navigator->handleButtonEvent (buttonEvent); |
ViniR | 0:92357d1220f3 | 163 | } |
ViniR | 0:92357d1220f3 | 164 | else |
ViniR | 0:92357d1220f3 | 165 | { |
ViniR | 0:92357d1220f3 | 166 | buttonEvent.action = MenbedButtonEvent::BUTTON_ACTION_RELEASED_SHORT; |
ViniR | 0:92357d1220f3 | 167 | navigator->handleButtonEvent (buttonEvent); |
ViniR | 0:92357d1220f3 | 168 | } |
ViniR | 0:92357d1220f3 | 169 | } |
ViniR | 0:92357d1220f3 | 170 | |
ViniR | 0:92357d1220f3 | 171 | buttonAlreadyDepressed[i] = false; |
ViniR | 0:92357d1220f3 | 172 | } |
ViniR | 0:92357d1220f3 | 173 | } |
ViniR | 0:92357d1220f3 | 174 | } |
ViniR | 0:92357d1220f3 | 175 | |
ViniR | 0:92357d1220f3 | 176 | |
ViniR | 0:92357d1220f3 | 177 | bool MenbedButtonHandler::isButtonDepressed (MenbedButtonEvent::ButtonName name) |
ViniR | 0:92357d1220f3 | 178 | { |
ViniR | 0:92357d1220f3 | 179 | switch (name) |
ViniR | 0:92357d1220f3 | 180 | { |
ViniR | 0:92357d1220f3 | 181 | case MenbedButtonEvent::ButtonSelect: |
ViniR | 0:92357d1220f3 | 182 | return *select; |
ViniR | 0:92357d1220f3 | 183 | case MenbedButtonEvent::ButtonDown: |
ViniR | 0:92357d1220f3 | 184 | if (numButtons >= 2) |
ViniR | 0:92357d1220f3 | 185 | return *down; |
ViniR | 0:92357d1220f3 | 186 | else |
ViniR | 0:92357d1220f3 | 187 | return false; |
ViniR | 0:92357d1220f3 | 188 | case MenbedButtonEvent::ButtonUp: |
ViniR | 0:92357d1220f3 | 189 | if (numButtons >= 3) |
ViniR | 0:92357d1220f3 | 190 | return *up; |
ViniR | 0:92357d1220f3 | 191 | else |
ViniR | 0:92357d1220f3 | 192 | return false; |
ViniR | 0:92357d1220f3 | 193 | case MenbedButtonEvent::ButtonCancel: |
ViniR | 0:92357d1220f3 | 194 | if (numButtons == 4) |
ViniR | 0:92357d1220f3 | 195 | return *cancel; |
ViniR | 0:92357d1220f3 | 196 | else |
ViniR | 0:92357d1220f3 | 197 | return false; |
ViniR | 0:92357d1220f3 | 198 | } |
ViniR | 0:92357d1220f3 | 199 | |
ViniR | 0:92357d1220f3 | 200 | return false; |
ViniR | 0:92357d1220f3 | 201 | } |