mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-05-11 19:29:20 +08:00
Merge branch 'dev'
This commit is contained in:
commit
fb1d5ace65
29
examples/beaglebone_button.go
Normal file
29
examples/beaglebone_button.go
Normal file
@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/hybridgroup/gobot"
|
||||
"github.com/hybridgroup/gobot/platforms/beaglebone"
|
||||
"github.com/hybridgroup/gobot/platforms/gpio"
|
||||
)
|
||||
|
||||
func main() {
|
||||
gbot := gobot.NewGobot()
|
||||
|
||||
beagleboneAdaptor := beaglebone.NewBeagleboneAdaptor("beaglebone")
|
||||
button := gpio.NewButtonDriver(beagleboneAdaptor, "button", "P8_9")
|
||||
|
||||
work := func() {
|
||||
gobot.On(button.Events["push"], func(data interface{}) {
|
||||
fmt.Println("button pressed")
|
||||
})
|
||||
|
||||
gobot.On(button.Events["release"], func(data interface{}) {
|
||||
fmt.Println("button released")
|
||||
})
|
||||
}
|
||||
|
||||
gbot.Robots = append(gbot.Robots,
|
||||
gobot.NewRobot("buttonBot", []gobot.Connection{beagleboneAdaptor}, []gobot.Device{button}, work))
|
||||
gbot.Start()
|
||||
}
|
30
examples/beaglebone_direct_pin.go
Normal file
30
examples/beaglebone_direct_pin.go
Normal file
@ -0,0 +1,30 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/hybridgroup/gobot"
|
||||
"github.com/hybridgroup/gobot/platforms/beaglebone"
|
||||
"github.com/hybridgroup/gobot/platforms/gpio"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
gbot := gobot.NewGobot()
|
||||
|
||||
beagleboneAdaptor := beaglebone.NewBeagleboneAdaptor("beaglebone")
|
||||
led := gpio.NewDirectPinDriver(beagleboneAdaptor, "led", "P8_10")
|
||||
button := gpio.NewDirectPinDriver(beagleboneAdaptor, "button", "P8_9")
|
||||
|
||||
work := func() {
|
||||
gobot.Every(500*time.Millisecond, func() {
|
||||
if button.DigitalRead() == 1 {
|
||||
led.DigitalWrite(1)
|
||||
} else {
|
||||
led.DigitalWrite(0)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
gbot.Robots = append(gbot.Robots,
|
||||
gobot.NewRobot("pinBot", []gobot.Connection{beagleboneAdaptor}, []gobot.Device{led}, work))
|
||||
gbot.Start()
|
||||
}
|
29
examples/beaglebone_makey_button.go
Normal file
29
examples/beaglebone_makey_button.go
Normal file
@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/hybridgroup/gobot"
|
||||
"github.com/hybridgroup/gobot/platforms/beaglebone"
|
||||
"github.com/hybridgroup/gobot/platforms/gpio"
|
||||
)
|
||||
|
||||
func main() {
|
||||
gbot := gobot.NewGobot()
|
||||
|
||||
beagleboneAdaptor := beaglebone.NewBeagleboneAdaptor("beaglebone")
|
||||
button := gpio.NewMakeyButtonDriver(beagleboneAdaptor, "button", "P8_9")
|
||||
|
||||
work := func() {
|
||||
gobot.On(button.Events["push"], func(data interface{}) {
|
||||
fmt.Println("button pressed")
|
||||
})
|
||||
|
||||
gobot.On(button.Events["release"], func(data interface{}) {
|
||||
fmt.Println("button released")
|
||||
})
|
||||
}
|
||||
|
||||
gbot.Robots = append(gbot.Robots,
|
||||
gobot.NewRobot("makeyBot", []gobot.Connection{beagleboneAdaptor}, []gobot.Device{button}, work))
|
||||
gbot.Start()
|
||||
}
|
@ -13,22 +13,9 @@ type analogPin struct {
|
||||
}
|
||||
|
||||
func newAnalogPin(pinNum string) *analogPin {
|
||||
var err error
|
||||
var fi *os.File
|
||||
|
||||
d := new(analogPin)
|
||||
d.pinNum = pinNum
|
||||
|
||||
slot, err := filepath.Glob(Slots)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fi, err = os.OpenFile(fmt.Sprintf("%v/slots", slot[0]), os.O_WRONLY|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fi.WriteString("cape-bone-iio")
|
||||
fi.Close()
|
||||
return d
|
||||
}
|
||||
|
||||
|
@ -1,8 +1,13 @@
|
||||
package beaglebone
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"github.com/hybridgroup/gobot"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const Slots = "/sys/devices/bone_capemgr.*"
|
||||
@ -103,6 +108,7 @@ type BeagleboneAdaptor struct {
|
||||
pwmPins map[string]*pwmPin
|
||||
analogPins map[string]*analogPin
|
||||
i2cDevice *i2cDevice
|
||||
connect func()
|
||||
}
|
||||
|
||||
func NewBeagleboneAdaptor(name string) *BeagleboneAdaptor {
|
||||
@ -110,6 +116,10 @@ func NewBeagleboneAdaptor(name string) *BeagleboneAdaptor {
|
||||
Adaptor: gobot.Adaptor{
|
||||
Name: name,
|
||||
},
|
||||
connect: func() {
|
||||
ensureSlot("cape-bone-iio")
|
||||
ensureSlot("am33xx_pwm")
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@ -117,6 +127,7 @@ func (b *BeagleboneAdaptor) Connect() bool {
|
||||
b.digitalPins = make([]*digitalPin, 120)
|
||||
b.pwmPins = make(map[string]*pwmPin)
|
||||
b.analogPins = make(map[string]*analogPin)
|
||||
b.connect()
|
||||
return true
|
||||
}
|
||||
|
||||
@ -140,10 +151,7 @@ func (b *BeagleboneAdaptor) Reconnect() bool { return true }
|
||||
func (b *BeagleboneAdaptor) Disconnect() bool { return true }
|
||||
|
||||
func (b *BeagleboneAdaptor) PwmWrite(pin string, val byte) {
|
||||
i := b.pwmPin(pin)
|
||||
period := 500000.0
|
||||
duty := gobot.FromScale(float64(^val), 0, 255.0)
|
||||
b.pwmPins[i].pwmWrite(strconv.Itoa(int(period)), strconv.Itoa(int(period*duty)))
|
||||
b.pwmWrite(pin, val)
|
||||
}
|
||||
|
||||
func (b *BeagleboneAdaptor) InitServo() {}
|
||||
@ -154,6 +162,11 @@ func (b *BeagleboneAdaptor) ServoWrite(pin string, val byte) {
|
||||
b.pwmPins[i].pwmWrite(strconv.Itoa(int(period)), strconv.Itoa(int(period*duty)))
|
||||
}
|
||||
|
||||
func (b *BeagleboneAdaptor) DigitalRead(pin string) int {
|
||||
i := b.digitalPin(pin, "r")
|
||||
return b.digitalPins[i].digitalRead()
|
||||
}
|
||||
|
||||
func (b *BeagleboneAdaptor) DigitalWrite(pin string, val byte) {
|
||||
i := b.digitalPin(pin, "w")
|
||||
b.digitalPins[i].digitalWrite(strconv.Itoa(int(val)))
|
||||
@ -164,6 +177,10 @@ func (b *BeagleboneAdaptor) AnalogRead(pin string) int {
|
||||
return b.analogPins[i].analogRead()
|
||||
}
|
||||
|
||||
func (b *BeagleboneAdaptor) AnalogWrite(pin string, val byte) {
|
||||
b.pwmWrite(pin, val)
|
||||
}
|
||||
|
||||
func (b *BeagleboneAdaptor) I2cStart(address byte) {
|
||||
b.i2cDevice = newI2cDevice(I2CLocation, address)
|
||||
b.i2cDevice.start()
|
||||
@ -227,3 +244,45 @@ func (b *BeagleboneAdaptor) pwmPin(pin string) string {
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
func (b *BeagleboneAdaptor) pwmWrite(pin string, val byte) {
|
||||
i := b.pwmPin(pin)
|
||||
period := 500000.0
|
||||
duty := gobot.FromScale(float64(^val), 0, 255.0)
|
||||
b.pwmPins[i].pwmWrite(strconv.Itoa(int(period)), strconv.Itoa(int(period*duty)))
|
||||
}
|
||||
|
||||
func ensureSlot(item string) {
|
||||
var err error
|
||||
var fi *os.File
|
||||
|
||||
slot, err := filepath.Glob(Slots)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fi, err = os.OpenFile(fmt.Sprintf("%v/slots", slot[0]), os.O_RDWR|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer fi.Close()
|
||||
|
||||
//ensure the slot is not already written into the capemanager (from: https://github.com/mrmorphic/hwio/blob/master/module_bb_pwm.go#L190)
|
||||
scanner := bufio.NewScanner(fi)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if strings.Index(line, item) > 0 {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
fi.WriteString(item)
|
||||
fi.Sync()
|
||||
|
||||
scanner = bufio.NewScanner(fi)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if strings.Index(line, item) > 0 {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,9 @@ import (
|
||||
)
|
||||
|
||||
func initTestBeagleboneAdaptor() *BeagleboneAdaptor {
|
||||
return NewBeagleboneAdaptor("bot")
|
||||
b := NewBeagleboneAdaptor("bot")
|
||||
b.connect = func() {}
|
||||
return b
|
||||
}
|
||||
|
||||
func TestBeagleboneAdaptorFinalize(t *testing.T) {
|
||||
|
@ -26,8 +26,9 @@ func newDigitalPin(pinNum int, mode string) *digitalPin {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer fi.Close()
|
||||
|
||||
fi.WriteString(d.PinNum)
|
||||
fi.Close()
|
||||
|
||||
d.setMode(mode)
|
||||
|
||||
@ -71,6 +72,18 @@ func (d *digitalPin) digitalWrite(value string) {
|
||||
d.PinFile.Sync()
|
||||
}
|
||||
|
||||
func (d *digitalPin) digitalRead() int {
|
||||
if d.Mode != "r" {
|
||||
d.setMode("r")
|
||||
}
|
||||
|
||||
var buf []byte = make([]byte, 1)
|
||||
d.PinFile.ReadAt(buf, 0)
|
||||
|
||||
i, _ := strconv.Atoi(string(buf[0]))
|
||||
return i
|
||||
}
|
||||
|
||||
func (d *digitalPin) close() {
|
||||
fi, err := os.OpenFile(GPIOPath+"/unexport", os.O_WRONLY|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
|
@ -5,7 +5,6 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type pwmPin struct {
|
||||
@ -17,21 +16,11 @@ func newPwmPin(pinNum string) *pwmPin {
|
||||
var err error
|
||||
var fi *os.File
|
||||
|
||||
d := new(pwmPin)
|
||||
d.pinNum = strings.ToUpper(pinNum)
|
||||
slots, err := filepath.Glob(Slots)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
d := &pwmPin{
|
||||
pinNum: strings.ToUpper(pinNum),
|
||||
}
|
||||
fi, err = os.OpenFile(fmt.Sprintf("%v/slots", slots[0]), os.O_WRONLY|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fi.WriteString("am33xx_pwm")
|
||||
fi.Sync()
|
||||
fi.WriteString(fmt.Sprintf("bone_pwm_%v", d.pinNum))
|
||||
fi.Sync()
|
||||
fi.Close()
|
||||
|
||||
ensureSlot(fmt.Sprintf("bone_pwm_%v", d.pinNum))
|
||||
|
||||
ocp, err := filepath.Glob(Ocp)
|
||||
if err != nil {
|
||||
@ -42,18 +31,32 @@ func newPwmPin(pinNum string) *pwmPin {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
d.pwmDevice = pwmDevice[0]
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
fi, err = os.OpenFile(fmt.Sprintf("%v/run", d.pwmDevice), os.O_WRONLY|os.O_APPEND, 0666)
|
||||
fi, err = os.OpenFile(fmt.Sprintf("%v/run", d.pwmDevice), os.O_RDWR|os.O_APPEND, 0666)
|
||||
if err != nil && i == 9 {
|
||||
panic(err)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
|
||||
fi.WriteString("1")
|
||||
fi.Sync()
|
||||
fi.Close()
|
||||
|
||||
for {
|
||||
if _, err := os.Stat(fmt.Sprintf("%v/period", d.pwmDevice)); err == nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
for {
|
||||
if _, err := os.Stat(fmt.Sprintf("%v/duty", d.pwmDevice)); err == nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user