Comments (13)
In my use case I chose debounce over throttle and it worked great. @fivdi I'd say there is no reason to keep your debounce code if an alternate solution works fine. So I'd close this issue but with a mention of these lodash options in the readme though.
debounce = require('lodash/debounce')
switchPortIntr.watch(
debounce(function(err, value) {
if (err) {
throw err;
}
// whatever you need to do when interrupt happens, but only do it once without "bouncing"
}, 200, {
'leading': false,
'trailing': true
})
)
leading false and trailing true will do it at the end of the 200ms. If you need a faster response try leading true and tailing false. If your interrupting device is really sloppy you might need more that 200ms. I could have used was less 200ms but that is plenty fast enough for a human actuated switch even with trailing true.
from onoff.
Are you using onoff
for buttons or switches? If so, please try gpio-button
instead. It provides better support for such use-cases.
from onoff.
It's a signal from a light stack that goes though a optocopler.
from onoff.
Note that debounceTimeout
isn't documented as it's not really that good.
Can the code be changed to add a readSync after the timeout and fire the watch listeners if the value changed.
If this can be used to resolve the issue then it should be possible to achieve this without modifying onoff
. onoff
could be used without making used of the debounceTimeout
option. The watch
callback in your program can start a timeout and the timeout handler can call readSync
and fire an event or call a callback if the value changed.
from onoff.
Correct I can add the check to my code.
It just seemed the watch
callback was intended to be a "on change" (rising or falling edge) callback and when it gets called with no change in value, it seems to not work as expected.
from onoff.
The problem here is the bounce on the interrupt input. onoff
is notified about the interrupt, but by the time onoff
gets around to reading the corresponding input, it may have changed state. The watch callback is called irrespective of the state of the input.
from onoff.
The onoff
debounce code really isn't the best and should be either improved or removed. I'll try to improve it but it may take a while.
from onoff.
yo. i just made a debouncer based on watch: https://gist.github.com/GottZ/e4be78c928817c6701155f6ae910d841
@fivdi if you'd want me to, i could clean it up and create a pull request. just let me know 👍
video of it in action: https://usercontent.irccloud-cdn.com/file/c8pO8syE/VID-20160729-WA0001.mp4
i'm not using any resistor at all. this button goes directly to ground and gpio 3
any idea how i should name this function?
i'd also need to add a dequeue function like unwatch if this should go in.
or maybe, it would be a good idea to just add this to watcher itself.
let me know about your thoughts.
i'm thinking about something like .watch(func, delay) where delay is 0 ms by default wich means no debouncing.
from onoff.
Hi guys,
I had the same noisy input problem but I solved with lodash.
You can use https://lodash.com/docs#throttle or https://lodash.com/docs#debounce
var last = 0;
sensor.watch(_.throttle(function (err, value) {
if (err) {
// ...
}
if (value != last) {
last = value;
// ...
}
}, 100)); // throttle value
from onoff.
Although debounceTimeout
isn't actually documented, I did suggest that people use it in issues like this. This makes removing the code a little difficult. However, documenting a solution based on lodash or replacing the existing solution with a lodash based solution would be a good idea.
from onoff.
besides lodash there is this one off package by Sindre Sorhus. He has a throttle as well
https://github.com/sindresorhus/p-debounce
from onoff.
I did a package with a node module adding watchFilter usage with onoff package.
I believe it can help others developers with the same problems
https://www.npmjs.com/package/inputmonitor
https://github.com/rafaelquines/inputmonitor
;)
from onoff.
It took a while but this issue has been resolved and onoff v3.0.0 has an effective implementation for debouncing based lodash.debounce.
For more details see here.
An example showing how to debounce buttons can be seen at Debouncing Buttons
Sorry for taking so long to fix this.
from onoff.
Related Issues (20)
- Problem using watch function with relay HOT 10
- Initialize GPIO without change pin status. HOT 6
- GPIO not working in a worker_thread - nodejs 14.15 HOT 2
- EACCES: permission denied, open '/sys/class/gpio/export' as superuser HOT 2
- Cannot run on non Rpi system HOT 1
- TypeError: writeSync is not a function HOT 4
- Raspberry Pi GPIO control in electron App HOT 8
- My computer does not have a /sys/class/gpio directory, but has /dev/gpiochip* HOT 1
- Interrupt misfire HOT 3
- Raspberry 4 - malloc(): corrupted top size HOT 3
- Error: EPERM: operation not permitted, write HOT 4
- Does not work with Compute Module 4. HOT 2
- Event is fired multiple times
- Superb library - I am running on Node v20 HOT 2
- Pi 5 Compatibility issues HOT 9
- Compatibility with ppp asked?
- RPI 5 gpio numbers HOT 2
- fs error HOT 8
- Newer version of epoll
- Button.watch spams endlessly HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from onoff.