hal

package module
v0.0.0-...-7d44144 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2022 License: Apache-2.0 Imports: 6 Imported by: 52

README

reef-pi - Hardware Abstraction Layer

Build Status Coverage Status Go Report Card License GoDoc

Introduction

The hal package provide common types for hardware capabilities in reef-pi. It hides device specific details from the controller logic. reef-pi modules like ATO, and pH, uses hal interfaces to perform hardware based operations. The HAL interfaces cover metdata and methods for digital or analog I/O. Check out reef-pi/drivers for examples.

'hal' is mostly made of interfaces and void of business logic and or dependency package. A No-Op or null driver is included to ease testing.

Usage

import(
  "github.com/reef-pi/hal"
)

func main() {
  var d hal.Driver = hal.NewNoopDriver()
  d.Metadata()
  defer d.Close()

  input, _ := d.(hal.InputDriver)
  pin, _ := input.InputPin("GP4")
  v, _ := pin.Read()
  for _, pin := range input.InputPins() {
    fmt.Println(pin.Name())
  }

  output, _ := d.(hal.OutputDriver)
  pin, _ := output.OutputPin("GP4")
  pin.Write(false)

  var pwm PWMDriver = hal.NewNoopDrive()
  ch, _ := pwm.PWMChannel("foo")
  ch.Set(10.23)
  for _, ch := range pwm.PWMChannels() {
     fmt.Println(ch.Name())
  }
}


Documentation

Overview

Package hal defines the interfaces for implementing a driver in reef-pi

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertToInt

func ConvertToInt(val interface{}) (int, bool)

ConvertToInt converts an interface to int if possible.

func NewDigitalNoopPin

func NewDigitalNoopPin() *noopDigitalPin

func ToErrorString

func ToErrorString(failures map[string][]string) string

ToErrorString converts a map of failures to a single string

Types

type AnalogInputDriver

type AnalogInputDriver interface {
	Driver
	AnalogInputPins() []AnalogInputPin
	AnalogInputPin(int) (AnalogInputPin, error)
}

type AnalogInputPin

type AnalogInputPin interface {
	Pin
	Value() (float64, error)
	Calibrate([]Measurement) error
	Measure() (float64, error)
}

type Calibrator

type Calibrator interface {
	Calibrate(float64) float64
}

func CalibratorFactory

func CalibratorFactory(points []Measurement) (Calibrator, error)

type Capability

type Capability int

Capability represents the capabilities of a driver

const (
	None Capability = iota
	DigitalInput
	DigitalOutput
	PWM
	AnalogInput
)

func (Capability) String

func (c Capability) String() string

type ConfigParameter

type ConfigParameter struct {
	Name    string              `json:"name"`
	Type    ConfigParameterType `json:"type"`
	Order   int                 `json:"order"`
	Default interface{}         `json:"default"`
}

ConfigParameter represent a configuration parameter required by a driver

type ConfigParameterType

type ConfigParameterType int

ConfigParameterType indicates the type of a configuration parameter

const (
	String ConfigParameterType = iota + 1
	Integer
	Decimal
	Boolean
)

type DigitalInputDriver

type DigitalInputDriver interface {
	Driver
	DigitalInputPins() []DigitalInputPin
	DigitalInputPin(int) (DigitalInputPin, error)
}

type DigitalInputPin

type DigitalInputPin interface {
	Pin
	// Read returns whether this input is logical high (true) or low (false)
	Read() (bool, error)
}

InputPin represents an input pin with a single digital input value.

type DigitalOutputDriver

type DigitalOutputDriver interface {
	Driver
	DigitalOutputPins() []DigitalOutputPin
	DigitalOutputPin(int) (DigitalOutputPin, error)
}

type DigitalOutputPin

type DigitalOutputPin interface {
	Pin
	Write(state bool) error
	LastState() bool
}

type Driver

type Driver interface {
	io.Closer
	Metadata() Metadata
	Pins(Capability) ([]Pin, error)
}

type DriverFactory

type DriverFactory interface {

	//GetParameters returns the parameters that the driver expects.
	GetParameters() []ConfigParameter

	//ValidateParameters validates the parameters for a driver.
	//The boolean result is true if the parameters are valid (and the array of failure messages should be nil or empty).
	//The array of failures should contain all of the validation errors. It should not short circuit after the first failure.
	ValidateParameters(parameters map[string]interface{}) (bool, map[string][]string)

	//Metadata returns the Metadata the driver can provide.
	Metadata() Metadata

	//CreateDriver validates the parameters and returns the driver if validation succeeds.
	NewDriver(parameters map[string]interface{}, hardwareResources interface{}) (Driver, error)
}

DriverFactory is responsible for creating drivers and providing information about a driver.

func NoopFactory

func NoopFactory() DriverFactory

NoopFactory provides the factory to get NoopDriver parameters and NoopDrivers

type Measurement

type Measurement struct {
	Expected float64 `json:"expected"`
	Observed float64 `json:"observed"`
}

type Metadata

type Metadata struct {
	Name         string       `json:"name"`
	Description  string       `json:"description"`
	Capabilities []Capability `json:"capabilities"`
}

Metadata represents basic information about a driver for the API response.

func (Metadata) HasCapability

func (m Metadata) HasCapability(cap Capability) bool

type PWMChannel

type PWMChannel interface {
	DigitalOutputPin
	Set(value float64) error
}

type PWMDriver

type PWMDriver interface {
	DigitalOutputDriver
	PWMChannels() []PWMChannel
	PWMChannel(int) (PWMChannel, error)
}

type Pin

type Pin interface {
	io.Closer
	Name() string
	Number() int
}

Pin represents a single-bit digital input or output

Jump to

Keyboard shortcuts

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