gpio

package
v2.3.0+incompatible Latest Latest
Warning

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

Go to latest
Published: May 27, 2018 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package gpio defines digital pins.

While all GPIO implementations are expected to implement PinIO, they may expose more specific functionality like PinPWM, PinDefaultPull, etc.

Example
package main

import (
	"fmt"
	"log"

	"periph.io/x/periph/conn/gpio/gpioreg"
	"periph.io/x/periph/host"
)

func main() {
	// Make sure periph is initialized.
	if _, err := host.Init(); err != nil {
		log.Fatal(err)
	}

	// Use gpioreg GPIO pin registry to find a GPIO pin by name.
	p := gpioreg.ByName("GPIO6")
	if p == nil {
		log.Fatal("Failed to find GPIO6")
	}

	// A pin can be read, independent of its state; it doesn't matter if it is
	// set as input or output.
	fmt.Printf("%s is %s\n", p, p.Read())
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Duty

type Duty int32

Duty is the duty cycle for a PWM.

Valid values are between 0 and DutyMax.

const (
	// DutyMax is a duty cycle of 100%.
	DutyMax Duty = 65535
	// DutyHalf is a 50% duty PWM, which boils down to a normal clock.
	DutyHalf Duty = DutyMax / 2
)

func ParseDuty

func ParseDuty(s string) (Duty, error)

ParseDuty parses a string and converts it to a Duty value.

Example
package main

import (
	"fmt"
	"log"

	"periph.io/x/periph/conn/gpio"
)

func main() {
	d, err := gpio.ParseDuty("33%")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%s\n", d)
}
Output:

33%

func (Duty) String

func (d Duty) String() string

func (Duty) Valid

func (d Duty) Valid() bool

Valid returns true if the Duty cycle value is valid.

type Edge

type Edge int

Edge specifies if an input pin should have edge detection enabled.

Only enable it when needed, since this causes system interrupts.

const (
	NoEdge      Edge = 0
	RisingEdge  Edge = 1
	FallingEdge Edge = 2
	BothEdges   Edge = 3
)

Acceptable edge detection values.

func (Edge) String

func (i Edge) String() string

type Level

type Level bool

Level is the level of the pin: Low or High.

const (
	// Low represents 0v.
	Low Level = false
	// High represents Vin, generally 3.3v or 5v.
	High Level = true
)

func (Level) String

func (l Level) String() string

type PinDefaultPull

type PinDefaultPull interface {
	// DefaultPull returns the pull that is initialized on CPU reset.
	DefaultPull() Pull
}

PinDefaultPull is optionally implemented to return the default pull at boot time. This is useful to determine if the pin is acceptable for operation with certain devices.

type PinIO

type PinIO interface {
	pin.Pin
	// PinIn
	In(pull Pull, edge Edge) error
	Read() Level
	WaitForEdge(timeout time.Duration) bool
	Pull() Pull
	// PinOut
	Out(l Level) error
}

PinIO is a GPIO pin that supports both input and output. It matches both interfaces PinIn and PinOut.

A GPIO pin implementing PinIO may fail at either input or output or both.

The GPIO pin may optionally support more interfaces, like PinPWM, PinDefaultPull.

var INVALID PinIO

INVALID implements PinIO and fails on all access.

type PinIn

type PinIn interface {
	pin.Pin
	// In setups a pin as an input.
	//
	// If WaitForEdge() is planned to be called, make sure to use one of the Edge
	// value. Otherwise, use None to not generated unneeded hardware interrupts.
	//
	// Calling In() will try to empty the accumulated edges but it cannot be 100%
	// reliable due to the OS (linux) and its driver. It is possible that on a
	// gpio that is as input, doing a quick Out(), In() may return an edge that
	// occurred before the Out() call.
	In(pull Pull, edge Edge) error
	// Read return the current pin level.
	//
	// Behavior is undefined if In() wasn't used before.
	//
	// In some rare case, it is possible that Read() fails silently. This happens
	// if another process on the host messes up with the pin after In() was
	// called. In this case, call In() again.
	Read() Level
	// WaitForEdge() waits for the next edge or immediately return if an edge
	// occurred since the last call.
	//
	// Only waits for the kind of edge as specified in a previous In() call.
	// Behavior is undefined if In() with a value other than None wasn't called
	// before.
	//
	// Returns true if an edge was detected during or before this call. Return
	// false if the timeout occurred or In() was called while waiting, causing the
	// function to exit.
	//
	// Multiple edges may or may not accumulate between two calls to
	// WaitForEdge(). The behavior in this case is undefined and is OS driver
	// specific.
	//
	// It is not required to call Read() to reset the edge detection.
	//
	// Specify -1 to effectively disable timeout.
	WaitForEdge(timeout time.Duration) bool
	// Pull returns the internal pull resistor if the pin is set as input pin.
	//
	// Returns PullNoChange if the value cannot be read.
	Pull() Pull
}

PinIn is an input GPIO pin.

It may optionally support internal pull resistor and edge based triggering.

Example
package main

import (
	"fmt"
	"log"

	"periph.io/x/periph/conn/gpio"
	"periph.io/x/periph/conn/gpio/gpioreg"
	"periph.io/x/periph/host"
)

func main() {
	// Make sure periph is initialized.
	if _, err := host.Init(); err != nil {
		log.Fatal(err)
	}

	// Use gpioreg GPIO pin registry to find a GPIO pin by name.
	p := gpioreg.ByName("GPIO6")
	if p == nil {
		log.Fatal("Failed to find GPIO6")
	}

	// Set it as input, with a pull down (defaults to Low when unconnected) and
	// enable rising edge triggering.
	if err := p.In(gpio.PullDown, gpio.RisingEdge); err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%s is %s\n", p, p.Read())

	// Wait for rising edges (Low -> High) and print when one occur.
	for p.WaitForEdge(-1) {
		fmt.Printf("%s went %s\n", p, gpio.High)
	}
}
Output:

type PinOut

type PinOut interface {
	pin.Pin
	// Out sets a pin as output if it wasn't already and sets the initial value.
	//
	// After the initial call to ensure that the pin has been set as output, it
	// is generally safe to ignore the error returned.
	//
	// Out() tries to empty the accumulated edges detected if the gpio was
	// previously set as input but this is not 100% guaranteed due to the OS.
	Out(l Level) error
}

PinOut is an output GPIO pin.

Example
package main

import (
	"log"

	"periph.io/x/periph/conn/gpio"
	"periph.io/x/periph/conn/gpio/gpioreg"
	"periph.io/x/periph/host"
)

func main() {
	// Make sure periph is initialized.
	if _, err := host.Init(); err != nil {
		log.Fatal(err)
	}

	// Use gpioreg GPIO pin registry to find a GPIO pin by name.
	p := gpioreg.ByName("GPIO6")
	if p == nil {
		log.Fatal("Failed to find GPIO6")
	}

	// Set the pin as output High.
	if err := p.Out(gpio.High); err != nil {
		log.Fatal(err)
	}
}
Output:

type PinPWM

type PinPWM interface {
	// PWM sets the PWM output on supported pins.
	//
	// To use as a general purpose clock, set duty to DutyHalf. Some pins may
	// only support DutyHalf and no other value.
	//
	// Using 0 as period will use the optimal value as supported/preferred by the
	// pin.
	PWM(duty Duty, period time.Duration) error
}

PinPWM exposes hardware PWM.

The driver may uses DMA controller underneath for zero CPU implementation.

Example
package main

import (
	"log"

	"periph.io/x/periph/conn/gpio"
	"periph.io/x/periph/conn/gpio/gpioreg"
	"periph.io/x/periph/host"
)

func main() {
	// Make sure periph is initialized.
	if _, err := host.Init(); err != nil {
		log.Fatal(err)
	}

	// Use gpioreg GPIO pin registry to find a GPIO pin by name.
	p := gpioreg.ByName("GPIO6")
	if p == nil {
		log.Fatal("Failed to find GPIO6")
	}
	// Resolve alias if necessary.
	if r, ok := p.(gpio.RealPin); ok {
		p = r.Real()
	}

	pwm, ok := p.(gpio.PinPWM)
	if !ok {
		log.Fatalf("Pin %s cannot be used as PWM", p)
	}

	// Generate a 33% duty cycle 10KHz signal.
	if err := pwm.PWM(gpio.DutyMax/3, 10000); err != nil {
		log.Fatal(err)
	}
}
Output:

type Pull

type Pull uint8

Pull specifies the internal pull-up or pull-down for a pin set as input.

const (
	Float        Pull = 0 // Let the input float
	PullDown     Pull = 1 // Apply pull-down
	PullUp       Pull = 2 // Apply pull-up
	PullNoChange Pull = 3 // Do not change the previous pull resistor setting or an unknown value
)

Acceptable pull values.

func (Pull) String

func (i Pull) String() string

type RealPin

type RealPin interface {
	Real() PinIO // Real returns the real pin behind an Alias
}

RealPin is implemented by aliased pin and allows the retrieval of the real pin underlying an alias.

Aliases are created by RegisterAlias. Aliases permits presenting a user friendly GPIO pin name while representing the underlying real pin.

The purpose of the RealPin is to be able to cleanly test whether an arbitrary gpio.PinIO returned by ByName is an alias for another pin, and resolve it. This is needed to be able to test for other interfaces, like PinPWM, PinDefaultPull, etc.

Example
package main

import (
	"fmt"
	"log"

	"periph.io/x/periph/conn/gpio"
	"periph.io/x/periph/conn/gpio/gpioreg"
	"periph.io/x/periph/host"
)

func main() {
	// Make sure periph is initialized.
	if _, err := host.Init(); err != nil {
		log.Fatal(err)
	}

	// Use gpioreg GPIO pin registry to find a GPIO pin by name.
	p := gpioreg.ByName("P1_3")
	if p == nil {
		log.Fatal("Failed to find P1_3")
	}
	fmt.Printf("P1_3: %s", p)

	// Resolve the real underlying pin.
	if r, ok := p.(gpio.RealPin); ok {
		// On Raspberry Pis, pin #3 on header P1 is an alias for GPIO2.
		fmt.Printf("%s is in fact %s", p, r.Real())
		p = r.Real()
	} else {
		log.Printf("%s is not an alias", p)
	}

	// Then this is possible to find if the pin implements another interface.
	if d, ok := p.(gpio.PinDefaultPull); ok {
		fmt.Printf("%s default pull is %s", d, d.DefaultPull())
	}

}
Output:

Directories

Path Synopsis
Package gpioreg defines a registry for the known digital pins.
Package gpioreg defines a registry for the known digital pins.
Package gpiosmoketest is leveraged by periph-smoketest to verify that basic GPIO pin functionality work.
Package gpiosmoketest is leveraged by periph-smoketest to verify that basic GPIO pin functionality work.
Package gpiostream defines digital streams.
Package gpiostream defines digital streams.
gpiostreamtest
Package gpiostreamtest enables testing device driver using gpiostream.PinIn or PinOut.
Package gpiostreamtest enables testing device driver using gpiostream.PinIn or PinOut.
Package gpiotest is meant to be used to test drivers using fake Pins.
Package gpiotest is meant to be used to test drivers using fake Pins.

Jump to

Keyboard shortcuts

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