|
|
|
|
@ -5,6 +5,7 @@
|
|
|
|
|
package ds18b20
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"reflect"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
@ -84,6 +85,50 @@ func TestSense(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TestParseTemperature tests a temperature parsing from scratchpad for DS18S20
|
|
|
|
|
// and DS18B20
|
|
|
|
|
func TestParseTemperature(t *testing.T) {
|
|
|
|
|
var testData = []struct {
|
|
|
|
|
family Family
|
|
|
|
|
scratchpad []byte
|
|
|
|
|
expectedTemp float64
|
|
|
|
|
}{
|
|
|
|
|
{DS18B20, []byte{0xD0, 0x07, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x10}, 125},
|
|
|
|
|
{DS18B20, []byte{0x50, 0x05, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x10}, 85},
|
|
|
|
|
{DS18B20, []byte{0x91, 0x01, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x10}, 25.0625},
|
|
|
|
|
{DS18B20, []byte{0xA2, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x10}, 10.125},
|
|
|
|
|
{DS18B20, []byte{0x08, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x10}, 0.5},
|
|
|
|
|
{DS18B20, []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x10}, 0},
|
|
|
|
|
{DS18B20, []byte{0xF8, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x10}, -0.5},
|
|
|
|
|
{DS18B20, []byte{0x5E, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x10}, -10.125},
|
|
|
|
|
{DS18B20, []byte{0x6F, 0xFE, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x10}, -25.0625},
|
|
|
|
|
{DS18B20, []byte{0x90, 0xFC, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x10}, -55},
|
|
|
|
|
|
|
|
|
|
{DS18S20, []byte{0xFA, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x0C, 0x10}, 125},
|
|
|
|
|
{DS18S20, []byte{0xAA, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x0C, 0x10}, 85},
|
|
|
|
|
{DS18S20, []byte{0x32, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x0B, 0x10}, 25.0625},
|
|
|
|
|
{DS18S20, []byte{0x32, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x0C, 0x10}, 25},
|
|
|
|
|
{DS18S20, []byte{0x14, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x0A, 0x10}, 10.125},
|
|
|
|
|
{DS18S20, []byte{0x01, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x04, 0x10}, 0.5},
|
|
|
|
|
{DS18S20, []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x0C, 0x10}, 0},
|
|
|
|
|
{DS18S20, []byte{0xFF, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0x04, 0x10}, -0.5},
|
|
|
|
|
{DS18S20, []byte{0xEC, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0x0E, 0x10}, -10.125},
|
|
|
|
|
{DS18S20, []byte{0xCE, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0x0C, 0x10}, -25},
|
|
|
|
|
{DS18S20, []byte{0xCE, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0x0D, 0x10}, -25.0625},
|
|
|
|
|
{DS18S20, []byte{0x92, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0x0C, 0x10}, -55},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, entry := range testData {
|
|
|
|
|
t.Run(fmt.Sprintf("%s>%f", entry.family, entry.expectedTemp), func(st *testing.T) {
|
|
|
|
|
d := &Dev{onewire: onewire.Dev{Addr: onewire.Address(0x740000070e41ac00 + int64(entry.family))}}
|
|
|
|
|
c := d.parseTemperature(entry.scratchpad)
|
|
|
|
|
if c.Celsius() != entry.expectedTemp {
|
|
|
|
|
st.Errorf("expected %f, got %f", entry.expectedTemp, c.Celsius())
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TestConvertAll tests a temperature conversion on all ds18b20 using
|
|
|
|
|
// recorded bus transactions.
|
|
|
|
|
func TestConvertAll(t *testing.T) {
|
|
|
|
|
|