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