1
0
Fork 0
mirror of https://github.com/kidoman/embd synced 2025-07-03 11:57:38 +02:00

bring in the idea of a hardware abstraction layer

This commit is contained in:
Karan Misra 2014-02-27 04:24:53 +05:30
parent b4de382833
commit b5e2d0acc7
35 changed files with 994 additions and 971 deletions

2
samples/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
gpio
gpiodetect

View file

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

View file

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

View file

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

View file

@ -3,31 +3,31 @@ package main
import (
"time"
"github.com/kidoman/embd/gpio"
"github.com/kidoman/embd"
)
func main() {
io := gpio.New()
defer io.Close()
gpio, err := embd.NewGPIO()
if err != nil {
panic(err)
}
defer gpio.Close()
pin, err := io.Pin("MOSI")
led, err := gpio.DigitalPin(10)
if err != nil {
panic(err)
}
if err := pin.Output(); err != nil {
if err := led.SetDir(embd.Out); err != nil {
panic(err)
}
if err := pin.ActiveLow(); err != nil {
panic(err)
}
if err := pin.Low(); err != nil {
if err := led.Write(embd.High); err != nil {
panic(err)
}
time.Sleep(1 * time.Second)
if err := pin.Input(); err != nil {
if err := led.SetDir(embd.In); err != nil {
panic(err)
}
}

51
samples/gpiodetect.go Normal file
View file

@ -0,0 +1,51 @@
package main
import (
"time"
"github.com/kidoman/embd"
"github.com/kidoman/embd/host"
)
func main() {
h, _, err := host.Detect()
if err != nil {
return
}
var pinNo interface{}
switch h {
case host.BBB:
pinNo = "P9_31"
case host.RPi:
pinNo = 10
default:
panic("host not supported (yet :P)")
}
gpio, err := embd.NewGPIO()
if err != nil {
panic(err)
}
defer gpio.Close()
led, err := gpio.DigitalPin(pinNo)
if err != nil {
panic(err)
}
defer led.Close()
if err := led.SetDir(embd.Out); err != nil {
panic(err)
}
if err := led.Write(embd.High); err != nil {
panic(err)
}
time.Sleep(1 * time.Second)
if err := led.SetDir(embd.In); err != nil {
panic(err)
}
}

View file

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

View file

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

View file

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

View file

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

View file

@ -1,20 +1,25 @@
package main
import (
"log"
"os"
"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() {
bus := i2c.NewBus(1)
i2c, err := embd.NewI2C()
if err != nil {
panic(err)
}
pwm := pca9685.New(bus, 0x41, 50)
bus := i2c.Bus(1)
pwm := pca9685.New(bus, 0x41)
pwm.Freq = 50
pwm.Debug = true
defer pwm.Close()

View file

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

View file

@ -4,15 +4,27 @@ import (
"log"
"time"
"github.com/kidoman/embd"
"github.com/kidoman/embd/sensor/us020"
"github.com/stianeikeland/go-rpio"
)
func main() {
rpio.Open()
defer rpio.Close()
gpio, err := embd.NewGPIO()
if err != nil {
panic(err)
}
defer gpio.Close()
rf := us020.New(10, 9, nil)
echoPin, err := gpio.DigitalPin(10)
if err != nil {
panic(err)
}
triggerPin, err := gpio.DigitalPin(9)
if err != nil {
panic(err)
}
rf := us020.New(echoPin, triggerPin, nil)
defer rf.Close()
for {