diff --git a/devices/ds248x/ds248x.go b/devices/ds248x/ds248x.go index e7fab5b..f95283c 100644 --- a/devices/ds248x/ds248x.go +++ b/devices/ds248x/ds248x.go @@ -35,8 +35,7 @@ const ( // Opts contains options to pass to the constructor. type Opts struct { - Addr uint16 // I²C address, default 0x18 - PassivePullup bool // false:use active pull-up, true: disable active pullup + PassivePullup bool // false:use active pull-up, true: disable active pullup // The following options are only available on the ds2483 (not ds2482-100). // The actual value used is the closest possible value (rounded up or down). @@ -47,21 +46,28 @@ type Opts struct { PullupRes PupOhm // passive pull-up resistance, true: 500Ω, false: 1kΩ } +// DefaultOpts is the recommended default options. +var DefaultOpts = Opts{ + PassivePullup: false, + ResetLow: 560 * time.Microsecond, + PresenceDetect: 68 * time.Microsecond, + Write0Low: 64 * time.Microsecond, + Write0Recovery: 5250 * time.Nanosecond, + PullupRes: R1000Ω, +} + // New returns a device object that communicates over I²C to the DS2482/DS2483 // controller. // // This device object implements onewire.Bus and can be used to // access devices on the bus. -func New(i i2c.Bus, opts *Opts) (*Dev, error) { - addr := uint16(0x18) - if opts != nil { - switch opts.Addr { - case 0x18, 0x19, 0x20, 0x21: - addr = opts.Addr - case 0x00: - default: - return nil, errors.New("ds248x: given address not supported by device") - } +// +// Valid I²C addresses are 0x18, 0x19, 0x20 and 0x21. +func New(i i2c.Bus, addr uint16, opts *Opts) (*Dev, error) { + switch addr { + case 0x18, 0x19, 0x20, 0x21: + default: + return nil, errors.New("ds248x: given address not supported by device") } d := &Dev{i2c: &i2c.Dev{Bus: i, Addr: addr}} if err := d.makeDev(opts); err != nil { @@ -241,25 +247,6 @@ func (d *Dev) waitIdle(delay time.Duration) byte { } func (d *Dev) makeDev(opts *Opts) error { - // Doctor the opts to apply default values. - if opts == nil { - opts = &defaults - } - if opts.ResetLow == 0 { - opts.ResetLow = defaults.ResetLow - } - if opts.PresenceDetect == 0 { - opts.PresenceDetect = defaults.PresenceDetect - } - if opts.Write0Low == 0 { - opts.Write0Low = defaults.Write0Low - } - if opts.Write0Recovery == 0 { - opts.Write0Recovery = defaults.Write0Recovery - } - if opts.PullupRes == 0 { - opts.PullupRes = defaults.PullupRes - } d.tReset = 2 * opts.ResetLow d.tSlot = opts.Write0Low + opts.Write0Recovery @@ -335,16 +322,6 @@ var sleep = time.Sleep var _ conn.Resource = &Dev{} var _ fmt.Stringer = &Dev{} -// defaults holds default values for optional parameters. -var defaults = Opts{ - PassivePullup: false, - ResetLow: 560 * time.Microsecond, - PresenceDetect: 68 * time.Microsecond, - Write0Low: 64 * time.Microsecond, - Write0Recovery: 5250 * time.Nanosecond, - PullupRes: R1000Ω, -} - const ( cmdReset = 0xf0 // reset ds248x cmdSetReadPtr = 0xe1 // set the read pointer diff --git a/devices/ds248x/ds248x_test.go b/devices/ds248x/ds248x_test.go index 126e8b4..345f8ea 100644 --- a/devices/ds248x/ds248x_test.go +++ b/devices/ds248x/ds248x_test.go @@ -21,7 +21,7 @@ func TestNew(t *testing.T) { {Addr: 0x18, W: []byte{0xc3, 0x6, 0x26, 0x46, 0x66, 0x86}}, }, } - d, err := New(&bus, nil) + d, err := New(&bus, 0x18, &DefaultOpts) if err != nil { t.Fatal(err) } @@ -46,8 +46,7 @@ func TestNew_opts(t *testing.T) { {Addr: 0x18, W: []byte{0xc3, 0x6, 0x26, 0x46, 0x66, 0x86}}, }, } - opts := &Opts{Addr: 0x18} - if _, err := New(&bus, opts); err != nil { + if _, err := New(&bus, 0x18, &DefaultOpts); err != nil { t.Fatal(err) } if err := bus.Close(); err != nil { @@ -79,7 +78,7 @@ func TestRecordInit(t *testing.T) { } i2cBus := &i2ctest.Record{Bus: i2cReal} // Now init the ds248x. - owBus, err := New(i2cBus, nil) + owBus, err := New(i2cBus, 0x18, &DefaultOpts) if err != nil { t.Fatal(err) } diff --git a/devices/ds248x/example_test.go b/devices/ds248x/example_test.go index afd207c..42ae202 100644 --- a/devices/ds248x/example_test.go +++ b/devices/ds248x/example_test.go @@ -27,7 +27,7 @@ func Example() { defer b.Close() // Open the DS248x to get a 1-wire bus. - ob, err := ds248x.New(b, nil) + ob, err := ds248x.New(b, 0x18, &ds248x.DefaultOpts) if err != nil { log.Fatal(err) }