Documentation ¶
Overview ¶
Package gpioutil includes utilities to filter or augment GPIOs.
Example ¶
package main import ( "fmt" "log" "time" "periph.io/x/conn/v3/driver/driverreg" "periph.io/x/conn/v3/gpio" "periph.io/x/conn/v3/gpio/gpioreg" "periph.io/x/conn/v3/gpio/gpioutil" "periph.io/x/conn/v3/physic" ) func main() { // Make sure periph is initialized. // TODO: Use host.Init(). It is not used in this example to prevent circular // go package import. if _, err := driverreg.Init(); err != nil { log.Fatal(err) } // 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 ¶
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/conn/v3/driver/driverreg" "periph.io/x/conn/v3/gpio" "periph.io/x/conn/v3/gpio/gpioreg" "periph.io/x/conn/v3/gpio/gpioutil" ) func main() { // Make sure periph is initialized. // TODO: Use host.Init(). It is not used in this example to prevent circular // go package import. if _, err := driverreg.Init(); err != nil { log.Fatal(err) } 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 ¶
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/conn/v3/driver/driverreg" "periph.io/x/conn/v3/gpio" "periph.io/x/conn/v3/gpio/gpioreg" "periph.io/x/conn/v3/gpio/gpioutil" "periph.io/x/conn/v3/physic" ) func main() { // Make sure periph is initialized. // TODO: Use host.Init(). It is not used in this example to prevent circular // go package import. if _, err := driverreg.Init(); err != nil { log.Fatal(err) } // 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:
func PulseIn ¶ added in v3.6.10
PulseIn reads a pulse (either HIGH or LOW) on a pin.
For example, if lvl is HIGH, PulseIn() waits for the pin to go from LOW to HIGH, starts timing, then waits for the pin to go LOW and stops timing.
Returns the length of the pulse as a time.Duration or gives up and returns 0 if no complete pulse was received within the timeout.
Types ¶
This section is empty.