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