mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-04 15:07:46 +00:00
6763521eff
This type should be used whenever a sensor (or actuator?) works with a
temperature. For example, this commit changes the signature:
ReadTemperature() (int32, error)
to the following:
ReadTemperature() (drivers.Temperature, error)
I believe this is much clearer in intent. It also makes it trivial to
introduce common conversions. For example, there are already Celsius()
and Fahrenheit() methods to convert to the given units, as a floating
point. More units could be added as needed, for example a CelsiusInt().
31 lines
730 B
Go
31 lines
730 B
Go
// Connects to an LSM6DS3 I2C a 6 axis Inertial Measurement Unit (IMU)
|
|
package main
|
|
|
|
import (
|
|
"machine"
|
|
"time"
|
|
|
|
"tinygo.org/x/drivers/lsm6ds3"
|
|
)
|
|
|
|
func main() {
|
|
machine.I2C0.Configure(machine.I2CConfig{})
|
|
|
|
accel := lsm6ds3.New(machine.I2C0)
|
|
accel.Configure(lsm6ds3.Configuration{})
|
|
if !accel.Connected() {
|
|
println("LSM6DS3 not connected")
|
|
return
|
|
}
|
|
|
|
for {
|
|
x, y, z := accel.ReadAcceleration()
|
|
println("Acceleration:", float32(x)/1000000, float32(y)/1000000, float32(z)/1000000)
|
|
x, y, z = accel.ReadRotation()
|
|
println("Gyroscope:", float32(x)/1000000, float32(y)/1000000, float32(z)/1000000)
|
|
t, _ := accel.ReadTemperature()
|
|
println("Degrees C", t.Celsius(), "\n\n")
|
|
time.Sleep(time.Millisecond * 1000)
|
|
}
|
|
}
|