Skip to content
Closed
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions mpu9250/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"log"
"time"

"periph.io/x/conn/v3/i2c"
"periph.io/x/conn/v3/i2c/i2creg"

"periph.io/x/conn/v3/gpio/gpioreg"
"periph.io/x/host/v3"

Expand All @@ -19,6 +22,8 @@
)

var (
ifType = flag.String("iftype", "spi", "Interface Type (spi, i2c)")
i2cAddr = flag.Int("i2caddr", 0x68, "I2C Address - Default 0x68")
accRes = flag.String("accRes", "2", "Acceleration resolution (2, 4, 8, 16G)")
continuous = flag.Bool("cont", false, "Continuous read")
sensitivity int
Expand All @@ -44,16 +49,28 @@
if _, err := host.Init(); err != nil {
log.Fatal("Error initializing host", err)
}
cs := gpioreg.ByName("8")
if cs == nil {
log.Fatal("Can't initialize CS pin")

var t *mpu9250.Transport

Check failure on line 53 in mpu9250/main.go

View workflow job for this annotation

GitHub Actions / lint: ubuntu-latest

undefined: mpu9250.Transport

Check failure on line 53 in mpu9250/main.go

View workflow job for this annotation

GitHub Actions / test: ubuntu-latest

undefined: mpu9250.Transport

Check failure on line 53 in mpu9250/main.go

View workflow job for this annotation

GitHub Actions / lint: windows-latest

undefined: mpu9250.Transport

Check failure on line 53 in mpu9250/main.go

View workflow job for this annotation

GitHub Actions / test: windows-latest

undefined: mpu9250.Transport

Check failure on line 53 in mpu9250/main.go

View workflow job for this annotation

GitHub Actions / test: macos-latest

undefined: mpu9250.Transport

Check failure on line 53 in mpu9250/main.go

View workflow job for this annotation

GitHub Actions / lint: macos-latest

undefined: mpu9250.Transport
var err error

if *ifType == "spi" {
cs := gpioreg.ByName("8")
if cs == nil {
log.Fatal("Can't initialize CS pin")
}
t, err = mpu9250.NewSpiTransport("", cs)
} else {
var bus i2c.Bus
bus, err = i2creg.Open("")
if err == nil {
t, err = mpu9250.NewI2cTransport(bus, uint16(*i2cAddr))

Check failure on line 66 in mpu9250/main.go

View workflow job for this annotation

GitHub Actions / lint: ubuntu-latest

undefined: mpu9250.NewI2cTransport (compile)

Check failure on line 66 in mpu9250/main.go

View workflow job for this annotation

GitHub Actions / test: ubuntu-latest

undefined: mpu9250.NewI2cTransport

Check failure on line 66 in mpu9250/main.go

View workflow job for this annotation

GitHub Actions / lint: windows-latest

undefined: mpu9250.NewI2cTransport (compile)

Check failure on line 66 in mpu9250/main.go

View workflow job for this annotation

GitHub Actions / test: windows-latest

undefined: mpu9250.NewI2cTransport

Check failure on line 66 in mpu9250/main.go

View workflow job for this annotation

GitHub Actions / test: macos-latest

undefined: mpu9250.NewI2cTransport

Check failure on line 66 in mpu9250/main.go

View workflow job for this annotation

GitHub Actions / lint: macos-latest

undefined: mpu9250.NewI2cTransport (compile)
}
}
t, err := mpu9250.NewSpiTransport("", cs)
if err != nil {
log.Fatal("Can't initialize SPI bus ", err)
log.Fatalf("Can't initialize %s bus: %s", *ifType, err)
}

dev, err := mpu9250.New(t)
dev, err := mpu9250.New(*t)
if err != nil {
log.Fatal(err)
}
Expand All @@ -75,15 +92,11 @@

st, err := dev.SelfTest()
if err != nil {
log.Fatal("Self test failed", err)
log.Fatal("Self test failed: ", err)
}

if err = dev.Calibrate(); err != nil {
log.Fatal("Can't calibrate", err)
}

if err != nil {
log.Fatal("Can't render self-test ", err)
log.Fatal("Can't calibrate: ", err)
}

fmt.Printf("Accelerometer Deviation: X: %.2f%%, Y: %.2f%%, Z:%.2f%%\n", st.AccelDeviation.X, st.AccelDeviation.Y, st.AccelDeviation.Z)
Expand Down
Loading