-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
bleclient_microbit_io_button.go
72 lines (56 loc) · 1.58 KB
/
bleclient_microbit_io_button.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//go:build example
// +build example
//
// Do not build by default.
/*
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 button to
pin number 0 on the Microbit, and also wire an external LED to
pin number 1 on the Microbit. This example is intended
to demonstrate using Gobot GPIO drivers with the Microbit.
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_io_button.go "BBC micro:bit [yowza]"
NOTE: sudo is required to use BLE in Linux
*/
package main
import (
"fmt"
"os"
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/ble/microbit"
"gobot.io/x/gobot/v2/drivers/gpio"
"gobot.io/x/gobot/v2/platforms/bleclient"
)
func main() {
bleAdaptor := bleclient.NewAdaptor(os.Args[1])
ubit := microbit.NewIOPinDriver(bleAdaptor)
button := gpio.NewButtonDriver(ubit, "0")
led := gpio.NewLedDriver(ubit, "1")
work := func() {
_ = button.On(gpio.ButtonPush, func(data interface{}) {
if err := led.On(); err != nil {
fmt.Println(err)
}
})
_ = button.On(gpio.ButtonRelease, func(data interface{}) {
if err := led.Off(); err != nil {
fmt.Println(err)
}
})
}
robot := gobot.NewRobot("buttonBot",
[]gobot.Connection{bleAdaptor},
[]gobot.Device{ubit, button, led},
work,
)
if err := robot.Start(); err != nil {
panic(err)
}
}