board

package
v0.2.10 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2022 License: AGPL-3.0 Imports: 22 Imported by: 17

Documentation

Overview

Package board defines the interfaces that typically live on a single-board computer such as a Raspberry Pi.

Besides the board itself, some other interfaces it defines are analog readers and digital interrupts.

Package board contains a gRPC based board client.

Package board contains a gRPC based Board service server.

Index

Constants

View Source
const ServoRollingAverageWindow = 10

ServoRollingAverageWindow is how many entries to average over for servo ticks.

View Source
const SubtypeName = resource.SubtypeName("board")

SubtypeName is a constant that identifies the component resource subtype string "board".

Variables

Subtype is a constant that identifies the component resource subtype.

Functions

func CreateStatus

func CreateStatus(ctx context.Context, b Board, extra map[string]interface{}) (*commonpb.BoardStatus, error)

CreateStatus constructs a new up to date status from the given board. The operation can take time and be expensive, so it can be cancelled by the given context.

func DependencyTypeError

func DependencyTypeError(name string, actual interface{}) error

DependencyTypeError is used when a resource doesn't implement the expected interface.

func Named

func Named(name string) resource.Name

Named is a helper for getting the named board's typed resource name.

func NamesFromRobot

func NamesFromRobot(r robot.Robot) []string

NamesFromRobot is a helper for getting all board names from the given Robot.

func NewServer

func NewServer(s subtype.Service) pb.BoardServiceServer

NewServer constructs an board gRPC service server.

func NewUnimplementedInterfaceError

func NewUnimplementedInterfaceError(actual interface{}) error

NewUnimplementedInterfaceError is used when there is a failed interface check.

func WrapWithReconfigurable

func WrapWithReconfigurable(r interface{}, name resource.Name) (resource.Reconfigurable, error)

WrapWithReconfigurable converts a regular Board implementation to a reconfigurableBoard. If board is already a reconfigurableBoard, then nothing is done.

Types

type AnalogConfig

type AnalogConfig struct {
	Name              string `json:"name"`
	Pin               string `json:"pin"`         // analog input pin on the ADC itself
	SPIBus            string `json:"spi_bus"`     // name of the SPI bus (which is configured elsewhere in the config file)
	ChipSelect        string `json:"chip_select"` // the CS line for the ADC chip, typically a pin number on the board
	AverageOverMillis int    `json:"average_over_ms,omitempty"`
	SamplesPerSecond  int    `json:"samples_per_sec,omitempty"`
}

AnalogConfig describes the configuration of an analog reader on a board.

func (*AnalogConfig) Validate

func (config *AnalogConfig) Validate(path string) error

Validate ensures all parts of the config are valid.

type AnalogReader

type AnalogReader interface {
	// Read reads off the current value.
	Read(ctx context.Context, extra map[string]interface{}) (int, error)
}

An AnalogReader represents an analog pin reader that resides on a board.

func SmoothAnalogReader

func SmoothAnalogReader(r AnalogReader, c AnalogConfig, logger golog.Logger) AnalogReader

SmoothAnalogReader wraps the given reader in a smoother.

type AnalogSmoother

type AnalogSmoother struct {
	Raw               AnalogReader
	AverageOverMillis int
	SamplesPerSecond  int
	// contains filtered or unexported fields
}

An AnalogSmoother smooths the readings out from an underlying reader.

func (*AnalogSmoother) Close

func (as *AnalogSmoother) Close()

Close stops the smoothing routine.

func (*AnalogSmoother) Read

func (as *AnalogSmoother) Read(ctx context.Context, extra map[string]interface{}) (int, error)

Read returns the smoothed out reading.

func (*AnalogSmoother) Start

func (as *AnalogSmoother) Start(ctx context.Context)

Start begins the smoothing routine that reads from the underlying analog reader.

type BasicDigitalInterrupt

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

A BasicDigitalInterrupt records how many ticks/interrupts happen and can report when they happen to interested callbacks.

func (*BasicDigitalInterrupt) AddCallback

func (i *BasicDigitalInterrupt) AddCallback(c chan bool)

AddCallback adds a listener for interrupts.

func (*BasicDigitalInterrupt) AddPostProcessor

func (i *BasicDigitalInterrupt) AddPostProcessor(pp PostProcessor)

AddPostProcessor sets the post processor that will modify the value that Value returns.

