From 2811295e3f5cd796de98d8d6186040f4d39397db Mon Sep 17 00:00:00 2001 From: deadprogram Date: Tue, 18 Oct 2016 19:32:45 +0200 Subject: [PATCH] examples: better example of using Master Gobot Signed-off-by: deadprogram --- examples/sphero_multiple.go | 65 ++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/examples/sphero_multiple.go b/examples/sphero_multiple.go index 85bbb62e..62ff8774 100644 --- a/examples/sphero_multiple.go +++ b/examples/sphero_multiple.go @@ -8,8 +8,40 @@ import ( "github.com/hybridgroup/gobot/platforms/sphero" ) +func NewSwarmBot(port string) *gobot.Robot { + spheroAdaptor := sphero.NewAdaptor(port) + spheroDriver := sphero.NewSpheroDriver(spheroAdaptor) + spheroDriver.SetName("Sphero" + port) + + work := func() { + spheroDriver.Stop() + + spheroDriver.On(sphero.Collision, func(data interface{}) { + fmt.Println("Collision Detected!") + }) + + gobot.Every(1*time.Second, func() { + spheroDriver.Roll(100, uint16(gobot.Rand(360))) + }) + gobot.Every(3*time.Second, func() { + spheroDriver.SetRGB(uint8(gobot.Rand(255)), + uint8(gobot.Rand(255)), + uint8(gobot.Rand(255)), + ) + }) + } + + robot := gobot.NewRobot("sphero", + []gobot.Connection{spheroAdaptor}, + []gobot.Device{spheroDriver}, + work, + ) + + return robot +} + func main() { - gbot := gobot.NewMaster() + master := gobot.NewMaster() spheros := []string{ "/dev/rfcomm0", @@ -19,35 +51,8 @@ func main() { } for _, port := range spheros { - spheroAdaptor := sphero.NewAdaptor(port) - spheroDriver := sphero.NewSpheroDriver(spheroAdaptor) - spheroDriver.SetName("Sphero" + port) - - work := func() { - spheroDriver.Stop() - - spheroDriver.On(sphero.Collision, func(data interface{}) { - fmt.Println("Collision Detected!") - }) - - gobot.Every(1*time.Second, func() { - spheroDriver.Roll(100, uint16(gobot.Rand(360))) - }) - gobot.Every(3*time.Second, func() { - spheroDriver.SetRGB(uint8(gobot.Rand(255)), - uint8(gobot.Rand(255)), - uint8(gobot.Rand(255)), - ) - }) - } - - robot := gobot.NewRobot("sphero", - []gobot.Connection{spheroAdaptor}, - []gobot.Device{spheroDriver}, - work, - ) - gbot.AddRobot(robot) + master.AddRobot(NewSwarmBot(port)) } - gbot.Start() + master.Start() }