46 lines
1005 B
Go
46 lines
1005 B
Go
// Package hardware contains all hardware interface implementations.
|
|
package hardware
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"periph.io/x/conn/v3/gpio"
|
|
"periph.io/x/conn/v3/gpio/gpioreg"
|
|
)
|
|
|
|
// Driver is a piece of hardware that can be stopped.
|
|
type Driver interface {
|
|
// Type of the driver.
|
|
Type() string
|
|
|
|
// Halt shuts down the driver.
|
|
Halt() error
|
|
}
|
|
|
|
// PinNotFound error indicating that the requested pin could not be found.
|
|
type PinNotFound struct {
|
|
Alias string
|
|
Name string
|
|
}
|
|
|
|
func (err PinNotFound) Error() string {
|
|
if err.Alias == "" {
|
|
return fmt.Sprintf("invalid pin %q", err.Name)
|
|
}
|
|
return fmt.Sprintf("invalid %s pin %q", err.Alias, err.Name)
|
|
}
|
|
|
|
// LookupPin returns a GPIO pin by name or number.
|
|
func LookupPin(name string) (gpio.PinIO, error) {
|
|
pin := gpioreg.ByName(name)
|
|
if !IsValidPin(pin) {
|
|
return nil, PinNotFound{Name: name}
|
|
}
|
|
return pin, nil
|
|
}
|
|
|
|
// IsValidPin checks if the provided GPIO pin can be used.
|
|
func IsValidPin(pin gpio.PinIO) bool {
|
|
return pin != nil && pin != gpio.INVALID
|
|
}
|