gopi

package module
v2.0.4 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2020 License: BSD-2-Clause Imports: 11 Imported by: 33

README

GOPI Go Language Application Framework

CircleCI

This repository contains an application framework for the Go language, which will allow you to develop applications which utilize a number of features of your computer. It's targetted at the Raspberry Pi presently. The following features are intended to be supported:

  • The GPIO, I2C and SPI interfaces
  • Display and display surfaces, bitmaps and vector graphics
  • GPU acceleration for 2D graphics
  • Font loading and rendering in bitmap and vector forms
  • Input devices like the mouse, keyboard and touchscreen
  • Infrared transmission and receiving, for example for remote controls
  • Network microservices, announcement and discovery

It would also be great to support the following features in the future:

  • Image and video encoding/decoding, including utilizing hardware acceleration
  • Connected cameras
  • 3D graphics
  • Audio devices
  • User interface widgets and layout
  • Building for Darwin (Macintosh) targets

Documentation

Please read the documentation at gopi.mutablelogic.com for all the relevant information about requirements, getting started and contributing.

Contributing and License

This repository is released under the Apache License. Please see the LICENSE file in the root of the repository for a copy of this license and for a list of the conditions for redistribution and use.

I welcome contributions, feature requests and issues. In order to contribute, please fork and send pull requests. Feel free to contact me for feature requests and bugs by filing issues here.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func UnitRegister

func UnitRegister(unit UnitConfig)

func UnitReset added in v2.0.2

func UnitReset()

Types

type App

type App interface {

	// Run the application and return the return code
	Run() int

	// WaitForSignal stalls the application until a signal is returned,
	// context is cancelled or deadline exceeded
	WaitForSignal(context.Context, ...os.Signal) error // Wait for interrupt signal with context

	// Flags returns the set of key/value configuration parameters
	Flags() Flags

	// Emit events
	Emit(Event)

	// Return unit instances
	UnitInstance(string) Unit // Return singular unit for name
	Log() Logger              // Return logger unit
	Timer() Timer             // Return timer unit
	Bus() Bus                 // Return event bus unit
	Platform() Platform       // Return hardware platform unit
	Display() Display         // Return display
	I2C() I2C                 // Return I2C interface
}

App encapsulates the lifecycle of a running application which is created through the 'app' package and run by calling the Run method. A running application has access to unit instances (for example, logger, timer and event bus).

type Bus

type Bus interface {
	Unit

	// Emit an event on the bus
	Emit(Event)

	// NewHandler registers an event handler for an event name
	NewHandler(EventHandler) error

	// DefaultHandler registers a default handler for an event namespace
	DefaultHandler(EventNS, EventHandlerFunc) error
}

Bus unit - handles events

type Channel added in v2.0.2

type Channel chan struct{}

Channel is an arbitary communication channel

type CompoundError added in v2.0.2

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

CompoundError represents a set of errors

func NewCompoundError added in v2.0.2

func NewCompoundError(errs ...error) *CompoundError

func (*CompoundError) Add added in v2.0.2

func (this *CompoundError) Add(err error) error

func (*CompoundError) Error added in v2.0.2

func (this *CompoundError) Error() string

func (*CompoundError) ErrorOrSelf added in v2.0.2

func (this *CompoundError) ErrorOrSelf() error

func (*CompoundError) Is added in v2.0.2

func (this *CompoundError) Is(other error) bool

type Config

type Config interface {
	Name() string             // Returns name of the unit
	New(Logger) (Unit, error) // Opens the driver from configuration, or returns error
}

Unit configuration interface

type ConfigFunc

type ConfigFunc func(App) error

type Display added in v2.0.2

type Display interface {
	// Return display number
	DisplayId() uint

	// Return name of the display
	Name() string

	// Return display size for nominated display number, or (0,0) if display does not exist
	Size() (uint32, uint32)

	// Return the PPI (pixels-per-inch) for the display, or return zero if unknown
	PixelsPerInch() uint32

	// Implements gopi.Unit
	Unit
}

