From d576f39ed5247e6dbc4c8457773c891408b4ae10 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 4 Nov 2018 18:58:02 -0500 Subject: [PATCH 1/2] Add user read function --- src/clickButton.cpp | 106 ++++++++++++++++++++++++++++---------------- src/clickButton.h | 20 +++++---- 2 files changed, 79 insertions(+), 47 deletions(-) diff --git a/src/clickButton.cpp b/src/clickButton.cpp index da09877..35dc071 100644 --- a/src/clickButton.cpp +++ b/src/clickButton.cpp @@ -1,18 +1,18 @@ /* ClickButton - + A library that decodes multiple clicks on one button. Also copes with long clicks and click-and-hold. - + Usage: ClickButton buttonObject(pin [LOW/HIGH, [CLICKBTN_PULLUP]]); - + where LOW/HIGH denotes active LOW or HIGH button (default is LOW) CLICKBTN_PULLUP is only possible with active low buttons. - + Returned click counts: A positive number denotes the number of (short) clicks after a released button A negative number denotes the number of "long" clicks - + NOTE! This is the OPPOSITE/negative of click codes from the last pre-2013 versions! (this seemed more logical and simpler, so I finally changed it) @@ -22,17 +22,17 @@ NOTE! Copyright (C) 2010,2012, 2013 raron GNU GPLv3 license - + This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - + You should have received a copy of the GNU General Public License along with this program. If not, see . @@ -51,6 +51,23 @@ NOTE! #include "clickButton.h" +ClickButton::ClickButton(bool (*readFunc)(uint8_t), uint8_t pin, bool activeType, uint8_t minTimeBetweenReads){ + _pin = pin; + _activeHigh = activeType; // Assume active-low button + _btnState = !_activeHigh; // initial button state in active-high logic + _lastState = _btnState; + _clickCount = 0; + clicks = 0; + depressed = false; + _lastBounceTime= 0; + debounceTime = 20; // Debounce timer in ms + multiclickTime = 250; // Time limit for multi clicks + longClickTime = 1000; // time until long clicks register + + _minTimeBetweenReads = minTimeBetweenReads; + _readFunction = readFunc; +} + ClickButton::ClickButton(uint8_t buttonPin) { _pin = buttonPin; @@ -110,38 +127,49 @@ ClickButton::ClickButton(uint8_t buttonPin, boolean activeType, boolean internal void ClickButton::Update() { long now = (long)millis(); // get current time - _btnState = digitalRead(_pin); // current appearant button state - - // Make the button logic active-high in code - if (!_activeHigh) _btnState = !_btnState; - - // If the switch changed, due to noise or a button press, reset the debounce timer - if (_btnState != _lastState) _lastBounceTime = now; - - - // debounce the button (Check if a stable, changed state has occured) - if (now - _lastBounceTime > debounceTime && _btnState != depressed) - { - depressed = _btnState; - if (depressed) _clickCount++; + bool didRead = false; + if(_readFunction){ + if(now - _lastRead > _minTimeBetweenReads){ + _lastRead = now; + _btnState = _readFunction(_pin); + didRead = true; + } + } else { + _btnState = pinReadFast(_pin); // current appearant button state + didRead = true; } - // If the button released state is stable, report nr of clicks and start new cycle - if (!depressed && (now - _lastBounceTime) > multiclickTime) - { - // positive count for released buttons - clicks = _clickCount; - _clickCount = 0; + if(didRead){ + // Make the button logic active-high in code + if (!_activeHigh) _btnState = !_btnState; + + // If the switch changed, due to noise or a button press, reset the debounce timer + if (_btnState != _lastState) _lastBounceTime = now; + + + // debounce the button (Check if a stable, changed state has occured) + if (now - _lastBounceTime > debounceTime && _btnState != depressed) + { + depressed = _btnState; + if (depressed) _clickCount++; + } + + // If the button released state is stable, report nr of clicks and start new cycle + if (!depressed && (now - _lastBounceTime) > multiclickTime) + { + // positive count for released buttons + clicks = _clickCount; + _clickCount = 0; + } + + // Check for "long click" + if (depressed && (now - _lastBounceTime > longClickTime)) + { + // negative count for long clicks + clicks = 0 - _clickCount; + _clickCount = 0; + } + + _lastState = _btnState; } - - // Check for "long click" - if (depressed && (now - _lastBounceTime > longClickTime)) - { - // negative count for long clicks - clicks = 0 - _clickCount; - _clickCount = 0; - } - - _lastState = _btnState; } - diff --git a/src/clickButton.h b/src/clickButton.h index 4791da4..54c11a5 100644 --- a/src/clickButton.h +++ b/src/clickButton.h @@ -1,18 +1,18 @@ /* ClickButton - + A library that decodes multiple clicks on one button. Also copes with long clicks and click-and-hold. - + Usage: ClickButton buttonObject(pin [LOW/HIGH, [CLICKBTN_PULLUP]]); - + where LOW/HIGH denotes active LOW or HIGH button (default is LOW) CLICKBTN_PULLUP is only possible with active low buttons. - + Returned click counts: A positive number denotes the number of (short) clicks after a released button A negative number denotes the number of "long" clicks - + NOTE! This is the OPPOSITE/negative of click codes from the last pre-2013 versions! (this seemed more logical and simpler, so I finally changed it) @@ -22,17 +22,17 @@ NOTE! Copyright (C) 2010,2012, 2013 raron GNU GPLv3 license - + This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - + You should have received a copy of the GNU General Public License along with this program. If not, see . @@ -57,6 +57,7 @@ NOTE! class ClickButton { public: + ClickButton(bool (*readFunc)(uint8_t), uint8_t pin, bool activeType, uint8_t minTimeBetweenReads); ClickButton(uint8_t buttonPin); ClickButton(uint8_t buttonPin, boolean active); ClickButton(uint8_t buttonPin, boolean active, boolean internalPullup); @@ -73,6 +74,9 @@ class ClickButton boolean _lastState; // previous button reading int _clickCount; // Number of button clicks within multiclickTime milliseconds long _lastBounceTime; // the last time the button input pin was toggled, due to noise or a press + bool (*_readFunction)(uint8_t) = NULL; // read function, must have single paramter uint8_t and return bool + uint8_t _minTimeBetweenReads = 0; + uint32_t _lastRead; }; // -------- end clickButton.h -------- From 690be1e4d30289cd3d9d866edfa8cceb4e38074e Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 3 Feb 2019 17:11:56 -0500 Subject: [PATCH 2/2] Remove min time between reads for user defined function --- src/clickButton.cpp | 7 +------ src/clickButton.h | 3 +-- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/src/clickButton.cpp b/src/clickButton.cpp index 35dc071..b4030d9 100644 --- a/src/clickButton.cpp +++ b/src/clickButton.cpp @@ -51,7 +51,7 @@ NOTE! #include "clickButton.h" -ClickButton::ClickButton(bool (*readFunc)(uint8_t), uint8_t pin, bool activeType, uint8_t minTimeBetweenReads){ +ClickButton::ClickButton(bool (*readFunc)(uint8_t), uint8_t pin, bool activeType){ _pin = pin; _activeHigh = activeType; // Assume active-low button _btnState = !_activeHigh; // initial button state in active-high logic @@ -63,8 +63,6 @@ ClickButton::ClickButton(bool (*readFunc)(uint8_t), uint8_t pin, bool activeType debounceTime = 20; // Debounce timer in ms multiclickTime = 250; // Time limit for multi clicks longClickTime = 1000; // time until long clicks register - - _minTimeBetweenReads = minTimeBetweenReads; _readFunction = readFunc; } @@ -129,11 +127,8 @@ void ClickButton::Update() long now = (long)millis(); // get current time bool didRead = false; if(_readFunction){ - if(now - _lastRead > _minTimeBetweenReads){ - _lastRead = now; _btnState = _readFunction(_pin); didRead = true; - } } else { _btnState = pinReadFast(_pin); // current appearant button state didRead = true; diff --git a/src/clickButton.h b/src/clickButton.h index 54c11a5..25e968d 100644 --- a/src/clickButton.h +++ b/src/clickButton.h @@ -57,7 +57,7 @@ NOTE! class ClickButton { public: - ClickButton(bool (*readFunc)(uint8_t), uint8_t pin, bool activeType, uint8_t minTimeBetweenReads); + ClickButton(bool (*readFunc)(uint8_t), uint8_t pin, bool activeType); ClickButton(uint8_t buttonPin); ClickButton(uint8_t buttonPin, boolean active); ClickButton(uint8_t buttonPin, boolean active, boolean internalPullup); @@ -75,7 +75,6 @@ class ClickButton int _clickCount; // Number of button clicks within multiclickTime milliseconds long _lastBounceTime; // the last time the button input pin was toggled, due to noise or a press bool (*_readFunction)(uint8_t) = NULL; // read function, must have single paramter uint8_t and return bool - uint8_t _minTimeBetweenReads = 0; uint32_t _lastRead; };