func (*BasicDigitalInterrupt) Config

Config returns the config used to create this interrupt.

func (*BasicDigitalInterrupt) RemoveCallback

func (i *BasicDigitalInterrupt) RemoveCallback(c chan bool)

RemoveCallback removes a listener for interrupts.

func (*BasicDigitalInterrupt) Tick

func (i *BasicDigitalInterrupt) Tick(ctx context.Context, high bool, not uint64) error

Tick records an interrupt and notifies any interested callbacks.

func (*BasicDigitalInterrupt) Ticks

func (i *BasicDigitalInterrupt) Ticks(ctx context.Context, num int, now uint64) error

Ticks is really just for testing.

func (*BasicDigitalInterrupt) Value

func (i *BasicDigitalInterrupt) Value(ctx context.Context, extra map[string]interface{}) (int64, error)

Value returns the amount of ticks that have occurred.

type Board

type Board interface {
	// AnalogReaderByName returns an analog reader by name.
	AnalogReaderByName(name string) (AnalogReader, bool)

	// DigitalInterruptByName returns a digital interrupt by name.
	DigitalInterruptByName(name string) (DigitalInterrupt, bool)

	// GPIOPinByName returns a GPIOPin by name.
	GPIOPinByName(name string) (GPIOPin, error)

	// SPINames returns the names of all known SPI buses.
	SPINames() []string

	// I2CNames returns the names of all known I2C buses.
	I2CNames() []string

	// AnalogReaderNames returns the name of all known analog readers.
	AnalogReaderNames() []string

	// DigitalInterruptNames returns the name of all known digital interrupts.
	DigitalInterruptNames() []string

	// GPIOPinNames returns the names of all known GPIO pins.
	GPIOPinNames() []string

	// Status returns the current status of the board. Usually you
	// should use the CreateStatus helper instead of directly calling
	// this.
	Status(ctx context.Context, extra map[string]interface{}) (*commonpb.BoardStatus, error)

	// ModelAttributes returns attributes related to the model of this board.
	ModelAttributes() ModelAttributes

	generic.Generic
}

A Board represents a physical general purpose board that contains various components such as analog readers, and digital interrupts.

func FromDependencies

func FromDependencies(deps registry.Dependencies, name string) (Board, error)

FromDependencies is a helper for getting the named board from a collection of dependencies.

func FromRobot

func FromRobot(r robot.Robot, name string) (Board, error)

FromRobot is a helper for getting the named board from the given Robot.

func NewClientFromConn

func NewClientFromConn(ctx context.Context, conn rpc.ClientConn, name string, logger golog.Logger) Board

NewClientFromConn constructs a new Client from connection passed in.

type DigitalInterrupt

type DigitalInterrupt interface {
	// Value returns the current value of the interrupt which is
	// based on the type of interrupt.
	Value(ctx context.Context, extra map[string]interface{}) (int64, error)

	// Tick is to be called either manually if the interrupt is a proxy to some real
	// hardware interrupt or for tests.
	// nanos is from an arbitrary point in time, but always increasing and always needs
	// to be accurate. Using time.Now().UnixNano() would be acceptable, but is
	// not required.
	Tick(ctx context.Context, high bool, nanos uint64) error

	// AddCallback adds a callback to be sent a low/high value to when a tick
	// happens.
	// Note(erd): not all interrupts can have callbacks so this should probably be a
	// separate interface.
	AddCallback(c chan bool)

	// AddPostProcessor adds a post processor that should be used to modify
	// what is returned by Value.
	AddPostProcessor(pp PostProcessor)

	// RemoveCallback removes a listener for interrupts
	RemoveCallback(c chan bool)
}

A DigitalInterrupt represents a configured interrupt on the board that when interrupted, calls the added callbacks. Post processors can also be added to modify what Value ultimately returns.

func CreateDigitalInterrupt

func CreateDigitalInterrupt(cfg DigitalInterruptConfig) (DigitalInterrupt, error)

CreateDigitalInterrupt is a factory method for creating a specific DigitalInterrupt based on the given config. If no type is specified, a BasicDigitalInterrupt is returned.

type DigitalInterruptConfig

type DigitalInterruptConfig struct {
	Name    string `json:"name"`
	Pin     string `json:"pin"`
	Type    string `json:"type,omitempty"` // e.g. basic, servo
	Formula string `json:"formula,omitempty"`
}

