gobot.io/x/gobot/v2@v2.1.0/examples/microbit_io_button.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 requires that you wire an external button to
    16   pin number 0 on the Microbit, and also wire an external LED to
    17   pin number 1 on the Microbit. This example is intended
    18   to demonstrate using Gobot GPIO drivers with the Microbit.
    19  
    20   You run the Go program on your computer and communicate
    21   wirelessly with the Microbit.
    22  
    23   How to run
    24   Pass the Bluetooth name or address as first param:
    25  
    26  	go run examples/microbit_io_button.go "BBC micro:bit [yowza]"
    27  
    28   NOTE: sudo is required to use BLE in Linux
    29  */
    30  
    31  package main
    32  
    33  import (
    34  	"os"
    35  
    36  	"gobot.io/x/gobot/v2"
    37  	"gobot.io/x/gobot/v2/drivers/gpio"
    38  	"gobot.io/x/gobot/v2/platforms/ble"
    39  	"gobot.io/x/gobot/v2/platforms/microbit"
    40  )
    41  
    42  func main() {
    43  	bleAdaptor := ble.NewClientAdaptor(os.Args[1])
    44  
    45  	ubit := microbit.NewIOPinDriver(bleAdaptor)
    46  	button := gpio.NewButtonDriver(ubit, "0")
    47  	led := gpio.NewLedDriver(ubit, "1")
    48  
    49  	work := func() {
    50  		button.On(gpio.ButtonPush, func(data interface{}) {
    51  			led.On()
    52  		})
    53  		button.On(gpio.ButtonRelease, func(data interface{}) {
    54  			led.Off()
    55  		})
    56  	}
    57  
    58  	robot := gobot.NewRobot("buttonBot",
    59  		[]gobot.Connection{bleAdaptor},
    60  		[]gobot.Device{ubit, button, led},
    61  		work,
    62  	)
    63  
    64  	robot.Start()
    65  }