Display implements a pixel-based display device. Displays are always numbered from zero onwards

type Error

type Error uint

Error represents a gopi error

const (
	ErrNone               Error = iota // No error condition
	ErrNotImplemented                  // Method or feature not implemented
	ErrBadParameter                    // Error with parameter passed to method
	ErrNotFound                        // Missing object
	ErrHelp                            // Help requested from command line
	ErrInternalAppError                // Internal application error
	ErrSignalCaught                    // Signal caught
	ErrUnexpectedResponse              // Unexpected Response
	ErrDuplicateItem                   // Duplicate Item
	ErrMax                = ErrDuplicateItem
)

func (Error) Error

func (this Error) Error() string

func (Error) WithPrefix

func (this Error) WithPrefix(prefix string) error

type Event

type Event interface {
	Source() Unit       // Source of the event
	Name() string       // Name of the event
	NS() EventNS        // Namespace for the event
	Value() interface{} // Any value associated with the event
}

Event emitted on the event bus

var (
	NullEvent Event
)

type EventHandler

type EventHandler struct {
	// The name of the event
	Name string

	// The handler function for the event
	Handler EventHandlerFunc

	// The namespace of the event, usually 0
	EventNS EventNS

	// The timeout value for the handler, usually 0
	Timeout time.Duration
}

EventHandler defines how an emitted event is handled in the application

type EventHandlerFunc added in v2.0.2

type EventHandlerFunc func(context.Context, App, Event)

EventHandlerFunc is the handler for an emitted event, which should cancel when context.Done() signal is received

type EventId added in v2.0.2

type EventId uint

EventId is a unique ID for each event

type EventNS

type EventNS uint

EventNS is the namespace in which events are emitted, usually EVENT_NS_DEFAULT

const (
	EVENT_NS_DEFAULT EventNS = iota // Default event namespace
	EVENT_NS_MAX             = EVENT_NS_DEFAULT
)

func (EventNS) String added in v2.0.2

func (v EventNS) String() string

type FlagNS added in v2.0.2

type FlagNS uint

FlagNS is the namespace for a flag

const (
	FLAG_NS_DEFAULT FlagNS = iota
	FLAG_NS_VERSION
)

func (FlagNS) String added in v2.0.2

func (v FlagNS) String() string

type Flags

type Flags interface {
	Name() string                // Return name of tool
	Parse([]string) error        // Parse command-line flags
	Parsed() bool                // Returns true if Parsed() has been called
	Args() []string              // Return command-line arguments
	HasFlag(string, FlagNS) bool // HasFlag returns true if a flag exists

	Usage(io.Writer)   // Write out usage for the application
	Version(io.Writer) // Write out version for the application

	// Define flags in default namespace
	FlagBool(name string, value bool, usage string) *bool
	FlagString(name, value, usage string) *string
	FlagDuration(name string, value time.Duration, usage string) *time.Duration
	FlagInt(name string, value int, usage string) *int
	FlagUint(name string, value uint, usage string) *uint
	FlagFloat64(name string, value float64, usage string) *float64

	// Get flag values
	GetBool(string, FlagNS) bool
	GetString(string, FlagNS) string
	GetDuration(string, FlagNS) time.Duration
	GetInt(string, FlagNS) int
	GetUint(string, FlagNS) uint
	GetFloat64(string, FlagNS) float64

	// Set flag values
	SetBool(string, FlagNS, bool)
	SetString(string, FlagNS, string)
	SetDuration(string, FlagNS, time.Duration)
	SetInt(string, FlagNS, int)
	SetUint(string, FlagNS, uint)
	SetFloat64(string, FlagNS, float64)
}

Flags encapsulates a set of key/value pairs in several namespaces with parsing of command-line flags in the default namespace

type GPIO added in v2.0.2