DigitalInterruptConfig describes the configuration of digital interrupt for a board.

func (*DigitalInterruptConfig) Validate

func (config *DigitalInterruptConfig) Validate(path string) error

Validate ensures all parts of the config are valid.

type GPIOPin

type GPIOPin interface {
	// Set sets the pin to either low or high.
	Set(ctx context.Context, high bool, extra map[string]interface{}) error

	// Get gets the high/low state of the pin.
	Get(ctx context.Context, extra map[string]interface{}) (bool, error)

	// PWM gets the pin's given duty cycle.
	PWM(ctx context.Context, extra map[string]interface{}) (float64, error)

	// SetPWM sets the pin to the given duty cycle.
	SetPWM(ctx context.Context, dutyCyclePct float64, extra map[string]interface{}) error

	// PWMFreq gets the PWM frequency of the pin.
	PWMFreq(ctx context.Context, extra map[string]interface{}) (uint, error)

	// SetPWMFreq sets the given pin to the given PWM frequency. 0 will use the board's default PWM frequency.
	SetPWMFreq(ctx context.Context, freqHz uint, extra map[string]interface{}) error
}

A GPIOPin represents an individual GPIO pin on a board.

type I2C

type I2C interface {
	// OpenHandle locks returns a handle interface that MUST be closed when done.
	// you cannot have 2 open for the same addr
	OpenHandle(addr byte) (I2CHandle, error)
}

I2C represents a shareable I2C bus on the board.

type I2CConfig

type I2CConfig struct {
	Name string `json:"name"`
	Bus  string `json:"bus"`
}

I2CConfig enumerates a specific, shareable I2C bus.

func (*I2CConfig) Validate

func (config *I2CConfig) Validate(path string) error

Validate ensures all parts of the config are valid.

type I2CHandle

type I2CHandle interface {
	Write(ctx context.Context, tx []byte) error
	Read(ctx context.Context, count int) ([]byte, error)

	ReadByteData(ctx context.Context, register byte) (byte, error)
	WriteByteData(ctx context.Context, register, data byte) error

	ReadWordData(ctx context.Context, register byte) (uint16, error)
	WriteWordData(ctx context.Context, register byte, data uint16) error

	ReadBlockData(ctx context.Context, register byte, numBytes uint8) ([]byte, error)
	WriteBlockData(ctx context.Context, register byte, numBytes uint8, data []byte) error

	// Close closes the handle and releases the lock on the bus.
	Close() error
}

I2CHandle is similar to an io handle. It MUST be closed to release the bus.

type I2CRegister

type I2CRegister struct {
	Handle   I2CHandle
	Register byte
}

An I2CRegister is a lightweight wrapper around a handle for a particular register.

func (*I2CRegister) ReadByteData

func (reg *I2CRegister) ReadByteData(ctx context.Context) (byte, error)

ReadByteData reads a byte from the I2C channel register.

func (*I2CRegister) ReadWordData

func (reg *I2CRegister) ReadWordData(ctx context.Context) (uint16, error)

ReadWordData reads a word from the I2C channel register.

func (*I2CRegister) WriteByteData

func (reg *I2CRegister) WriteByteData(ctx context.Context, data byte) error

WriteByteData writes a byte to the I2C channel register.

func (*I2CRegister) WriteWordData

func (reg *I2CRegister) WriteWordData(ctx context.Context, data uint16) error

WriteWordData writes a word to the I2C channel register.

type LocalBoard

type LocalBoard interface {
	Board

	// SPIByName returns an SPI bus by name.
	SPIByName(name string) (SPI, bool)

	// I2CByName returns an I2C bus by name.
	I2CByName(name string) (I2C, bool)
}

A LocalBoard represents a Board where you can request SPIs and I2Cs by name.

type MCP3008AnalogReader

type MCP3008AnalogReader struct {
	Channel int
	Bus     SPI
	Chip    string
}

MCP3008AnalogReader implements a board.AnalogReader using an MCP3008 ADC via SPI.

func (*MCP3008AnalogReader) Read

func (mar *MCP3008AnalogReader) Read(ctx context.Context, extra map[string]interface{}) (value int, err error)

type ModelAttributes

type ModelAttributes struct {
	// Remote signifies this board is accessed over a remote connection.
	// e.g. gRPC
	Remote bool
}

