25 #include "TimingStats.h" 26 #include "AceButton.h" 33 #error HIGH must be defined to be 1 36 #error LOW must be defined to be 0 41 init(pin, defaultReleasedState,
id);
45 mButtonConfig(buttonConfig) {
54 mLastDebounceTime = 0;
56 setDefaultReleasedState(defaultReleasedState);
59 void AceButton::setDefaultReleasedState(uint8_t state) {
61 mFlags |= kFlagDefaultReleasedState;
63 mFlags &= ~kFlagDefaultReleasedState;
68 return (mFlags & kFlagDefaultReleasedState) ? HIGH : LOW;
81 uint16_t now = mButtonConfig->
getClock();
83 uint8_t buttonState = mButtonConfig->
readButton(mPin);
86 if (checkDebounced(now, buttonState)) {
88 if (checkInitialized(buttonState)) {
89 checkEvent(now, buttonState);
94 if (stats !=
nullptr) {
95 uint16_t elapsedMicros = mButtonConfig->
getClockMicros() - nowMicros;
96 stats->update(elapsedMicros);
100 void AceButton::checkEvent(uint16_t now, uint8_t buttonState) {
114 checkPostponedClick(now);
115 checkOrphanedClick(now);
119 checkLongPress(now, buttonState);
122 checkRepeatPress(now, buttonState);
125 checkChanged(now, buttonState);
129 bool AceButton::checkDebounced(uint16_t now, uint8_t buttonState) {
130 if (isDebouncing()) {
141 uint16_t elapsedTime = now - mLastDebounceTime;
143 bool isDebouncingTimeOver =
146 if (isDebouncingTimeOver) {
162 mLastDebounceTime = now;
167 bool AceButton::checkInitialized(uint16_t buttonState) {
178 mLastButtonState = buttonState;
182 void AceButton::checkLongPress(uint16_t now, uint8_t buttonState) {
187 if (isPressed() && !isLongPressed()) {
188 uint16_t elapsedTime = now - mLastPressTime;
196 void AceButton::checkRepeatPress(uint16_t now, uint8_t buttonState) {
202 if (isRepeatPressed()) {
203 uint16_t elapsedTime = now - mLastRepeatPressTime;
206 mLastRepeatPressTime = now;
209 uint16_t elapsedTime = now - mLastPressTime;
215 mLastRepeatPressTime = now;
221 void AceButton::checkChanged(uint16_t now, uint8_t buttonState) {
222 mLastButtonState = buttonState;
223 checkPressed(now, buttonState);
224 checkReleased(now, buttonState);
227 void AceButton::checkPressed(uint16_t now, uint8_t buttonState) {
233 mLastPressTime = now;
238 void AceButton::checkReleased(uint16_t now, uint8_t buttonState) {
255 (isRepeatPressed() &&
260 (isDoubleClicked() &&
266 clearDoubleClicked();
268 clearRepeatPressed();
275 void AceButton::checkClicked(uint16_t now) {
284 uint16_t elapsedTime = now - mLastPressTime;
292 checkDoubleClicked(now);
298 if (isDoubleClicked()) {
304 mLastClickTime = now;
314 void AceButton::checkDoubleClicked(uint16_t now) {
316 clearDoubleClicked();
320 uint16_t elapsedTime = now - mLastClickTime;
322 clearDoubleClicked();
332 if (isClickPostponed()) {
333 clearClickPostponed();
339 void AceButton::checkOrphanedClick(uint16_t now) {
350 uint16_t elapsedTime = now - mLastClickTime;
351 if (isClicked() && (elapsedTime >= orphanedClickDelay)) {
356 void AceButton::checkPostponedClick(uint16_t now) {
358 uint16_t elapsedTime = now - mLastClickTime;
359 if (isClickPostponed() && elapsedTime >= postponedClickDelay) {
361 clearClickPostponed();
365 void AceButton::handleEvent(uint8_t eventType) {