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
+4 -2
View File
@@ -29,6 +29,8 @@ package thermistor // import "tinygo.org/x/drivers/thermistor"
import (
"machine"
"math"
"tinygo.org/x/drivers"
)
// Device holds the ADC pin and the needed settings for calculating the
@@ -61,7 +63,7 @@ func (d *Device) Configure() {
}
// ReadTemperature returns the temperature in celsius milli degrees (°C/1000)
func (d *Device) ReadTemperature() (temperature int32, err error) {
func (d *Device) ReadTemperature() (temperature drivers.Temperature, err error) {
var reading uint32
if d.HighSide {
// Thermistor connected from analog input to high logic level.
@@ -82,5 +84,5 @@ func (d *Device) ReadTemperature() (temperature int32, err error) {
steinhart = 1.0 / steinhart // Invert
steinhart -= 273.15 // convert to C
return int32(steinhart * 1000), nil
return drivers.Temperature(steinhart * 1000), nil
}