From 79423600fb49f100408002763e6c1a3beb387640 Mon Sep 17 00:00:00 2001 From: Marc-Antoine Ruel Date: Sat, 26 May 2018 21:56:50 -0400 Subject: [PATCH] ds18b20: change Dev to implement physic.SenseEnv; tweak String() This removes the Temperature() function for coherency with the other drivers. This removes the dependency on package fmt. --- devices/ds18b20/ds18b20.go | 33 ++++++++++++++++++++++++--------- devices/ds18b20/ds18b20_test.go | 15 +++++++-------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/devices/ds18b20/ds18b20.go b/devices/ds18b20/ds18b20.go index 4126ffe..7a411dd 100644 --- a/devices/ds18b20/ds18b20.go +++ b/devices/ds18b20/ds18b20.go @@ -23,7 +23,6 @@ package ds18b20 import ( "errors" - "fmt" "time" "periph.io/x/periph/conn" @@ -100,7 +99,7 @@ type Dev struct { } func (d *Dev) String() string { - return fmt.Sprintf("DS18B20{%v}", d.onewire) + return "DS18B20{" + d.onewire.String() + "}" } // Halt implements conn.Resource. @@ -108,13 +107,29 @@ func (d *Dev) Halt() error { return nil } -// Temperature performs a conversion and returns the temperature. -func (d *Dev) Temperature() (physic.Temperature, error) { +// Sense implements physic.SenseEnv. +func (d *Dev) Sense(e *physic.Env) error { if err := d.onewire.TxPower([]byte{0x44}, nil); err != nil { - return 0, err + return err } conversionSleep(d.resolution) - return d.LastTemp() + t, err := d.LastTemp() + if err != nil { + return err + } + e.Temperature = t + return nil +} + +// SenseContinuous implements physic.SenseEnv. +func (d *Dev) SenseContinuous(time.Duration) (<-chan physic.Env, error) { + // TODO(maruel): Manually poll in a loop via time.NewTicker. + return nil, errors.New("ds18b20: not implemented") +} + +// Precision implements physic.SenseEnv. +func (d *Dev) Precision(e *physic.Env) { + e.Temperature = physic.Kelvin / 16 } // LastTemp reads the temperature resulting from the last conversion from the @@ -131,8 +146,8 @@ func (d *Dev) LastTemp() (physic.Temperature, error) { // spad[1] is MSB, spad[0] is LSB and has 4 fractional bits. Need to do sign // extension multiply by 1000 to get Millis, divide by 16 due to 4 fractional // bits. Datasheet p.4. - v := (int(int8(spad[1]))<<8 + int(spad[0])) * 1000 / 16 - c := physic.Temperature(v)*physic.MilliKelvin + physic.ZeroCelsius + v := physic.Temperature((int(int8(spad[1]))<<8 + int(spad[0]))) + c := v*physic.Kelvin/16 + physic.ZeroCelsius // The device powers up with a value of 85°C, so if we read that odds are // very high that either no conversion was performed or that the conversion @@ -185,4 +200,4 @@ func (d *Dev) readScratchpad() ([]byte, error) { var sleep = time.Sleep var _ conn.Resource = &Dev{} -var _ fmt.Stringer = &Dev{} +var _ physic.SenseEnv = &Dev{} diff --git a/devices/ds18b20/ds18b20_test.go b/devices/ds18b20/ds18b20_test.go index b8a2806..26518db 100644 --- a/devices/ds18b20/ds18b20_test.go +++ b/devices/ds18b20/ds18b20_test.go @@ -30,9 +30,9 @@ func TestNew_fail_read(t *testing.T) { } } -// TestTemperature tests a temperature conversion on a ds18b20 using +// TestSense tests a temperature conversion on a ds18b20 using // recorded bus transactions. -func TestTemperature(t *testing.T) { +func TestSense(t *testing.T) { // set-up playback using the recording output. ops := []onewiretest.IO{ // Match ROM + Read Scratchpad (init) @@ -52,26 +52,25 @@ func TestTemperature(t *testing.T) { }, } var addr onewire.Address = 0x740000070e41ac28 - temp := 30*physic.Celsius + physic.ZeroCelsius bus := onewiretest.Playback{Ops: ops} dev, err := New(&bus, addr, 10) if err != nil { t.Fatal(err) } - if s := dev.String(); s != "DS18B20{{playback 8358680938703596584}}" { + if s := dev.String(); s != "DS18B20{playback(0x740000070e41ac28)}" { t.Fatal(s) } // Read the temperature. var sleeps []time.Duration sleep = func(d time.Duration) { sleeps = append(sleeps, d) } defer func() { sleep = func(time.Duration) {} }() - now, err := dev.Temperature() - if err != nil { + e := physic.Env{} + if err := dev.Sense(&e); err != nil { t.Fatal(err) } // Expect the correct value. - if now != temp { - t.Errorf("expected %s, got %s", temp.String(), now.String()) + if expected := 30*physic.Celsius + physic.ZeroCelsius; e.Temperature != expected { + t.Errorf("expected %s, got %s", expected.String(), e.Temperature.String()) } // Expect it to take >187ms if !reflect.DeepEqual(sleeps, []time.Duration{188 * time.Millisecond}) {