From 8bca85318d615396c264bc21ef26e56199fdd46b Mon Sep 17 00:00:00 2001 From: M-A Date: Tue, 8 Nov 2016 12:23:05 -0500 Subject: [PATCH] conn: Simplify Conn, rename i2c.Conn to i2c.Bus. (#40) - Remove fmt.Stringer and io.Writer from conn.Conn. This makes it compatible with exp/io. - Rename i2c.Conn to i2c.Bus since this was confusion. This clarifies the distinction between a point-to-point connection and a bus. - Add these to bus specific interfaces, i2c.Bus, spi.Conn, uart.Conn. Fixes #15. --- devices/bme280/bme280.go | 7 +++---- devices/ssd1306/ssd1306.go | 4 ++-- experimental/devices/bitbang/i2c.go | 8 ++++---- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/devices/bme280/bme280.go b/devices/bme280/bme280.go index 830a227..7d85825 100644 --- a/devices/bme280/bme280.go +++ b/devices/bme280/bme280.go @@ -137,7 +137,7 @@ type Opts struct { // // It is recommended to call Stop() when done with the device so it stops // sampling. -func NewI2C(i i2c.Conn, opts *Opts) (*Dev, error) { +func NewI2C(b i2c.Bus, opts *Opts) (*Dev, error) { addr := uint16(0x76) if opts != nil { switch opts.Address { @@ -149,7 +149,7 @@ func NewI2C(i i2c.Conn, opts *Opts) (*Dev, error) { return nil, errors.New("given address not supported by device") } } - d := &Dev{d: &i2c.Dev{Conn: i, Addr: addr}, isSPI: false} + d := &Dev{d: &i2c.Dev{Bus: b, Addr: addr}, isSPI: false} if err := d.makeDev(opts); err != nil { return nil, err } @@ -294,8 +294,7 @@ func (d *Dev) writeCommands(b []byte) error { b[i] &^= 0x80 } } - _, err := d.d.Write(b) - return err + return d.d.Tx(b, nil) } // Register table: diff --git a/devices/ssd1306/ssd1306.go b/devices/ssd1306/ssd1306.go index 10f05e5..503a51d 100644 --- a/devices/ssd1306/ssd1306.go +++ b/devices/ssd1306/ssd1306.go @@ -88,8 +88,8 @@ func NewSPI(s spi.Conn, w, h int, rotated bool) (*Dev, error) { // // As per datasheet, maximum clock speed is 1/2.5µs = 400KHz. It's worth // bumping up from default bus speed of 100KHz if possible. -func NewI2C(i i2c.Conn, w, h int, rotated bool) (*Dev, error) { - return newDev(&i2c.Dev{Conn: i, Addr: 0x3C}, w, h, rotated) +func NewI2C(i i2c.Bus, w, h int, rotated bool) (*Dev, error) { + return newDev(&i2c.Dev{Bus: i, Addr: 0x3C}, w, h, rotated) } // newDev is the common initialization code that is independent of the bus diff --git a/experimental/devices/bitbang/i2c.go b/experimental/devices/bitbang/i2c.go index 98717d2..d0784ef 100644 --- a/experimental/devices/bitbang/i2c.go +++ b/experimental/devices/bitbang/i2c.go @@ -35,12 +35,12 @@ func (i *I2C) String() string { return fmt.Sprintf("bitbang/i2c(%s, %s)", i.scl, i.sda) } -// Close implements i2c.ConnCloser. +// Close implements i2c.BusCloser. func (i *I2C) Close() error { return nil } -// Tx implements i2c.Conn. +// Tx implements i2c.Bus. func (i *I2C) Tx(addr uint16, w, r []byte) error { i.mu.Lock() defer i.mu.Unlock() @@ -88,7 +88,7 @@ func (i *I2C) Tx(addr uint16, w, r []byte) error { return nil } -// Speed implements i2c.Conn. +// Speed implements i2c.Bus. func (i *I2C) Speed(hz int64) error { i.mu.Lock() defer i.mu.Unlock() @@ -254,4 +254,4 @@ func (i *I2C) sleepHalfCycle() { host.Nanospin(i.halfCycle) } -var _ i2c.Conn = &I2C{} +var _ i2c.Bus = &I2C{}