15
15
* #include "EasyButtonAtInt01.hpp"
16
16
* EasyButton Button0AtPin2(true);
17
17
*
18
- * Copyright (C) 2018-2022 Armin Joachimsmeyer
18
+ * Copyright (C) 2018-2024 Armin Joachimsmeyer
19
19
20
20
*
21
21
* This file is part of EasyButtonAtInt01 https://github.com/ArminJo/EasyButtonAtInt01.
@@ -222,6 +222,12 @@ void EasyButton::init(bool aIsButtonAtINT0) {
222
222
sPointerToButton0ForISR = this ;
223
223
# if defined(USE_ATTACH_INTERRUPT)
224
224
attachInterrupt (digitalPinToInterrupt (INT0_PIN), &handleINT0Interrupt, CHANGE);
225
+
226
+ # elif defined(USE_INT2_FOR_BUTTON_0)
227
+ EICRA |= _BV (ISC20); // interrupt on any logical change
228
+ EIFR |= _BV (INTF2);// clear interrupt bit
229
+ EIMSK |= _BV (INT2);// enable interrupt on next change
230
+
225
231
# else
226
232
EICRA |= _BV (ISC00); // interrupt on any logical change
227
233
EIFR |= _BV (INTF0);// clear interrupt bit
@@ -340,7 +346,9 @@ void EasyButton::init(bool aIsButtonAtINT0) {
340
346
}
341
347
342
348
/*
343
- * Negative logic for readButtonState() true means button pin is LOW, if button is active low (default)
349
+ * if NOT defined BUTTON_IS_ACTIVE_HIGH we have negative logic for readButtonState()!
350
+ * In this case BUTTON_IS_ACTIVE (true) means button pin is LOW
351
+ * @return BUTTON_IS_ACTIVE (true) or BUTTON_IS_INACTIVE (false)
344
352
*/
345
353
bool EasyButton::readButtonState () {
346
354
#if defined(USE_BUTTON_0) && not defined(USE_BUTTON_1)
@@ -384,11 +392,14 @@ bool EasyButton::getButtonStateIsActive() {
384
392
}
385
393
/*
386
394
* Returns stored state if in debouncing period otherwise current state of button
395
+ * If button is in bouncing period, we do not know button state, so it is only save to return BUTTON_IS_INACTIVE
396
+ * @return BUTTON_IS_ACTIVE (true) or BUTTON_IS_INACTIVE (false)
387
397
*/
388
398
bool EasyButton::readDebouncedButtonState () {
389
- // Check for bouncing period
399
+ // Check if we are in bouncing period
390
400
if (millis () - ButtonLastChangeMillis <= BUTTON_DEBOUNCING_MILLIS) {
391
- return ButtonStateIsActive;
401
+ // If button is in bouncing period, we do not know button state, so it is only save to return BUTTON_IS_INACTIVE
402
+ return BUTTON_IS_INACTIVE;
392
403
}
393
404
return readButtonState ();
394
405
}
@@ -445,14 +456,15 @@ uint16_t EasyButton::updateButtonPressDuration() {
445
456
*/
446
457
uint8_t EasyButton::checkForLongPress (uint16_t aLongPressThresholdMillis) {
447
458
uint8_t tRetvale = EASY_BUTTON_LONG_PRESS_ABORT;
448
- if (readDebouncedButtonState ()) {
459
+ // noInterrupts() is required, since otherwise we may get wrong results if interrupted during processing by button ISR
460
+ noInterrupts ();
461
+ if (readDebouncedButtonState () != BUTTON_IS_INACTIVE) {
449
462
// Button still active -> update current ButtonPressDurationMillis
450
- // noInterrupts() is required, since otherwise we may get wrong results if interrupted during load of long value by button ISR
451
- noInterrupts ();
463
+
452
464
ButtonPressDurationMillis = millis () - ButtonLastChangeMillis;
453
- interrupts ();
454
465
tRetvale = EASY_BUTTON_LONG_PRESS_STILL_POSSIBLE; // if not detected, you may try again
455
466
}
467
+ interrupts ();
456
468
if (ButtonPressDurationMillis >= aLongPressThresholdMillis) {
457
469
// long press detected
458
470
return EASY_BUTTON_LONG_PRESS_DETECTED;
@@ -716,8 +728,8 @@ void __attribute__ ((weak)) handleINT1Interrupt() {
716
728
// ISR for PIN PD2
717
729
// Cannot make the vector itself weak, since the vector table is already filled by weak vectors resulting in ignoring my weak one:-(
718
730
// ISR(INT0_vect, __attribute__ ((weak))) {
719
- # if defined(USE_BUTTON_0 )
720
- ISR (INT0_vect ) {
731
+ # if defined(USE_INT2_FOR_BUTTON_0 )
732
+ ISR (INT2_vect ) {
721
733
# if defined(MEASURE_EASY_BUTTON_INTERRUPT_TIMING)
722
734
digitalWriteFast (INTERRUPT_TIMING_OUTPUT_PIN, HIGH);
723
735
# endif
@@ -726,6 +738,18 @@ ISR(INT0_vect) {
726
738
digitalWriteFast (INTERRUPT_TIMING_OUTPUT_PIN, LOW);
727
739
# endif
728
740
}
741
+ # else
742
+ # if defined(USE_BUTTON_0)
743
+ ISR (INT0_vect) {
744
+ # if defined(MEASURE_EASY_BUTTON_INTERRUPT_TIMING)
745
+ digitalWriteFast (INTERRUPT_TIMING_OUTPUT_PIN, HIGH);
746
+ # endif
747
+ handleINT0Interrupt ();
748
+ # if defined(MEASURE_EASY_BUTTON_INTERRUPT_TIMING)
749
+ digitalWriteFast (INTERRUPT_TIMING_OUTPUT_PIN, LOW);
750
+ # endif
751
+ }
752
+ # endif
729
753
# endif
730
754
731
755
# if defined(USE_BUTTON_1)
0 commit comments