Skip to content

Commit

Permalink
Add function to measure how long a switch has been on for.
Browse files Browse the repository at this point in the history
  • Loading branch information
blackketter committed Dec 2, 2018
1 parent 6d457ef commit 419268d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Switch.cpp
Expand Up @@ -147,3 +147,15 @@ void Switch::setDoubleClickCallback(switchCallback_t cb, void* param)
_doubleClickCallback = cb;
_doubleClickCallbackParam = param;
}

unsigned long Switch::pushedDuration() {
if (on()) {
unsigned long dur = millis() - pushedTime;
if (!dur) {
dur = 1; // minimum 1ms
}
return dur;
} else {
return 0;
}
}
2 changes: 2 additions & 0 deletions Switch.h
Expand Up @@ -27,6 +27,8 @@ class Switch

unsigned long _switchedTime, pushedTime;

unsigned long pushedDuration(); // returns duration of press in milliseconds, 0 if not pressed

// Set methods for event callbacks
void setPushedCallback(switchCallback_t cb, void* param = nullptr);
void setReleasedCallback(switchCallback_t cb, void* param = nullptr);
Expand Down

0 comments on commit 419268d

Please sign in to comment.