Skip to content

Instantly share code, notes, and snippets.

@nobonobo
Created June 11, 2024 09:53
Show Gist options
  • Save nobonobo/ba31820afb2f2dc63ce32f41bae64c1a to your computer and use it in GitHub Desktop.
Save nobonobo/ba31820afb2f2dc63ce32f41bae64c1a to your computer and use it in GitHub Desktop.
TinyGo + MicroBit:v2 で IMUを読む
package main
import (
"fmt"
"machine"
"time"
"tinygo.org/x/drivers/lsm303agr"
)
func main() {
machine.I2C0.Configure(machine.I2CConfig{})
sensor := lsm303agr.New(machine.I2C0)
if err := sensor.Configure(lsm303agr.Configuration{}); err != nil {
for {
println(err)
time.Sleep(5 * time.Second)
}
}
for {
time.Sleep(time.Second)
if !sensor.Connected() {
println("LSM303AGR/MAG not connected!")
continue
}
x, y, z, err := sensor.ReadAcceleration()
if err != nil {
println(err)
continue
}
xf, yf, zf := float64(x)/100000, float64(y)/100000, float64(z)/100000
fmt.Printf("Acceleration: %.1f, %.1f, %.1f\n", xf, yf, zf)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment