mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-12 02:43:42 +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:
+3
-3
@@ -46,7 +46,7 @@ func (d *Device) Connected() bool {
|
||||
}
|
||||
|
||||
// Reads the temperature from the sensor and returns it in celsius milli degrees (°C/1000).
|
||||
func (d *Device) ReadTemperature() (temperature int32, err error) {
|
||||
func (d *Device) ReadTemperature() (temperature drivers.Temperature, err error) {
|
||||
|
||||
tmpData := make([]byte, 2)
|
||||
|
||||
@@ -62,7 +62,7 @@ func (d *Device) ReadTemperature() (temperature int32, err error) {
|
||||
temperatureSum |= int32(0xf800)
|
||||
}
|
||||
|
||||
temperature = temperatureSum * 625
|
||||
temperature = drivers.Temperature(temperatureSum * 625 / 10)
|
||||
|
||||
return temperature / 10, nil
|
||||
return temperature, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user