Skip to content

Commit

Permalink
i2c and spi: clarify with SetSpeed vs LimitSpeed.
Browse files Browse the repository at this point in the history
Otherwise when reading code is it very unclear if it may be slower or not.

Yet another breaking change.
  • Loading branch information
maruel committed Apr 5, 2017
1 parent 0ae4ac5 commit fd99445
Show file tree
Hide file tree
Showing 18 changed files with 45 additions and 45 deletions.
2 changes: 1 addition & 1 deletion cmd/apa102/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func mainImpl() error {
}
defer bus.Close()
if *speed != 0 {
if err := bus.Speed(int64(*speed)); err != nil {
if err := bus.LimitSpeed(int64(*speed)); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/bme280/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func mainImpl() error {
printPin("CS", p.CS())
}
// Slow down bus speed in case wires are too long.
if err := bus.Speed(100000); err != nil {
if err := bus.LimitSpeed(100000); err != nil {
return err
}
if dev, err = bme280.NewSPI(bus, &opts); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/ssd1306/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func mainImpl() error {
}
defer bus.Close()
if *speed != 0 {
if err := bus.Speed(int64(*speed)); err != nil {
if err := bus.LimitSpeed(int64(*speed)); err != nil {
return err
}
}
Expand Down
4 changes: 2 additions & 2 deletions conn/i2c/i2c.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (
// object.
type Bus interface {
Tx(addr uint16, w, r []byte) error
// Speed changes the bus speed, if supported.
Speed(hz int64) error
// SetSpeed changes the bus speed, if supported.
SetSpeed(hz int64) error
}

// BusCloser is an I²C bus that can be closed.
Expand Down
2 changes: 1 addition & 1 deletion conn/i2c/i2c_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (f *fakeBus) Tx(addr uint16, w, r []byte) error {
return f.err
}

func (f *fakeBus) Speed(hz int64) error {
func (f *fakeBus) SetSpeed(hz int64) error {
f.speed = hz
return f.err
}
2 changes: 1 addition & 1 deletion conn/i2c/i2creg/i2creg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func (f *fakeBus) Tx(addr uint16, w, r []byte) error {
return f.err
}

func (f *fakeBus) Speed(hz int64) error {
func (f *fakeBus) SetSpeed(hz int64) error {
f.speed = hz
return f.err
}
10 changes: 5 additions & 5 deletions conn/i2c/i2ctest/i2ctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ func (r *Record) Tx(addr uint16, w, read []byte) error {
return nil
}

// Speed implements i2c.Bus.
func (r *Record) Speed(hz int64) error {
// SetSpeed implements i2c.Bus.
func (r *Record) SetSpeed(hz int64) error {
if r.Bus != nil {
return r.Bus.Speed(hz)
return r.Bus.SetSpeed(hz)
}
return nil
}
Expand Down Expand Up @@ -136,8 +136,8 @@ func (p *Playback) Tx(addr uint16, w, r []byte) error {
return nil
}

// Speed implements i2c.Bus.
func (p *Playback) Speed(hz int64) error {
// SetSpeed implements i2c.Bus.
func (p *Playback) SetSpeed(hz int64) error {
return nil
}

Expand Down
6 changes: 3 additions & 3 deletions conn/i2c/i2ctest/i2ctest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestRecord_empty(t *testing.T) {
if s := r.String(); s != "record" {
t.Fatal(s)
}
if err := r.Speed(-100); err != nil {
if err := r.SetSpeed(-100); err != nil {
t.Fatal(err)
}
if r.Tx(1, nil, []byte{'a'}) == nil {
Expand Down Expand Up @@ -61,7 +61,7 @@ func TestPlayback(t *testing.T) {
if s := p.String(); s != "playback" {
t.Fatal(s)
}
if err := p.Speed(-100); err != nil {
if err := p.SetSpeed(-100); err != nil {
t.Fatal(err)
}
if err := p.Close(); err != nil {
Expand Down Expand Up @@ -144,7 +144,7 @@ func TestRecord_Playback(t *testing.T) {
SCLPin: &gpiotest.Pin{N: "CL"},
},
}
if err := r.Speed(-100); err != nil {
if err := r.SetSpeed(-100); err != nil {
t.Fatal(err)
}
if n := r.SDA().Name(); n != "DA" {
Expand Down
4 changes: 2 additions & 2 deletions conn/spi/spi.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type Conn interface {
type ConnCloser interface {
io.Closer
Conn
// Speed sets the maximum bus speed.
// LimitSpeed sets the maximum bus speed.
//
// It lets an application use a device at a lower speed than the maximum
// speed as rated by the device driver. This is useful for example when the
Expand All @@ -59,7 +59,7 @@ type ConnCloser interface {
// This function can be called multiple times and resets the previous value.
// 0 is not a value value for maxHz. The lowest speed between the bus speed
// and the device speed is selected.
Speed(maxHz int64) error
LimitSpeed(maxHz int64) error
}

// Pins defines the pins that a SPI bus interconnect is using on the host.
Expand Down
2 changes: 1 addition & 1 deletion conn/spi/spireg/spireg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (f *fakeBus) Duplex() conn.Duplex {
return conn.DuplexUnknown
}

func (f *fakeBus) Speed(maxHz int64) error {
func (f *fakeBus) LimitSpeed(maxHz int64) error {
return errors.New("not implemented")
}

Expand Down
22 changes: 11 additions & 11 deletions conn/spi/spitest/spitest.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func (r *RecordRaw) Close() error {
return nil
}

// Speed is a no-op.
func (r *RecordRaw) Speed(maxHz int64) error {
// LimitSpeed is a no-op.
func (r *RecordRaw) LimitSpeed(maxHz int64) error {
return nil
}

Expand Down Expand Up @@ -95,10 +95,10 @@ func (r *Record) Close() error {
return nil
}

// Speed implements spi.ConnCloser.
func (r *Record) Speed(maxHz int64) error {
// LimitSpeed implements spi.ConnCloser.
func (r *Record) LimitSpeed(maxHz int64) error {
if r.Conn != nil {
return r.Conn.Speed(maxHz)
return r.Conn.LimitSpeed(maxHz)
}
return nil
}
Expand Down Expand Up @@ -162,8 +162,8 @@ func (p *Playback) Close() error {
return p.Playback.Close()
}

// Speed implements spi.ConnCloser.
func (p *Playback) Speed(maxHz int64) error {
// LimitSpeed implements spi.ConnCloser.
func (p *Playback) LimitSpeed(maxHz int64) error {
return nil
}

Expand Down Expand Up @@ -204,10 +204,10 @@ func (l *Log) Close() error {
return err
}

// Speed implements spi.ConnCloser.
func (l *Log) Speed(maxHz int64) error {
err := l.Conn.Speed(maxHz)
log.Printf("%s.Speed(%d) = %v", l.Conn, maxHz, err)
// LimitSpeed implements spi.ConnCloser.
func (l *Log) LimitSpeed(maxHz int64) error {
err := l.Conn.LimitSpeed(maxHz)
log.Printf("%s.LimitSpeed(%d) = %v", l.Conn, maxHz, err)
return err
}

Expand Down
10 changes: 5 additions & 5 deletions conn/spi/spitest/spitest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
func TestRecordRaw(t *testing.T) {
b := bytes.Buffer{}
r := NewRecordRaw(&b)
if err := r.Speed(-100); err != nil {
if err := r.LimitSpeed(-100); err != nil {
t.Fatal(err)
}
if err := r.DevParams(0, spi.Mode0, 0); err != nil {
Expand All @@ -34,7 +34,7 @@ func TestRecord_empty(t *testing.T) {
if s := r.String(); s != "record" {
t.Fatal(s)
}
if err := r.Speed(-100); err != nil {
if err := r.LimitSpeed(-100); err != nil {
t.Fatal(err)
}
if err := r.DevParams(0, spi.Mode0, 0); err != nil {
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestPlayback(t *testing.T) {
if s := p.String(); s != "playback" {
t.Fatal(s)
}
if err := p.Speed(-100); err != nil {
if err := p.LimitSpeed(-100); err != nil {
t.Fatal(err)
}
if err := p.DevParams(0, spi.Mode0, 0); err != nil {
Expand Down Expand Up @@ -173,7 +173,7 @@ func TestRecord_Playback(t *testing.T) {
CSPin: &gpiotest.Pin{N: "CS"},
},
}
if err := r.Speed(-100); err != nil {
if err := r.LimitSpeed(-100); err != nil {
t.Fatal(err)
}
if err := r.DevParams(0, spi.Mode0, 0); err != nil {
Expand Down Expand Up @@ -229,7 +229,7 @@ func TestLog_Playback(t *testing.T) {
CSPin: &gpiotest.Pin{N: "CS"},
},
}
if err := r.Speed(-100); err != nil {
if err := r.LimitSpeed(-100); err != nil {
t.Fatal(err)
}
if err := r.DevParams(0, spi.Mode0, 0); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions experimental/devices/bitbang/i2c.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ func (i *I2C) Tx(addr uint16, w, r []byte) error {
return nil
}

// Speed implements i2c.Bus.
func (i *I2C) Speed(hz int64) error {
// SetSpeed implements i2c.Bus.
func (i *I2C) SetSpeed(hz int64) error {
i.mu.Lock()
defer i.mu.Unlock()
i.halfCycle = time.Second / time.Duration(hz) / time.Duration(2)
Expand Down
4 changes: 2 additions & 2 deletions experimental/devices/bitbang/spi.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func (s *SPI) Duplex() conn.Duplex {
return conn.Full
}

// Speed implements spi.ConnCloser.
func (s *SPI) Speed(maxHz int64) error {
// LimitSpeed implements spi.ConnCloser.
func (s *SPI) LimitSpeed(maxHz int64) error {
if maxHz <= 0 {
return errors.New("bitbang-spi: invalid maxHz")
}
Expand Down
4 changes: 2 additions & 2 deletions host/sysfs/i2c.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ func (i *I2C) Tx(addr uint16, w, r []byte) error {
return i.ioctl(ioctlRdwr, pp)
}

// Speed implements i2c.Bus.
func (i *I2C) Speed(hz int64) error {
// SetSpeed implements i2c.Bus.
func (i *I2C) SetSpeed(hz int64) error {
i.mu.Lock()
defer i.mu.Unlock()
// One has to resort to kernel driver specific value to be able to achieve
Expand Down
2 changes: 1 addition & 1 deletion host/sysfs/i2c_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestI2C_faked(t *testing.T) {
bus.Tx(1, []byte{0}, nil)
bus.Tx(1, nil, []byte{0})
bus.Tx(1, []byte{0}, []byte{0})
bus.Speed(0)
bus.SetSpeed(0)
bus.SCL()
bus.SDA()
if err := bus.Close(); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions host/sysfs/spi.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ func (s *SPI) String() string {
return fmt.Sprintf("SPI%d.%d", s.busNumber, s.chipSelect)
}

// Speed implements spi.ConnCloser.
func (s *SPI) Speed(maxHz int64) error {
// LimitSpeed implements spi.ConnCloser.
func (s *SPI) LimitSpeed(maxHz int64) error {
if maxHz < 1 || maxHz >= 1<<32 {
return fmt.Errorf("sysfs-spi: invalid speed %d", maxHz)
}
Expand Down
4 changes: 2 additions & 2 deletions host/sysfs/spi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func TestSPI_faked(t *testing.T) {
bus.Tx([]byte{0}, nil)
bus.Tx(nil, []byte{0})
bus.Tx([]byte{0}, []byte{0})
bus.Speed(0)
bus.Speed(1)
bus.LimitSpeed(0)
bus.LimitSpeed(1)
bus.CLK()
bus.MOSI()
bus.MISO()
Expand Down

0 comments on commit fd99445

Please sign in to comment.