Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Dec 18, 2013
1 parent bc96ff4 commit e8d2acc
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 90 deletions.
87 changes: 39 additions & 48 deletions examples/sphero.go
Original file line number Diff line number Diff line change
@@ -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()
}
81 changes: 39 additions & 42 deletions examples/sphero_multiple.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit e8d2acc

Please sign in to comment.