Skip to content

Commit

Permalink
Autorelease revised
Browse files Browse the repository at this point in the history
  • Loading branch information
DoImant committed Jun 7, 2024
1 parent 713621a commit a0cdb6b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Button_SL",
"version": "1.1.4",
"version": "1.1.5",
"keywords": "button, debounce, signal, input, ouput",
"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.",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Button_SL
version=1.1.4
version=1.1.5
author=Kai R.
maintainer=Kai R.
sentence=Button query
Expand Down
16 changes: 11 additions & 5 deletions src/Button_SL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@
/// @version 1.0.1 change CAPS Enums
///
/// @date 2022-14-11
/// @version 1.1.0 The ButtonSL class can now provide a status after the specified time period
/// @version 1.1.0 The ButtonSL class can now provide a status after the specified time period
/// for a long button press, even if the button remains permanently pressed.
///
/// @date 2023-12-06
/// @version 1.1.4 Repeated debounce in the button class removed.
///
/// @date 2024-07-06
/// @version 1.1.5 The autorelease is only executed once as long as the button has not been released.
/// Previously, if the button was held down continuously, the autorelease was executed
/// every time the time for a long button press expired.
///
/// @copyright Copyright (c) 2022
/// MIT license, check license.md for more information
/// All text above must be included in any redistribution
Expand Down Expand Up @@ -58,7 +63,7 @@ bool Button::tick() {
case HIGH:
if (millis() - timeStamp > dbTime_ms) {
// If the button press is equal to the specified debounce time, confirm the button press with true.
flag = true;
flag = true;
}
break;
}
Expand Down Expand Up @@ -87,15 +92,16 @@ ButtonState ButtonSL::tick() {
state = digitalRead(pin);
if (state == activeState && compareState != activeState) {
timeStamp = now;
hasReleased = false;
} else if (state != activeState && compareState == activeState) {
timeStamp = now - timeStamp;
if (timeStamp >= dbTime_ms && !hasReleased) { // released after debounce time?
return (timeStamp >= time_ms) ? ButtonState::longPressed : ButtonState::shortPressed;
}
hasReleased = false;
} else if (release && (compareState == activeState)) {
// hasReleased = false;
} else if (autoRelease && not hasReleased && (compareState == activeState)) {
if (now - timeStamp >= time_ms) {
state = !compareState;
// state = !compareState;
hasReleased = true;
timeStamp = time_ms;
return ButtonState::longPressed;
Expand Down
6 changes: 3 additions & 3 deletions src/Button_SL.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ class ButtonSL : public Button {
ButtonSL(uint8_t pinNr = 0, uint16_t t_ms = 1000, bool as = LOW) : Button{pinNr, as} { time_ms = t_ms; }
ButtonState tick();
void setTimeThreshold_ms(uint16_t);
void releaseOn() { release = true; }
void releaseOff() { release = false; }
void releaseOn() { autoRelease = true; }
void releaseOff() { autoRelease = false; }
uint32_t getDuration_ms() const;

private:
uint16_t time_ms; // Saves the time (in ms) from which a key press is recognized as long.
uint8_t state{!activeState};
uint16_t pressingTime; // Saves the length of time that the button was pressed (ms).
bool release{false};
bool autoRelease{false};
bool hasReleased{false};
};
} // namespace Btn
Expand Down

0 comments on commit a0cdb6b

Please sign in to comment.