forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sphero_dpad.go
46 lines (36 loc) · 828 Bytes
/
sphero_dpad.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
package main
import (
"time"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/api"
"github.com/hybridgroup/gobot/platforms/sphero"
)
func main() {
gbot := gobot.NewGobot()
a := api.NewAPI(gbot)
a.Start()
conn := sphero.NewSpheroAdaptor("Sphero", "/dev/rfcomm0")
ball := sphero.NewSpheroDriver(conn, "sphero")
robot := gobot.NewRobot("sphero-dpad",
[]gobot.Connection{conn},
[]gobot.Device{ball},
)
robot.AddCommand("move", func(params map[string]interface{}) interface{} {
direction := params["direction"].(string)
switch direction {
case "up":
ball.Roll(100, 0)
case "down":
ball.Roll(100, 180)
case "left":
ball.Roll(100, 270)
case "right":
ball.Roll(100, 90)
}
time.Sleep(2 * time.Second)
ball.Stop()
return "ok"
})
gbot.AddRobot(robot)
gbot.Start()
}