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:
Ayke van Laethem
2021-10-21 23:17:32 +02:00
parent 3fe3fc64e4
commit 6763521eff
31 changed files with 106 additions and 104 deletions
+17
View File
@@ -0,0 +1,17 @@
package drivers
// This file contains some common units that can be used in a sensor driver.
// Temperature is a temperature in Celsius milli degrees (°C/1000). For example,
// the value 25000 is 25°C.
type Temperature int32
// Celsius returns the temperature in degrees Celsius.
func (t Temperature) Celsius() float32 {
return float32(t) / 1000
}
// Fahrenheit returns the temperature in degrees Fahrenheit.
func (t Temperature) Fahrenheit() float32 {
return t.Celsius()*1.8 + 32
}