periph / conn

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

PulseIn #17

Closed 1995parham closed 2 years ago

1995parham commented 2 years ago

What kind of new feature are you looking for?

func PulseIn(pin gpio.PinIn, lvl gpio.Level, t time.Duration) (time.Duration, error) {
    var e1, e2 gpio.Edge

    if lvl == gpio.High {
        e1 = gpio.RisingEdge
        e2 = gpio.FallingEdge
    } else {
        e1 = gpio.FallingEdge
        e2 = gpio.RisingEdge
    }

    if err := pin.In(gpio.PullNoChange, e1); err != nil {
        return 0, fmt.Errorf("pin %s in setup failed %w", pin, err)
    }

    pin.WaitForEdge(t)

    now := time.Now()

    if err := pin.In(gpio.PullNoChange, e2); err != nil {
        return 0, fmt.Errorf("pin %s in setup failed %w", pin, err)
    }

    pin.WaitForEdge(t)

    return time.Since(now), nil
}

Do you plan to:

if you agree with the function existence in gpioutil I can contribute and add it.

maruel commented 2 years ago

Ok. No need to wrap the error, you can return the pin.In() error as-is.

1995parham commented 2 years ago

Thanks for the point, so I will create a PR for this.