embd/samples/mpu6050.go

49 lines
803 B
Go

// +build ignore
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"time"
"github.com/kidoman/embd"
"github.com/kidoman/embd/sensor/mpu6050"
_ "github.com/kidoman/embd/host/all"
)
func main() {
flag.Parse()
if err := embd.InitI2C(); err != nil {
panic(err)
}
defer embd.CloseI2C()
bus := embd.NewI2CBus(1)
sensor, _ := mpu6050.New(bus, &mpu6050.Config{GiroScale: "1000", Dlpf: "6"})
sensor.Start()
defer sensor.Close()
// catch ctrl+c and ctrl+x so the sensor.Close is executed
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, os.Kill)
for {
select {
case <-stop:
return
default:
v := sensor.Read().Orientation()
print("\033[H\033[2J") // clear the screen for every read
fmt.Println(v)
time.Sleep(200 * time.Millisecond)
}
}
}