1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-05-02 22:17:12 +08:00
hybridgroup.gobot/examples/serial_sphero_master.go

57 lines
1009 B
Go
Raw Normal View History

//go:build example
// +build example
//
// Do not build by default.
2013-11-23 09:15:44 -08:00
package main
import (
2014-07-10 17:21:21 -07:00
"time"
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/serial"
"gobot.io/x/gobot/v2/platforms/serialport"
2013-11-23 09:15:44 -08:00
)
func main() {
master := gobot.NewMaster()
2013-11-24 14:56:13 -08:00
spheros := map[string]string{
2013-12-03 10:43:22 -08:00
"Sphero-BPO": "/dev/rfcomm0",
2013-11-24 14:56:13 -08:00
}
for name, port := range spheros {
spheroAdaptor := serialport.NewAdaptor(port)
spheroDriver := serial.NewSpheroDriver(spheroAdaptor)
2013-11-24 14:56:13 -08:00
work := func() {
2014-04-28 11:40:20 -07:00
spheroDriver.SetRGB(uint8(255), uint8(0), uint8(0))
2013-11-24 14:56:13 -08:00
}
2014-07-08 18:36:14 -07:00
robot := gobot.NewRobot(name,
[]gobot.Connection{spheroAdaptor},
[]gobot.Device{spheroDriver},
work,
)
master.AddRobot(robot)
2013-11-24 14:56:13 -08:00
}
2014-07-08 18:36:14 -07:00
robot := gobot.NewRobot("",
2014-05-22 21:20:16 -07:00
func() {
gobot.Every(1*time.Second, func() {
sphero := master.Robot("Sphero-BPO").Device("sphero").(*serial.SpheroDriver)
2014-07-08 18:36:14 -07:00
sphero.SetRGB(uint8(gobot.Rand(255)),
uint8(gobot.Rand(255)),
uint8(gobot.Rand(255)),
)
2013-11-24 14:56:13 -08:00
})
},
2014-07-08 18:36:14 -07:00
)
master.AddRobot(robot)
2013-11-24 14:56:13 -08:00
master.Start()
2013-11-23 09:15:44 -08:00
}