gobot.io/x/gobot/v2@v2.1.0/examples/microbit_buttons.go (about)

     1  //go:build example
     2  // +build example
     3  
     4  //
     5  // Do not build by default.
     6  
     7  /*
     8   How to setup
     9   You must be using a BBC Microbit microcontroller that has
    10   been flashed with the firmware from @sandeepmistry
    11  
    12   More info:
    13   https://gobot.io/documentation/platforms/microbit/#how-to-install
    14  
    15   This example uses the Microbit's built-in buttons. You run
    16   the Go program on your computer and communicate wirelessly
    17   with the Microbit.
    18  
    19   How to run
    20   Pass the Bluetooth name or address as first param:
    21  
    22  	go run examples/microbit_buttons_led.go "BBC micro:bit [yowza]"
    23  */
    24  
    25  package main
    26  
    27  import (
    28  	"fmt"
    29  	"os"
    30  
    31  	"gobot.io/x/gobot/v2"
    32  	"gobot.io/x/gobot/v2/platforms/ble"
    33  	"gobot.io/x/gobot/v2/platforms/microbit"
    34  )
    35  
    36  func main() {
    37  	bleAdaptor := ble.NewClientAdaptor(os.Args[1])
    38  	ubit := microbit.NewButtonDriver(bleAdaptor)
    39  
    40  	work := func() {
    41  		ubit.On(microbit.ButtonA, func(data interface{}) {
    42  			fmt.Println("button A", data)
    43  		})
    44  
    45  		ubit.On(microbit.ButtonB, func(data interface{}) {
    46  			fmt.Println("button B", data)
    47  		})
    48  	}
    49  
    50  	robot := gobot.NewRobot("buttonBot",
    51  		[]gobot.Connection{bleAdaptor},
    52  		[]gobot.Device{ubit},
    53  		work,
    54  	)
    55  
    56  	robot.Start()
    57  }