periph/conn

PulseIn

Closed this issue · 2 comments

What kind of new feature are you looking for?

  • Software: PulseIn is a function for detecting the duration of high or low signal over input pin. it also exists on Arduino library and can be defined as follow:
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:

  • Contribute an initial package: Yes
  • Write unit tests: Yes

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

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

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