mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-20 14:39:00 +00:00
Add files via upload
This commit is contained in:
+33
-16
@@ -3,37 +3,54 @@ package main
|
|||||||
import (
|
import (
|
||||||
"machine"
|
"machine"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"tinygo.org/x/drivers/lsm303agr"
|
"tinygo.org/x/drivers/lsm303agr"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
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{})
|
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!")
|
println("LSM303AGR/MAG not connected!")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
accel_mag.Configure(lsm303agr.Configuration{}) //default settings
|
|
||||||
|
|
||||||
for {
|
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()
|
// mag_x, mag_y, mag_z := sensor.ReadMagneticField()
|
||||||
pitch, roll := accel_mag.ReadPitchRoll()
|
// println("MAG_X:", mag_x/100000, " MAG_Y:", mag_y/100000, " MAG_Z:", mag_z/100000)
|
||||||
mag_x, mag_y, mag_z := accel_mag.ReadMagneticField()
|
|
||||||
heading := accel_mag.ReadCompass()
|
pitch, roll := sensor.ReadPitchRoll()
|
||||||
temp, _ := accel_mag.ReadTemperature()
|
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")
|
println("\n")
|
||||||
|
time.Sleep(time.Millisecond * 250)
|
||||||
time.Sleep(time.Millisecond * 100)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user