Skip to content

Commit

Permalink
Merge pull request #4 from cederstrom/feature/prevent-bouncing
Browse files Browse the repository at this point in the history
Feature/prevent bouncing
  • Loading branch information
Jopyth authored Jun 10, 2017
2 parents 2a00451 + 8d64c75 commit 832e462
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
3 changes: 2 additions & 1 deletion MMM-Buttons.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ Module.register("MMM-Buttons", {
buttons: [],
minShortPressTime: 0,
maxShortPressTime: 500,
minLongPressTime: 3000
minLongPressTime: 3000,
bounceTimeout: 300
},

// Define start sequence.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ Here is full documentation of options for the modules configuration:
| `minShortPressTime` | Minimum duration to trigger a short press in `ms`. Default is `0`. |
| `maxShortPressTime` | Maximum duration to trigger a short press in `ms`. Default is `500`. |
| `minLongPressTime` | Minimum time needed to trigger a long press in `ms`. Default is `3000`. Any press duration between `maxShortPressTime` and `minLongPressTime` does not do anything. |
| `bounceTimeout` | Duration to ignore bouncing (unintentional doubble press on the button). |

### Button Configuration

Expand Down
19 changes: 15 additions & 4 deletions node_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,31 @@ module.exports = NodeHelper.create({

return function (err, value) {
if (value == 1) {
self.buttons[index].pressed = new Date().getTime();
var start = new Date().getTime();
if (self.buttons[index].downBounceTimeoutEnd > start) {
// We're bouncing!
return;
}

self.buttons[index].pressed = start;
self.buttons[index].downBounceTimeoutEnd = start + self.config.bounceTimeout;
self.sendSocketNotification("BUTTON_DOWN", {
index: index
});
setTimeout(self.watchHandler(index), self.config.minLongPressTime, undefined, 0);
return;
}
if (value == 0 && self.buttons[index].pressed !== undefined) {
var start = self.buttons[index].pressed;
var end = new Date().getTime();
var time = end - start;
var end = new Date().getTime();
if (self.buttons[index].upBounceTimeoutEnd > end) {
// We're bouncing!
return;
}

self.buttons[index].pressed = undefined;
self.buttons[index].upBounceTimeoutEnd = end + self.config.bounceTimeout;

var time = end - start;
self.sendSocketNotification("BUTTON_UP", {
index: index,
duration: time
Expand Down

0 comments on commit 832e462

Please sign in to comment.