mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-02 05:57: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().
18 lines
491 B
Go
18 lines
491 B
Go
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
|
|
}
|