gpioutil

package
v3.8.0+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 30, 2021 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package gpioutil includes utilities to filter or augment GPIOs.

Example
package main

import (
	"fmt"
	"log"
	"time"

	"periph.io/x/periph/conn/gpio"
	"periph.io/x/periph/conn/gpio/gpioreg"
	"periph.io/x/periph/conn/physic"
	"periph.io/x/periph/experimental/conn/gpio/gpioutil"
)

func main() {
	// Complete solution:
	// - Fallback to software polling if the GPIO doesn't support hardware edge
	//   detection.
	// - Denoise and debounce the reading.
	//
	// Order is important, as Debounce() requires working edge detection.
	p := gpioreg.ByName("XOI-P1")
	if p != nil {
		log.Fatal("please open another GPIO")
	}
	if err := p.In(gpio.PullDown, gpio.BothEdges); err == nil {
		// Try to fallback into software polling, then reinitialize.
		p = gpioutil.PollEdge(p, 50*physic.Hertz)
		if err = p.In(gpio.PullDown, gpio.BothEdges); err != nil {
			log.Fatal(err)
		}
	}

	// Ignore glitches lasting less than 10ms, and ignore repeated edges within
	// 30ms. Make sure to not use denoiser period lower than the software poller
	// frequency.
	d, err := gpioutil.Debounce(p, 10*time.Millisecond, 30*time.Millisecond, gpio.BothEdges)
	if err != nil {
		log.Fatal(err)
	}

	defer d.Halt()
	for {
		if d.WaitForEdge(-1) {
			fmt.Println(d.Read())
		}
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debounce

func Debounce(p gpio.PinIO, denoise, debounce time.Duration, edge gpio.Edge) (gpio.PinIO, error)

Debounce returns a debounced gpio.PinIO from a gpio.PinIO source. Only the PinIn behavior is mutated.

denoise is a noise filter, which waits a pin to be steady for this amount of time BEFORE reporting the new level.

debounce will lock on a level for this amount of time AFTER the pin changed state, ignoring following state changes.

Either value can be 0.

Example
package main

import (
	"fmt"
	"log"
	"time"

	"periph.io/x/periph/conn/gpio"
	"periph.io/x/periph/conn/gpio/gpioreg"
	"periph.io/x/periph/experimental/conn/gpio/gpioutil"
)

func main() {
	p := gpioreg.ByName("GPIO16")
	if p != nil {
		log.Fatal("please open another GPIO")
	}

	// Ignore glitches lasting less than 3ms, and ignore repeated edges within
	// 30ms.
	d, err := gpioutil.Debounce(p, 3*time.Millisecond, 30*time.Millisecond, gpio.BothEdges)
	if err != nil {
		log.Fatal(err)
	}

	defer d.Halt()
	for {
		if d.WaitForEdge(-1) {
			fmt.Println(d.Read())
		}
	}
}
Output:

func PollEdge

func PollEdge(p gpio.PinIO, freq physic.Frequency) gpio.PinIO

PollEdge returns a gpio.PinIO which implements edge detection via polling.

Example of GPIOs without edge detection are GPIOs accessible over an I²C chip or over USB.

freq must be above 0. A reasonable value is 20Hz reading. High rate essentially means a busy loop.

Example
package main

import (
	"fmt"
	"log"

	"periph.io/x/periph/conn/gpio"
	"periph.io/x/periph/conn/gpio/gpioreg"
	"periph.io/x/periph/conn/physic"
	"periph.io/x/periph/experimental/conn/gpio/gpioutil"
)

func main() {
	// Flow when it is known that the GPIO does not support edge detection.
	p := gpioreg.ByName("XOI-P1")
	if p != nil {
		log.Fatal("please open another GPIO")
	}
	p = gpioutil.PollEdge(p, 20*physic.Hertz)
	if err := p.In(gpio.PullDown, gpio.RisingEdge); err != nil {
		log.Fatal(err)
	}

	defer p.Halt()
	for {
		if p.WaitForEdge(-1) {
			fmt.Println(p.Read())
		}
	}
}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL