gobot.io/x/gobot@v1.16.0/examples/microbit_accelerometer.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 accelerometer.
    14   You run the Go program on your computer and communicate
    15   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_accelerometer.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  	"fmt"
    29  	"os"
    30  
    31  	"gobot.io/x/gobot"
    32  	"gobot.io/x/gobot/platforms/ble"
    33  	"gobot.io/x/gobot/platforms/microbit"
    34  )
    35  
    36  func main() {
    37  	bleAdaptor := ble.NewClientAdaptor(os.Args[1])
    38  	ubit := microbit.NewAccelerometerDriver(bleAdaptor)
    39  
    40  	work := func() {
    41  		ubit.On(microbit.Accelerometer, func(data interface{}) {
    42  			fmt.Println("Accelerometer", data)
    43  		})
    44  	}
    45  
    46  	robot := gobot.NewRobot("buttonBot",
    47  		[]gobot.Connection{bleAdaptor},
    48  		[]gobot.Device{ubit},
    49  		work,
    50  	)
    51  
    52  	robot.Start()
    53  }