drivers

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2025 License: BSD-2-Clause Imports: 17 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

This section is empty.

Variables

This section is empty.

Functions

func GetI2CDriver

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

Types

type ADS1115

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

ADS1115 is an i2c ADC chip that will use the i2c device type to provide 4 single analog pins to be used by the raspberry pi to access analog sensors via the i2c bus. In a sense this device is a higher level device than the device_i2c.

func GetADS1115

func GetADS1115() *ADS1115

GetADS1115 will return the default ads1115 struct singleton. The first time GetADS1115 is called it will create a new device. Subsequent calls will return the global variable.

func NewADS1115

func NewADS1115(name string, bus string, addr int) *ADS1115

NewADS creates a new ADS1115 giving it the provided name, I2C bus (default /dev/i2c-1) and address (default 0x48).

func (*ADS1115) Close

func (a *ADS1115) Close() error

Close the ads1115 and shutdown all the pins

func (*ADS1115) Init

func (a *ADS1115) Init() (err error)

Init prepares the chip for usage

func (*ADS1115) JSON

func (a *ADS1115) JSON() []byte

JSON returns JSON more clean!

func (*ADS1115) Pin

func (a *ADS1115) Pin(name string, ch int, opts any) (pin *ADS1115Pin, err error)

Pin allocates and prepares one of the ads1115 pins (0 - 3) for use.

func (*ADS1115) String

func (a *ADS1115) String() string

String returns a string. Clean code!

type ADS1115Pin

type ADS1115Pin struct {
	ads1x15.PinADC
	// contains filtered or unexported fields
}

ADS1115Pin is an analog analagous to a digital pin

func (ADS1115Pin) Close

func (a ADS1115Pin) Close() error

Close the pin and set it back to it's defaults. TODO set the pin back to its defaults

func (*ADS1115Pin) Name

func (p *ADS1115Pin) Name() string

Name is what we call this pin

func (ADS1115Pin) Read

func (p ADS1115Pin) Read() (float64, error)

Read returns a single float64 reading from the pin

func (ADS1115Pin) ReadContinuous

func (p ADS1115Pin) ReadContinuous() <-chan float64

ReadContinous returns a channel that will continually read data from respective ads1115 pin and make the float64 values available as soon as the data is ready.

func (ADS1115Pin) Set

func (p ADS1115Pin) Set(val float64) error

Set the value on the PIN

func (*ADS1115Pin) String

func (p *ADS1115Pin) String() string

String is written text to hopefully drop some old eyeball style of knowledge.

type AnalogPin

type AnalogPin interface {
	Read() (float64, error)
	ReadContinuous() <-chan float64
	Close() error
}

type DigitalPin

type DigitalPin struct {
	Line

	On  func() error
	Off func() error

	gpiocdev.EventHandler `json:"event-handler"`
	EvtQ                  chan gpiocdev.LineEvent
	// contains filtered or unexported fields
}

func NewDigitalPin

func NewDigitalPin(name string, offset int, opts ...gpiocdev.LineReqOption) *DigitalPin

func (*DigitalPin) Close

func (d *DigitalPin) Close() error

func (*DigitalPin) EventLoop

func (d *DigitalPin) EventLoop(done chan any, readpub func())

func (*DigitalPin) Get

func (pin *DigitalPin) Get() (int, error)

Get returns the value of the pin, an error is returned if the GPIO value fails. Note: you can Get() the value of an input pin so no direction checks are done

func (*DigitalPin) ID

func (p *DigitalPin) ID() string

func (*DigitalPin) Init

func (p *DigitalPin) Init() error

Init the pin from the offset and mode

func (*DigitalPin) MockHWInput

func (d *DigitalPin) MockHWInput(v int)

func (*DigitalPin) OFF

func (pin *DigitalPin) OFF() error

Off sets the value of the pin to 0

func (*DigitalPin) ON

func (pin *DigitalPin) ON() error

func (*DigitalPin) PinName

func (p *DigitalPin) PinName() string

func (*DigitalPin) Set

func (pin *DigitalPin) Set(v int) error

Set the value of the pin. Note: you can NOT set the value of an input pin, so we will check it and return an error. This maybe worthy of making it a panic!

func (*DigitalPin) SetOpts

func (p *DigitalPin) SetOpts(opts ...gpiocdev.LineReqOption)

func (*DigitalPin) String

func (pin *DigitalPin) String() string

String returns a string representation of the GPIO pin

func (*DigitalPin) Toggle

func (pin *DigitalPin) Toggle() error

Toggle with flip the value of the pin from 1 to 0 or 0 to 1

type GPIO

type GPIO struct {
	Chipname string `json:"chipname"`

	Mock bool `json:"mock"`
	// contains filtered or unexported fields
}

GPIO is used to initialize the GPIO and pins on a raspberry pi

func GetGPIO

func GetGPIO() *GPIO

GetGPIO returns the GPIO singleton for the Raspberry PI

func (*GPIO) Close

func (gpio *GPIO) Close() error

Shutdown resets the GPIO line allowing use by another program

func (*GPIO) JSON

func (gpio *GPIO) JSON() (j []byte, err error)

JSON returns the JSON representation of the GPIO

func (*GPIO) Pin

func (gpio *GPIO) Pin(name string, offset int, opts ...gpiocdev.LineReqOption) *DigitalPin

Pin initializes the given GPIO pin, name and mode

func (*GPIO) String

func (gpio *GPIO) String() string

String returns the string representation of the GPIO

type Line

type Line interface {
	Close() error
	Offset() int
	SetValue(int) error
	Reconfigure(...gpiocdev.LineConfigOption) error
	Value() (int, error)
}

Line interface is used to emulate a GPIO pin as implemented by the go-gpiocdev package

type MockAnalogPin

type MockAnalogPin struct {
	PinName string
	Offset  int
	// contains filtered or unexported fields
}

func NewMockAnalogPin

func NewMockAnalogPin(name string, pin int, opts ...any) *MockAnalogPin

func (*MockAnalogPin) Close

func (a *MockAnalogPin) Close() error

func (*MockAnalogPin) Read

func (a *MockAnalogPin) Read() (float64, error)

func (*MockAnalogPin) ReadContinuous

func (a *MockAnalogPin) ReadContinuous() <-chan float64

type MockLine

type MockLine struct {
	Val                   int `json:"val"`
	gpiocdev.EventHandler `json:"event-handler"`
	// contains filtered or unexported fields
}

MockGPIO fakes the Line interface on computers that don't actually have GPIO pins mostly for mocking tests

func GetMockLine

func GetMockLine(offset int, opts ...gpiocdev.LineReqOption) *MockLine

func (MockLine) Close

func (m MockLine) Close() error

func (*MockLine) MockHWInput

func (m *MockLine) MockHWInput(v int)

func (MockLine) Offset

func (m MockLine) Offset() int

func (MockLine) Reconfigure

func (m MockLine) Reconfigure(...gpiocdev.LineConfigOption) error

func (*MockLine) SetValue

func (m *MockLine) SetValue(val int) error

func (MockLine) Value

func (m MockLine) Value() (int, error)

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)

Jump to

Keyboard shortcuts

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