From 5059a8c2f21ba49ac68d235fea9f07667e012b8b Mon Sep 17 00:00:00 2001 From: deadprogram Date: Sat, 15 Apr 2017 17:46:43 +0200 Subject: [PATCH] microbit: add example showing gpio Button and LED Signed-off-by: deadprogram --- examples/microbit_io_button.go | 39 ++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 examples/microbit_io_button.go diff --git a/examples/microbit_io_button.go b/examples/microbit_io_button.go new file mode 100644 index 00000000..ceeecb43 --- /dev/null +++ b/examples/microbit_io_button.go @@ -0,0 +1,39 @@ +// +build example +// +// Do not build by default. + +package main + +import ( + "os" + + "gobot.io/x/gobot" + "gobot.io/x/gobot/drivers/gpio" + "gobot.io/x/gobot/platforms/ble" + "gobot.io/x/gobot/platforms/microbit" +) + +func main() { + bleAdaptor := ble.NewClientAdaptor(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{}) { + led.On() + }) + button.On(gpio.ButtonRelease, func(data interface{}) { + led.Off() + }) + } + + robot := gobot.NewRobot("buttonBot", + []gobot.Connection{bleAdaptor, ubit}, + []gobot.Device{ubit, button, led}, + work, + ) + + robot.Start() +}