type GPIO interface {
	// Return number of physical pins, or 0 if if cannot be returned
	// or nothing is known about physical pins
	NumberOfPhysicalPins() uint

	// Return array of available logical pins or nil if nothing is
	// known about pins
	Pins() []GPIOPin

	// Return logical pin for physical pin number. Returns
	// GPIO_PIN_NONE where there is no logical pin at that position
	// or we don't now about the physical pins
	PhysicalPin(uint) GPIOPin

	// Return physical pin number for logical pin. Returns 0 where there
	// is no physical pin for this logical pin, or we don't know anything
	// about the layout
	PhysicalPinForPin(GPIOPin) uint

	// Read pin state
	ReadPin(GPIOPin) GPIOState

	// Write pin state
	WritePin(GPIOPin, GPIOState)

	// Get pin mode
	GetPinMode(GPIOPin) GPIOMode

	// Set pin mode
	SetPinMode(GPIOPin, GPIOMode)

	// Set pull mode to pull down or pull up - will
	// return ErrNotImplemented if not supported
	SetPullMode(GPIOPin, GPIOPull) error

	// Start watching for rising and/or falling edge,
	// or stop watching when GPIO_EDGE_NONE is passed.
	// Will return ErrNotImplemented if not supported
	Watch(GPIOPin, GPIOEdge) error

	// Implements gopi.Unit
	Unit
}

GPIO implements the GPIO interface for simple input and output

type GPIOEdge added in v2.0.2

type GPIOEdge uint8

GPIOEdge is a rising or falling edge

const (
	GPIO_EDGE_NONE GPIOEdge = iota
	GPIO_EDGE_RISING
	GPIO_EDGE_FALLING
	GPIO_EDGE_BOTH
)

func (GPIOEdge) String added in v2.0.2

func (e GPIOEdge) String() string

type GPIOMode added in v2.0.2

type GPIOMode uint8

GPIOMode is the GPIO Pin mode

const (
	// Set pin mode and/or function
	GPIO_INPUT GPIOMode = iota
	GPIO_OUTPUT
	GPIO_ALT5
	GPIO_ALT4
	GPIO_ALT0
	GPIO_ALT1
	GPIO_ALT2
	GPIO_ALT3
	GPIO_NONE
)

func (GPIOMode) String added in v2.0.2

func (m GPIOMode) String() string

type GPIOPin added in v2.0.2

type GPIOPin uint8

GPIOPin is the logical GPIO pin

const (
	// Invalid pin constant
	GPIO_PIN_NONE GPIOPin = 0xFF
)

func (GPIOPin) String added in v2.0.2

func (p GPIOPin) String() string

type GPIOPull added in v2.0.2

type GPIOPull uint8

GPIOPull is the GPIO Pin resistor configuration (pull up/down or floating)

const (
	GPIO_PULL_OFF GPIOPull = iota
	GPIO_PULL_DOWN
	GPIO_PULL_UP
)

func (GPIOPull) String added in v2.0.2

func (p GPIOPull) String() string

type GPIOState added in v2.0.2

type GPIOState uint8

GPIOState is the GPIO Pin state

const (
	GPIO_LOW GPIOState = iota
	GPIO_HIGH
)

func (GPIOState) String added in v2.0.2

func (s GPIOState) String() string

type I2C added in v2.0.2

type I2C interface {

	// Set current slave address
	SetSlave(uint8) error

	// Get current slave address
	GetSlave() uint8

	// Return true if a slave was detected at a particular address
	DetectSlave(uint8) (bool, error)

	// Read Byte (8-bits), Word (16-bits) & Block ([]byte) from registers
	ReadUint8(reg uint8) (uint8, error)
	ReadInt8(reg uint8) (int8, error)
	ReadUint16(reg uint8) (uint16, error)
	ReadInt16(reg uint8) (int16, error)
	ReadBlock(reg, length uint8) ([]byte, error)

	// Write Byte (8-bits) & Word (16-bits) to registers
	WriteUint8(reg, value uint8) error
	WriteInt8(reg uint8, value int8) error
	WriteUint16(reg uint8, value uint16) error
	WriteInt16(reg uint8, value int16) error

	// Implements gopi.Unit
	Unit
}

