From d877ffd2514328f7449d2653b511e39d77cce9a8 Mon Sep 17 00:00:00 2001 From: Adrian Zankich Date: Thu, 22 May 2014 21:20:16 -0700 Subject: [PATCH] Update sphero package and examples --- examples/sphero.go | 24 ++++++++----------- examples/sphero_api.go | 26 ++++++++------------ examples/sphero_conways.go | 23 +++++++----------- examples/sphero_master.go | 32 +++++++++++-------------- examples/sphero_multiple.go | 24 +++++++------------ platforms/sphero/sphero_adaptor.go | 6 ++++- platforms/sphero/sphero_adaptor_test.go | 2 +- platforms/sphero/sphero_driver.go | 3 ++- platforms/sphero/sphero_driver_test.go | 4 ++-- 9 files changed, 62 insertions(+), 82 deletions(-) diff --git a/examples/sphero.go b/examples/sphero.go index baac3b02..c06643e1 100644 --- a/examples/sphero.go +++ b/examples/sphero.go @@ -3,27 +3,26 @@ 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)) @@ -31,11 +30,8 @@ func main() { }) } - 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() } diff --git a/examples/sphero_api.go b/examples/sphero_api.go index fa0f9b48..47006a8b 100644 --- a/examples/sphero_api.go +++ b/examples/sphero_api.go @@ -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") @@ -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() diff --git a/examples/sphero_conways.go b/examples/sphero_conways.go index 24c4c056..839c1d22 100644 --- a/examples/sphero_conways.go +++ b/examples/sphero_conways.go @@ -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 { @@ -14,7 +15,7 @@ type conway struct { } func main() { - master := gobot.NewMaster() + master := gobot.NewGobot() spheros := []string{ "/dev/rfcomm0", @@ -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() { @@ -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() diff --git a/examples/sphero_master.go b/examples/sphero_master.go index 8c7dfb18..b185d961 100644 --- a/examples/sphero_master.go +++ b/examples/sphero_master.go @@ -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() } diff --git a/examples/sphero_multiple.go b/examples/sphero_multiple.go index 45a9e8c7..0d87bdad 100644 --- a/examples/sphero_multiple.go +++ b/examples/sphero_multiple.go @@ -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", @@ -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() @@ -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() diff --git a/platforms/sphero/sphero_adaptor.go b/platforms/sphero/sphero_adaptor.go index d0a8979b..4d9e4cdf 100644 --- a/platforms/sphero/sphero_adaptor.go +++ b/platforms/sphero/sphero_adaptor.go @@ -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) diff --git a/platforms/sphero/sphero_adaptor_test.go b/platforms/sphero/sphero_adaptor_test.go index 9756fcc9..2fd623d7 100644 --- a/platforms/sphero/sphero_adaptor_test.go +++ b/platforms/sphero/sphero_adaptor_test.go @@ -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) {} }) diff --git a/platforms/sphero/sphero_driver.go b/platforms/sphero/sphero_driver.go index b4fc4d98..843cac48 100644 --- a/platforms/sphero/sphero_driver.go +++ b/platforms/sphero/sphero_driver.go @@ -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", diff --git a/platforms/sphero/sphero_driver_test.go b/platforms/sphero/sphero_driver_test.go index 81f03ea3..278a866e 100644 --- a/platforms/sphero/sphero_driver_test.go +++ b/platforms/sphero/sphero_driver_test.go @@ -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() {