periph / conn

Go·Hardware·Lean - Interfaces
https://periph.io
Apache License 2.0
74 stars 13 forks source link

Debounce has issues and can be more accurate and precise #38

Open chran554 opened 1 year ago

chran554 commented 1 year ago

Problem There are issues with the current implementation of debounce. All of them are not severe but the implementation can be more accurate and precise.

Current implementation of debounce use sleep Current implementation of debounce use two parameters (both denoise and debounce) Current implementation do not wait for bounce to subside but merely wait yet another duration (denoise) after first edge and hope bouncing is all done. Attribute debounce is never used. Parameter "edge" in constructor is never used.

What kind of new feature are you looking for? (Keep the one that applies, please describe)

Do you plan to:

chran554 commented 1 year ago

Implementation suggestion for debounced WaitForEdge and Read (work in progress, not final!).

// debounced is a gpio.PinIO where reading and edge detection pass through a
// debouncing algorithm.
type debounced struct {
    // Immutable.
    gpio.PinIO
    // debounce is the time to wait for a changing signal to stabilize
    // if say a button introduces signal chatter.
    debounce time.Duration

    // Mutable.
    clock clockwork.Clock
}
// WaitForEdge implements gpio.PinIO.
//
// Waits for the edge of a changed (stable) signal.
// Any intermittent signal chatter from bounce is filtered even though the stable signal does not change.
// A signal is considered stable when there is no new signal change (edge) for a debounce amount of time.
func (d *debounced) WaitForEdge(timeout time.Duration) bool {
    start := d.clock.Now()

    // Cannot decide if signal change is stable if timeout is less than debounce time.
    if (timeout != -1) && (timeout < d.debounce) {
        return false
    }

    initialLevel := d.PinIO.Read()

    for timeLeft := max(0, timeout-d.clock.Since(start)); (timeout == -1) || (timeLeft >= d.debounce); timeLeft = max(0, timeout-d.clock.Since(start)) {
        if timeout == -1 {
            timeLeft = -1
        }

        // Wait for initial change of signal (edge)
        if !d.PinIO.WaitForEdge(timeLeft) {
            return false
        }

        // Wait for signal (edge) bounce to subside.
        // Bounce/edge chatter is considered filtered and the signal stable
        // when debounce delay has surpassed without any further signal change (edges).
        //
        // Debounce wait can be applied several consecutive times (until timeout kicks in)
        // as long as new signal changes occur during the debounce period.
        for (timeout == -1) || ((timeout - d.clock.Since(start)) >= d.debounce) {
            if !d.PinIO.WaitForEdge(d.debounce) {
                // Stable signal is achieved
                currentLevel := d.PinIO.Read()
                if currentLevel != initialLevel {
                    // An edge is only detected in the stable signal (after bounce filtering)
                    // if the signal is changed from its initial level.
                    return true
                } else {
                    // Stable signal does not differ from the initial level
                    break // Break debounce loop and continue another round and wait for a new initial signal change
                }
            }
        }
    }

    return false
}
// Read implements gpio.PinIO.
//
// Reads the value of stable signal.
// Note that this read always takes at least debounce amount of time.
//
// A signal is considered stable when there is no new signal edge for a debounce amount of time.
func (d *debounced) Read() gpio.Level {
    for d.PinIO.WaitForEdge(d.debounce) {
        // Nothing by intention
    }
    return d.PinIO.Read()
}