From e8d2acc3e924ea7936ab9773e980681b2c242ceb Mon Sep 17 00:00:00 2001 From: Adrian Zankich Date: Wed, 18 Dec 2013 13:21:18 -0800 Subject: [PATCH] Update examples --- examples/sphero.go | 87 +++++++++++++++++-------------------- examples/sphero_multiple.go | 81 +++++++++++++++++----------------- 2 files changed, 78 insertions(+), 90 deletions(-) diff --git a/examples/sphero.go b/examples/sphero.go index 917fca0..ec69991 100644 --- a/examples/sphero.go +++ b/examples/sphero.go @@ -1,55 +1,46 @@ package main + import ( - "github.com/hybridgroup/gobot" - "github.com/hybridgroup/gobot-sphero" - "fmt" + "fmt" + "github.com/hybridgroup/gobot" + "github.com/hybridgroup/gobot-sphero" ) func main() { - spheroAdaptor := new(gobotSphero.SpheroAdaptor) - spheroAdaptor.Name = "Sphero" - spheroAdaptor.Port = "127.0.0.1:4560" - - sphero := gobotSphero.NewSphero(spheroAdaptor) - sphero.Name = "Sphero" - - connections := []interface{} { - spheroAdaptor, - } - devices := []interface{} { - sphero, - } - - work := func(){ - - sphero.Stop() - - go func() { - for{ - gobot.On(sphero.Events["Collision"]) - fmt.Println("Collision Detected!") - } - }() - - gobot.Every("2s", func(){ - dir := uint16(gobot.Random(0, 360)) - sphero.Roll(100, dir) - }) - - gobot.Every("3s", func(){ - r := uint8(gobot.Random(0, 255)) - g := uint8(gobot.Random(0, 255)) - b := uint8(gobot.Random(0, 255)) - sphero.SetRGB(r, g, b) - }) - } - - robot := gobot.Robot{ - Connections: connections, - Devices: devices, - Work: work, - } - - robot.Start() + spheroAdaptor := new(gobotSphero.SpheroAdaptor) + spheroAdaptor.Name = "Sphero" + spheroAdaptor.Port = "127.0.0.1:4560" + + sphero := gobotSphero.NewSphero(spheroAdaptor) + sphero.Name = "sphero" + + work := func() { + + sphero.Stop() + + gobot.On(sphero.Events["Collision"], func(data interface{}) { + fmt.Println("Collision Detected!") + }) + + gobot.Every("2s", func() { + dir := uint16(gobot.Random(0, 360)) + sphero.Roll(100, dir) + }) + + gobot.Every("3s", func() { + r := uint8(gobot.Random(0, 255)) + g := uint8(gobot.Random(0, 255)) + b := uint8(gobot.Random(0, 255)) + sphero.SetRGB(r, g, b) + }) + } + + robot := gobot.Robot{ + Connections: []interface{}{spheroAdaptor}, + Devices: []interface{}{sphero}, + Work: work, + } + + robot.Start() } diff --git a/examples/sphero_multiple.go b/examples/sphero_multiple.go index a5ac779..d666566 100644 --- a/examples/sphero_multiple.go +++ b/examples/sphero_multiple.go @@ -1,50 +1,47 @@ package main import ( - "github.com/hybridgroup/gobot" - "github.com/hybridgroup/gobot-sphero" - "fmt" + "fmt" + "github.com/hybridgroup/gobot" + "github.com/hybridgroup/gobot-sphero" ) func main() { - var robots []gobot.Robot - spheros := []string{ - "127.0.0.1:4560", - "127.0.0.1:4561", - "127.0.0.1:4562", - "127.0.0.1:4563", - } - - for s := range spheros { - spheroAdaptor := new(gobotSphero.SpheroAdaptor) - spheroAdaptor.Name = "Sphero" - spheroAdaptor.Port = spheros[s] - - sphero := gobotSphero.NewSphero(spheroAdaptor) - sphero.Name = "Sphero" + spheros[s] - sphero.Interval = "0.5s" - - work := func(){ - sphero.Stop() - - go func() { - for{ - gobot.On(sphero.Events["Collision"]) - fmt.Println("Collision Detected", sphero.Name) - } - }() - - gobot.Every("1s", func() { - sphero.Roll(100, uint16(gobot.Random(0, 360))) - }) - gobot.Every("3s", func() { - sphero.SetRGB(uint8(gobot.Random(0, 255)), uint8(gobot.Random(0, 255)), uint8(gobot.Random(0, 255))) - }) - } - - robots = append(robots, gobot.Robot{Connections: []interface{} {spheroAdaptor}, Devices: []interface{} {sphero}, Work: work,}) - } - - gobot.Work(robots) + var robots []gobot.Robot + spheros := []string{ + "127.0.0.1:4560", + "127.0.0.1:4561", + "127.0.0.1:4562", + "127.0.0.1:4563", + } + + for s := range spheros { + spheroAdaptor := new(gobotSphero.SpheroAdaptor) + spheroAdaptor.Name = "Sphero" + spheroAdaptor.Port = spheros[s] + + sphero := gobotSphero.NewSphero(spheroAdaptor) + sphero.Name = "Sphero" + spheros[s] + sphero.Interval = "0.5s" + + work := func() { + sphero.Stop() + + gobot.On(sphero.Events["Collision"], func(data interface{}) { + fmt.Println("Collision Detected!") + }) + + gobot.Every("1s", func() { + sphero.Roll(100, uint16(gobot.Random(0, 360))) + }) + gobot.Every("3s", func() { + sphero.SetRGB(uint8(gobot.Random(0, 255)), uint8(gobot.Random(0, 255)), uint8(gobot.Random(0, 255))) + }) + } + + robots = append(robots, gobot.Robot{Connections: []interface{}{spheroAdaptor}, Devices: []interface{}{sphero}, Work: work}) + } + + gobot.Work(robots) }