1
0
mirror of https://github.com/kidoman/embd synced 2024-12-22 12:50:19 +01:00

make the process of registered available hosts less clunky

This commit is contained in:
Karan Misra 2014-02-27 05:59:38 +05:30
parent 2504678ba9
commit ef87ad7879
7 changed files with 66 additions and 69 deletions

View File

@ -1,31 +0,0 @@
package embd
import (
"errors"
"github.com/kidoman/embd/gpio"
"github.com/kidoman/embd/host/bbb"
"github.com/kidoman/embd/host/rpi"
"github.com/kidoman/embd/i2c"
)
type descriptor interface {
GPIO() gpio.GPIO
I2C() i2c.I2C
}
func describeHost() (descriptor, error) {
host, rev, err := DetectHost()
if err != nil {
return nil, err
}
switch host {
case HostRPi:
return rpi.Descriptor(rev), nil
case HostBBB:
return bbb.Descriptor(rev), nil
default:
return nil, errors.New("host: invalid host")
}
}

5
hal.go
View File

@ -2,6 +2,7 @@ package embd
import (
"github.com/kidoman/embd/gpio"
"github.com/kidoman/embd/host"
"github.com/kidoman/embd/i2c"
)
@ -16,7 +17,7 @@ const (
)
func NewGPIO() (gpio.GPIO, error) {
desc, err := describeHost()
desc, err := host.Describe()
if err != nil {
return nil, err
}
@ -25,7 +26,7 @@ func NewGPIO() (gpio.GPIO, error) {
}
func NewI2C() (i2c.I2C, error) {
desc, err := describeHost()
desc, err := host.Describe()
if err != nil {
return nil, err
}

View File

@ -4,20 +4,18 @@ import (
lgpio "github.com/kidoman/embd/driver/linux/gpio"
li2c "github.com/kidoman/embd/driver/linux/i2c"
"github.com/kidoman/embd/gpio"
"github.com/kidoman/embd/i2c"
"github.com/kidoman/embd/host"
)
type descriptor struct {
func init() {
host.Describers[host.BBB] = describer
}
func (d *descriptor) GPIO() gpio.GPIO {
func describer(rev int) *host.Descriptor {
return &host.Descriptor{
GPIO: func() gpio.GPIO {
return lgpio.New(pins)
}
func (d *descriptor) I2C() i2c.I2C {
return li2c.New()
}
func Descriptor(rev int) *descriptor {
return &descriptor{}
},
I2C: li2c.New,
}
}

31
host/descriptor.go Normal file
View File

@ -0,0 +1,31 @@
package host
import (
"errors"
"github.com/kidoman/embd/gpio"
"github.com/kidoman/embd/i2c"
)
type Descriptor struct {
GPIO func() gpio.GPIO
I2C func() i2c.I2C
}
type Describer func(rev int) *Descriptor
var Describers = map[Host]Describer{}
func Describe() (*Descriptor, error) {
host, rev, err := Detect()
if err != nil {
return nil, err
}
describer, ok := Describers[host]
if !ok {
return nil, errors.New("host: invalid host")
}
return describer(rev), nil
}

View File

@ -1,4 +1,4 @@
package embd
package host
import (
"fmt"
@ -10,11 +10,11 @@ import (
type Host int
const (
HostNull Host = iota
HostRPi
HostBBB
HostCubieTruck
HostGalileo
Null Host = iota
RPi
BBB
CubieTruck
Galileo
)
func execOutput(name string, arg ...string) (output string, err error) {
@ -51,7 +51,7 @@ func kernelVersion() (major, minor, patch int, err error) {
return
}
func DetectHost() (host Host, rev int, err error) {
func Detect() (host Host, rev int, err error) {
major, minor, patch, err := kernelVersion()
if err != nil {
return
@ -69,9 +69,9 @@ func DetectHost() (host Host, rev int, err error) {
switch node {
case "raspberrypi":
host = HostRPi
host = RPi
case "beaglebone":
host = HostBBB
host = BBB
default:
err = fmt.Errorf("embd: your host %q is not supported at this moment. please request support at https://github.com/kidoman/embd/issues", node)
}

View File

@ -7,23 +7,20 @@ import (
"github.com/kidoman/embd/i2c"
)
type descriptor struct {
rev int
func init() {
host.Describers[host.RPi] = describer
}
func (d *descriptor) GPIO() gpio.GPIO {
func describer(rev int) *host.Descriptor {
var pins = rev1Pins
if d.rev > 1 {
pins = rev2Pins
}
return &host.Descriptor{
GPIO: func() gpio.GPIO {
return lgpio.New(pins)
}
func (d *descriptor) I2C() i2c.I2C {
return li2c.New()
}
func Descriptor(rev int) *descriptor {
return &descriptor{rev}
},
I2C: li2c.New,
}
}

View File

@ -4,10 +4,11 @@ import (
"time"
"github.com/kidoman/embd"
"github.com/kidoman/embd/host"
)
func main() {
h, _, err := embd.DetectHost()
h, _, err := host.Detect()
if err != nil {
return
}
@ -15,9 +16,9 @@ func main() {
var pinNo interface{}
switch h {
case embd.HostBBB:
case host.BBB:
pinNo = "P9_31"
case embd.HostRPi:
case host.RPi:
pinNo = 10
default:
panic("host not supported (yet :P)")