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.
menbedButtonHandler.cpp
00001 #include "mbed.h" 00002 #include "include/menbedNavigator.h" 00003 #include "include/menbedButtonHandlerTimespec.h" 00004 #include "include/menbedButtonHandler.h" 00005 00006 MenbedButtonHandler::MenbedButtonHandler (PinName selectPin, PinName downPin, 00007 PinName upPin, PinName cancelPin, MenbedButtonHandlerTimespec *timespec, 00008 MenbedNavigator *navigator) : 00009 //select(select), down(down), up(up), cancel(cancel), 00010 timespec(timespec), navigator(navigator) 00011 { 00012 select = new DigitalIn(selectPin); 00013 down = new DigitalIn(downPin); 00014 up = new DigitalIn(upPin); 00015 cancel = new DigitalIn(cancelPin); 00016 00017 numButtons = 4; 00018 00019 select->mode (PullDown); 00020 down->mode (PullDown); 00021 up->mode (PullDown); 00022 cancel->mode (PullDown); 00023 00024 init(); 00025 } 00026 00027 00028 MenbedButtonHandler::MenbedButtonHandler (PinName selectPin, PinName downPin, 00029 PinName upPin, MenbedButtonHandlerTimespec *timespec, 00030 MenbedNavigator *navigator) : 00031 timespec(timespec), navigator(navigator) 00032 { 00033 select = new DigitalIn(selectPin); 00034 down = new DigitalIn(downPin); 00035 up = new DigitalIn(upPin); 00036 00037 numButtons = 3; 00038 00039 select->mode (PullDown); 00040 down->mode (PullDown); 00041 up->mode (PullDown); 00042 00043 init(); 00044 } 00045 00046 00047 MenbedButtonHandler::MenbedButtonHandler (PinName selectPin, PinName downPin, 00048 MenbedButtonHandlerTimespec *timespec, 00049 MenbedNavigator *navigator) : 00050 timespec(timespec), navigator(navigator) 00051 { 00052 select = new DigitalIn(selectPin); 00053 down = new DigitalIn(downPin); 00054 00055 numButtons = 2; 00056 00057 select->mode (PullDown); 00058 down->mode (PullDown); 00059 00060 init(); 00061 } 00062 00063 00064 void MenbedButtonHandler::init() 00065 { 00066 for (int i=0; i<4; i++) 00067 { 00068 buttonDebounced[i] = false; 00069 buttonAlreadyDepressed[i] = false; 00070 } 00071 00072 currentTime_us = 0; 00073 ticker.attach_us (this, &MenbedButtonHandler::tick, 00074 timespec->scanPeriod_us); 00075 } 00076 00077 00078 void MenbedButtonHandler::tick (void) 00079 { 00080 // Accumulated amount of time that buttons have been continuously depressed 00081 uint32_t buttonDepressedTime_us; 00082 bool buttonCurrentlyDepressed; 00083 00084 MenbedButtonEvent buttonEvent; 00085 buttonEvent.numButtons = numButtons; 00086 00087 currentTime_us += timespec->scanPeriod_us; 00088 00089 // Cycle through each of the buttons 00090 for (int i=MenbedButtonEvent::ButtonSelect; 00091 (i<=MenbedButtonEvent::ButtonCancel) && (i<numButtons); 00092 i++) 00093 { 00094 buttonEvent.name = (MenbedButtonEvent::ButtonName)i; 00095 buttonCurrentlyDepressed = 00096 isButtonDepressed((MenbedButtonEvent::ButtonName)i); 00097 00098 // The amount of time the current button has been depressed is 00099 // the current time minus the time which the button was first 00100 // depressed. 00101 buttonDepressedTime_us = currentTime_us - buttonPushedTime_us[i]; 00102 00103 if (buttonCurrentlyDepressed && buttonAlreadyDepressed[i]) 00104 { 00105 if ((buttonDepressedTime_us >= timespec->typematicX10Delay_us) && 00106 (currentTime_us - buttonLastTypematicTime_us[i] >= 00107 timespec->typematicX10Period_us )) 00108 { 00109 buttonLastTypematicTime_us[i] = currentTime_us; 00110 buttonEvent.action = MenbedButtonEvent::BUTTON_ACTION_PUSHED; 00111 navigator->handleButtonEvent (buttonEvent); 00112 } 00113 else if ((buttonDepressedTime_us >= 00114 timespec->typematicPeriod_us) 00115 && (currentTime_us - buttonLastTypematicTime_us[i] >= 00116 timespec->typematicPeriod_us)) 00117 { 00118 buttonLastTypematicTime_us[i] = currentTime_us; 00119 buttonEvent.action = MenbedButtonEvent::BUTTON_ACTION_PUSHED; 00120 navigator->handleButtonEvent (buttonEvent); 00121 } 00122 else if ((buttonDepressedTime_us >= timespec->debounceDelay_us) 00123 && (!buttonDebounced[i])) 00124 { 00125 buttonLastTypematicTime_us[i] = currentTime_us; 00126 buttonDebounced[i] = true; 00127 buttonEvent.action = MenbedButtonEvent::BUTTON_ACTION_PUSHED; 00128 navigator->handleButtonEvent (buttonEvent); 00129 } 00130 00131 if (buttonDebounced[i] && 00132 (buttonDepressedTime_us >= 00133 timespec->longReleaseDelay_us)) 00134 { 00135 buttonEvent.action = MenbedButtonEvent::BUTTON_ACTION_HELD_LONG; 00136 navigator->handleButtonEvent (buttonEvent); 00137 } 00138 00139 buttonAlreadyDepressed[i] = true; 00140 } 00141 // Otherwise, if the button was just depressed, we indicate that it 00142 // has not yet debounced and record the time so that we know when it 00143 // was first pressed. 00144 else if (buttonCurrentlyDepressed && !buttonAlreadyDepressed[i]) 00145 { 00146 buttonDebounced[i] = false; 00147 buttonPushedTime_us[i] = currentTime_us; 00148 00149 buttonAlreadyDepressed[i] = true; 00150 } 00151 // Otherwise, if the button is not depressed but it was previously 00152 // depressed and the amount of time it was depressed exceeds the 00153 // debounce time, then we say the button has been released. 00154 else if (!buttonCurrentlyDepressed && buttonAlreadyDepressed[i]) 00155 { 00156 if (buttonDebounced[i]) 00157 { 00158 if (buttonDepressedTime_us >= 00159 timespec->longReleaseDelay_us) 00160 { 00161 buttonEvent.action = MenbedButtonEvent::BUTTON_ACTION_RELEASED_LONG; 00162 navigator->handleButtonEvent (buttonEvent); 00163 } 00164 else 00165 { 00166 buttonEvent.action = MenbedButtonEvent::BUTTON_ACTION_RELEASED_SHORT; 00167 navigator->handleButtonEvent (buttonEvent); 00168 } 00169 } 00170 00171 buttonAlreadyDepressed[i] = false; 00172 } 00173 } 00174 } 00175 00176 00177 bool MenbedButtonHandler::isButtonDepressed (MenbedButtonEvent::ButtonName name) 00178 { 00179 switch (name) 00180 { 00181 case MenbedButtonEvent::ButtonSelect: 00182 return *select; 00183 case MenbedButtonEvent::ButtonDown: 00184 if (numButtons >= 2) 00185 return *down; 00186 else 00187 return false; 00188 case MenbedButtonEvent::ButtonUp: 00189 if (numButtons >= 3) 00190 return *up; 00191 else 00192 return false; 00193 case MenbedButtonEvent::ButtonCancel: 00194 if (numButtons == 4) 00195 return *cancel; 00196 else 00197 return false; 00198 } 00199 00200 return false; 00201 }
Generated on Thu Jul 14 2022 12:14:44 by
