Simplify function

pull/76/head
George Sexton 2 years ago
parent 5a677b58ed
commit 3895b5fef8

@ -167,13 +167,12 @@ func twosComplement(value uint16) uint16 {
// countToTemperature returns the temperature from the raw device count. // countToTemperature returns the temperature from the raw device count.
func countToTemperature(bytes []byte) physic.Temperature { func countToTemperature(bytes []byte) physic.Temperature {
var t physic.Temperature
count := (uint16(bytes[0]) << 4) | (uint16(bytes[1]) >> 4) count := (uint16(bytes[0]) << 4) | (uint16(bytes[1]) >> 4)
negative := (count & (1 << 11)) > 0 negative := (count & (1 << 11)) > 0
if negative { if negative {
count = twosComplement(count) + 1 count = twosComplement(count) + 1
}
var t physic.Temperature
if negative {
t = physic.ZeroCelsius - (physic.Temperature(count) * _DEGREES_RESOLUTION) t = physic.ZeroCelsius - (physic.Temperature(count) * _DEGREES_RESOLUTION)
} else { } else {
t = physic.ZeroCelsius + (physic.Temperature(count) * _DEGREES_RESOLUTION) t = physic.ZeroCelsius + (physic.Temperature(count) * _DEGREES_RESOLUTION)

Loading…
Cancel
Save