Skip to content

Commit

Permalink
Update sphero package and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed May 23, 2014
1 parent f81aa43 commit d877ffd
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 82 deletions.
24 changes: 10 additions & 14 deletions examples/sphero.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,35 @@ package main
import (
"fmt"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/sphero"
"github.com/hybridgroup/gobot/platforms/sphero"
"time"
)

func main() {
adaptor := sphero.NewAdaptor()
adaptor.Name = "Sphero"
adaptor.Port = "/dev/rfcomm0"
gbot := gobot.NewGobot()

spheroDriver := sphero.NewSpheroDriver(adaptor)
spheroDriver.Name = "sphero"
adaptor := sphero.NewSpheroAdaptor("Sphero", "/dev/rfcomm0")
spheroDriver := sphero.NewSpheroDriver(adaptor, "sphero")

work := func() {
gobot.On(spheroDriver.Events["Collision"], func(data interface{}) {
fmt.Println("Collision Detected!")
})

gobot.Every("3s", func() {
gobot.Every(3*time.Second, func() {
spheroDriver.Roll(30, uint16(gobot.Rand(360)))
})

gobot.Every("1s", func() {
gobot.Every(1*time.Second, func() {
r := uint8(gobot.Rand(255))
g := uint8(gobot.Rand(255))
b := uint8(gobot.Rand(255))
spheroDriver.SetRGB(r, g, b)
})
}

robot := gobot.Robot{
Connections: []gobot.Connection{adaptor},
Devices: []gobot.Device{spheroDriver},
Work: work,
}
gbot.Robots = append(gbot.Robots,
gobot.NewRobot("sphero", []gobot.Connection{adaptor}, []gobot.Device{spheroDriver}, work))

robot.Start()
gbot.Start()
}
26 changes: 10 additions & 16 deletions examples/sphero_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package main

import (
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/sphero"
"github.com/hybridgroup/gobot/api"
"github.com/hybridgroup/gobot/platforms/sphero"
)

var Master *gobot.Master = gobot.NewMaster()
Master := gobot.NewGobot()

func TurnBlue(params map[string]interface{}) bool {
spheroDriver := Master.FindRobotDevice(params["robotname"].(string), "sphero")
Expand All @@ -14,32 +15,25 @@ func TurnBlue(params map[string]interface{}) bool {
}

func main() {
gobot.Api(Master)
api.Api(Master).Start()

spheros := map[string]string{
"Sphero-BPO": "/dev/rfcomm0",
}

for name, port := range spheros {
spheroAdaptor := sphero.NewSpheroAdaptor()
spheroAdaptor.Name = "sphero"
spheroAdaptor.Port = port
spheroAdaptor := sphero.NewSpheroAdaptor("sphero", port)

spheroDriver := sphero.NewSpheroDriver(spheroAdaptor)
spheroDriver.Name = "sphero"
spheroDriver.Interval = "0.5s"
spheroDriver := sphero.NewSpheroDriver(spheroAdaptor, "sphero")

work := func() {
spheroDriver.SetRGB(uint8(255), uint8(0), uint8(0))
}

Master.Robots = append(Master.Robots, &gobot.Robot{
Name: name,
Connections: []gobot.Connection{spheroAdaptor},
Devices: []gobot.Device{spheroDriver},
Work: work,
Commands: map[string]interface{}{"TurnBlue": TurnBlue},
})
robot := gobot.NewRobot(name, []gobot.Connection{spheroAdaptor}, []gobot.Device{spheroDriver}, work)
robot.Commands = map[string]interface{}{"TurnBlue": TurnBlue}

Master.Robots = append(Master.Robots, robot)
}

Master.Start()
Expand Down
23 changes: 9 additions & 14 deletions examples/sphero_conways.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package main
import (
"fmt"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/sphero"
"github.com/hybridgroup/gobot/platforms/sphero"
"time"
)

type conway struct {
Expand All @@ -14,7 +15,7 @@ type conway struct {
}

func main() {
master := gobot.NewMaster()
master := gobot.NewGobot()

spheros := []string{
"/dev/rfcomm0",
Expand All @@ -23,12 +24,9 @@ func main() {
}

for s := range spheros {
spheroAdaptor := sphero.NewSpheroAdaptor()
spheroAdaptor.Name = "Sphero"
spheroAdaptor.Port = spheros[s]
spheroAdaptor := sphero.NewSpheroAdaptor("Sphero", spheros[s])

cell := sphero.NewSpheroDriver(spheroAdaptor)
cell.Name = "Sphero" + spheros[s]
cell := sphero.NewSpheroDriver(spheroAdaptor, "Sphero"+spheros[s])

work := func() {

Expand All @@ -41,24 +39,21 @@ func main() {
conway.contact()
})

gobot.Every("3s", func() {
gobot.Every(3*time.Second, func() {
if conway.alive == true {
conway.movement()
}
})

gobot.Every("10s", func() {
gobot.Every(10*time.Second, func() {
if conway.alive == true {
conway.birthday()
}
})
}

master.Robots = append(master.Robots, &gobot.Robot{
Connections: []gobot.Connection{spheroAdaptor},
Devices: []gobot.Device{cell},
Work: work,
})
master.Robots = append(master.Robots,
gobot.NewRobot("conway", []gobot.Connection{spheroAdaptor}, []gobot.Device{cell}, work))
}

master.Start()
Expand Down
32 changes: 14 additions & 18 deletions examples/sphero_master.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,40 @@ package main

import (
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/sphero"
"github.com/hybridgroup/gobot/platforms/sphero"
"time"
)

func main() {
master := gobot.GobotMaster()
master := gobot.NewGobot()

spheros := map[string]string{
"Sphero-BPO": "/dev/rfcomm0",
}

for name, port := range spheros {
spheroAdaptor := new(sphero.Adaptor)
spheroAdaptor.Name = "sphero"
spheroAdaptor.Port = port
spheroAdaptor := sphero.NewSpheroAdaptor("sphero", port)

spheroDriver := sphero.NewSpheroDriver(spheroAdaptor)
spheroDriver.Name = "sphero"
spheroDriver.Interval = "0.5s"
spheroDriver := sphero.NewSpheroDriver(spheroAdaptor, "sphero")

work := func() {
spheroDriver.SetRGB(uint8(255), uint8(0), uint8(0))
}

master.Robots = append(master.Robots, &gobot.Robot{
Name: name,
Connections: []gobot.Connection{spheroAdaptor},
Devices: []gobot.Device{spheroDriver},
Work: work,
})
master.Robots = append(master.Robots,
gobot.NewRobot(name, []gobot.Connection{spheroAdaptor}, []gobot.Device{spheroDriver}, work))
}

master.Robots = append(master.Robots, &gobot.Robot{
Work: func() {
gobot.Every("1s", func() {
master.Robots = append(master.Robots, gobot.NewRobot(
""
nil,
nil,
func() {
gobot.Every(1*time.Second, func() {
gobot.Call(master.FindRobot("Sphero-BPO").GetDevice("spheroDriver").Driver, "SetRGB", uint8(gobot.Rand(255)), uint8(gobot.Rand(255)), uint8(gobot.Rand(255)))
})
},
})
))

master.Start()
}
24 changes: 9 additions & 15 deletions examples/sphero_multiple.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package main
import (
"fmt"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/sphero"
"github.com/hybridgroup/gobot/platforms/sphero"
"time"
)

func main() {
master := gobot.NewMaster()
master := gobot.NewGobot()

spheros := []string{
"/dev/rfcomm0",
Expand All @@ -17,13 +18,9 @@ func main() {
}

for s := range spheros {
spheroAdaptor := sphero.NewSpheroAdaptor()
spheroAdaptor.Name = "Sphero"
spheroAdaptor.Port = spheros[s]
spheroAdaptor := sphero.NewSpheroAdaptor("Sphero", spheros[s])

spheroDriver := sphero.NewSpheroDriver(spheroAdaptor)
spheroDriver.Name = "Sphero" + spheros[s]
spheroDriver.Interval = "0.5s"
spheroDriver := sphero.NewSpheroDriver(spheroAdaptor, "Sphero"+spheros[s])

work := func() {
spheroDriver.Stop()
Expand All @@ -32,19 +29,16 @@ func main() {
fmt.Println("Collision Detected!")
})

gobot.Every("1s", func() {
gobot.Every(1*time.Second, func() {
spheroDriver.Roll(100, uint16(gobot.Rand(360)))
})
gobot.Every("3s", func() {
gobot.Every(3*time.Second, func() {
spheroDriver.SetRGB(uint8(gobot.Rand(255)), uint8(gobot.Rand(255)), uint8(gobot.Rand(255)))
})
}

master.Robots = append(master.Robots, &gobot.Robot{
Connections: []gobot.Connection{spheroAdaptor},
Devices: []gobot.Device{spheroDriver},
Work: work,
})
master.Robots = append(master.Robots,
gobot.NewRobot("sphero", []gobot.Connection{spheroAdaptor}, []gobot.Device{spheroDriver}, work))
}

master.Start()
Expand Down
6 changes: 5 additions & 1 deletion platforms/sphero/sphero_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ type SpheroAdaptor struct {
connect func(*SpheroAdaptor)
}

func NewSpheroAdaptor() *SpheroAdaptor {
func NewSpheroAdaptor(name string, port string) *SpheroAdaptor {
return &SpheroAdaptor{
Adaptor: gobot.Adaptor{
Name: name,
Port: port,
},
connect: func(a *SpheroAdaptor) {
c := &serial.Config{Name: a.Port, Baud: 115200}
s, err := serial.OpenPort(c)
Expand Down
2 changes: 1 addition & 1 deletion platforms/sphero/sphero_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var _ = Describe("SpheroAdaptor", func() {
)

BeforeEach(func() {
a = NewSpheroAdaptor()
a = NewSpheroAdaptor("bot", "/dev/null")
a.sp = sp{}
a.connect = func(a *SpheroAdaptor) {}
})
Expand Down
3 changes: 2 additions & 1 deletion platforms/sphero/sphero_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ type SpheroDriver struct {
response_channel chan []uint8
}

func NewSpheroDriver(a *SpheroAdaptor) *SpheroDriver {
func NewSpheroDriver(a *SpheroAdaptor, name string) *SpheroDriver {
return &SpheroDriver{
Driver: gobot.Driver{
Name: name,
Events: make(map[string]chan interface{}),
Commands: []string{
"SetRGBC",
Expand Down
4 changes: 2 additions & 2 deletions platforms/sphero/sphero_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ var _ = Describe("SpheroDriver", func() {
)

BeforeEach(func() {
a = NewSpheroAdaptor()
a = NewSpheroAdaptor("bot", "/dev/null")
a.sp = sp{}
s = NewSpheroDriver(a)
s = NewSpheroDriver(a, "bot")
})

It("Must be able to Start", func() {
Expand Down

0 comments on commit d877ffd

Please sign in to comment.