I2C implements the I2C interface for sensors, etc.

type LIRC added in v2.0.2

type LIRC interface {
	// Get receive and send modes
	RcvMode() LIRCMode
	SendMode() LIRCMode
	SetRcvMode(mode LIRCMode) error
	SetSendMode(mode LIRCMode) error

	// Receive parameters
	GetRcvResolution() (uint32, error)
	SetRcvTimeout(micros uint32) error
	SetRcvTimeoutReports(enable bool) error
	SetRcvCarrierHz(value uint32) error
	SetRcvCarrierRangeHz(min uint32, max uint32) error

	// Send parameters
	SetSendCarrierHz(value uint32) error
	SetSendDutyCycle(value uint32) error

	// Send Pulse Mode, values are in milliseconds
	PulseSend(values []uint32) error

	// Implements gopi.Unit
	Unit
}

LIRC implements the IR send & receive interface

type LIRCMode added in v2.0.2

type LIRCMode uint32

LIRCMode is the LIRC Mode

const (
	LIRC_MODE_NONE     LIRCMode = 0x00000000
	LIRC_MODE_RAW      LIRCMode = 0x00000001
	LIRC_MODE_PULSE    LIRCMode = 0x00000002 // send only
	LIRC_MODE_MODE2    LIRCMode = 0x00000004 // rcv only
	LIRC_MODE_LIRCCODE LIRCMode = 0x00000010 // rcv only
	LIRC_MODE_MAX      LIRCMode = LIRC_MODE_LIRCCODE
)

func (LIRCMode) String added in v2.0.2

func (m LIRCMode) String() string

type LIRCType added in v2.0.2

type LIRCType uint32

LIRCType is the LIRC Type

const (
	LIRC_TYPE_SPACE     LIRCType = 0x00000000
	LIRC_TYPE_PULSE     LIRCType = 0x01000000
	LIRC_TYPE_FREQUENCY LIRCType = 0x02000000
	LIRC_TYPE_TIMEOUT   LIRCType = 0x03000000
	LIRC_TYPE_MAX       LIRCType = LIRC_TYPE_TIMEOUT
)

func (LIRCType) String added in v2.0.2

func (t LIRCType) String() string

type Logger

type Logger interface {
	Unit

	// Name returns the name of the logger
	Name() string

	// Clone returns a new logger with a different name
	Clone(string) Logger

	// Error logs an error, and returns an error with the name prefixed
	Error(error) error

	// Debug will log a debug message when debugging is on
	Debug(args ...interface{})

	// IsDebug returns true if debugging is enabled
	IsDebug() bool
}

Abstract logging interface

type MainCommandFunc

type MainCommandFunc func(App, []string) error

MainCommandFunc is the main handler for command line tool

type MainTestFunc added in v2.0.2

type MainTestFunc func(App, *testing.T)

MainTestFunc is the main handler for a testing tool

type NewFunc added in v2.0.2

type NewFunc func(App) (Unit, error)

type PWM added in v2.0.2

type PWM interface {
	// Return array of pins which are enabled for PWM
	Pins() []GPIOPin

	// Period
	Period(GPIOPin) (time.Duration, error)
	SetPeriod(time.Duration, ...GPIOPin) error

	// Duty Cycle between 0.0 and 1.0 (0.0 is always off, 1.0 is always on)
	DutyCycle(GPIOPin) (float32, error)
	SetDutyCycle(float32, ...GPIOPin) error

	// Implements gopi.Unit
	Unit
}

PWM implements the PWM interface for actuators, motors, etc.

type Platform added in v2.0.2

type Platform interface {

	// Product returns product name
	Product() string

	// Type returns flags identifying platform type
	Type() PlatformType

	// SerialNumber returns unique serial number for host
	SerialNumber() string

	// Uptime returns uptime for host
	Uptime() time.Duration

	// LoadAverages returns 1, 5 and 15 minute load averages
	LoadAverages() (float64, float64, float64)

	// NumberOfDisplays returns the number of possible displays for this host
	NumberOfDisplays() uint

	// Implements gopi.Unit
	Unit
}

