ds248x: switch to use DefaultOpts pattern

Simplifies the expectations a bit, and the code too.

I²C address is not an option, for consistency with the other drivers.
pull/1/head
Marc-Antoine Ruel 8 years ago
parent ad7ff7dcea
commit ffc32d2881

@ -35,8 +35,7 @@ const (
// Opts contains options to pass to the constructor. // Opts contains options to pass to the constructor.
type Opts struct { 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 following options are only available on the ds2483 (not ds2482-100).
// The actual value used is the closest possible value (rounded up or down). // 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Ω 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 // New returns a device object that communicates over I²C to the DS2482/DS2483
// controller. // controller.
// //
// This device object implements onewire.Bus and can be used to // This device object implements onewire.Bus and can be used to
// access devices on the bus. // access devices on the bus.
func New(i i2c.Bus, opts *Opts) (*Dev, error) { //
addr := uint16(0x18) // Valid I²C addresses are 0x18, 0x19, 0x20 and 0x21.
if opts != nil { func New(i i2c.Bus, addr uint16, opts *Opts) (*Dev, error) {
switch opts.Addr { switch addr {
case 0x18, 0x19, 0x20, 0x21: case 0x18, 0x19, 0x20, 0x21:
addr = opts.Addr default:
case 0x00: return nil, errors.New("ds248x: given address not supported by device")
default:
return nil, errors.New("ds248x: given address not supported by device")
}
} }
d := &Dev{i2c: &i2c.Dev{Bus: i, Addr: addr}} d := &Dev{i2c: &i2c.Dev{Bus: i, Addr: addr}}
if err := d.makeDev(opts); err != nil { 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 { 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.tReset = 2 * opts.ResetLow
d.tSlot = opts.Write0Low + opts.Write0Recovery d.tSlot = opts.Write0Low + opts.Write0Recovery
@ -335,16 +322,6 @@ var sleep = time.Sleep
var _ conn.Resource = &Dev{} var _ conn.Resource = &Dev{}
var _ fmt.Stringer = &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 ( const (
cmdReset = 0xf0 // reset ds248x cmdReset = 0xf0 // reset ds248x
cmdSetReadPtr = 0xe1 // set the read pointer cmdSetReadPtr = 0xe1 // set the read pointer

@ -21,7 +21,7 @@ func TestNew(t *testing.T) {
{Addr: 0x18, W: []byte{0xc3, 0x6, 0x26, 0x46, 0x66, 0x86}}, {Addr: 0x18, W: []byte{0xc3, 0x6, 0x26, 0x46, 0x66, 0x86}},
}, },
} }
d, err := New(&bus, nil) d, err := New(&bus, 0x18, &DefaultOpts)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -46,8 +46,7 @@ func TestNew_opts(t *testing.T) {
{Addr: 0x18, W: []byte{0xc3, 0x6, 0x26, 0x46, 0x66, 0x86}}, {Addr: 0x18, W: []byte{0xc3, 0x6, 0x26, 0x46, 0x66, 0x86}},
}, },
} }
opts := &Opts{Addr: 0x18} if _, err := New(&bus, 0x18, &DefaultOpts); err != nil {
if _, err := New(&bus, opts); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if err := bus.Close(); err != nil { if err := bus.Close(); err != nil {
@ -79,7 +78,7 @@ func TestRecordInit(t *testing.T) {
} }
i2cBus := &i2ctest.Record{Bus: i2cReal} i2cBus := &i2ctest.Record{Bus: i2cReal}
// Now init the ds248x. // Now init the ds248x.
owBus, err := New(i2cBus, nil) owBus, err := New(i2cBus, 0x18, &DefaultOpts)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

@ -27,7 +27,7 @@ func Example() {
defer b.Close() defer b.Close()
// Open the DS248x to get a 1-wire bus. // 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 { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

Loading…
Cancel
Save