1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-29 13:49:14 +08:00
hybridgroup.gobot/examples/raspi_hmc5883l.go

49 lines
783 B
Go
Raw Normal View History

//go:build example
2021-02-01 18:07:18 -08:00
// +build example
2021-02-01 18:07:18 -08:00
//
// Do not build by default.
/*
How to run
go run examples/raspi_hmc5883l.go
2021-02-01 18:07:18 -08:00
*/
package main
import (
"fmt"
"time"
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/i2c"
"gobot.io/x/gobot/v2/platforms/raspi"
2021-02-01 18:07:18 -08:00
)
func main() {
raspi := raspi.NewAdaptor()
hmc5883l := i2c.NewHMC5883LDriver(raspi)
2021-02-01 18:07:18 -08:00
work := func() {
gobot.Every(200*time.Millisecond, func() {
// get heading in radians, to convert to degrees multiply by 180/math.Pi
heading, _ := hmc5883l.Heading()
2021-02-01 18:07:18 -08:00
fmt.Println("Heading", heading)
// read the data in Gauss
x, y, z, _ := hmc5883l.Read()
2021-02-01 18:07:18 -08:00
fmt.Println(x, y, z)
})
}
robot := gobot.NewRobot("hmc5883LBot",
2021-02-01 18:07:18 -08:00
[]gobot.Connection{raspi},
[]gobot.Device{hmc5883l},
2021-02-01 18:07:18 -08:00
work,
)
robot.Start()
}