2023-05-20 20:25:21 +08:00
|
|
|
//go:build example
|
2017-04-15 23:07:53 +08:00
|
|
|
// +build example
|
2023-05-20 20:25:21 +08:00
|
|
|
|
2017-04-15 23:07:53 +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 requires that you wire an external LED to
|
|
|
|
pin number 0 on the Microbit, as this example is intended
|
|
|
|
to demonstrate the Microbit IOPinDriver.
|
|
|
|
|
|
|
|
You then 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_blink.go "BBC micro:bit [yowza]"
|
|
|
|
|
|
|
|
NOTE: sudo is required to use BLE in Linux
|
|
|
|
*/
|
|
|
|
|
2017-04-15 23:07:53 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-02-11 22:34:50 +08:00
|
|
|
"fmt"
|
2017-04-15 23:07:53 +08:00
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
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"
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2/drivers/gpio"
|
2024-02-05 01:50:43 +08:00
|
|
|
"gobot.io/x/gobot/v2/platforms/bleclient"
|
2017-04-15 23:07:53 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-02-05 01:50:43 +08:00
|
|
|
bleAdaptor := bleclient.NewAdaptor(os.Args[1])
|
2017-04-15 23:07:53 +08:00
|
|
|
|
|
|
|
ubit := microbit.NewIOPinDriver(bleAdaptor)
|
|
|
|
led := gpio.NewLedDriver(ubit, "0")
|
|
|
|
|
|
|
|
work := func() {
|
|
|
|
gobot.Every(1*time.Second, func() {
|
2024-02-11 22:34:50 +08:00
|
|
|
if err := led.Toggle(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2017-04-15 23:07:53 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
robot := gobot.NewRobot("bot",
|
2017-04-16 00:42:37 +08:00
|
|
|
[]gobot.Connection{bleAdaptor},
|
2017-04-15 23:07:53 +08:00
|
|
|
[]gobot.Device{ubit, led},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
2024-02-11 22:34:50 +08:00
|
|
|
if err := robot.Start(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-04-15 23:07:53 +08:00
|
|
|
}
|