gobot.io/x/gobot/v2@v2.1.0/examples/microbit_magnetometer.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 magnetometer.
    16   You run the Go program on your computer and communicate
    17   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_magnetometer.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  	"fmt"
    31  	"os"
    32  
    33  	"gobot.io/x/gobot/v2"
    34  	"gobot.io/x/gobot/v2/platforms/ble"
    35  	"gobot.io/x/gobot/v2/platforms/microbit"
    36  )
    37  
    38  func main() {
    39  	bleAdaptor := ble.NewClientAdaptor(os.Args[1])
    40  	ubit := microbit.NewMagnetometerDriver(bleAdaptor)
    41  
    42  	work := func() {
    43  		ubit.On(microbit.Magnetometer, func(data interface{}) {
    44  			fmt.Println("Magnetometer", data)
    45  		})
    46  	}
    47  
    48  	robot := gobot.NewRobot("magnetoBot",
    49  		[]gobot.Connection{bleAdaptor},
    50  		[]gobot.Device{ubit},
    51  		work,
    52  	)
    53  
    54  	robot.Start()
    55  }