Files
beatglow/hardware/hardware.go
2026-10-29 17:29:54 +01:00

46 lines
1007 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.
String() 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
}