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.
pull/1/head
M-A 10 years ago committed by GitHub
parent b939aeacd3
commit 8bca85318d

@ -137,7 +137,7 @@ type Opts struct {
// //
// It is recommended to call Stop() when done with the device so it stops // It is recommended to call Stop() when done with the device so it stops
// sampling. // sampling.
func NewI2C(i i2c.Conn, opts *Opts) (*Dev, error) { func NewI2C(b i2c.Bus, opts *Opts) (*Dev, error) {
addr := uint16(0x76) addr := uint16(0x76)
if opts != nil { if opts != nil {
switch opts.Address { 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") 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 { if err := d.makeDev(opts); err != nil {
return nil, err return nil, err
} }
@ -294,8 +294,7 @@ func (d *Dev) writeCommands(b []byte) error {
b[i] &^= 0x80 b[i] &^= 0x80
} }
} }
_, err := d.d.Write(b) return d.d.Tx(b, nil)
return err
} }
// Register table: // Register table:

@ -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 // 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. // bumping up from default bus speed of 100KHz if possible.
func NewI2C(i i2c.Conn, w, h int, rotated bool) (*Dev, error) { func NewI2C(i i2c.Bus, w, h int, rotated bool) (*Dev, error) {
return newDev(&i2c.Dev{Conn: i, Addr: 0x3C}, w, h, rotated) return newDev(&i2c.Dev{Bus: i, Addr: 0x3C}, w, h, rotated)
} }
// newDev is the common initialization code that is independent of the bus // newDev is the common initialization code that is independent of the bus

@ -35,12 +35,12 @@ func (i *I2C) String() string {
return fmt.Sprintf("bitbang/i2c(%s, %s)", i.scl, i.sda) return fmt.Sprintf("bitbang/i2c(%s, %s)", i.scl, i.sda)
} }
// Close implements i2c.ConnCloser. // Close implements i2c.BusCloser.
func (i *I2C) Close() error { func (i *I2C) Close() error {
return nil return nil
} }
// Tx implements i2c.Conn. // Tx implements i2c.Bus.
func (i *I2C) Tx(addr uint16, w, r []byte) error { func (i *I2C) Tx(addr uint16, w, r []byte) error {
i.mu.Lock() i.mu.Lock()
defer i.mu.Unlock() defer i.mu.Unlock()
@ -88,7 +88,7 @@ func (i *I2C) Tx(addr uint16, w, r []byte) error {
return nil return nil
} }
// Speed implements i2c.Conn. // Speed implements i2c.Bus.
func (i *I2C) Speed(hz int64) error { func (i *I2C) Speed(hz int64) error {
i.mu.Lock() i.mu.Lock()
defer i.mu.Unlock() defer i.mu.Unlock()
@ -254,4 +254,4 @@ func (i *I2C) sleepHalfCycle() {
host.Nanospin(i.halfCycle) host.Nanospin(i.halfCycle)
} }
var _ i2c.Conn = &I2C{} var _ i2c.Bus = &I2C{}

Loading…
Cancel
Save