Skip to content

Commit a0cdb6b

Browse files
committed
Autorelease revised
1 parent 713621a commit a0cdb6b

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

library.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "Button_SL",
3-
"version": "1.1.4",
3+
"version": "1.1.5",
44
"keywords": "button, debounce, signal, input, ouput",
55
"description": "Button_SL enables the query of buttons. The query is debounced. A query can be made as to whether the button was pressed for a short or long time.",
66
"repository": {

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Button_SL
2-
version=1.1.4
2+
version=1.1.5
33
author=Kai R.
44
maintainer=Kai R.
55
sentence=Button query

src/Button_SL.cpp

+11-5
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,17 @@
1818
/// @version 1.0.1 change CAPS Enums
1919
///
2020
/// @date 2022-14-11
21-
/// @version 1.1.0 The ButtonSL class can now provide a status after the specified time period
21+
/// @version 1.1.0 The ButtonSL class can now provide a status after the specified time period
2222
/// for a long button press, even if the button remains permanently pressed.
2323
///
2424
/// @date 2023-12-06
2525
/// @version 1.1.4 Repeated debounce in the button class removed.
2626
///
27+
/// @date 2024-07-06
28+
/// @version 1.1.5 The autorelease is only executed once as long as the button has not been released.
29+
/// Previously, if the button was held down continuously, the autorelease was executed
30+
/// every time the time for a long button press expired.
31+
///
2732
/// @copyright Copyright (c) 2022
2833
/// MIT license, check license.md for more information
2934
/// All text above must be included in any redistribution
@@ -58,7 +63,7 @@ bool Button::tick() {
5863
case HIGH:
5964
if (millis() - timeStamp > dbTime_ms) {
6065
// If the button press is equal to the specified debounce time, confirm the button press with true.
61-
flag = true;
66+
flag = true;
6267
}
6368
break;
6469
}
@@ -87,15 +92,16 @@ ButtonState ButtonSL::tick() {
8792
state = digitalRead(pin);
8893
if (state == activeState && compareState != activeState) {
8994
timeStamp = now;
95+
hasReleased = false;
9096
} else if (state != activeState && compareState == activeState) {
9197
timeStamp = now - timeStamp;
9298
if (timeStamp >= dbTime_ms && !hasReleased) { // released after debounce time?
9399
return (timeStamp >= time_ms) ? ButtonState::longPressed : ButtonState::shortPressed;
94100
}
95-
hasReleased = false;
96-
} else if (release && (compareState == activeState)) {
101+
// hasReleased = false;
102+
} else if (autoRelease && not hasReleased && (compareState == activeState)) {
97103
if (now - timeStamp >= time_ms) {
98-
state = !compareState;
104+
// state = !compareState;
99105
hasReleased = true;
100106
timeStamp = time_ms;
101107
return ButtonState::longPressed;

src/Button_SL.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ class ButtonSL : public Button {
6666
ButtonSL(uint8_t pinNr = 0, uint16_t t_ms = 1000, bool as = LOW) : Button{pinNr, as} { time_ms = t_ms; }
6767
ButtonState tick();
6868
void setTimeThreshold_ms(uint16_t);
69-
void releaseOn() { release = true; }
70-
void releaseOff() { release = false; }
69+
void releaseOn() { autoRelease = true; }
70+
void releaseOff() { autoRelease = false; }
7171
uint32_t getDuration_ms() const;
7272

7373
private:
7474
uint16_t time_ms; // Saves the time (in ms) from which a key press is recognized as long.
7575
uint8_t state{!activeState};
7676
uint16_t pressingTime; // Saves the length of time that the button was pressed (ms).
77-
bool release{false};
77+
bool autoRelease{false};
7878
bool hasReleased{false};
7979
};
8080
} // namespace Btn

0 commit comments

Comments
 (0)