From 3df5cef97b0e564c3eb7f07b8a4514d307228779 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Sun, 25 Dec 2016 19:39:39 +0100 Subject: [PATCH] Fix #17. Moved static variable 'pending' to class variable '_clicked' --- code/lib/DebounceEvent/DebounceEvent.cpp | 16 +++++++++------- code/lib/DebounceEvent/DebounceEvent.h | 1 + 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/code/lib/DebounceEvent/DebounceEvent.cpp b/code/lib/DebounceEvent/DebounceEvent.cpp index 2ccecb31..fccb41e4 100644 --- a/code/lib/DebounceEvent/DebounceEvent.cpp +++ b/code/lib/DebounceEvent/DebounceEvent.cpp @@ -45,7 +45,6 @@ DebounceEvent::DebounceEvent(uint8_t pin, uint8_t defaultStatus, unsigned long d bool DebounceEvent::loop() { // holds whether status has changed or not - static bool pending = false; bool changed = false; _event = EVENT_NONE; @@ -57,7 +56,7 @@ bool DebounceEvent::loop() { if (newStatus != _status) { changed = true; - pending = false; + _clicked = false; _status = newStatus; // released @@ -69,9 +68,13 @@ bool DebounceEvent::loop() { } else if (millis() - _last_start < DOUBLE_CLICK_DELAY ) { _event = EVENT_DOUBLE_CLICK; } else { + + // We are not setting the event type here because we still don't + // know what kind of event it will be (it might be a double click). + // Instead we are setting the _clicked variable to check later + _clicked = true; changed = false; - pending = true; - //_event = EVENT_SINGLE_CLICK; + } // pressed @@ -86,8 +89,8 @@ bool DebounceEvent::loop() { } } - if (pending && (millis() - _this_start > DOUBLE_CLICK_DELAY) && (!changed) && (_status == _defaultStatus)) { - pending = false; + if (_clicked && (millis() - _this_start > DOUBLE_CLICK_DELAY) && (!changed) && (_status == _defaultStatus)) { + _clicked = false; changed = true; _event = EVENT_SINGLE_CLICK; } @@ -99,7 +102,6 @@ bool DebounceEvent::loop() { } } - return changed; } diff --git a/code/lib/DebounceEvent/DebounceEvent.h b/code/lib/DebounceEvent/DebounceEvent.h index d3014e40..b7b30b51 100644 --- a/code/lib/DebounceEvent/DebounceEvent.h +++ b/code/lib/DebounceEvent/DebounceEvent.h @@ -42,6 +42,7 @@ class DebounceEvent { uint8_t _pin; uint8_t _status; uint8_t _event; + bool _clicked = false; unsigned long _this_start; unsigned long _last_start; uint8_t _defaultStatus;