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

make the framework easier to begin with

This commit is contained in:
Karan Misra 2014-03-01 20:19:44 +05:30
parent ef87ad7879
commit 3d08995000
26 changed files with 237 additions and 170 deletions

View File

@ -17,24 +17,27 @@ type digitalPin struct {
edge *os.File
}
func newDigitalPin(n int) (p *digitalPin, err error) {
func newDigitalPin(n int) (*digitalPin, error) {
p = &digitalPin{n: n}
err = p.init()
return
if err := p.init(); err != nil {
return nil, err
}
return p, nil
}
func (p *digitalPin) init() (err error) {
func (p *digitalPin) init() error {
var err error
if p.dir, err = p.directionFile(); err != nil {
return
return err
}
if p.val, err = p.valueFile(); err != nil {
return
return err
}
if p.activeLow, err = p.activeLowFile(); err != nil {
return
return err
}
return
return nil
}
func (p *digitalPin) basePath() string {
@ -57,43 +60,43 @@ func (p *digitalPin) activeLowFile() (*os.File, error) {
return p.openFile(path.Join(p.basePath(), "active_low"))
}
func (p *digitalPin) SetDir(dir gpio.Direction) (err error) {
func (p *digitalPin) SetDirection(dir gpio.Direction) error {
str := "in"
if dir == gpio.Out {
str = "out"
}
_, err = p.dir.WriteString(str)
_, err := p.dir.WriteString(str)
return
}
func (p *digitalPin) Read() (val int, err error) {
func (p *digitalPin) Read() (int, error) {
buf := make([]byte, 1)
if _, err = p.val.Read(buf); err != nil {
return
if _, err := p.val.Read(buf); err != nil {
return 0, err
}
val = 0
var val int
if buf[0] == '1' {
val = 1
}
return
return val, nil
}
func (p *digitalPin) Write(val int) (err error) {
func (p *digitalPin) Write(val int) error {
str := "0"
if val == gpio.High {
str = "1"
}
_, err = p.val.WriteString(str)
return
_, err := p.val.WriteString(str)
return err
}
func (p *digitalPin) ActiveLow(b bool) (err error) {
func (p *digitalPin) ActiveLow(b bool) error {
str := "0"
if b {
str = "1"
}
_, err = p.activeLow.WriteString(str)
return
_, err := p.activeLow.WriteString(str)
return err
}
func (p *digitalPin) Close() error {

View File

@ -64,38 +64,39 @@ func New(pinMap PinMap) *GPIO {
}
}
func (io *GPIO) init() (err error) {
func (io *GPIO) init() error {
if io.initialized {
return
return nil
}
var err error
if io.exporter, err = os.OpenFile("/sys/class/gpio/export", os.O_WRONLY, os.ModeExclusive); err != nil {
return
return err
}
if io.unexporter, err = os.OpenFile("/sys/class/gpio/unexport", os.O_WRONLY, os.ModeExclusive); err != nil {
return
return err
}
io.initialized = true
return
return nil
}
func (io *GPIO) lookupKey(key interface{}) (*PinDesc, bool) {
return io.pinMap.Lookup(key)
}
func (io *GPIO) export(n int) (err error) {
_, err = io.exporter.WriteString(strconv.Itoa(n))
return
func (io *GPIO) export(n int) error {
_, err := io.exporter.WriteString(strconv.Itoa(n))
return err
}
func (io *GPIO) unexport(n int) (err error) {
_, err = io.unexporter.WriteString(strconv.Itoa(n))
return
func (io *GPIO) unexport(n int) error {
_, err := io.unexporter.WriteString(strconv.Itoa(n))
return err
}
func (io *GPIO) digitalPin(key interface{}) (p *digitalPin, err error) {
func (io *GPIO) digitalPin(key interface{}) (*digitalPin, error) {
pd, found := io.lookupKey(key)
if !found {
err = fmt.Errorf("gpio: could not find pin matching %q", key)
@ -104,8 +105,8 @@ func (io *GPIO) digitalPin(key interface{}) (p *digitalPin, err error) {
n := pd.N
var ok bool
if p, ok = io.initializedPins[n]; ok {
p, ok := io.initializedPins[n]
if ok {
return
}
@ -118,18 +119,19 @@ func (io *GPIO) digitalPin(key interface{}) (p *digitalPin, err error) {
glog.Infof("gpio: pin %q is not a dedicated GPIO pin. please refer to the system reference manual for more details", key)
}
if err = io.export(n); err != nil {
return
if err := io.export(n); err != nil {
return nil, err
}
if p, err = newDigitalPin(n); err != nil {
p, err := newDigitalPin(n)
if err != nil {
io.unexport(n)
return
return nil, err
}
io.initializedPins[n] = p
return
return p, nil
}
func (io *GPIO) DigitalPin(key interface{}) (gpio.DigitalPin, error) {

View File

@ -1,5 +1,7 @@
package gpio
import "github.com/kidoman/embd/host"
type Direction int
const (
@ -16,14 +18,71 @@ type DigitalPin interface {
Write(val int) error
Read() (int, error)
SetDir(dir Direction) error
SetDirection(dir Direction) error
ActiveLow(b bool) error
Close() error
}
type GPIO interface {
type gpio interface {
DigitalPin(key interface{}) (DigitalPin, error)
Close() error
}
var instance gpio
func Open() error {
desc, err := host.Describe()
if err != nil {
return err
}
instance = desc.GPIO().(gpio)
return nil
}
func Close() error {
return instance.Close()
}
func NewDigitalPin(key interface{}) (DigitalPin, error) {
return instance.DigitalPin(key)
}
func DigitalWrite(key interface{}, val int) error {
pin, err := NewDigitalPin(key)
if err != nil {
return err
}
return pin.Write(val)
}
func DigitalRead(key interface{}) (int, error) {
pin, err := NewDigitalPin(key)
if err != nil {
return 0, err
}
return pin.Read()
}
func SetDirection(key interface{}, dir Direction) error {
pin, err := NewDigitalPin(key)
if err != nil {
return err
}
return pin.SetDirection(dir)
}
func ActiveLow(key interface{}, b bool) error {
pin, err := NewDigitalPin(key)
if err != nil {
return err
}
return pin.ActiveLow(b)
}

35
hal.go
View File

@ -1,35 +0,0 @@
package embd
import (
"github.com/kidoman/embd/gpio"
"github.com/kidoman/embd/host"
"github.com/kidoman/embd/i2c"
)
const (
In = gpio.In
Out = gpio.Out
)
const (
Low = gpio.Low
High = gpio.High
)
func NewGPIO() (gpio.GPIO, error) {
desc, err := host.Describe()
if err != nil {
return nil, err
}
return desc.GPIO(), nil
}
func NewI2C() (i2c.I2C, error) {
desc, err := host.Describe()
if err != nil {
return nil, err
}
return desc.I2C(), nil
}

View File

@ -3,7 +3,6 @@ package bbb
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/host"
)
@ -13,7 +12,7 @@ func init() {
func describer(rev int) *host.Descriptor {
return &host.Descriptor{
GPIO: func() gpio.GPIO {
GPIO: func() interface{} {
return lgpio.New(pins)
},
I2C: li2c.New,

View File

@ -1,15 +1,10 @@
package host
import (
"errors"
"github.com/kidoman/embd/gpio"
"github.com/kidoman/embd/i2c"
)
import "errors"
type Descriptor struct {
GPIO func() gpio.GPIO
I2C func() i2c.I2C
GPIO func() interface{}
I2C func() interface{}
}
type Describer func(rev int) *Descriptor

View File

@ -3,8 +3,7 @@ package rpi
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"
)
func init() {
@ -18,7 +17,7 @@ func describer(rev int) *host.Descriptor {
}
return &host.Descriptor{
GPIO: func() gpio.GPIO {
GPIO: func() interface{} {
return lgpio.New(pins)
},
I2C: li2c.New,

View File

@ -1,6 +1,8 @@
// Package i2c enables gophers i2c speaking ability.
package i2c
import "github.com/kidoman/embd/host"
type Bus interface {
// ReadByte reads a byte from the given address.
ReadByte(addr byte) (value byte, err error)
@ -29,3 +31,24 @@ type I2C interface {
Close() error
}
var instance I2C
func Open() error {
desc, err := host.Describe()
if err != nil {
return err
}
instance = desc.I2C().(I2C)
return nil
}
func Close() error {
return instance.Close()
}
func NewBus(l byte) Bus {
return instance.Bus(l)
}

3
samples/.gitignore vendored
View File

@ -3,13 +3,14 @@ bmp085
bmp180
gpio
gpiodetect
gpiodirect
gpioshort
l3gd20
lsm303
mcp4725
pca9685
servo
servoblaster
spi
tmp006
us020
watersensor

2
samples/Makefile Normal file
View File

@ -0,0 +1,2 @@
build: *.go
echo $^ | xargs -n1 -I {} go build {}

View File

@ -3,17 +3,18 @@ package main
import (
"log"
"time"
"github.com/kidoman/embd"
"github.com/kidoman/embd/i2c"
"github.com/kidoman/embd/sensor/bh1750fvi"
)
func main() {
i2c, err := embd.NewI2C()
if err != nil {
if err := i2c.Open(); err != nil {
panic(err)
}
defer i2c.Close()
bus := i2c.Bus(1)
bus := i2c.NewBus(1)
sensor := bh1750fvi.New(bh1750fvi.High, bus)
defer sensor.Close()

View File

@ -3,17 +3,18 @@ package main
import (
"log"
"time"
"github.com/kidoman/embd"
"github.com/kidoman/embd/i2c"
"github.com/kidoman/embd/sensor/bmp085"
)
func main() {
i2c, err := embd.NewI2C()
if err != nil {
if err := i2c.Open(); err != nil {
panic(err)
}
defer i2c.Close()
bus := i2c.Bus(1)
bus := i2c.NewBus(1)
baro := bmp085.New(bus)
defer baro.Close()

View File

@ -3,17 +3,18 @@ package main
import (
"log"
"time"
"github.com/kidoman/embd"
"github.com/kidoman/embd/i2c"
"github.com/kidoman/embd/sensor/bmp180"
)
func main() {
i2c, err := embd.NewI2C()
if err != nil {
if err := i2c.Open(); err != nil {
panic(err)
}
defer i2c.Close()
bus := i2c.Bus(1)
bus := i2c.NewBus(1)
baro := bmp180.New(bus)
defer baro.Close()

View File

@ -3,31 +3,30 @@ package main
import (
"time"
"github.com/kidoman/embd"
"github.com/kidoman/embd/gpio"
)
func main() {
gpio, err := embd.NewGPIO()
if err != nil {
if err := gpio.Open(); err != nil {
panic(err)
}
defer gpio.Close()
led, err := gpio.DigitalPin(10)
led, err := gpio.NewDigitalPin(10)
if err != nil {
panic(err)
}
if err := led.SetDir(embd.Out); err != nil {
if err := led.SetDirection(gpio.Out); err != nil {
panic(err)
}
if err := led.Write(embd.High); err != nil {
if err := led.Write(gpio.High); err != nil {
panic(err)
}
time.Sleep(1 * time.Second)
if err := led.SetDir(embd.In); err != nil {
if err := led.SetDirection(gpio.In); err != nil {
panic(err)
}
}

View File

@ -3,7 +3,7 @@ package main
import (
"time"
"github.com/kidoman/embd"
"github.com/kidoman/embd/gpio"
"github.com/kidoman/embd/host"
)
@ -24,28 +24,27 @@ func main() {
panic("host not supported (yet :P)")
}
gpio, err := embd.NewGPIO()
if err != nil {
if err := gpio.Open(); err != nil {
panic(err)
}
defer gpio.Close()
led, err := gpio.DigitalPin(pinNo)
led, err := gpio.NewDigitalPin(pinNo)
if err != nil {
panic(err)
}
defer led.Close()
if err := led.SetDir(embd.Out); err != nil {
if err := led.SetDirection(gpio.Out); err != nil {
panic(err)
}
if err := led.Write(embd.High); err != nil {
if err := led.Write(gpio.High); err != nil {
panic(err)
}
time.Sleep(1 * time.Second)
if err := led.SetDir(embd.In); err != nil {
if err := led.SetDirection(gpio.In); err != nil {
panic(err)
}
}

27
samples/gpiodirect.go Normal file
View File

@ -0,0 +1,27 @@
package main
import (
"time"
"github.com/kidoman/embd/gpio"
)
func main() {
if err := gpio.Open(); err != nil {
panic(err)
}
defer gpio.Close()
if err := gpio.SetDirection(10, gpio.Out); err != nil {
panic(err)
}
if err := gpio.DigitalWrite(10, gpio.High); err != nil {
panic(err)
}
time.Sleep(1 * time.Second)
if err := gpio.SetDirection(10, gpio.In); err != nil {
panic(err)
}
}

11
samples/gpioshort.go Normal file
View File

@ -0,0 +1,11 @@
package main
import "github.com/kidoman/embd/gpio"
func main() {
gpio.Open()
defer gpio.Close()
gpio.SetDirection(10, gpio.Out)
gpio.DigitalWrite(10, gpio.High)
}

View File

@ -5,17 +5,18 @@ import (
"os"
"os/signal"
"time"
"github.com/kidoman/embd"
"github.com/kidoman/embd/i2c"
"github.com/kidoman/embd/sensor/l3gd20"
)
func main() {
i2c, err := embd.NewI2C()
if err != nil {
if err := i2c.Open(); err != nil {
panic(err)
}
defer i2c.Close()
bus := i2c.Bus(1)
bus := i2c.NewBus(1)
gyro := l3gd20.New(bus, l3gd20.R250DPS)
gyro.Debug = true

View File

@ -4,18 +4,17 @@ import (
"log"
"time"
"github.com/kidoman/embd"
"github.com/kidoman/embd/i2c"
"github.com/kidoman/embd/sensor/lsm303"
)
func main() {
i2c, err := embd.NewI2C()
if err != nil {
if err := i2c.Open(); err != nil {
panic(err)
}
defer i2c.Close()
bus := i2c.Bus(1)
bus := i2c.NewBus(1)
mems := lsm303.New(bus)
defer mems.Close()

View File

@ -6,17 +6,17 @@ import (
"os"
"os/signal"
"github.com/kidoman/embd"
"github.com/kidoman/embd/controller/mcp4725"
"github.com/kidoman/embd/i2c"
)
func main() {
i2c, err := embd.NewI2C()
if err != nil {
if err := i2c.Open(); err != nil {
panic(err)
}
defer i2c.Close()
bus := i2c.Bus(1)
bus := i2c.NewBus(1)
dac := mcp4725.New(bus, 0x62)
defer dac.Close()

View File

@ -6,17 +6,17 @@ import (
"os/signal"
"time"
"github.com/kidoman/embd"
"github.com/kidoman/embd/controller/pca9685"
"github.com/kidoman/embd/i2c"
)
func main() {
i2c, err := embd.NewI2C()
if err != nil {
if err := i2c.Open(); err != nil {
panic(err)
}
defer i2c.Close()
bus := i2c.Bus(1)
bus := i2c.NewBus(1)
pca9685 := pca9685.New(bus, 0x41)
pca9685.Freq = 1000

View File

@ -5,18 +5,18 @@ import (
"os/signal"
"time"
"github.com/kidoman/embd"
"github.com/kidoman/embd/controller/pca9685"
"github.com/kidoman/embd/i2c"
"github.com/kidoman/embd/motion/servo"
)
func main() {
i2c, err := embd.NewI2C()
if err != nil {
if err := i2c.Open(); err != nil {
panic(err)
}
defer i2c.Close()
bus := i2c.Bus(1)
bus := i2c.NewBus(1)
pwm := pca9685.New(bus, 0x41)
pwm.Freq = 50

View File

@ -1,18 +0,0 @@
package main
import (
"fmt"
"github.com/kidoman/embd/spi"
)
func main() {
var rx_data uint8
fmt.Println("Hello")
bus, _ := spi.NewSpiBus()
rx_data, _ = bus.TransferAndRecieveByteData(8` )
fmt.Printf("Received %v \n", rx_data)
}

View File

@ -5,17 +5,17 @@ import (
"os"
"os/signal"
"github.com/kidoman/embd"
"github.com/kidoman/embd/i2c"
"github.com/kidoman/embd/sensor/tmp006"
)
func main() {
i2c, err := embd.NewI2C()
if err != nil {
if err := i2c.Open(); err != nil {
panic(err)
}
defer i2c.Close()
bus := i2c.Bus(1)
bus := i2c.NewBus(1)
sensor := tmp006.New(bus, 0x40)
if status, err := sensor.Present(); err != nil || !status {

View File

@ -4,22 +4,21 @@ import (
"log"
"time"
"github.com/kidoman/embd"
"github.com/kidoman/embd/gpio"
"github.com/kidoman/embd/sensor/us020"
)
func main() {
gpio, err := embd.NewGPIO()
if err != nil {
if err := gpio.Open(); err != nil {
panic(err)
}
defer gpio.Close()
echoPin, err := gpio.DigitalPin(10)
echoPin, err := gpio.NewDigitalPin(10)
if err != nil {
panic(err)
}
triggerPin, err := gpio.DigitalPin(9)
triggerPin, err := gpio.NewDigitalPin(9)
if err != nil {
panic(err)
}

View File

@ -6,7 +6,6 @@ import (
"sync"
"time"
"github.com/kidoman/embd"
"github.com/kidoman/embd/gpio"
)
@ -59,8 +58,8 @@ func (d *US020) setup() (err error) {
d.mu.Lock()
defer d.mu.Unlock()
d.TriggerPin.SetDir(embd.Out)
d.EchoPin.SetDir(embd.In)
d.TriggerPin.SetDirection(gpio.Out)
d.EchoPin.SetDirection(gpio.In)
if d.Thermometer == nil {
d.Thermometer = NullThermometer
@ -107,7 +106,7 @@ func (d *US020) Distance() (distance float64, err error) {
return 0, err
}
if v != embd.Low {
if v != gpio.Low {
break
}
}
@ -125,7 +124,7 @@ func (d *US020) Distance() (distance float64, err error) {
return 0, err
}
if v != embd.High {
if v != gpio.High {
break
}
}
@ -140,5 +139,5 @@ func (d *US020) Distance() (distance float64, err error) {
// Close.
func (d *US020) Close() {
d.EchoPin.SetDir(embd.Out)
d.EchoPin.SetDirection(gpio.Out)
}