mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-05-11 19:29:20 +08:00
Update beaglebone package
This commit is contained in:
parent
8c41b83316
commit
66af2c90eb
@ -1,4 +1,4 @@
|
|||||||
package gobotBeaglebone
|
package beaglebone
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package gobotBeaglebone
|
package beaglebone
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/hybridgroup/gobot"
|
"github.com/hybridgroup/gobot"
|
||||||
@ -97,7 +97,7 @@ var analogPins = map[string]string{
|
|||||||
"P8_35": "AIN6",
|
"P8_35": "AIN6",
|
||||||
}
|
}
|
||||||
|
|
||||||
type Beaglebone struct {
|
type BeagleboneAdaptor struct {
|
||||||
gobot.Adaptor
|
gobot.Adaptor
|
||||||
digitalPins []*digitalPin
|
digitalPins []*digitalPin
|
||||||
pwmPins map[string]*pwmPin
|
pwmPins map[string]*pwmPin
|
||||||
@ -105,14 +105,18 @@ type Beaglebone struct {
|
|||||||
i2cDevice *i2cDevice
|
i2cDevice *i2cDevice
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Beaglebone) Connect() bool {
|
func NewBeagleboneAdaptor() *BeagleboneAdaptor {
|
||||||
|
return &BeagleboneAdaptor{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *BeagleboneAdaptor) Connect() bool {
|
||||||
b.digitalPins = make([]*digitalPin, 120)
|
b.digitalPins = make([]*digitalPin, 120)
|
||||||
b.pwmPins = make(map[string]*pwmPin)
|
b.pwmPins = make(map[string]*pwmPin)
|
||||||
b.analogPins = make(map[string]*analogPin)
|
b.analogPins = make(map[string]*analogPin)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Beaglebone) Finalize() bool {
|
func (b *BeagleboneAdaptor) Finalize() bool {
|
||||||
for _, pin := range b.pwmPins {
|
for _, pin := range b.pwmPins {
|
||||||
if pin != nil {
|
if pin != nil {
|
||||||
pin.release()
|
pin.release()
|
||||||
@ -128,48 +132,48 @@ func (b *Beaglebone) Finalize() bool {
|
|||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
func (b *Beaglebone) Reconnect() bool { return true }
|
func (b *BeagleboneAdaptor) Reconnect() bool { return true }
|
||||||
func (b *Beaglebone) Disconnect() bool { return true }
|
func (b *BeagleboneAdaptor) Disconnect() bool { return true }
|
||||||
|
|
||||||
func (b *Beaglebone) PwmWrite(pin string, val byte) {
|
func (b *BeagleboneAdaptor) PwmWrite(pin string, val byte) {
|
||||||
i := b.pwmPin(pin)
|
i := b.pwmPin(pin)
|
||||||
period := 500000.0
|
period := 500000.0
|
||||||
duty := gobot.FromScale(float64(^val), 0, 255.0)
|
duty := gobot.FromScale(float64(^val), 0, 255.0)
|
||||||
b.pwmPins[i].pwmWrite(strconv.Itoa(int(period)), strconv.Itoa(int(period*duty)))
|
b.pwmPins[i].pwmWrite(strconv.Itoa(int(period)), strconv.Itoa(int(period*duty)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Beaglebone) InitServo() {}
|
func (b *BeagleboneAdaptor) InitServo() {}
|
||||||
func (b *Beaglebone) ServoWrite(pin string, val byte) {
|
func (b *BeagleboneAdaptor) ServoWrite(pin string, val byte) {
|
||||||
i := b.pwmPin(pin)
|
i := b.pwmPin(pin)
|
||||||
period := 20000000.0
|
period := 20000000.0
|
||||||
duty := gobot.FromScale(float64(^val), 0, 180.0)
|
duty := gobot.FromScale(float64(^val), 0, 180.0)
|
||||||
b.pwmPins[i].pwmWrite(strconv.Itoa(int(period)), strconv.Itoa(int(period*duty)))
|
b.pwmPins[i].pwmWrite(strconv.Itoa(int(period)), strconv.Itoa(int(period*duty)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Beaglebone) DigitalWrite(pin string, val byte) {
|
func (b *BeagleboneAdaptor) DigitalWrite(pin string, val byte) {
|
||||||
i := b.digitalPin(pin, "w")
|
i := b.digitalPin(pin, "w")
|
||||||
b.digitalPins[i].digitalWrite(strconv.Itoa(int(val)))
|
b.digitalPins[i].digitalWrite(strconv.Itoa(int(val)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Beaglebone) AnalogRead(pin string) int {
|
func (b *BeagleboneAdaptor) AnalogRead(pin string) int {
|
||||||
i := b.analogPin(pin)
|
i := b.analogPin(pin)
|
||||||
return b.analogPins[i].analogRead()
|
return b.analogPins[i].analogRead()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Beaglebone) I2cStart(address byte) {
|
func (b *BeagleboneAdaptor) I2cStart(address byte) {
|
||||||
b.i2cDevice = newI2cDevice(I2C_LOCATION, address)
|
b.i2cDevice = newI2cDevice(I2C_LOCATION, address)
|
||||||
b.i2cDevice.start()
|
b.i2cDevice.start()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Beaglebone) I2cWrite(data []byte) {
|
func (b *BeagleboneAdaptor) I2cWrite(data []byte) {
|
||||||
b.i2cDevice.write(data)
|
b.i2cDevice.write(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Beaglebone) I2cRead(size byte) []byte {
|
func (b *BeagleboneAdaptor) I2cRead(size byte) []byte {
|
||||||
return b.i2cDevice.read(size)
|
return b.i2cDevice.read(size)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Beaglebone) translatePin(pin string) int {
|
func (b *BeagleboneAdaptor) translatePin(pin string) int {
|
||||||
for key, value := range pins {
|
for key, value := range pins {
|
||||||
if key == pin {
|
if key == pin {
|
||||||
return value
|
return value
|
||||||
@ -178,7 +182,7 @@ func (b *Beaglebone) translatePin(pin string) int {
|
|||||||
panic("Not a valid pin")
|
panic("Not a valid pin")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Beaglebone) translatePwmPin(pin string) string {
|
func (b *BeagleboneAdaptor) translatePwmPin(pin string) string {
|
||||||
for key, value := range pwmPins {
|
for key, value := range pwmPins {
|
||||||
if key == pin {
|
if key == pin {
|
||||||
return value
|
return value
|
||||||
@ -187,7 +191,7 @@ func (b *Beaglebone) translatePwmPin(pin string) string {
|
|||||||
panic("Not a valid pin")
|
panic("Not a valid pin")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Beaglebone) translateAnalogPin(pin string) string {
|
func (b *BeagleboneAdaptor) translateAnalogPin(pin string) string {
|
||||||
for key, value := range analogPins {
|
for key, value := range analogPins {
|
||||||
if key == pin {
|
if key == pin {
|
||||||
return value
|
return value
|
||||||
@ -196,7 +200,7 @@ func (b *Beaglebone) translateAnalogPin(pin string) string {
|
|||||||
panic("Not a valid pin")
|
panic("Not a valid pin")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Beaglebone) analogPin(pin string) string {
|
func (b *BeagleboneAdaptor) analogPin(pin string) string {
|
||||||
i := b.translateAnalogPin(pin)
|
i := b.translateAnalogPin(pin)
|
||||||
if b.analogPins[i] == nil {
|
if b.analogPins[i] == nil {
|
||||||
b.analogPins[i] = newAnalogPin(i)
|
b.analogPins[i] = newAnalogPin(i)
|
||||||
@ -204,7 +208,7 @@ func (b *Beaglebone) analogPin(pin string) string {
|
|||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Beaglebone) digitalPin(pin string, mode string) int {
|
func (b *BeagleboneAdaptor) digitalPin(pin string, mode string) int {
|
||||||
i := b.translatePin(pin)
|
i := b.translatePin(pin)
|
||||||
if b.digitalPins[i] == nil || b.digitalPins[i].Mode != mode {
|
if b.digitalPins[i] == nil || b.digitalPins[i].Mode != mode {
|
||||||
b.digitalPins[i] = newDigitalPin(i, mode)
|
b.digitalPins[i] = newDigitalPin(i, mode)
|
||||||
@ -212,7 +216,7 @@ func (b *Beaglebone) digitalPin(pin string, mode string) int {
|
|||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Beaglebone) pwmPin(pin string) string {
|
func (b *BeagleboneAdaptor) pwmPin(pin string) string {
|
||||||
i := b.translatePwmPin(pin)
|
i := b.translatePwmPin(pin)
|
||||||
if b.pwmPins[i] == nil {
|
if b.pwmPins[i] == nil {
|
||||||
b.pwmPins[i] = newPwmPin(i)
|
b.pwmPins[i] = newPwmPin(i)
|
@ -1,4 +1,4 @@
|
|||||||
package gobotBeaglebone
|
package beaglebone
|
||||||
|
|
||||||
import (
|
import (
|
||||||
. "github.com/onsi/ginkgo"
|
. "github.com/onsi/ginkgo"
|
||||||
@ -7,23 +7,23 @@ import (
|
|||||||
|
|
||||||
var _ = Describe("Beaglebone", func() {
|
var _ = Describe("Beaglebone", func() {
|
||||||
var (
|
var (
|
||||||
adaptor *Beaglebone
|
b *BeagleboneAdaptor
|
||||||
)
|
)
|
||||||
|
|
||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
adaptor = new(Beaglebone)
|
b = NewBeagleboneAdaptor()
|
||||||
})
|
})
|
||||||
|
|
||||||
It("Must be able to Finalize", func() {
|
It("Must be able to Finalize", func() {
|
||||||
Expect(adaptor.Finalize()).To(Equal(true))
|
Expect(b.Finalize()).To(Equal(true))
|
||||||
})
|
})
|
||||||
It("Must be able to Connect", func() {
|
It("Must be able to Connect", func() {
|
||||||
Expect(adaptor.Connect()).To(Equal(true))
|
Expect(b.Connect()).To(Equal(true))
|
||||||
})
|
})
|
||||||
It("Must be able to Disconnect", func() {
|
It("Must be able to Disconnect", func() {
|
||||||
Expect(adaptor.Disconnect()).To(Equal(true))
|
Expect(b.Disconnect()).To(Equal(true))
|
||||||
})
|
})
|
||||||
It("Must be able to Reconnect", func() {
|
It("Must be able to Reconnect", func() {
|
||||||
Expect(adaptor.Reconnect()).To(Equal(true))
|
Expect(b.Reconnect()).To(Equal(true))
|
||||||
})
|
})
|
||||||
})
|
})
|
@ -1,4 +1,4 @@
|
|||||||
package gobotBeaglebone
|
package beaglebone
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
@ -1,28 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/hybridgroup/gobot"
|
|
||||||
"github.com/hybridgroup/gobot-beaglebone"
|
|
||||||
"github.com/hybridgroup/gobot-gpio"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
beaglebone := new(gobotBeaglebone.Beaglebone)
|
|
||||||
beaglebone.Name = "beaglebone"
|
|
||||||
|
|
||||||
led := gobotGPIO.NewLed(beaglebone)
|
|
||||||
led.Name = "led"
|
|
||||||
led.Pin = "P9_12"
|
|
||||||
|
|
||||||
work := func() {
|
|
||||||
gobot.Every("1s", func() { led.Toggle() })
|
|
||||||
}
|
|
||||||
|
|
||||||
robot := gobot.Robot{
|
|
||||||
Connections: []gobot.Connection{beaglebone},
|
|
||||||
Devices: []gobot.Device{led},
|
|
||||||
Work: work,
|
|
||||||
}
|
|
||||||
|
|
||||||
robot.Start()
|
|
||||||
}
|
|
@ -1,4 +1,4 @@
|
|||||||
package gobotBeaglebone
|
package beaglebone
|
||||||
|
|
||||||
import (
|
import (
|
||||||
. "github.com/onsi/ginkgo"
|
. "github.com/onsi/ginkgo"
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package gobotBeaglebone
|
package beaglebone
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package gobotBeaglebone
|
package beaglebone
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
30
examples/beaglebone_blink.go
Normal file
30
examples/beaglebone_blink.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/hybridgroup/gobot"
|
||||||
|
"github.com/hybridgroup/gobot/beaglebone"
|
||||||
|
"github.com/hybridgroup/gobot/gpio"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
beagleboneAdaptor := beaglebone.NewBeagleboneAdaptor()
|
||||||
|
beagleboneAdaptor.Name = "beaglebone"
|
||||||
|
|
||||||
|
led := gpio.NewLedDriver(beagleboneAdaptor)
|
||||||
|
led.Name = "led"
|
||||||
|
led.Pin = "P9_12"
|
||||||
|
|
||||||
|
work := func() {
|
||||||
|
gobot.Every("1s", func() {
|
||||||
|
led.Toggle()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
robot := gobot.Robot{
|
||||||
|
Connections: []gobot.Connection{beagleboneAdaptor},
|
||||||
|
Devices: []gobot.Device{led},
|
||||||
|
Work: work,
|
||||||
|
}
|
||||||
|
|
||||||
|
robot.Start()
|
||||||
|
}
|
@ -3,15 +3,15 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/hybridgroup/gobot"
|
"github.com/hybridgroup/gobot"
|
||||||
"github.com/hybridgroup/gobot-beaglebone"
|
"github.com/hybridgroup/gobot/beaglebone"
|
||||||
"github.com/hybridgroup/gobot-i2c"
|
"github.com/hybridgroup/gobot/i2c"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
beaglebone := new(gobotBeaglebone.Beaglebone)
|
beagleboneAdaptor := beaglebone.NewBeagleboneAdaptor()
|
||||||
beaglebone.Name = "beaglebone"
|
beagleboneAdaptor.Name = "beaglebone"
|
||||||
|
|
||||||
blinkm := gobotI2C.NewBlinkM(beaglebone)
|
blinkm := i2c.NewBlinkMDriver(beagleboneAdaptor)
|
||||||
blinkm.Name = "blinkm"
|
blinkm.Name = "blinkm"
|
||||||
|
|
||||||
work := func() {
|
work := func() {
|
||||||
@ -25,7 +25,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
robot := gobot.Robot{
|
robot := gobot.Robot{
|
||||||
Connections: []gobot.Connection{beaglebone},
|
Connections: []gobot.Connection{beagleboneAdaptor},
|
||||||
Devices: []gobot.Device{blinkm},
|
Devices: []gobot.Device{blinkm},
|
||||||
Work: work,
|
Work: work,
|
||||||
}
|
}
|
@ -2,15 +2,15 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/hybridgroup/gobot"
|
"github.com/hybridgroup/gobot"
|
||||||
"github.com/hybridgroup/gobot-beaglebone"
|
"github.com/hybridgroup/gobot/beaglebone"
|
||||||
"github.com/hybridgroup/gobot-gpio"
|
"github.com/hybridgroup/gobot/gpio"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
beaglebone := new(gobotBeaglebone.Beaglebone)
|
beagleboneAdaptor := beaglebone.NewBeagleboneAdaptor()
|
||||||
beaglebone.Name = "beaglebone"
|
beagleboneAdaptor.Name = "beaglebone"
|
||||||
|
|
||||||
led := gobotGPIO.NewLed(beaglebone)
|
led := gpio.NewLedDriver(beagleboneAdaptor)
|
||||||
led.Name = "led"
|
led.Name = "led"
|
||||||
led.Pin = "P9_14"
|
led.Pin = "P9_14"
|
||||||
|
|
||||||
@ -28,7 +28,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
robot := gobot.Robot{
|
robot := gobot.Robot{
|
||||||
Connections: []gobot.Connection{beaglebone},
|
Connections: []gobot.Connection{beagleboneAdaptor},
|
||||||
Devices: []gobot.Device{led},
|
Devices: []gobot.Device{led},
|
||||||
Work: work,
|
Work: work,
|
||||||
}
|
}
|
@ -3,26 +3,26 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/hybridgroup/gobot"
|
"github.com/hybridgroup/gobot"
|
||||||
"github.com/hybridgroup/gobot-beaglebone"
|
"github.com/hybridgroup/gobot/beaglebone"
|
||||||
"github.com/hybridgroup/gobot-gpio"
|
"github.com/hybridgroup/gobot/gpio"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
beaglebone := new(gobotBeaglebone.Beaglebone)
|
beagleboneAdaptor := beaglebone.NewBeagleboneAdaptor()
|
||||||
beaglebone.Name = "beaglebone"
|
beagleboneAdaptor.Name = "beaglebone"
|
||||||
|
|
||||||
sensor := gobotGPIO.NewAnalogSensor(beaglebone)
|
sensor := gpio.NewAnalogSensorDriver(beagleboneAdaptor)
|
||||||
sensor.Name = "sensor"
|
sensor.Name = "sensor"
|
||||||
sensor.Pin = "P9_33"
|
sensor.Pin = "P9_33"
|
||||||
|
|
||||||
led := gobotGPIO.NewLed(beaglebone)
|
led := gpio.NewLedDriver(beagleboneAdaptor)
|
||||||
led.Name = "led"
|
led.Name = "led"
|
||||||
led.Pin = "P9_14"
|
led.Pin = "P9_14"
|
||||||
|
|
||||||
work := func() {
|
work := func() {
|
||||||
gobot.Every("0.1s", func() {
|
gobot.Every("0.1s", func() {
|
||||||
val := sensor.Read()
|
val := sensor.Read()
|
||||||
brightness := uint8(gobotGPIO.ToPwm(val))
|
brightness := uint8(gpio.ToPwm(val))
|
||||||
fmt.Println("sensor", val)
|
fmt.Println("sensor", val)
|
||||||
fmt.Println("brightness", brightness)
|
fmt.Println("brightness", brightness)
|
||||||
led.Brightness(brightness)
|
led.Brightness(brightness)
|
||||||
@ -30,7 +30,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
robot := gobot.Robot{
|
robot := gobot.Robot{
|
||||||
Connections: []gobot.Connection{beaglebone},
|
Connections: []gobot.Connection{beagleboneAdaptor},
|
||||||
Devices: []gobot.Device{sensor, led},
|
Devices: []gobot.Device{sensor, led},
|
||||||
Work: work,
|
Work: work,
|
||||||
}
|
}
|
@ -3,15 +3,15 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/hybridgroup/gobot"
|
"github.com/hybridgroup/gobot"
|
||||||
"github.com/hybridgroup/gobot-beaglebone"
|
"github.com/hybridgroup/gobot/beaglebone"
|
||||||
"github.com/hybridgroup/gobot-gpio"
|
"github.com/hybridgroup/gobot/gpio"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
beaglebone := new(gobotBeaglebone.Beaglebone)
|
beagleboneAdaptor := beaglebone.NewBeagleboneAdaptor()
|
||||||
beaglebone.Name = "beaglebone"
|
beagleboneAdaptor.Name = "beaglebone"
|
||||||
|
|
||||||
servo := gobotGPIO.NewServo(beaglebone)
|
servo := gpio.NewServoDriver(beagleboneAdaptor)
|
||||||
servo.Name = "servo"
|
servo.Name = "servo"
|
||||||
servo.Pin = "P9_14"
|
servo.Pin = "P9_14"
|
||||||
|
|
||||||
@ -24,7 +24,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
robot := gobot.Robot{
|
robot := gobot.Robot{
|
||||||
Connections: []gobot.Connection{beaglebone},
|
Connections: []gobot.Connection{beagleboneAdaptor},
|
||||||
Devices: []gobot.Device{servo},
|
Devices: []gobot.Device{servo},
|
||||||
Work: work,
|
Work: work,
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user