diff --git a/devices/apa102/apa102.go b/devices/apa102/apa102.go index 992204b..cdd3556 100644 --- a/devices/apa102/apa102.go +++ b/devices/apa102/apa102.go @@ -293,8 +293,9 @@ func (d *Dev) Halt() error { // As per APA102-C spec, the chip's max refresh rate is 400hz. // https://en.wikipedia.org/wiki/Flicker_fusion_threshold is a recommended // reading. -func New(s spi.Conn, numLights int, intensity uint8, temperature uint16) (*Dev, error) { - if err := s.DevParams(20000000, spi.Mode3, 8); err != nil { +func New(p spi.Port, numLights int, intensity uint8, temperature uint16) (*Dev, error) { + c, err := p.DevParams(20000000, spi.Mode3, 8) + if err != nil { return nil, err } // End frames are needed to be able to push enough SPI clock signals due to @@ -308,7 +309,7 @@ func New(s spi.Conn, numLights int, intensity uint8, temperature uint16) (*Dev, return &Dev{ Intensity: intensity, Temperature: temperature, - s: s, + s: c, numLights: numLights, rawBuf: buf, pixels: buf[4 : 4+4*numLights], diff --git a/devices/apa102/apa102_test.go b/devices/apa102/apa102_test.go index a611ad7..9abfc52 100644 --- a/devices/apa102/apa102_test.go +++ b/devices/apa102/apa102_test.go @@ -703,8 +703,8 @@ type configFail struct { spitest.Record } -func (c *configFail) DevParams(maxHz int64, mode spi.Mode, bits int) error { - return errors.New("injected error") +func (c *configFail) DevParams(maxHz int64, mode spi.Mode, bits int) (spi.Conn, error) { + return nil, errors.New("injected error") } func equalUint16(a, b []uint16) bool { diff --git a/devices/bme280/bme280.go b/devices/bme280/bme280.go index 9bb6e9b..5996433 100644 --- a/devices/bme280/bme280.go +++ b/devices/bme280/bme280.go @@ -176,12 +176,13 @@ func NewI2C(b i2c.Bus, opts *Opts) (*Dev, error) { // // BUG(maruel): This code was not tested yet, still waiting for a SPI enabled // device in the mail. -func NewSPI(s spi.Conn, opts *Opts) (*Dev, error) { +func NewSPI(p spi.Port, opts *Opts) (*Dev, error) { // It works both in Mode0 and Mode3. - if err := s.DevParams(10000000, spi.Mode3, 8); err != nil { + c, err := p.DevParams(10000000, spi.Mode3, 8) + if err != nil { return nil, err } - d := &Dev{d: s, isSPI: true} + d := &Dev{d: c, isSPI: true} if err := d.makeDev(opts); err != nil { return nil, err } diff --git a/devices/bme280/bme280_test.go b/devices/bme280/bme280_test.go index 6f3d0f8..80d83f2 100644 --- a/devices/bme280/bme280_test.go +++ b/devices/bme280/bme280_test.go @@ -545,6 +545,6 @@ type spiFail struct { spitest.Playback } -func (s *spiFail) DevParams(maxHz int64, mode spi.Mode, bits int) error { - return errors.New("failing") +func (s *spiFail) DevParams(maxHz int64, mode spi.Mode, bits int) (spi.Conn, error) { + return nil, errors.New("failing") } diff --git a/devices/bme280/bme280smoketest/bme280smoketest.go b/devices/bme280/bme280smoketest/bme280smoketest.go index af9e6c8..88b2877 100644 --- a/devices/bme280/bme280smoketest/bme280smoketest.go +++ b/devices/bme280/bme280smoketest/bme280smoketest.go @@ -67,7 +67,7 @@ func (s *SmokeTest) Run(args []string) (err error) { } i2cRecorder := i2ctest.Record{Bus: i2cBus} - spiRecorder := spitest.Record{Conn: spiBus} + spiRecorder := spitest.Record{Port: spiBus} err = run(&i2cRecorder, &spiRecorder) if len(i2cRecorder.Ops) != 0 { fmt.Printf("I²C recorder Addr: 0x%02X\n", i2cRecorder.Ops[0].Addr) @@ -119,7 +119,7 @@ func (s *SmokeTest) Run(args []string) (err error) { return err } -func run(i2cBus i2c.Bus, spiBus spi.ConnCloser) (err error) { +func run(i2cBus i2c.Bus, spiBus spi.PortCloser) (err error) { opts := &bme280.Opts{ Temperature: bme280.O16x, Pressure: bme280.O16x, diff --git a/devices/lepton/lepton.go b/devices/lepton/lepton.go index 6217096..8f0c8c8 100644 --- a/devices/lepton/lepton.go +++ b/devices/lepton/lepton.go @@ -94,17 +94,17 @@ type Dev struct { // Maximum I²C speed is 1Mhz. // // MOSI is not used and should be grounded. -func New(s spi.Conn, i i2c.Bus, cs gpio.PinOut) (*Dev, error) { +func New(p spi.Port, i i2c.Bus, cs gpio.PinOut) (*Dev, error) { // Sadly the Lepton will unconditionally send 27fps, even if the effective // rate is 9fps. mode := spi.Mode3 if cs == nil { // Query the CS pin before disabling it. - p, ok := s.(spi.Pins) + pins, ok := p.(spi.Pins) if !ok { return nil, errors.New("lepton: require manual access to the CS pin") } - cs = p.CS() + cs = pins.CS() if cs == gpio.INVALID { return nil, errors.New("lepton: require manual access to a valid CS pin") } @@ -112,7 +112,8 @@ func New(s spi.Conn, i i2c.Bus, cs gpio.PinOut) (*Dev, error) { } // TODO(maruel): Switch to 16 bits per word, so that big endian 16 bits word // decoding is done by the SPI driver. - if err := s.DevParams(20000000, mode, 8); err != nil { + s, err := p.DevParams(20000000, mode, 8) + if err != nil { return nil, err } c, err := cci.New(i) diff --git a/devices/lepton/lepton_test.go b/devices/lepton/lepton_test.go index 9759aa8..58d492c 100644 --- a/devices/lepton/lepton_test.go +++ b/devices/lepton/lepton_test.go @@ -370,7 +370,7 @@ type spiStream struct { err error } -func (s *spiStream) DevParams(maxHz int64, mode spi.Mode, bits int) error { +func (s *spiStream) DevParams(maxHz int64, mode spi.Mode, bits int) (spi.Conn, error) { if maxHz != 20000000 { s.t.Fatal(maxHz) } @@ -380,7 +380,7 @@ func (s *spiStream) DevParams(maxHz int64, mode spi.Mode, bits int) error { if bits != 8 { s.t.Fatal(bits) } - return s.err + return s, s.err } func (s *spiStream) Tx(w, r []byte) error { diff --git a/devices/ssd1306/ssd1306.go b/devices/ssd1306/ssd1306.go index 7f75073..697d2f9 100644 --- a/devices/ssd1306/ssd1306.go +++ b/devices/ssd1306/ssd1306.go @@ -122,7 +122,7 @@ type Dev struct { // The RES (reset) pin can be used outside of this driver but is not supported // natively. In case of external reset via the RES pin, this device drive must // be reinstantiated. -func NewSPI(s spi.Conn, dc gpio.PinOut, w, h int, rotated bool) (*Dev, error) { +func NewSPI(p spi.Port, dc gpio.PinOut, w, h int, rotated bool) (*Dev, error) { if dc == gpio.INVALID { return nil, errors.New("ssd1306: use nil for dc to use 3-wire mode, do not use gpio.INVALID") } @@ -133,10 +133,11 @@ func NewSPI(s spi.Conn, dc gpio.PinOut, w, h int, rotated bool) (*Dev, error) { } else if err := dc.Out(gpio.Low); err != nil { return nil, err } - if err := s.DevParams(3300000, spi.Mode0, bits); err != nil { + c, err := p.DevParams(3300000, spi.Mode0, bits) + if err != nil { return nil, err } - return newDev(s, w, h, rotated, true, dc) + return newDev(c, w, h, rotated, true, dc) } // NewI2C returns a Dev object that communicates over I²C to a SSD1306 display diff --git a/devices/ssd1306/ssd1306_test.go b/devices/ssd1306/ssd1306_test.go index c6e12fe..1c5b3e3 100644 --- a/devices/ssd1306/ssd1306_test.go +++ b/devices/ssd1306/ssd1306_test.go @@ -575,8 +575,8 @@ type configFail struct { spitest.Record } -func (c *configFail) DevParams(maxHz int64, mode spi.Mode, bits int) error { - return errors.New("injected error") +func (c *configFail) DevParams(maxHz int64, mode spi.Mode, bits int) (spi.Conn, error) { + return nil, errors.New("injected error") } type failPin struct { diff --git a/devices/ssd1306/ssd1306smoketest/ssd1306smoketest.go b/devices/ssd1306/ssd1306smoketest/ssd1306smoketest.go index c005bb6..4fff62e 100644 --- a/devices/ssd1306/ssd1306smoketest/ssd1306smoketest.go +++ b/devices/ssd1306/ssd1306smoketest/ssd1306smoketest.go @@ -97,7 +97,7 @@ func (s *SmokeTest) Run(args []string) (err error) { } i2cRecorder := i2ctest.Record{Bus: i2cBus} - spiRecorder := spitest.Record{Conn: spiBus} + spiRecorder := spitest.Record{Port: spiBus} err = s.run(&i2cRecorder, &spiRecorder, dc, *w, *h, *rotated) if len(i2cRecorder.Ops) != 0 { fmt.Printf("I²C recorder Addr: 0x%02X\n", i2cRecorder.Ops[0].Addr) @@ -149,7 +149,7 @@ func (s *SmokeTest) Run(args []string) (err error) { return err } -func (s *SmokeTest) run(i2cBus i2c.Bus, spiBus spi.ConnCloser, dc gpio.PinOut, w, h int, rotated bool) (err error) { +func (s *SmokeTest) run(i2cBus i2c.Bus, spiBus spi.PortCloser, dc gpio.PinOut, w, h int, rotated bool) (err error) { s.timings = make([]time.Duration, 2) start := time.Now() i2cDev, err2 := ssd1306.NewI2C(i2cBus, w, h, rotated)