mirror of
https://github.com/kidoman/embd
synced 2024-12-22 04:40:04 +01:00
added documentation
This commit is contained in:
parent
3f88b67361
commit
a35692aabb
7
bbb.go
7
bbb.go
@ -1,3 +1,10 @@
|
||||
// BeagleBone Black support.
|
||||
// The following features are supported on Linux kernel 3.8+
|
||||
//
|
||||
// GPIO (both digital (rw) and analog (ro))
|
||||
// I2C
|
||||
// LED
|
||||
|
||||
package embd
|
||||
|
||||
import (
|
||||
|
@ -1,3 +1,5 @@
|
||||
// Host descriptor data structures.
|
||||
|
||||
package embd
|
||||
|
||||
import (
|
||||
@ -5,16 +7,20 @@ import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Descriptor represents a host descriptor.
|
||||
type Descriptor struct {
|
||||
GPIODriver func() GPIODriver
|
||||
I2CDriver func() I2CDriver
|
||||
LEDDriver func() LEDDriver
|
||||
}
|
||||
|
||||
// The Describer type is a Descriptor provider.
|
||||
type Describer func(rev int) *Descriptor
|
||||
|
||||
// Describers is a global list of registered host Describers.
|
||||
var Describers = map[Host]Describer{}
|
||||
|
||||
// DescribeHost returns the detected host descriptor.
|
||||
func DescribeHost() (*Descriptor, error) {
|
||||
host, rev, err := DetectHost()
|
||||
if err != nil {
|
||||
@ -29,5 +35,10 @@ func DescribeHost() (*Descriptor, error) {
|
||||
return describer(rev), nil
|
||||
}
|
||||
|
||||
// ErrFeatureNotSupported is returned when the host does not support a
|
||||
// particular feature.
|
||||
var ErrFeatureNotSupported = errors.New("embd: requested feature is not supported")
|
||||
|
||||
// ErrFeatureNotImplemented is returned when a particular feature is supported
|
||||
// by the host but not implemented yet.
|
||||
var ErrFeatureNotImplemented = errors.New("embd: requested feature is not implemented")
|
||||
|
13
detect.go
13
detect.go
@ -1,3 +1,5 @@
|
||||
// Host detection.
|
||||
|
||||
package embd
|
||||
|
||||
import (
|
||||
@ -7,13 +9,23 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// The Host type represents all the supported host types.
|
||||
type Host int
|
||||
|
||||
const (
|
||||
// HostNull reprents a null host.
|
||||
HostNull Host = iota
|
||||
|
||||
// HostRPi represents the RaspberryPi.
|
||||
HostRPi
|
||||
|
||||
// HostBBB represents the BeagleBone Black.
|
||||
HostBBB
|
||||
|
||||
// HostCubieTruck represents the Cubie Truck.
|
||||
HostCubieTruck
|
||||
|
||||
// HostGalileo represents the Intel Galileo board.
|
||||
HostGalileo
|
||||
)
|
||||
|
||||
@ -60,6 +72,7 @@ func kernelVersion() (major, minor, patch int, err error) {
|
||||
return parseVersion(output)
|
||||
}
|
||||
|
||||
// DetectHost returns the detected host and its revision number.
|
||||
func DetectHost() (Host, int, error) {
|
||||
major, minor, patch, err := kernelVersion()
|
||||
if err != nil {
|
||||
|
8
i2c.go
8
i2c.go
@ -1,5 +1,8 @@
|
||||
// I2C support.
|
||||
|
||||
package embd
|
||||
|
||||
// I2CBus interface is used to interact with the I2C bus.
|
||||
type I2CBus interface {
|
||||
// ReadByte reads a byte from the given address.
|
||||
ReadByte(addr byte) (value byte, err error)
|
||||
@ -23,6 +26,8 @@ type I2CBus interface {
|
||||
WriteWordToReg(addr, reg byte, value uint16) error
|
||||
}
|
||||
|
||||
// I2CDriver interface interacts with the host descriptors to allow us
|
||||
// control of I2C communication.
|
||||
type I2CDriver interface {
|
||||
Bus(l byte) I2CBus
|
||||
|
||||
@ -31,6 +36,7 @@ type I2CDriver interface {
|
||||
|
||||
var i2cDriverInstance I2CDriver
|
||||
|
||||
// InitI2C initializes the I2C driver.
|
||||
func InitI2C() error {
|
||||
desc, err := DescribeHost()
|
||||
if err != nil {
|
||||
@ -46,10 +52,12 @@ func InitI2C() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// CloseI2C gracefully closes the I2C driver.
|
||||
func CloseI2C() error {
|
||||
return i2cDriverInstance.Close()
|
||||
}
|
||||
|
||||
// NewI2CBus returns a I2CBus corresponding to the provided address.
|
||||
func NewI2CBus(l byte) I2CBus {
|
||||
return i2cDriverInstance.Bus(l)
|
||||
}
|
||||
|
12
led.go
12
led.go
@ -1,5 +1,8 @@
|
||||
// LED support.
|
||||
|
||||
package embd
|
||||
|
||||
// The LED interface is used to control a led on the prototyping board.
|
||||
type LED interface {
|
||||
On() error
|
||||
Off() error
|
||||
@ -9,6 +12,8 @@ type LED interface {
|
||||
Close() error
|
||||
}
|
||||
|
||||
// LEDDriver interface interacts with the host descriptors to allow us
|
||||
// control of the LEDs.
|
||||
type LEDDriver interface {
|
||||
LED(key interface{}) (LED, error)
|
||||
|
||||
@ -17,6 +22,7 @@ type LEDDriver interface {
|
||||
|
||||
var ledDriverInstance LEDDriver
|
||||
|
||||
// InitLED initializes the LED driver.
|
||||
func InitLED() error {
|
||||
desc, err := DescribeHost()
|
||||
if err != nil {
|
||||
@ -32,14 +38,18 @@ func InitLED() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// CloseLED gracefully closes the LED driver.
|
||||
func CloseLED() error {
|
||||
return ledDriverInstance.Close()
|
||||
}
|
||||
|
||||
// NewLED returns a LED interface which allows control over the LED
|
||||
// represented by key.
|
||||
func NewLED(key interface{}) (LED, error) {
|
||||
return ledDriverInstance.LED(key)
|
||||
}
|
||||
|
||||
// LEDOn switches the corresponding LED on.
|
||||
func LEDOn(key interface{}) error {
|
||||
led, err := NewLED(key)
|
||||
if err != nil {
|
||||
@ -49,6 +59,7 @@ func LEDOn(key interface{}) error {
|
||||
return led.On()
|
||||
}
|
||||
|
||||
// LEDOff switches the corresponding LED off.
|
||||
func LEDOff(key interface{}) error {
|
||||
led, err := NewLED(key)
|
||||
if err != nil {
|
||||
@ -58,6 +69,7 @@ func LEDOff(key interface{}) error {
|
||||
return led.Off()
|
||||
}
|
||||
|
||||
// LEDToggle toggles the corresponding LED.
|
||||
func LEDToggle(key interface{}) error {
|
||||
led, err := NewLED(key)
|
||||
if err != nil {
|
||||
|
Loading…
x
Reference in New Issue
Block a user