gobot.io/x/gobot/v2@v2.1.0/examples/ble_firmata_curie_imu.go (about)

     1  //go:build example
     2  // +build example
     3  
     4  //
     5  // Do not build by default.
     6  
     7  /*
     8   How to run
     9   Pass the BLE address or BLE name as first param:
    10  
    11  	go run examples/ble_firmata_curie_imu.go FIRMATA
    12  
    13   NOTE: sudo is required to use BLE in Linux
    14  */
    15  
    16  package main
    17  
    18  import (
    19  	"log"
    20  	"os"
    21  	"time"
    22  
    23  	"gobot.io/x/gobot/v2"
    24  	"gobot.io/x/gobot/v2/drivers/gpio"
    25  	"gobot.io/x/gobot/v2/platforms/firmata"
    26  	"gobot.io/x/gobot/v2/platforms/intel-iot/curie"
    27  )
    28  
    29  func main() {
    30  	var bleAdaptor interface{} = firmata.NewBLEAdaptor(os.Args[1])
    31  	firmataAdaptor := bleAdaptor.(*firmata.Adaptor)
    32  	led := gpio.NewLedDriver(firmataAdaptor, "13")
    33  	imu := curie.NewIMUDriver(firmataAdaptor)
    34  
    35  	work := func() {
    36  		imu.On("Accelerometer", func(data interface{}) {
    37  			log.Println("Accelerometer", data)
    38  		})
    39  
    40  		imu.On("Gyroscope", func(data interface{}) {
    41  			log.Println("Gyroscope", data)
    42  		})
    43  
    44  		imu.On("Temperature", func(data interface{}) {
    45  			log.Println("Temperature", data)
    46  		})
    47  
    48  		gobot.Every(1*time.Second, func() {
    49  			led.Toggle()
    50  		})
    51  
    52  		gobot.Every(100*time.Millisecond, func() {
    53  			imu.ReadAccelerometer()
    54  			imu.ReadGyroscope()
    55  			imu.ReadTemperature()
    56  		})
    57  	}
    58  
    59  	robot := gobot.NewRobot("bot",
    60  		[]gobot.Connection{firmataAdaptor},
    61  		[]gobot.Device{led, imu},
    62  		work,
    63  	)
    64  
    65  	robot.Start()
    66  }