gobot.io/x/gobot@v1.16.0/examples/ble_firmata_curie_imu.go (about)

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