From b6ce04bdfbd03a91b0cab3b6786879798be843b0 Mon Sep 17 00:00:00 2001 From: George Sexton Date: Sat, 15 Mar 2025 13:17:18 -0600 Subject: [PATCH 1/7] Add i2c interface support --- mpu9250/main.go | 51 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/mpu9250/main.go b/mpu9250/main.go index 866b394..e1c7453 100644 --- a/mpu9250/main.go +++ b/mpu9250/main.go @@ -11,14 +11,20 @@ import ( "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" "periph.io/x/devices/v3/mpu9250" "periph.io/x/devices/v3/mpu9250/accelerometer" + // "periph.io/x/devices/v3/mpu9250/reg" ) 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 @@ -44,16 +50,28 @@ func main() { 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 + 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)) + } } - 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) } @@ -72,18 +90,25 @@ func main() { } fmt.Printf("Dev ID: %x\n", id) - + err = dev.EnableGyro() + if err != nil { + log.Println(err) + } + err = dev.Reset() + if err != nil { + log.Println(err) + } + err = dev.EnableTemperature() + if err != nil { + log.Println(err) + } 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.Println("Can't calibrate: ", err) } fmt.Printf("Accelerometer Deviation: X: %.2f%%, Y: %.2f%%, Z:%.2f%%\n", st.AccelDeviation.X, st.AccelDeviation.Y, st.AccelDeviation.Z) From 18341d3953e16ebc9c71544a591d116b21b5413c Mon Sep 17 00:00:00 2001 From: George Sexton Date: Sat, 15 Mar 2025 13:23:58 -0600 Subject: [PATCH 2/7] Remove commented import --- mpu9250/main.go | 1 - 1 file changed, 1 deletion(-) diff --git a/mpu9250/main.go b/mpu9250/main.go index e1c7453..3c588a3 100644 --- a/mpu9250/main.go +++ b/mpu9250/main.go @@ -19,7 +19,6 @@ import ( "periph.io/x/devices/v3/mpu9250" "periph.io/x/devices/v3/mpu9250/accelerometer" - // "periph.io/x/devices/v3/mpu9250/reg" ) var ( From 8cb37926d133992aa9974df090c15100b6a51f3f Mon Sep 17 00:00:00 2001 From: George Sexton Date: Sat, 15 Mar 2025 13:26:25 -0600 Subject: [PATCH 3/7] Remove debug things added that aren't necessary --- mpu9250/main.go | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/mpu9250/main.go b/mpu9250/main.go index 3c588a3..bf10356 100644 --- a/mpu9250/main.go +++ b/mpu9250/main.go @@ -89,18 +89,7 @@ func main() { } fmt.Printf("Dev ID: %x\n", id) - err = dev.EnableGyro() - if err != nil { - log.Println(err) - } - err = dev.Reset() - if err != nil { - log.Println(err) - } - err = dev.EnableTemperature() - if err != nil { - log.Println(err) - } + st, err := dev.SelfTest() if err != nil { log.Fatal("Self test failed: ", err) From 9af6f161384eaf39cf78cbdecb6fba6aca55222e Mon Sep 17 00:00:00 2001 From: George Sexton Date: Sat, 15 Mar 2025 13:27:34 -0600 Subject: [PATCH 4/7] Remove debug things added that aren't necessary --- mpu9250/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpu9250/main.go b/mpu9250/main.go index bf10356..8102671 100644 --- a/mpu9250/main.go +++ b/mpu9250/main.go @@ -96,7 +96,7 @@ func main() { } if err = dev.Calibrate(); err != nil { - log.Println("Can't calibrate: ", 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) From 17efaab87afc2bfb5786f5c171208e83f276d733 Mon Sep 17 00:00:00 2001 From: George Sexton Date: Wed, 19 Mar 2025 17:20:44 -0600 Subject: [PATCH 5/7] Rev devices version --- go.mod | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 74a65fb..44c3563 100644 --- a/go.mod +++ b/go.mod @@ -4,14 +4,16 @@ module periph.io/x/cmd -go 1.22.6 +go 1.23.0 + +toolchain go1.24.1 require ( - golang.org/x/image v0.19.0 - periph.io/x/conn/v3 v3.7.1 + golang.org/x/image v0.23.0 + periph.io/x/conn/v3 v3.7.2 periph.io/x/d2xx v0.1.1 - periph.io/x/devices/v3 v3.7.1 - periph.io/x/host/v3 v3.8.2 + periph.io/x/devices/v3 v3.7.4 + periph.io/x/host/v3 v3.8.3 ) -require github.com/jonboulle/clockwork v0.4.0 // indirect +require github.com/jonboulle/clockwork v0.5.0 // indirect From df9544096e3ab2ddc40189c6021ec68e0e41ca52 Mon Sep 17 00:00:00 2001 From: George Sexton Date: Wed, 19 Mar 2025 17:21:34 -0600 Subject: [PATCH 6/7] Update go.mod --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 44c3563..5827c3a 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ go 1.23.0 toolchain go1.24.1 require ( - golang.org/x/image v0.23.0 + golang.org/x/image v0.25.0 periph.io/x/conn/v3 v3.7.2 periph.io/x/d2xx v0.1.1 periph.io/x/devices/v3 v3.7.4 From faab58cdcd40878af5b087bc0aae139eddef22ba Mon Sep 17 00:00:00 2001 From: George Sexton Date: Wed, 19 Mar 2025 17:27:41 -0600 Subject: [PATCH 7/7] Fix go.sum --- go.sum | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/go.sum b/go.sum index e706492..32b5f23 100644 --- a/go.sum +++ b/go.sum @@ -1,12 +1,12 @@ -github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST4RZ4= -github.com/jonboulle/clockwork v0.4.0/go.mod h1:xgRqUGwRcjKCO1vbZUEtSLrqKoPSsUpK7fnezOII0kc= -golang.org/x/image v0.19.0 h1:D9FX4QWkLfkeqaC62SonffIIuYdOk/UE2XKUBgRIBIQ= -golang.org/x/image v0.19.0/go.mod h1:y0zrRqlQRWQ5PXaYCOMLTW2fpsxZ8Qh9I/ohnInJEys= -periph.io/x/conn/v3 v3.7.1 h1:tMjNv3WO8jEz/ePuXl7y++2zYi8LsQ5otbmqGKy3Myg= -periph.io/x/conn/v3 v3.7.1/go.mod h1:c+HCVjkzbf09XzcqZu/t+U8Ss/2QuJj0jgRF6Nye838= +github.com/jonboulle/clockwork v0.5.0 h1:Hyh9A8u51kptdkR+cqRpT1EebBwTn1oK9YfGYbdFz6I= +github.com/jonboulle/clockwork v0.5.0/go.mod h1:3mZlmanh0g2NDKO5TWZVJAfofYk64M7XN3SzBPjZF60= +golang.org/x/image v0.25.0 h1:Y6uW6rH1y5y/LK1J8BPWZtr6yZ7hrsy6hFrXjgsc2fQ= +golang.org/x/image v0.25.0/go.mod h1:tCAmOEGthTtkalusGp1g3xa2gke8J6c2N565dTyl9Rs= +periph.io/x/conn/v3 v3.7.2 h1:qt9dE6XGP5ljbFnCKRJ9OOCoiOyBGlw7JZgoi72zZ1s= +periph.io/x/conn/v3 v3.7.2/go.mod h1:Ao0b4sFRo4QOx6c1tROJU1fLJN1hUIYggjOrkIVnpGg= periph.io/x/d2xx v0.1.1 h1:LHp+u+qAWLB5THrTT/AzyjdvfUhllvDF5wBJP7uvn+U= periph.io/x/d2xx v0.1.1/go.mod h1:rLM321G11Fc14Pp088khBkmXb70Pxx/kCPaIK7uRUBc= -periph.io/x/devices/v3 v3.7.1 h1:BsExlfYJlZUZoawzpMF7ksgC9f1eBAdqvKRCGvb+VYw= -periph.io/x/devices/v3 v3.7.1/go.mod h1:ezQOe8WknDaMbKZXVwQUQkIauyLyJshwAHkIohHXA94= -periph.io/x/host/v3 v3.8.2 h1:ayKUDzgUCN0g8+/xM9GTkWaOBhSLVcVHGTfjAOi8OsQ= -periph.io/x/host/v3 v3.8.2/go.mod h1:yFL76AesNHR68PboofSWYaQTKmvPXsQH2Apvp/ls/K4= +periph.io/x/devices/v3 v3.7.4 h1:g9CGKTtiXS9iyDFDba4sr9pYde4dy+ZCKRPuKpKJdKo= +periph.io/x/devices/v3 v3.7.4/go.mod h1:FqFG9RotW2aCkfIlAes3qxziwgjRTncTMS5cSOcizNg= +periph.io/x/host/v3 v3.8.3 h1:v90ozCFDWgEyfNElZ+JnOvq0jAdW0vmgjCUy8dYXDds= +periph.io/x/host/v3 v3.8.3/go.mod h1:uKrIpfXjELwHkwGBNe6aos//XiQ/3uxDa1P2BmLV6Ok=