diff --git a/examples/microbit_buttons.go b/examples/microbit_buttons.go index fc2d6d46..53dc14c3 100644 --- a/examples/microbit_buttons.go +++ b/examples/microbit_buttons.go @@ -15,11 +15,11 @@ func main() { work := func() { ubit.On(microbit.ButtonA, func(data interface{}) { - fmt.Println("button A") + fmt.Println("button A", data) }) ubit.On(microbit.ButtonB, func(data interface{}) { - fmt.Println("button B") + fmt.Println("button B", data) }) } diff --git a/examples/microbit_buttons_leds.go b/examples/microbit_buttons_leds.go new file mode 100644 index 00000000..7e8e45a2 --- /dev/null +++ b/examples/microbit_buttons_leds.go @@ -0,0 +1,46 @@ +package main + +import ( + "fmt" + "os" + + "gobot.io/x/gobot" + "gobot.io/x/gobot/platforms/ble" + "gobot.io/x/gobot/platforms/microbit" +) + +func main() { + bleAdaptor := ble.NewClientAdaptor(os.Args[1]) + buttons := microbit.NewButtonDriver(bleAdaptor) + leds := microbit.NewLEDDriver(bleAdaptor) + + work := func() { + buttons.On(microbit.ButtonA, func(data interface{}) { + fmt.Println("a", data) + if data.([]byte)[0] == 1 { + leds.UpLeftArrow() + return + } + + leds.Blank() + }) + + buttons.On(microbit.ButtonB, func(data interface{}) { + fmt.Println("b", data) + if data.([]byte)[0] == 1 { + leds.UpRightArrow() + return + } + + leds.Blank() + }) + } + + robot := gobot.NewRobot("buttonBot", + []gobot.Connection{bleAdaptor}, + []gobot.Device{buttons, leds}, + work, + ) + + robot.Start() +} diff --git a/platforms/microbit/button_driver.go b/platforms/microbit/button_driver.go index 70936c15..14871388 100644 --- a/platforms/microbit/button_driver.go +++ b/platforms/microbit/button_driver.go @@ -59,12 +59,12 @@ func (b *ButtonDriver) adaptor() *ble.ClientAdaptor { func (b *ButtonDriver) Start() (err error) { // subscribe to button A notifications b.adaptor().Subscribe(buttonACharacteristic, func(data []byte, e error) { - b.Publish(b.Event(ButtonA), nil) + b.Publish(b.Event(ButtonA), data) }) // subscribe to button B notifications b.adaptor().Subscribe(buttonBCharacteristic, func(data []byte, e error) { - b.Publish(b.Event(ButtonB), nil) + b.Publish(b.Event(ButtonB), data) }) return