mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-03 06:27:47 +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().
33 lines
543 B
Go
33 lines
543 B
Go
package main
|
|
|
|
import (
|
|
"time"
|
|
|
|
"machine"
|
|
|
|
"tinygo.org/x/drivers/bmp180"
|
|
)
|
|
|
|
func main() {
|
|
machine.I2C0.Configure(machine.I2CConfig{})
|
|
sensor := bmp180.New(machine.I2C0)
|
|
sensor.Configure()
|
|
|
|
connected := sensor.Connected()
|
|
if !connected {
|
|
println("BMP180 not detected")
|
|
return
|
|
}
|
|
println("BMP180 detected")
|
|
|
|
for {
|
|
temp, _ := sensor.ReadTemperature()
|
|
println("Temperature:", temp.Celsius(), "°C")
|
|
|
|
pressure, _ := sensor.ReadPressure()
|
|
println("Pressure", float32(pressure)/100000, "hPa")
|
|
|
|
time.Sleep(2 * time.Second)
|
|
}
|
|
}
|