2023-05-20 20:25:21 +08:00
|
|
|
//go:build example
|
2017-03-13 23:01:39 +08:00
|
|
|
// +build example
|
2023-05-20 20:25:21 +08:00
|
|
|
|
2017-03-13 23:01:39 +08:00
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
2014-09-03 00:48:23 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
2024-02-05 01:50:43 +08:00
|
|
|
"gobot.io/x/gobot/v2/drivers/serial"
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2/platforms/leap"
|
2024-02-05 01:50:43 +08:00
|
|
|
"gobot.io/x/gobot/v2/platforms/serialport"
|
2014-09-03 00:48:23 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2016-10-03 22:58:43 +08:00
|
|
|
leapAdaptor := leap.NewAdaptor("127.0.0.1:6437")
|
2024-02-05 01:50:43 +08:00
|
|
|
spheroAdaptor := serialport.NewAdaptor("/dev/tty.Sphero-YBW-RN-SPP")
|
2014-09-03 00:48:23 +08:00
|
|
|
|
2016-10-03 22:58:43 +08:00
|
|
|
leapDriver := leap.NewDriver(leapAdaptor)
|
2024-02-05 01:50:43 +08:00
|
|
|
spheroDriver := serial.NewSpheroDriver(spheroAdaptor)
|
2014-09-03 00:48:23 +08:00
|
|
|
|
|
|
|
work := func() {
|
2016-09-01 18:17:43 +08:00
|
|
|
leapDriver.On(leap.MessageEvent, func(data interface{}) {
|
2014-09-03 00:48:23 +08:00
|
|
|
hands := data.(leap.Frame).Hands
|
|
|
|
|
|
|
|
if len(hands) > 0 {
|
|
|
|
x := math.Abs(hands[0].Direction[0])
|
|
|
|
y := math.Abs(hands[0].Direction[1])
|
|
|
|
z := math.Abs(hands[0].Direction[2])
|
|
|
|
spheroDriver.SetRGB(scale(x), scale(y), scale(z))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
robot := gobot.NewRobot("leapBot",
|
|
|
|
[]gobot.Connection{leapAdaptor, spheroAdaptor},
|
|
|
|
[]gobot.Device{leapDriver, spheroDriver},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
2016-10-19 03:08:25 +08:00
|
|
|
robot.Start()
|
2014-09-03 00:48:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func scale(position float64) uint8 {
|
|
|
|
return uint8(gobot.ToScale(gobot.FromScale(position, 0, 1), 0, 255))
|
|
|
|
}
|