gpioreg

package
v3.7.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2022 License: Apache-2.0 Imports: 4 Imported by: 149

Documentation

Overview

Package gpioreg defines a registry for the known digital pins.

Example
package main

import (
	"flag"
	"log"

	"periph.io/x/conn/v3/driver/driverreg"
	"periph.io/x/conn/v3/gpio"
	"periph.io/x/conn/v3/gpio/gpioreg"
)

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)
	}

	// A command line tool may let the user choose a GPIO pin.
	name := flag.String("p", "", "GPIO pin to use")
	flag.Parse()
	if *name == "" {
		log.Fatal("-p is required")
	}
	p := gpioreg.ByName(*name)
	if p == nil {
		log.Fatalf("Failed to find %s", *name)
	}

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

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Aliases

func Aliases() []gpio.PinIO

Aliases returns all pin aliases.

The list is guaranteed to be in order of aliase name.

func All

func All() []gpio.PinIO

All returns all the GPIO pins available on this host.

The list is guaranteed to be in order of name using 'natural sorting'.

This list excludes aliases.

This list excludes non-GPIO pins like GROUND, V3_3, etc, since they are not GPIO.

Example
package main

import (
	"fmt"
	"log"

	"periph.io/x/conn/v3/driver/driverreg"
	"periph.io/x/conn/v3/gpio/gpioreg"
)

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)
	}

	fmt.Print("GPIO pins available:\n")
	for _, p := range gpioreg.All() {
		fmt.Printf("- %s: %s\n", p, p.Function())
	}
}
Output:

func ByName

func ByName(name string) gpio.PinIO

ByName returns a GPIO pin from its name, gpio number or one of its aliases.

For example on a Raspberry Pi, the following values will return the same GPIO: the gpio as a number "2", the chipset name "GPIO2", the board pin position "P1_3", it's function name "I2C1_SDA".

Returns nil if the gpio pin is not present.

Example (Alias)
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"
)

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)
	}

	// LCD-D2 is a pin found on the C.H.I.P.
	p := gpioreg.ByName("LCD-D2")
	if p == nil {
		log.Fatal("Failed to find LCD-D2")
	}
	if rp, ok := p.(gpio.RealPin); ok {
		fmt.Printf("%s is an alias for %s\n", p, rp.Real())
	} else {
		fmt.Printf("%s is not an alias!\n", p)
	}
}
Output:

Example (Number)
package main

import (
	"fmt"
	"log"

	"periph.io/x/conn/v3/driver/driverreg"
	"periph.io/x/conn/v3/gpio/gpioreg"
)

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)
	}

	// The string representation of a number works too.
	p := gpioreg.ByName("6")
	if p == nil {
		log.Fatal("Failed to find GPIO6")
	}
	fmt.Printf("%s: %s\n", p, p.Function())
}
Output:

func Register

func Register(p gpio.PinIO) error

Register registers a GPIO pin.

Registering the same pin number or name twice is an error.

The pin registered cannot implement the interface RealPin.

func RegisterAlias

func RegisterAlias(alias string, dest string) error

RegisterAlias registers an alias for a GPIO pin.

It is possible to register an alias for a pin that itself has not been registered yet. It is valid to register an alias to another alias. It is valid to register the same alias multiple times, overriding the previous alias.

func Unregister

func Unregister(name string) error

Unregister removes a previously registered GPIO pin or alias from the GPIO pin registry.

This can happen when a GPIO pin is exposed via an USB device and the device is unplugged, or when a generic OS provided pin is superseded by a CPU specific implementation.

Types

This section is empty.

Jump to

Keyboard shortcuts

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