mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-10 01:43:40 +00:00
all: introduce a temperature type
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().
This commit is contained in:
@@ -19,8 +19,8 @@ func main() {
|
||||
sensor.Configure()
|
||||
|
||||
for {
|
||||
temp := sensor.ReadTempF()
|
||||
fmt.Printf("temperature: %f\r\n", temp)
|
||||
temp, _ := sensor.ReadTemperature()
|
||||
fmt.Printf("temperature: %f°C\r\n", temp.Celsius())
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ func main() {
|
||||
|
||||
for {
|
||||
temp, _ := sensor.ReadTemperature()
|
||||
println("Temperature:", strconv.FormatFloat(float64(temp)/1000, 'f', 2, 64), "°C")
|
||||
println("Temperature:", strconv.FormatFloat(float64(temp.Celsius()), 'f', 2, 64), "°C")
|
||||
press, _ := sensor.ReadPressure()
|
||||
println("Pressure:", strconv.FormatFloat(float64(press)/100000, 'f', 2, 64), "hPa")
|
||||
hum, _ := sensor.ReadHumidity()
|
||||
|
||||
@@ -27,7 +27,7 @@ func main() {
|
||||
println("Error reading temperature", err)
|
||||
continue
|
||||
}
|
||||
fmt.Printf("Temperature: %.2f °C\n", float32(t)/1000)
|
||||
fmt.Printf("Temperature: %.2f °C\n", t.Celsius())
|
||||
|
||||
accelX, accelY, accelZ, err := sensor.ReadAcceleration()
|
||||
if err != nil {
|
||||
|
||||
@@ -22,7 +22,7 @@ func main() {
|
||||
|
||||
for {
|
||||
temp, _ := sensor.ReadTemperature()
|
||||
println("Temperature:", float32(temp)/1000, "°C")
|
||||
println("Temperature:", temp.Celsius(), "°C")
|
||||
|
||||
pressure, _ := sensor.ReadPressure()
|
||||
println("Pressure", float32(pressure)/100000, "hPa")
|
||||
|
||||
@@ -30,7 +30,7 @@ func main() {
|
||||
println("Error reading temperature")
|
||||
}
|
||||
// Temperature in degrees Celsius
|
||||
fmt.Printf("Temperature: %.2f °C\n", float32(t)/1000)
|
||||
fmt.Printf("Temperature: %.2f °C\n", t.Celsius())
|
||||
|
||||
p, err := sensor.ReadPressure()
|
||||
if err != nil {
|
||||
|
||||
@@ -38,13 +38,13 @@ func main() {
|
||||
}
|
||||
|
||||
for {
|
||||
temp, err := sensor.ReadTemperature() // returns the temperature in centicelsius
|
||||
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: " + strconv.FormatInt(int64(temp), 10) + " cC")
|
||||
println("Temperature:", temp/1000, "C")
|
||||
println("Pressure: " + strconv.FormatInt(int64(press), 10) + " cPa\n")
|
||||
}
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ func main() {
|
||||
fmt.Printf("Date: %d/%s/%02d %02d:%02d:%02d \r\n", dt.Year(), dt.Month(), dt.Day(), dt.Hour(), dt.Minute(), dt.Second())
|
||||
}
|
||||
temp, _ := rtc.ReadTemperature()
|
||||
fmt.Printf("Temperature: %.2f °C \r\n", float32(temp)/1000)
|
||||
fmt.Printf("Temperature: %.2f °C \r\n", temp.Celsius())
|
||||
|
||||
time.Sleep(time.Second * 1)
|
||||
}
|
||||
|
||||
@@ -23,8 +23,8 @@ func main() {
|
||||
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)
|
||||
x, _ = accel.ReadTemperature()
|
||||
println("Degrees C", float32(x)/1000, "\n\n")
|
||||
t, _ := accel.ReadTemperature()
|
||||
println("Degrees C", t.Celsius(), "\n\n")
|
||||
time.Sleep(time.Millisecond * 1000)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"machine"
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers"
|
||||
"tinygo.org/x/drivers/lsm6dsox"
|
||||
)
|
||||
|
||||
@@ -76,7 +77,7 @@ func calibrateGyro(device *lsm6dsox.Device) {
|
||||
}
|
||||
|
||||
// Arduino IDE's Serial Plotter
|
||||
func printPlotter(ax, ay, az, gx, gy, gz, t int32) {
|
||||
func printPlotter(ax, ay, az, gx, gy, gz int32, t drivers.Temperature) {
|
||||
if SHOW_ACCELERATION {
|
||||
fmt.Printf("AX:%f, AY:%f, AZ:%f,", axis(ax, 0), axis(ay, 0), axis(az, 0))
|
||||
}
|
||||
@@ -84,7 +85,7 @@ func printPlotter(ax, ay, az, gx, gy, gz, t int32) {
|
||||
fmt.Printf("GX:%f, GY:%f, GZ:%f,", axis(gx, cal[0]), axis(gy, cal[1]), axis(gz, cal[2]))
|
||||
}
|
||||
if SHOW_TEMPERATURE {
|
||||
fmt.Printf("T:%f", float32(t)/1000)
|
||||
fmt.Printf("T:%f", t.Celsius())
|
||||
}
|
||||
println()
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ func main() {
|
||||
println("Magnetic readings:", x, y, z)
|
||||
|
||||
c, _ := mag.ReadTemperature()
|
||||
println("Temperature:", float32(c)/1000, "°C")
|
||||
println("Temperature:", c.Celsius(), "°C")
|
||||
|
||||
time.Sleep(time.Millisecond * 100)
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ func main() {
|
||||
|
||||
temp, _ := thermo.ReadTemperature()
|
||||
|
||||
print(fmt.Sprintf("%.2f°C\r\n", float32(temp)/1000.0))
|
||||
print(fmt.Sprintf("%.2f°C\r\n", temp.Celsius()))
|
||||
|
||||
time.Sleep(time.Millisecond * 1000)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user