Git Product home page Git Product logo

Comments (13)

dkebler avatar dkebler commented on July 22, 2024 1

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.

fivdi avatar fivdi commented on July 22, 2024

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.

thisboyiscrazy avatar thisboyiscrazy commented on July 22, 2024

It's a signal from a light stack that goes though a optocopler.

from onoff.

fivdi avatar fivdi commented on July 22, 2024

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.

thisboyiscrazy avatar thisboyiscrazy commented on July 22, 2024

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.

fivdi avatar fivdi commented on July 22, 2024

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.

fivdi avatar fivdi commented on July 22, 2024

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.

GottZ avatar GottZ commented on July 22, 2024

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.

raffaelecassia avatar raffaelecassia commented on July 22, 2024

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.

fivdi avatar fivdi commented on July 22, 2024

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.

dkebler avatar dkebler commented on July 22, 2024

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.

rafaelquines avatar rafaelquines commented on July 22, 2024

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.

fivdi avatar fivdi commented on July 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.