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 edge *os.File
} }
func newDigitalPin(n int) (p *digitalPin, err error) { func newDigitalPin(n int) (*digitalPin, error) {
p = &digitalPin{n: n} p = &digitalPin{n: n}
err = p.init() if err := p.init(); err != nil {
return 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 { if p.dir, err = p.directionFile(); err != nil {
return return err
} }
if p.val, err = p.valueFile(); err != nil { if p.val, err = p.valueFile(); err != nil {
return return err
} }
if p.activeLow, err = p.activeLowFile(); err != nil { if p.activeLow, err = p.activeLowFile(); err != nil {
return return err
} }
return return nil
} }
func (p *digitalPin) basePath() string { 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")) 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" str := "in"
if dir == gpio.Out { if dir == gpio.Out {
str = "out" str = "out"
} }
_, err = p.dir.WriteString(str) _, err := p.dir.WriteString(str)
return return
} }
func (p *digitalPin) Read() (val int, err error) { func (p *digitalPin) Read() (int, error) {
buf := make([]byte, 1) buf := make([]byte, 1)
if _, err = p.val.Read(buf); err != nil { if _, err := p.val.Read(buf); err != nil {
return return 0, err
} }
val = 0 var val int
if buf[0] == '1' { if buf[0] == '1' {
val = 1 val = 1
} }
return return val, nil
} }
func (p *digitalPin) Write(val int) (err error) { func (p *digitalPin) Write(val int) error {
str := "0" str := "0"
if val == gpio.High { if val == gpio.High {
str = "1" str = "1"
} }
_, err = p.val.WriteString(str) _, err := p.val.WriteString(str)
return return err
} }
func (p *digitalPin) ActiveLow(b bool) (err error) { func (p *digitalPin) ActiveLow(b bool) error {
str := "0" str := "0"
if b { if b {
str = "1" str = "1"
} }
_, err = p.activeLow.WriteString(str) _, err := p.activeLow.WriteString(str)
return return err
} }
func (p *digitalPin) Close() error { 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 { 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 { 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 { if io.unexporter, err = os.OpenFile("/sys/class/gpio/unexport", os.O_WRONLY, os.ModeExclusive); err != nil {
return return err
} }
io.initialized = true io.initialized = true
return return nil
} }
func (io *GPIO) lookupKey(key interface{}) (*PinDesc, bool) { func (io *GPIO) lookupKey(key interface{}) (*PinDesc, bool) {
return io.pinMap.Lookup(key) return io.pinMap.Lookup(key)
} }
func (io *GPIO) export(n int) (err error) { func (io *GPIO) export(n int) error {
_, err = io.exporter.WriteString(strconv.Itoa(n)) _, err := io.exporter.WriteString(strconv.Itoa(n))
return return err
} }
func (io *GPIO) unexport(n int) (err error) { func (io *GPIO) unexport(n int) error {
_, err = io.unexporter.WriteString(strconv.Itoa(n)) _, err := io.unexporter.WriteString(strconv.Itoa(n))
return 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) pd, found := io.lookupKey(key)
if !found { if !found {
err = fmt.Errorf("gpio: could not find pin matching %q", key) 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 n := pd.N
var ok bool p, ok := io.initializedPins[n]
if p, ok = io.initializedPins[n]; ok { if ok {
return 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) 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 { if err := io.export(n); err != nil {
return return nil, err
} }
if p, err = newDigitalPin(n); err != nil { p, err := newDigitalPin(n)
if err != nil {
io.unexport(n) io.unexport(n)
return return nil, err
} }
io.initializedPins[n] = p io.initializedPins[n] = p
return return p, nil
} }
func (io *GPIO) DigitalPin(key interface{}) (gpio.DigitalPin, error) { func (io *GPIO) DigitalPin(key interface{}) (gpio.DigitalPin, error) {

View File

@ -1,5 +1,7 @@
package gpio package gpio
import "github.com/kidoman/embd/host"
type Direction int type Direction int
const ( const (
@ -16,14 +18,71 @@ type DigitalPin interface {
Write(val int) error Write(val int) error
Read() (int, error) Read() (int, error)
SetDir(dir Direction) error SetDirection(dir Direction) error
ActiveLow(b bool) error ActiveLow(b bool) error
Close() error Close() error
} }
type GPIO interface { type gpio interface {
DigitalPin(key interface{}) (DigitalPin, error) DigitalPin(key interface{}) (DigitalPin, error)
Close() 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 ( import (
lgpio "github.com/kidoman/embd/driver/linux/gpio" lgpio "github.com/kidoman/embd/driver/linux/gpio"
li2c "github.com/kidoman/embd/driver/linux/i2c" li2c "github.com/kidoman/embd/driver/linux/i2c"
"github.com/kidoman/embd/gpio"
"github.com/kidoman/embd/host" "github.com/kidoman/embd/host"
) )
@ -13,7 +12,7 @@ func init() {
func describer(rev int) *host.Descriptor { func describer(rev int) *host.Descriptor {
return &host.Descriptor{ return &host.Descriptor{
GPIO: func() gpio.GPIO { GPIO: func() interface{} {
return lgpio.New(pins) return lgpio.New(pins)
}, },
I2C: li2c.New, I2C: li2c.New,

View File

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

View File

@ -3,8 +3,7 @@ package rpi
import ( import (
lgpio "github.com/kidoman/embd/driver/linux/gpio" lgpio "github.com/kidoman/embd/driver/linux/gpio"
li2c "github.com/kidoman/embd/driver/linux/i2c" li2c "github.com/kidoman/embd/driver/linux/i2c"
"github.com/kidoman/embd/gpio" "github.com/kidoman/embd/host"
"github.com/kidoman/embd/i2c"
) )
func init() { func init() {
@ -18,7 +17,7 @@ func describer(rev int) *host.Descriptor {
} }
return &host.Descriptor{ return &host.Descriptor{
GPIO: func() gpio.GPIO { GPIO: func() interface{} {
return lgpio.New(pins) return lgpio.New(pins)
}, },
I2C: li2c.New, I2C: li2c.New,

View File

@ -1,6 +1,8 @@
// Package i2c enables gophers i2c speaking ability. // Package i2c enables gophers i2c speaking ability.
package i2c package i2c
import "github.com/kidoman/embd/host"
type Bus interface { type Bus 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)
@ -29,3 +31,24 @@ type I2C interface {
Close() error 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 bmp180
gpio gpio
gpiodetect gpiodetect
gpiodirect
gpioshort
l3gd20 l3gd20
lsm303 lsm303
mcp4725 mcp4725
pca9685 pca9685
servo servo
servoblaster servoblaster
spi
tmp006 tmp006
us020 us020
watersensor 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 ( import (
"log" "log"
"time" "time"
"github.com/kidoman/embd"
"github.com/kidoman/embd/i2c"
"github.com/kidoman/embd/sensor/bh1750fvi" "github.com/kidoman/embd/sensor/bh1750fvi"
) )
func main() { func main() {
i2c, err := embd.NewI2C() if err := i2c.Open(); err != nil {
if err != nil {
panic(err) panic(err)
} }
defer i2c.Close()
bus := i2c.Bus(1) bus := i2c.NewBus(1)
sensor := bh1750fvi.New(bh1750fvi.High, bus) sensor := bh1750fvi.New(bh1750fvi.High, bus)
defer sensor.Close() defer sensor.Close()

View File

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

View File

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

View File

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

View File

@ -3,7 +3,7 @@ package main
import ( import (
"time" "time"
"github.com/kidoman/embd" "github.com/kidoman/embd/gpio"
"github.com/kidoman/embd/host" "github.com/kidoman/embd/host"
) )
@ -24,28 +24,27 @@ func main() {
panic("host not supported (yet :P)") panic("host not supported (yet :P)")
} }
gpio, err := embd.NewGPIO() if err := gpio.Open(); err != nil {
if err != nil {
panic(err) panic(err)
} }
defer gpio.Close() defer gpio.Close()
led, err := gpio.DigitalPin(pinNo) led, err := gpio.NewDigitalPin(pinNo)
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer led.Close() defer led.Close()
if err := led.SetDir(embd.Out); err != nil { if err := led.SetDirection(gpio.Out); err != nil {
panic(err) panic(err)
} }
if err := led.Write(embd.High); err != nil { if err := led.Write(gpio.High); err != nil {
panic(err) panic(err)
} }
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
if err := led.SetDir(embd.In); err != nil { if err := led.SetDirection(gpio.In); err != nil {
panic(err) 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"
"os/signal" "os/signal"
"time" "time"
"github.com/kidoman/embd"
"github.com/kidoman/embd/i2c"
"github.com/kidoman/embd/sensor/l3gd20" "github.com/kidoman/embd/sensor/l3gd20"
) )
func main() { func main() {
i2c, err := embd.NewI2C() if err := i2c.Open(); err != nil {
if err != nil {
panic(err) panic(err)
} }
defer i2c.Close()
bus := i2c.Bus(1) bus := i2c.NewBus(1)
gyro := l3gd20.New(bus, l3gd20.R250DPS) gyro := l3gd20.New(bus, l3gd20.R250DPS)
gyro.Debug = true gyro.Debug = true

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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