microbit: improve button implementation, add integrated example
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
parent
9634a9822c
commit
ec9de10a10
|
@ -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)
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -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()
|
||||
}
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue