25 #include "AceButton.h"
27 namespace ace_button {
32 #define CONCAT_(x, y) x##y
33 #define CONCAT(x,y) CONCAT_(x,y)
34 #define COMPILE_TIME_ASSERT(cond, msg) \
35 extern char CONCAT(compile_time_assert, __LINE__)[(cond) ? 1 : -1];
39 COMPILE_TIME_ASSERT(HIGH == 1,
"HIGH must be 1")
40 COMPILE_TIME_ASSERT(LOW == 0, "LOW must be 0")
44 #if ARDUINO_API_VERSION >= 10000
51 "kButtonStateUnknown conflicts with PinStatus enum")
59 mLastDebounceTime = 0;
61 setDefaultReleasedState(defaultReleasedState);
65 uint8_t defaultReleasedState, uint8_t
id) {
66 mButtonConfig = buttonConfig;
67 init(pin, defaultReleasedState,
id);
70 void AceButton::setDefaultReleasedState(uint8_t state) {
72 mFlags |= kFlagDefaultReleasedState;
74 mFlags &= ~kFlagDefaultReleasedState;
79 return (mFlags & kFlagDefaultReleasedState) ? HIGH : LOW;
85 uint8_t buttonState = mButtonConfig->
readButton(mPin);
94 uint16_t now = mButtonConfig->
getClock();
97 if (checkDebounced(now, buttonState)) {
99 if (checkInitialized(buttonState)) {
100 checkEvent(now, buttonState);
105 void AceButton::checkEvent(uint16_t now, uint8_t buttonState) {
119 checkPostponedClick(now);
120 checkOrphanedClick(now);
124 checkLongPress(now, buttonState);
127 checkRepeatPress(now, buttonState);
130 checkChanged(now, buttonState);
134 bool AceButton::checkDebounced(uint16_t now, uint8_t buttonState) {
135 if (isDebouncing()) {
146 uint16_t elapsedTime = now - mLastDebounceTime;
148 bool isDebouncingTimeOver =
151 if (isDebouncingTimeOver) {
167 mLastDebounceTime = now;
172 bool AceButton::checkInitialized(uint16_t buttonState) {
183 mLastButtonState = buttonState;
187 void AceButton::checkLongPress(uint16_t now, uint8_t buttonState) {
192 if (isPressed() && !isLongPressed()) {
193 uint16_t elapsedTime = now - mLastPressTime;
201 void AceButton::checkRepeatPress(uint16_t now, uint8_t buttonState) {
207 if (isRepeatPressed()) {
208 uint16_t elapsedTime = now - mLastRepeatPressTime;
211 mLastRepeatPressTime = now;
214 uint16_t elapsedTime = now - mLastPressTime;
220 mLastRepeatPressTime = now;
226 void AceButton::checkChanged(uint16_t now, uint8_t buttonState) {
227 mLastButtonState = buttonState;
228 checkPressed(now, buttonState);
229 checkReleased(now, buttonState);
232 void AceButton::checkPressed(uint16_t now, uint8_t buttonState) {
238 mLastPressTime = now;
243 void AceButton::checkReleased(uint16_t now, uint8_t buttonState) {
256 bool wasLongPressed = isLongPressed();
263 (isRepeatPressed() &&
268 (isDoubleClicked() &&
277 clearDoubleClicked();
279 clearRepeatPressed();
284 if (wasLongPressed) {
292 void AceButton::checkClicked(uint16_t now) {
301 uint16_t elapsedTime = now - mLastPressTime;
309 checkDoubleClicked(now);
315 if (isDoubleClicked()) {
321 mLastClickTime = now;
331 void AceButton::checkDoubleClicked(uint16_t now) {
333 clearDoubleClicked();
337 uint16_t elapsedTime = now - mLastClickTime;
339 clearDoubleClicked();
349 if (isClickPostponed()) {
350 clearClickPostponed();
356 void AceButton::checkOrphanedClick(uint16_t now) {
367 uint16_t elapsedTime = now - mLastClickTime;
368 if (isClicked() && (elapsedTime >= orphanedClickDelay)) {
373 void AceButton::checkPostponedClick(uint16_t now) {
375 uint16_t elapsedTime = now - mLastClickTime;
376 if (isClickPostponed() && elapsedTime >= postponedClickDelay) {
378 clearClickPostponed();
382 void AceButton::handleEvent(uint8_t eventType) {