ModelAttributes provide info related to a board model.

type PostProcessor

type PostProcessor func(raw int64) int64

A PostProcessor takes a raw input and transforms it into a new value. Multiple post processors can be stacked on each other. This is currently only used in DigitalInterrupt readings.

type SPI

type SPI interface {
	// OpenHandle locks the shared bus and returns a handle interface that MUST be closed when done.
	OpenHandle() (SPIHandle, error)
}

SPI represents a shareable SPI bus on the board.

type SPIConfig

type SPIConfig struct {
	Name      string `json:"name"`
	BusSelect string `json:"bus_select"` // "0" or "1" for main/aux in libpigpio
}

SPIConfig enumerates a specific, shareable SPI bus.

func (*SPIConfig) Validate

func (config *SPIConfig) Validate(path string) error

Validate ensures all parts of the config are valid.

type SPIHandle

type SPIHandle interface {
	// Xfer performs a single SPI transfer, that is, the complete transaction from chipselect enable to chipselect disable.
	// SPI transfers are synchronous, number of bytes received will be equal to the number of bytes sent.
	// Write-only transfers can usually just discard the returned bytes.
	// Read-only transfers usually transmit a request/address and continue with some number of null bytes to equal the expected size of the
	// returning data.
	// Large transmissions are usually broken up into multiple transfers.
	// There are many different paradigms for most of the above, and implementation details are chip/device specific.
	Xfer(
		ctx context.Context,
		baud uint,
		chipSelect string,
		mode uint,
		tx []byte,
	) ([]byte, error)
	// Close closes the handle and releases the lock on the bus.
	Close() error
}

SPIHandle is similar to an io handle. It MUST be closed to release the bus.

type ServoDigitalInterrupt

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

A ServoDigitalInterrupt is an interrupt associated with a servo in order to track the amount of time that has passed between low signals (pulse width). Post processors make meaning of these widths.

func (*ServoDigitalInterrupt) AddCallback

func (i *ServoDigitalInterrupt) AddCallback(c chan bool)

AddCallback currently panics.

func (*ServoDigitalInterrupt) AddPostProcessor

func (i *ServoDigitalInterrupt) AddPostProcessor(pp PostProcessor)

AddPostProcessor sets the post processor that will modify the value that Value returns.

func (*ServoDigitalInterrupt) Config

Config returns the config the interrupt was created with.

func (*ServoDigitalInterrupt) RemoveCallback

func (i *ServoDigitalInterrupt) RemoveCallback(c chan bool)

RemoveCallback currently panics.

func (*ServoDigitalInterrupt) Tick

func (i *ServoDigitalInterrupt) Tick(ctx context.Context, high bool, now uint64) error

Tick records the time between two successive low signals (pulse width). How it is interpreted is based off the consumer of Value.

func (*ServoDigitalInterrupt) Value

func (i *ServoDigitalInterrupt) Value(ctx context.Context, extra map[string]interface{}) (int64, error)

Value will return the window averaged value followed by its post processed result.

Directories

Path Synopsis
Package arduino implements the arduino board and some peripherals.
Package arduino implements the arduino board and some peripherals.
Package beaglebone implements a beaglebone based board.
Package beaglebone implements a beaglebone based board.
Package commonsysfs implements a sysfs (https://en.wikipedia.org/wiki/Sysfs) based board.
Package commonsysfs implements a sysfs (https://en.wikipedia.org/wiki/Sysfs) based board.
Package fake implements a fake board.
Package fake implements a fake board.
hat
pca9685
Package pca9685 implements a PCA9685 HAT.
Package pca9685 implements a PCA9685 HAT.
Package jetson implements a jetson based board.
Package jetson implements a jetson based board.
Package numato is for numato IO boards.
Package numato is for numato IO boards.
pi
Package pi implements a Board and its related interfaces for a Raspberry Pi.
Package pi implements a Board and its related interfaces for a Raspberry Pi.
common
Package picommon contains shared information for supported and non-supported pi boards.
Package picommon contains shared information for supported and non-supported pi boards.
Package register registers all relevant Boards and also subtype specific functions
Package register registers all relevant Boards and also subtype specific functions
Package ti implements a ti based board.
Package ti implements a ti based board.

Jump to

Keyboard shortcuts

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