hybridgroup.gobot/examples/minidrone.go

54 lines
982 B
Go
Raw Normal View History

package main
import (
"fmt"
"os"
"time"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/ble"
)
func main() {
gbot := gobot.NewMaster()
bleAdaptor := ble.NewClientAdaptor(os.Args[1])
drone := ble.NewMinidroneDriver(bleAdaptor)
work := func() {
drone.On(drone.Event("battery"), func(data interface{}) {
fmt.Printf("battery: %d\n", data)
})
drone.On(drone.Event("status"), func(data interface{}) {
fmt.Printf("status: %d\n", data)
})
drone.On(drone.Event("flying"), func(data interface{}) {
fmt.Println("flying!")
gobot.After(5*time.Second, func() {
fmt.Println("landing...")
drone.Land()
2016-07-05 05:54:44 +08:00
drone.Land()
})
})
drone.On(drone.Event("landed"), func(data interface{}) {
fmt.Println("landed.")
})
2016-07-05 05:54:44 +08:00
<-time.After(1000 * time.Millisecond)
drone.TakeOff()
}
robot := gobot.NewRobot("bleBot",
[]gobot.Connection{bleAdaptor},
[]gobot.Device{drone},
work,
)
gbot.AddRobot(robot)
gbot.Start()
}