gobot.io/x/gobot@v1.16.0/examples/microbit_buttons_leds.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 uses the Microbit's built-in buttons and
    14   built-in LED matrix. You run the Go program on your computer and
    15   communicate wirelessly with the Microbit.
    16  
    17   How to run
    18   Pass the Bluetooth name or address as first param:
    19  
    20  	go run examples/microbit_buttons_led.go "BBC micro:bit [yowza]"
    21  
    22   NOTE: sudo is required to use BLE in Linux
    23  */
    24  
    25  package main
    26  
    27  import (
    28  	"os"
    29  
    30  	"gobot.io/x/gobot"
    31  	"gobot.io/x/gobot/platforms/ble"
    32  	"gobot.io/x/gobot/platforms/microbit"
    33  )
    34  
    35  func main() {
    36  	bleAdaptor := ble.NewClientAdaptor(os.Args[1])
    37  	buttons := microbit.NewButtonDriver(bleAdaptor)
    38  	leds := microbit.NewLEDDriver(bleAdaptor)
    39  
    40  	work := func() {
    41  		buttons.On(microbit.ButtonA, func(data interface{}) {
    42  			if data.([]byte)[0] == 1 {
    43  				leds.UpLeftArrow()
    44  				return
    45  			}
    46  
    47  			leds.Blank()
    48  		})
    49  
    50  		buttons.On(microbit.ButtonB, func(data interface{}) {
    51  			if data.([]byte)[0] == 1 {
    52  				leds.UpRightArrow()
    53  				return
    54  			}
    55  
    56  			leds.Blank()
    57  		})
    58  	}
    59  
    60  	robot := gobot.NewRobot("buttonBot",
    61  		[]gobot.Connection{bleAdaptor},
    62  		[]gobot.Device{buttons, leds},
    63  		work,
    64  	)
    65  
    66  	robot.Start()
    67  }