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

     1  // +build example
     2  //
     3  // Do not build by default.
     4  
     5  /*
     6   How to run
     7  
     8  	go run examples/firmata_hmc8553l.go
     9  */
    10  
    11  package main
    12  
    13  import (
    14  	"fmt"
    15  	"time"
    16  
    17  	"gobot.io/x/gobot"
    18  	"gobot.io/x/gobot/drivers/i2c"
    19  	"gobot.io/x/gobot/platforms/raspi"
    20  )
    21  
    22  func main() {
    23  	raspi := raspi.NewAdaptor()
    24  	hmc8553l := i2c.NewHMC8553LDriver(raspi)
    25  
    26  	work := func() {
    27  		gobot.Every(200*time.Millisecond, func() {
    28  
    29  			// get heading in radians, to convert to degrees multiply by 180/math.Pi
    30  			heading, _ := hmc8553l.Heading()
    31  			fmt.Println("Heading", heading)
    32  
    33  			// read the raw data from the device, this is useful for calibration
    34  			x, y, z, _ := hmc8553l.ReadRawData()
    35  			fmt.Println(x, y, z)
    36  		})
    37  	}
    38  
    39  	robot := gobot.NewRobot("hmc8553LBot",
    40  		[]gobot.Connection{raspi},
    41  		[]gobot.Device{hmc8553l},
    42  		work,
    43  	)
    44  
    45  	robot.Start()
    46  }