2023-05-20 20:25:21 +08:00
|
|
|
//go:build example
|
2017-03-20 19:31:23 +08:00
|
|
|
// +build example
|
2023-05-20 20:25:21 +08:00
|
|
|
|
2017-03-20 19:31:23 +08:00
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
2017-06-10 19:18:29 +08:00
|
|
|
/*
|
|
|
|
How to setup
|
|
|
|
You must be using a BBC Microbit microcontroller that has
|
|
|
|
been flashed with the firmware from @sandeepmistry
|
|
|
|
|
|
|
|
More info:
|
|
|
|
https://gobot.io/documentation/platforms/microbit/#how-to-install
|
|
|
|
|
|
|
|
This example uses the Microbit's built-in accelerometer.
|
|
|
|
You run the Go program on your computer and communicate
|
|
|
|
wirelessly with the Microbit.
|
|
|
|
|
|
|
|
How to run
|
|
|
|
Pass the Bluetooth name or address as first param:
|
|
|
|
|
|
|
|
go run examples/microbit_accelerometer.go "BBC micro:bit [yowza]"
|
|
|
|
|
|
|
|
NOTE: sudo is required to use BLE in Linux
|
|
|
|
*/
|
|
|
|
|
2017-03-01 08:23:50 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
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/ble/microbit"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/bleclient"
|
2017-03-01 08:23:50 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-02-05 01:50:43 +08:00
|
|
|
bleAdaptor := bleclient.NewAdaptor(os.Args[1])
|
2017-03-01 08:23:50 +08:00
|
|
|
ubit := microbit.NewAccelerometerDriver(bleAdaptor)
|
|
|
|
|
|
|
|
work := func() {
|
2024-02-11 22:34:50 +08:00
|
|
|
_ = ubit.On(microbit.AccelerometerEvent, func(data interface{}) {
|
2017-03-01 08:23:50 +08:00
|
|
|
fmt.Println("Accelerometer", data)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
robot := gobot.NewRobot("buttonBot",
|
|
|
|
[]gobot.Connection{bleAdaptor},
|
|
|
|
[]gobot.Device{ubit},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
2024-02-11 22:34:50 +08:00
|
|
|
if err := robot.Start(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-03-01 08:23:50 +08:00
|
|
|
}
|