type PlatformType added in v2.0.2

type PlatformType uint
const (
	PLATFORM_NONE   PlatformType = 0
	PLATFORM_DARWIN PlatformType = (1 << iota) >> 1
	PLATFORM_RPI
	PLATFORM_LINUX
	PLATFORM_MIN = PLATFORM_DARWIN
	PLATFORM_MAX = PLATFORM_LINUX
)

func (PlatformType) FlagString added in v2.0.2

func (p PlatformType) FlagString() string

func (PlatformType) String added in v2.0.2

func (p PlatformType) String() string

type Publisher added in v2.0.2

type Publisher interface {
	// Emit sends values to be received by handlers
	Emit(queue uint, value interface{})

	// Subscribe and handle messages which are emitted. This method call returns
	// when the receive loop has started as a goroutine, so is non-blocking.
	Subscribe(queue uint, capacity int, callback func(value interface{})) Channel

	// Unsubscribe from a channel. This method will return when the receive loop
	// has completed
	Unsubscribe(Channel)
}

Publisher interface for Emit/Receive mechanism for arbitary messages

type RPCEvent added in v2.0.2

type RPCEvent interface {
	// Type of event
	Type() RPCEventType

	// Service record associated with event
	Service() RPCServiceRecord

	// Time-to-live value for event
	TTL() time.Duration

	Event
}

type RPCEventType added in v2.0.2

type RPCEventType uint // RPCEventType is an enumeration of event types
const (
	RPC_EVENT_NONE            RPCEventType = iota
	RPC_EVENT_SERVER_STARTED               // RPC Server started
	RPC_EVENT_SERVER_STOPPED               // RPC Server stopped
	RPC_EVENT_SERVICE_ADDED                // Service instance lookup (new)
	RPC_EVENT_SERVICE_UPDATED              // Service instance lookup (updated)
	RPC_EVENT_SERVICE_REMOVED              // Service instance lookup (removed)
	RPC_EVENT_SERVICE_EXPIRED              // Service instance lookup (expired)
	RPC_EVENT_SERVICE_NAME                 // Service name discovered
	RPC_EVENT_SERVICE_RECORD               // Service record lookup
	RPC_EVENT_CLIENT_CONNECTED
	RPC_EVENT_CLIENT_DISCONNECTED
	RPC_EVENT_MAX = RPC_EVENT_CLIENT_DISCONNECTED
)

func (RPCEventType) String added in v2.0.2

func (t RPCEventType) String() string

type RPCFlag added in v2.0.2

type RPCFlag uint // RPCFlag is a set of flags modifying behavior
const (
	RPC_FLAG_NONE          RPCFlag = 0
	RPC_FLAG_INET_UDP      RPCFlag = (1 << iota) >> 1 // Use UDP protocol (TCP assumed otherwise)
	RPC_FLAG_INET_V4                                  // Use V4 addressing
	RPC_FLAG_INET_V6                                  // Use V6 addressing
	RPC_FLAG_SERVICE_FIRST                            // Use first service
	RPC_FLAG_SERVICE_ANY                              // Use any service
	RPC_FLAG_MIN           = RPC_FLAG_INET_UDP
	RPC_FLAG_MAX           = RPC_FLAG_SERVICE_ANY
)

func (RPCFlag) FlagString added in v2.0.2

func (v RPCFlag) FlagString() string

func (RPCFlag) String added in v2.0.2

func (f RPCFlag) String() string

type RPCServiceDiscovery added in v2.0.2

type RPCServiceDiscovery interface {
	// Lookup service instances by name
	Lookup(ctx context.Context, service string) ([]RPCServiceRecord, error)

	// Return list of service names
	EnumerateServices(ctx context.Context) ([]string, error)
}

