drivers

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2025 License: BSD-2-Clause Imports: 9 Imported by: 0

Documentation

Overview

The drivers include GPIO (digital), analog (via ads1115), serial, I2C at this point.

I have to admit these drivers are not the cleanest of interfaces that they could perhaps be.

Each of the said drivers can be put into mock mode for testing and development on a non-raspberry pi.

Index

Constants

View Source
const PIN_COUNT = 64

PIN_COUNT provides the number of pins per VPIO bank

Variables

View Source
var (
	ErrOutOfRange    = errors.New("pin out of range")
	ErrPinIsAnOutput = errors.New("can not set input pin")
)

Functions

func GetI2CDriver

func GetI2CDriver(bus string, addr int) (device *i2c.Device, err error)

func ResetGPIO added in v0.0.2

func ResetGPIO()

ResetGPIO clears all singleton instances (useful for testing)

func SetEventHandler added in v0.0.2

func SetEventHandler(handler func(gpiocdev.LineEvent))

Types

type AnalogPin

type AnalogPin Pin[float64]

type DigitalPin

type DigitalPin Pin[bool]

DigitalPin

type EventType added in v0.0.2

type EventType int

type GPIO

type GPIO[T any] interface {
	Open() error
	Close() error
	SetPin(name string, pin int, options ...PinOptions) (Pin[T], error)
	Get(pin int) (T, error)
	Set(pin int, v T) error
}

func GetGPIO

func GetGPIO[T Value]() GPIO[T]

GetGPIO returns the singleton GPIO instance for the specified type. On non-Linux systems or when mocking is enabled, it returns VPIO. On Linux systems, it returns GPIOCDev for hardware access. Supported types: bool, int, float64

type GPIOCDev added in v0.0.2

type GPIOCDev struct {
	Chipname string
	// contains filtered or unexported fields
}

GPIOCDev implements a GPIO controller using go-gpiocdev for real hardware

func GetGPIOCdev added in v0.0.2

func GetGPIOCdev() *GPIOCDev

GetGPIOCdev treats the GPIOCdev device as a singleton

func NewGPIOCDev added in v0.0.2

func NewGPIOCDev(chipname string) *GPIOCDev

NewGPIOCDev creates a new GPIOCDev instance chipname should be "gpiochip0" for Raspberry Pi Zero or "gpiochip4" for Raspberry Pi 5

func (*GPIOCDev) Close added in v0.0.2

func (g *GPIOCDev) Close() error

Close releases all GPIO resources

func (*GPIOCDev) Get added in v0.0.2

func (g *GPIOCDev) Get(pinIndex int) (int, error)

Get retrieves the value of a specific pin

func (*GPIOCDev) Open added in v0.0.2

func (g *GPIOCDev) Open() error

func (*GPIOCDev) Set added in v0.0.2

func (g *GPIOCDev) Set(pinIndex int, value int) error

Set sets the value of a specific pin

func (*GPIOCDev) SetPin added in v0.0.2

func (g *GPIOCDev) SetPin(name string, pinIndex int, options PinOptions) (*GPIOCDevPin, error)

Pin initializes and returns a GPIO pin

type GPIOCDevPin added in v0.0.2

type GPIOCDevPin struct {
	// contains filtered or unexported fields
}

GPIOCDevPin implements the Pin interface for hardware GPIO pins

func (*GPIOCDevPin) Close added in v0.0.2

func (p *GPIOCDevPin) Close() error

Close releases the pin resources

func (*GPIOCDevPin) EventLoop added in v0.0.2

func (p *GPIOCDevPin) EventLoop(done chan any, callback func(evt gpiocdev.LineEvent))

EventLoop processes GPIO events

func (*GPIOCDevPin) Get added in v0.0.2

func (p *GPIOCDevPin) Get() (int, error)

Get reads the current value of the pin

func (*GPIOCDevPin) ID added in v0.0.2

func (p *GPIOCDevPin) ID() string

ID returns the pin identifier

func (*GPIOCDevPin) Index added in v0.0.2

func (p *GPIOCDevPin) Index() int

Index returns the pin index/offset

func (*GPIOCDevPin) Off added in v0.0.2

func (p *GPIOCDevPin) Off() error

Off sets the pin low (0)

func (*GPIOCDevPin) On added in v0.0.2

func (p *GPIOCDevPin) On() error

On sets the pin high (1)

func (*GPIOCDevPin) Options added in v0.0.2

func (p *GPIOCDevPin) Options() PinOptions

Direction returns the pin direction

