forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sphero.go
49 lines (37 loc) · 1.01 KB
/
sphero.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"fmt"
"time"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/sphero"
)
func main() {
gbot := gobot.NewGobot()
adaptor := sphero.NewSpheroAdaptor("Sphero", "/dev/rfcomm0")
spheroDriver := sphero.NewSpheroDriver(adaptor, "sphero")
work := func() {
spheroDriver.SetDataStreaming(sphero.DefaultDataStreamingConfig())
gobot.On(spheroDriver.Event(sphero.Collision), func(data interface{}) {
fmt.Printf("Collision! %+v\n", data)
})
gobot.On(spheroDriver.Event(sphero.SensorData), func(data interface{}) {
fmt.Printf("Streaming Data! %+v\n", data)
})
gobot.Every(3*time.Second, func() {
spheroDriver.Roll(30, uint16(gobot.Rand(360)))
})
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.NewRobot("sphero",
[]gobot.Connection{adaptor},
[]gobot.Device{spheroDriver},
work,
)
gbot.AddRobot(robot)
gbot.Start()
}