From 0d40c99d1fe51d75b31223819012a4eba6c80169 Mon Sep 17 00:00:00 2001 From: Alan Wang <44191076+alankrantas@users.noreply.github.com> Date: Sat, 18 Dec 2021 17:07:24 +0800 Subject: [PATCH] Add files via upload --- examples/lsm303agr/main.go | 49 +++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/examples/lsm303agr/main.go b/examples/lsm303agr/main.go index 9dd60c9..109290a 100644 --- a/examples/lsm303agr/main.go +++ b/examples/lsm303agr/main.go @@ -3,37 +3,54 @@ package main import ( "machine" "time" + "tinygo.org/x/drivers/lsm303agr" ) func main() { + // LSM303AGR/MAG is connected to the I2C0 bus on micro:bit v1 (the same as P19/P20) and v2 (internal) machine.I2C0.Configure(machine.I2CConfig{}) - accel_mag := lsm303agr.New(machine.I2C0) - if !accel_mag.Connected() { + sensor := lsm303agr.New(machine.I2C0) + sensor.Configure(lsm303agr.Configuration{}) //default settings + + // you can specify the following options to adjust accuracy, sensor range or save power. + // see https://github.com/tinygo-org/drivers/blob/release/lsm303agr/registers.go for details: + /* + sensor.Configure(lsm303agr.Configuration{ + AccelPowerMode: lsm303agr.ACCEL_POWER_NORMAL, + AccelRange: lsm303agr.ACCEL_RANGE_2G, + AccelDataRate: lsm303agr.ACCEL_DATARATE_100HZ, + MagPowerMode: lsm303agr.MAG_POWER_NORMAL, + MagSystemMode: lsm303agr.MAG_SYSTEM_CONTINUOUS, + MagDataRate: lsm303agr.MAG_DATARATE_10HZ, + }) + */ + + if !sensor.Connected() { println("LSM303AGR/MAG not connected!") return } - accel_mag.Configure(lsm303agr.Configuration{}) //default settings - for { + // accel_x, accel_y, accel_z := sensor.ReadAcceleration() + // println("ACCEL_X:", accel_x/100000, " ACCEL_Y:", accel_y/100000, " ACCEL_Z:", accel_z/100000) - accel_x, accel_y, accel_z := accel_mag.ReadAcceleration() - pitch, roll := accel_mag.ReadPitchRoll() - mag_x, mag_y, mag_z := accel_mag.ReadMagneticField() - heading := accel_mag.ReadCompass() - temp, _ := accel_mag.ReadTemperature() + // mag_x, mag_y, mag_z := sensor.ReadMagneticField() + // println("MAG_X:", mag_x/100000, " MAG_Y:", mag_y/100000, " MAG_Z:", mag_z/100000) + + pitch, roll := sensor.ReadPitchRoll() + println("Pitch:", float32(pitch)/100000, " Roll:", float32(roll)/100000) + + heading := sensor.ReadCompass() + println("Heading:", float32(heading)/100000, "degrees") + + temp, _ := sensor.ReadTemperature() + println("Temperature:", float32(temp)/1000, "*C") - println("ACCEL_X:", accel_x, " ACCEL_Y:", accel_y, " ACCEL_Z:", accel_z) - println("MAG_X:", mag_x, " MAG_Y:", mag_y, " MAG_Z:", mag_z) - println("Pitch:", pitch, " Roll:", roll) - println("Heading:", heading) - println("Temperature:", temp/1000) println("\n") - - time.Sleep(time.Millisecond * 100) + time.Sleep(time.Millisecond * 250) } }