2023-05-20 14:25:21 +02:00
|
|
|
//go:build example
|
2017-03-13 11:01:39 -04:00
|
|
|
// +build example
|
2023-05-20 14:25:21 +02:00
|
|
|
|
2017-03-13 11:01:39 -04:00
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
2017-06-10 13:18:29 +02:00
|
|
|
/*
|
|
|
|
How to run
|
|
|
|
Pass the Bluetooth address or name as the first param:
|
|
|
|
|
|
|
|
go run examples/bb8-collision.go BB-1234
|
|
|
|
|
|
|
|
NOTE: sudo is required to use BLE in Linux
|
|
|
|
*/
|
|
|
|
|
2017-01-24 16:53:29 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2023-05-20 14:25:21 +02:00
|
|
|
"gobot.io/x/gobot/v2"
|
2024-02-04 18:50:43 +01:00
|
|
|
"gobot.io/x/gobot/v2/drivers/ble/sphero"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/bleclient"
|
2017-01-24 16:53:29 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-02-04 18:50:43 +01:00
|
|
|
bleAdaptor := bleclient.NewAdaptor(os.Args[1])
|
|
|
|
bb := sphero.NewBB8Driver(bleAdaptor)
|
2017-01-24 16:53:29 -05:00
|
|
|
|
|
|
|
work := func() {
|
2024-02-11 15:34:50 +01:00
|
|
|
_ = bb.On("collision", func(data interface{}) {
|
2017-01-24 16:53:29 -05:00
|
|
|
fmt.Printf("collision detected = %+v \n", data)
|
|
|
|
bb.SetRGB(255, 0, 0)
|
|
|
|
})
|
|
|
|
|
|
|
|
bb.SetRGB(0, 255, 0)
|
|
|
|
bb.Roll(80, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
robot := gobot.NewRobot("bb8",
|
|
|
|
[]gobot.Connection{bleAdaptor},
|
|
|
|
[]gobot.Device{bb},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
2024-02-11 15:34:50 +01:00
|
|
|
if err := robot.Start(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-01-24 16:53:29 -05:00
|
|
|
}
|