driverreg

package
v3.6.8 Latest Latest
Warning

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

Go to latest
Published: May 28, 2021 License: Apache-2.0 Imports: 5 Imported by: 139

Documentation

Overview

Package driverreg is a registry for all host driver implementation that can be automatically discovered.

Example
package main

import (
	"fmt"
	"log"

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

func main() {
	// Make sure periph is initialized.
	// TODO: Use host.Init(). It is not used in this example to prevent circular
	// go package import.
	state, err := driverreg.Init()
	if err != nil {
		log.Fatal(err)
	}

	// Prints the loaded driver.
	fmt.Printf("Using drivers:\n")
	for _, driver := range state.Loaded {
		fmt.Printf("- %s\n", driver)
	}

	// Prints the driver that were skipped as irrelevant on the platform.
	fmt.Printf("Drivers skipped:\n")
	for _, failure := range state.Skipped {
		fmt.Printf("- %s: %s\n", failure.D, failure.Err)
	}

	// Having drivers failing to load may not require process termination. It
	// is possible to continue to run in partial failure mode.
	fmt.Printf("Drivers failed to load:\n")
	for _, failure := range state.Failed {
		fmt.Printf("- %s: %v\n", failure.D, failure.Err)
	}

	// Use pins, buses, devices, etc.
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustRegister

func MustRegister(d driver.Impl)

MustRegister calls Register() and panics if registration fails.

This is the function to call in a driver's package init() function.

func Register

func Register(d driver.Impl) error

Register registers a driver to be initialized automatically on Init().

The d.String() value must be unique across all registered drivers.

It is an error to call Register() after Init() was called.

Types

type DriverFailure

type DriverFailure struct {
	D   driver.Impl
	Err error
}

DriverFailure is a driver that wasn't loaded, either because it was skipped or because it failed to load.

func (DriverFailure) String

func (d DriverFailure) String() string

type State

type State struct {
	Loaded  []driver.Impl
	Skipped []DriverFailure
	Failed  []DriverFailure
}

State is the state of loaded device drivers.

Each list is sorted by the driver name.

func Init

func Init() (*State, error)

Init initialises all the relevant drivers.

Drivers are started concurrently.

It is safe to call this function multiple times, the previous state is returned on later calls.

Users will want to use host.Init(), which guarantees a baseline of included host drivers.

Jump to

Keyboard shortcuts

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