func (*GPIOCDevPin) Reconfigure added in v0.0.2

func (p *GPIOCDevPin) Reconfigure(opts ...gpiocdev.LineConfigOption) error

Reconfigure changes the pin configuration

func (*GPIOCDevPin) Set added in v0.0.2

func (p *GPIOCDevPin) Set(value int) error

Set writes a value to the pin

func (*GPIOCDevPin) Toggle added in v0.0.2

func (p *GPIOCDevPin) Toggle() error

Toggle flips the pin value

type Pin added in v0.0.2

type Pin[T any] interface {
	ID() string
	Index() int
	Options() []PinOptions
	Get() (T, error)
	Set(v T) error
	ReadContinuous() <-chan T
}

type PinOptions added in v0.0.2

type PinOptions int
const (
	PinInput       PinOptions = 1 << 0
	PinOutput      PinOptions = 1 << 1
	PinOutputLow   PinOptions = 1 << 2
	PinOutputHigh  PinOptions = 1 << 3
	PinPullUp      PinOptions = 1 << 4
	PinPullDown    PinOptions = 1 << 5
	PinRisingEdge  PinOptions = 1 << 6
	PinFallingEdge PinOptions = 1 << 7
	PinBothEdges   PinOptions = 1 << 8
)

type Serial

type Serial struct {
	PortName string
	Baud     int

	*serial.Mode
	serial.Port
	// contains filtered or unexported fields
}

func GetSerial

func GetSerial(port string) *Serial

func NewSerial

func NewSerial(port string, baud int) (s *Serial, err error)

func (*Serial) ID

func (s *Serial) ID() string

func (*Serial) Open

func (s *Serial) Open() (err error)

type Transaction added in v0.0.2

type Transaction[T Value] struct {
	time.Time
	// contains filtered or unexported fields
}

type VPIO added in v0.0.2

type VPIO[T Value] struct {
	// contains filtered or unexported fields
}

func NewVPIO added in v0.0.2

func NewVPIO[T Value]() *VPIO[T]

func (*VPIO[T]) ClearTransactions added in v0.0.2

func (v *VPIO[T]) ClearTransactions()

ClearTransactions removes all recorded transactions

func (*VPIO[T]) Close added in v0.0.2

func (v *VPIO[T]) Close() error

Close releases VPIO resources (no-op for virtual GPIO)

func (*VPIO[T]) Get added in v0.0.2

func (v *VPIO[T]) Get(i int) (T, error)

func (*VPIO[T]) GetTransactions added in v0.0.2

func (v *VPIO[T]) GetTransactions() []*Transaction[T]

GetTransactions returns all recorded transactions

func (*VPIO[T]) Open added in v0.0.2

func (v *VPIO[T]) Open() error

func (*VPIO[T]) PlayTransactions added in v0.0.2

func (v *VPIO[T]) PlayTransactions(f func())

PlayTranactions takes a function that will set values in the VPIO accordingly

func (*VPIO[T]) Set added in v0.0.2

func (v *VPIO[T]) Set(i int, val T) error

func (*VPIO[T]) SetPin added in v0.0.2

func (v *VPIO[T]) SetPin(id string, i int, opts ...PinOptions) (Pin[T], error)

func (*VPIO[T]) StartRecording added in v0.0.2

func (v *VPIO[T]) StartRecording()

StartRecording enables transaction recording

func (*VPIO[T]) StopRecording added in v0.0.2

func (v *VPIO[T]) StopRecording()

StopRecording disables transaction recording

type VPin added in v0.0.2

type VPin[T Value] struct {
	// contains filtered or unexported fields
}

func (*VPin[T]) Get added in v0.0.2

func (p *VPin[T]) Get() (T, error)

Get returns the current value of the pin

func (*VPin[T]) ID added in v0.0.2

func (p *VPin[T]) ID() string

ID returns the pin identifier

func (*VPin[T]) Index added in v0.0.2

func (p *VPin[T]) Index() int

Index returns the pin index

func (*VPin[T]) Options added in v0.0.2

func (p *VPin[T]) Options() []PinOptions

Direction returns the pin direction

func (*VPin[T]) ReadContinuous added in v0.0.2

func (v *VPin[T]) ReadContinuous() <-chan T

func (*VPin[T]) Set added in v0.0.2

func (p *VPin[T]) Set(val T) error

Set sets the value of the pin

type Value added in v0.0.2

type Value interface {
	~int | ~float64 | ~bool
}

Jump to

Keyboard shortcuts

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