gobot.io/x/gobot/v2@v2.1.0/examples/microbit_buttons_leds.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 and
    16   built-in LED matrix. You run the Go program on your computer and
    17   communicate wirelessly 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   NOTE: sudo is required to use BLE in Linux
    25  */
    26  
    27  package main
    28  
    29  import (
    30  	"os"
    31  
    32  	"gobot.io/x/gobot/v2"
    33  	"gobot.io/x/gobot/v2/platforms/ble"
    34  	"gobot.io/x/gobot/v2/platforms/microbit"
    35  )
    36  
    37  func main() {
    38  	bleAdaptor := ble.NewClientAdaptor(os.Args[1])
    39  	buttons := microbit.NewButtonDriver(bleAdaptor)
    40  	leds := microbit.NewLEDDriver(bleAdaptor)
    41  
    42  	work := func() {
    43  		buttons.On(microbit.ButtonA, func(data interface{}) {
    44  			if data.([]byte)[0] == 1 {
    45  				leds.UpLeftArrow()
    46  				return
    47  			}
    48  
    49  			leds.Blank()
    50  		})
    51  
    52  		buttons.On(microbit.ButtonB, func(data interface{}) {
    53  			if data.([]byte)[0] == 1 {
    54  				leds.UpRightArrow()
    55  				return
    56  			}
    57  
    58  			leds.Blank()
    59  		})
    60  	}
    61  
    62  	robot := gobot.NewRobot("buttonBot",
    63  		[]gobot.Connection{bleAdaptor},
    64  		[]gobot.Device{buttons, leds},
    65  		work,
    66  	)
    67  
    68  	robot.Start()
    69  }