gpio

package module
v0.0.0-...-283bb63 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2019 License: BSD-3-Clause Imports: 8 Imported by: 0

README

GPIO

This is a Go library for general purpose pins on Linux devices which support /sys/class/gpio. This implementation conforms to the spec. An advantage of using /sys/class/gpio is that we can receive interrupt-like notifications from the kernel when an input changes, rather than polling an input periodically. See the notes on Watcher for more info.

I have only tested it so far on Raspberry Pi but it should also work on similar systems like the Beaglebone. If you test this library on another system please tell me so that I can confirm it -- I'll give you credit here for the confirmation.

Note that the GPIO numbers we want here as the CPU/kernel knows them, not as they may be marked on any external hardware headers.

Input

Call pin := gpio.NewInput(number) to create a new input with the given pin numbering. You can then access the value of this pin with pin.Read(), which returns 0 when the pin's value is logic low and 1 when high.

If you are only concerned with when the pin's value changes, consider using gpio.Watcher instead.

Output

Call pin := gpio.NewOutput(number, high), where high is a bool that describes the initial value of the pin -- set to false if you'd like the pin to initialize low, and true if you'd like it to initialize high.

Once you have a pin, you can change its value with pin.Low() and pin.High().

Watcher

The Watcher is a type which listens on the GPIO pins you specify and then notifies you when the values of those pins change. It uses a select() call so that it does not need to actively poll, which saves CPU time and gives you better latencies from your inputs.

Here is an example of how to use the Watcher.

watcher := gpio.NewWatcher()
watcher.AddPin(22)
watcher.AddPin(27)
defer watcher.Close()

go func() {
    for {
        pin, value := watcher.Watch()
        fmt.Printf("read %d from gpio %d\n", value, pin)
    }
}()

This example would print once each time the value read on either pin 22 or 27 changes. It also prints each pin once when starting.

Alternately, users may receive from watcher.Notification directly rather than calling watcher.Watch(). This channel yields WatcherNotification objects with Pin and Value fields.

License

3-clause BSD

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Edge

type Edge uint
const (
	EdgeNone Edge = iota
	EdgeRising
	EdgeFalling
	EdgeBoth
)

type LogicLevel

type LogicLevel uint
const (
	ActiveHigh LogicLevel = iota
	ActiveLow
)

type Pin

type Pin struct {
	Number uint
	// contains filtered or unexported fields
}

Pin represents a single pin, which can be used either for reading or writing

func NewInput

func NewInput(p uint) Pin

NewInput opens the given pin number for reading. The number provided should be the pin number known by the kernel

func NewOutput

func NewOutput(p uint, initHigh bool) Pin

NewOutput opens the given pin number for writing. The number provided should be the pin number known by the kernel NewOutput also needs to know whether the pin should be initialized high (true) or low (false)

func (Pin) Close

func (p Pin) Close()

Close releases the resources related to Pin

func (Pin) High

func (p Pin) High() error

High sets the value of an output pin to logic high

func (Pin) Low

func (p Pin) Low() error

Low sets the value of an output pin to logic low

func (Pin) Read

func (p Pin) Read() (value uint, err error)

Read returns the value read at the pin as reported by the kernel. This should only be used for input pins

func (Pin) SetLogicLevel

func (p Pin) SetLogicLevel(logicLevel LogicLevel) error

SetLogicLevel sets the logic level for the Pin. This can be either "active high" or "active low"

type Value

type Value uint
const (
	Inactive Value = 0
	Active   Value = 1
)

type Watcher

type Watcher struct {
	Notification chan WatcherNotification
	// contains filtered or unexported fields
}

Watcher provides asynchronous notifications on input changes The user should supply it pins to watch with AddPin and then wait for changes with Watch Alternately, users may receive directly from the Notification channel

func NewWatcher

func NewWatcher() *Watcher

NewWatcher creates a new Watcher instance for asynchronous inputs

func (*Watcher) AddPin

func (w *Watcher) AddPin(p uint)

AddPin adds a new pin to be watched for changes. The pin is configured with logic level "active high" and watched for both rising and falling edges. The pin provided should be the pin known by the kernel

func (*Watcher) AddPinWithEdgeAndLogic

func (w *Watcher) AddPinWithEdgeAndLogic(p uint, edge Edge, logicLevel LogicLevel)

AddPinWithEdgeAndLogic adds a new pin to be watched for changes. Edges can be configured to be either rising, falling, or both. Logic level can be active high or active low. The pin provided should be the pin known by the kernel.

func (*Watcher) Close

func (w *Watcher) Close()

Close stops the watcher and releases all resources

func (*Watcher) RemovePin

func (w *Watcher) RemovePin(p uint)

RemovePin stops the watcher from watching the specified pin

func (*Watcher) Watch

func (w *Watcher) Watch() (p uint, v uint)

Watch blocks until one change occurs on one of the watched pins It returns the pin which changed and its new value Because the Watcher is not perfectly realtime it may miss very high frequency changes If that happens, it's possible to see consecutive changes with the same value Also, if the input is connected to a mechanical switch, the user of this library must deal with debouncing Users can either use Watch() or receive from Watcher.Notification directly

type WatcherNotification

type WatcherNotification struct {
	Pin   uint
	Value uint
}

WatcherNotification represents a single pin change The new value of the pin numbered by Pin is Value

Jump to

Keyboard shortcuts

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