RPCServiceDiscovery will lookup services and classes of service

type RPCServiceRecord added in v2.0.2

type RPCServiceRecord struct {
	Name    string
	Service string
	Host    string
	Port    uint16
	Addrs   []net.IP
	Txt     []string
}

RPCServiceRecord defines a service which can be registered or discovered on the network

func (RPCServiceRecord) String added in v2.0.2

func (this RPCServiceRecord) String() string

type RPCServiceRegister added in v2.0.2

type RPCServiceRegister interface {
	// Register service record, and de-register when deadline is exceeded
	Register(ctx context.Context, record RPCServiceRecord) error
}

RPCServiceRegister will register services

type SPI added in v2.0.2

type SPI interface {
	// Get SPI mode
	Mode() SPIMode
	// Get SPI speed
	MaxSpeedHz() uint32
	// Get Bits Per Word
	BitsPerWord() uint8
	// Set SPI mode
	SetMode(SPIMode) error
	// Set SPI speed
	SetMaxSpeedHz(uint32) error
	// Set Bits Per Word
	SetBitsPerWord(uint8) error

	// Read/Write
	Transfer(send []byte) ([]byte, error)

	// Read
	Read(len uint32) ([]byte, error)

	// Write
	Write(send []byte) error

	// Implements gopi.Unit
	Unit
}

SPI implements the SPI interface for sensors, etc.

type SPIMode added in v2.0.2

type SPIMode uint8

SPIMode is the SPI Mode

const (
	SPI_MODE_CPHA SPIMode = 0x01
	SPI_MODE_CPOL SPIMode = 0x02
	SPI_MODE_0    SPIMode = 0x00
	SPI_MODE_1    SPIMode = (0x00 | SPI_MODE_CPHA)
	SPI_MODE_2    SPIMode = (SPI_MODE_CPOL | 0x00)
	SPI_MODE_3    SPIMode = (SPI_MODE_CPOL | SPI_MODE_CPHA)
	SPI_MODE_NONE SPIMode = 0xFF
)

func (SPIMode) String added in v2.0.2

func (m SPIMode) String() string

type Timer

type Timer interface {
	Unit

	NewTicker(time.Duration) EventId // Create periodic event at interval
	NewTimer(time.Duration) EventId  // Create one-shot event after interval
	Cancel(EventId) error            // Cancel events
}

Timer unit - sends out messages on the event bus

type Unit

type Unit interface {
	Close() error   // Close closes the driver and frees the underlying resources
	String() string // String returns a string representation of the unit
}

Unit interface

func New

func New(config Config, log Logger) (Unit, error)

type UnitConfig

type UnitConfig struct {
	Name     string   // Unique name of the unit
	Type     UnitType // Unit type
	Requires []string // Unit dependencies
	Config   ConfigFunc
	New      NewFunc
	// contains filtered or unexported fields
}

func UnitWithDependencies added in v2.0.2

func UnitWithDependencies(unitNames ...string) ([]*UnitConfig, error)

UnitWithDependencies returns units with all dependencies satisfied in reverse order of when they need to be configured and initialized

func UnitsByName

func UnitsByName(unitName string) []*UnitConfig

func UnitsByType

func UnitsByType(unitType UnitType) []*UnitConfig

func (UnitConfig) String

func (u UnitConfig) String() string

type UnitType

type UnitType uint
const (
	UNIT_NONE UnitType = iota
	UNIT_LOGGER
	UNIT_TIMER
	UNIT_BUS
	UNIT_PLATFORM
	UNIT_DISPLAY
	UNIT_I2C
	UNIT_LIRC
	UNIT_GPIO
	UNIT_SPI
	UNIT_RPC_DISCOVERY
	UNIT_RPC_REGISTER
	UNIT_MAX = UNIT_PLATFORM
)

func (UnitType) String

func (v UnitType) String() string

Directories

Path Synopsis
cmd
sys
rpi
unit
bus
i2c

Jump to

Keyboard shortcuts

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