gobot.io/x/gobot@v1.16.0/examples/microbit_io_button.go (about)

     1  // +build example
     2  //
     3  // Do not build by default.
     4  
     5  /*
     6   How to setup
     7   You must be using a BBC Microbit microcontroller that has
     8   been flashed with the firmware from @sandeepmistry
     9  
    10   More info:
    11   https://gobot.io/documentation/platforms/microbit/#how-to-install
    12  
    13   This example requires that you wire an external button to
    14   pin number 0 on the Microbit, and also wire an external LED to
    15   pin number 1 on the Microbit. This example is intended
    16   to demonstrate using Gobot GPIO drivers with the Microbit.
    17  
    18   You run the Go program on your computer and communicate
    19   wirelessly with the Microbit.
    20  
    21   How to run
    22   Pass the Bluetooth name or address as first param:
    23  
    24  	go run examples/microbit_io_button.go "BBC micro:bit [yowza]"
    25  
    26   NOTE: sudo is required to use BLE in Linux
    27  */
    28  
    29  package main
    30  
    31  import (
    32  	"os"
    33  
    34  	"gobot.io/x/gobot"
    35  	"gobot.io/x/gobot/drivers/gpio"
    36  	"gobot.io/x/gobot/platforms/ble"
    37  	"gobot.io/x/gobot/platforms/microbit"
    38  )
    39  
    40  func main() {
    41  	bleAdaptor := ble.NewClientAdaptor(os.Args[1])
    42  
    43  	ubit := microbit.NewIOPinDriver(bleAdaptor)
    44  	button := gpio.NewButtonDriver(ubit, "0")
    45  	led := gpio.NewLedDriver(ubit, "1")
    46  
    47  	work := func() {
    48  		button.On(gpio.ButtonPush, func(data interface{}) {
    49  			led.On()
    50  		})
    51  		button.On(gpio.ButtonRelease, func(data interface{}) {
    52  			led.Off()
    53  		})
    54  	}
    55  
    56  	robot := gobot.NewRobot("buttonBot",
    57  		[]gobot.Connection{bleAdaptor},
    58  		[]gobot.Device{ubit, button, led},
    59  		work,
    60  	)
    61  
    62  	robot.Start()
    63  }