tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/examples/lsm303agr/main.go (about) 1 package main 2 3 import ( 4 "machine" 5 "time" 6 7 "tinygo.org/x/drivers/lsm303agr" 8 ) 9 10 func main() { 11 12 // LSM303AGR/MAG is connected to the I2C0 bus on micro:bit v1 (the same as P19/P20) and v2 (internal) 13 machine.I2C0.Configure(machine.I2CConfig{}) 14 15 sensor := lsm303agr.New(machine.I2C0) 16 err := sensor.Configure(lsm303agr.Configuration{}) //default settings 17 if err != nil { 18 for { 19 println("Failed to configure", err.Error()) 20 time.Sleep(time.Second) 21 } 22 } 23 24 // you can specify the following options to adjust accuracy, sensor range or save power. 25 // see https://github.com/tinygo-org/drivers/blob/release/lsm303agr/registers.go for details: 26 /* 27 sensor.Configure(lsm303agr.Configuration{ 28 AccelPowerMode: lsm303agr.ACCEL_POWER_NORMAL, 29 AccelRange: lsm303agr.ACCEL_RANGE_2G, 30 AccelDataRate: lsm303agr.ACCEL_DATARATE_100HZ, 31 MagPowerMode: lsm303agr.MAG_POWER_NORMAL, 32 MagSystemMode: lsm303agr.MAG_SYSTEM_CONTINUOUS, 33 MagDataRate: lsm303agr.MAG_DATARATE_10HZ, 34 }) 35 */ 36 37 for { 38 39 if !sensor.Connected() { 40 println("LSM303AGR/MAG not connected!") 41 time.Sleep(time.Second) 42 continue 43 } 44 45 // accel_x, accel_y, accel_z := sensor.ReadAcceleration() 46 // println("ACCEL_X:", accel_x/100000, " ACCEL_Y:", accel_y/100000, " ACCEL_Z:", accel_z/100000) 47 48 // mag_x, mag_y, mag_z := sensor.ReadMagneticField() 49 // println("MAG_X:", mag_x/100000, " MAG_Y:", mag_y/100000, " MAG_Z:", mag_z/100000) 50 51 pitch, roll, _ := sensor.ReadPitchRoll() 52 println("Pitch:", float32(pitch)/100000, " Roll:", float32(roll)/100000) 53 54 heading, _ := sensor.ReadCompass() 55 println("Heading:", float32(heading)/100000, "degrees") 56 57 temp, _ := sensor.ReadTemperature() 58 println("Temperature:", float32(temp)/1000, "*C") 59 60 println("\n") 61 time.Sleep(time.Millisecond * 250) 62 } 63 64 }