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().
54 lines
1.4 KiB
Go
54 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"machine"
|
|
"strconv"
|
|
"time"
|
|
|
|
"tinygo.org/x/drivers/bmp388"
|
|
)
|
|
|
|
func main() {
|
|
|
|
machine.I2C0.Configure(machine.I2CConfig{})
|
|
|
|
sensor := bmp388.New(machine.I2C0)
|
|
if !sensor.Connected() {
|
|
println("Uh oh, BMP388 not detected")
|
|
return
|
|
}
|
|
|
|
// The accuracy of the sensor can be increased, at the cost of a slower output rate. Table 9 in Section 3.5 of the
|
|
// datasheet has recommended settings for common use cases. If increasing the sampling rate, the output data rate
|
|
// (ODR) will likely have to be decreased. Configure() will return an error if there's a problem with the
|
|
// configuration settings - keep decreasing the ODR and cycling the power to the sensor until it is happy.
|
|
err := sensor.Configure(bmp388.Config{
|
|
Pressure: bmp388.Sampling8X,
|
|
Temperature: bmp388.Sampling2X,
|
|
ODR: bmp388.Odr25,
|
|
IIR: bmp388.Coeff0,
|
|
Mode: bmp388.Normal,
|
|
})
|
|
|
|
// This is also fine
|
|
// err := sensor.Configure(bmp388.BMP388Config{})
|
|
|
|
if err != nil {
|
|
println(err)
|
|
}
|
|
|
|
for {
|
|
temp, err := sensor.ReadTemperature() // returns the temperature in millicelsius
|
|
press, err := sensor.ReadPressure() // returns the pressure in centipascals
|
|
|
|
if err != nil {
|
|
println(err)
|
|
} else {
|
|
println("Temperature:", temp/1000, "C")
|
|
println("Pressure: " + strconv.FormatInt(int64(press), 10) + " cPa\n")
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
|
}
|
|
}
|