Skip to content

Commit f3945b1

Browse files
committed
apply suggestions from team
1 parent 06e4c02 commit f3945b1

File tree

9 files changed

+573
-43
lines changed

9 files changed

+573
-43
lines changed

apa102/apa102.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ package apa102 // import "tinygo.org/x/drivers/apa102"
55

66
import (
77
"image/color"
8-
"machine"
98

109
"tinygo.org/x/drivers"
10+
"tinygo.org/x/drivers/internal/legacy"
1111
)
1212

1313
const (
@@ -37,8 +37,11 @@ func New(b drivers.SPI) *Device {
3737

3838
// NewSoftwareSPI returns a new APA102 driver that will use a software based
3939
// implementation of the SPI protocol.
40-
func NewSoftwareSPI(sckPin, sdoPin machine.Pin, delay uint32) *Device {
41-
return New(&bbSPI{SCK: sckPin, SDO: sdoPin, Delay: delay})
40+
func NewSoftwareSPI(sckPin, sdoPin legacy.PinOutput, delay uint32) *Device {
41+
return New(&bbSPI{SCK: sckPin.Set, SDO: sdoPin.Set, Delay: delay, config: func() {
42+
legacy.ConfigurePinOut(sckPin)
43+
legacy.ConfigurePinOut(sdoPin)
44+
}})
4245
}
4346

4447
// WriteColors writes the given RGBA color slice out using the APA102 protocol.

apa102/softspi.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
package apa102
22

3-
import "machine"
3+
import (
4+
"tinygo.org/x/drivers"
5+
)
46

57
// bbSPI is a dumb bit-bang implementation of SPI protocol that is hardcoded
68
// to mode 0 and ignores trying to receive data. Just enough for the APA102.
79
// Note: making this unexported for now because it is probable not suitable
810
// most purposes other than the APA102 package. It might be desirable to make
911
// this more generic and include it in the TinyGo "machine" package instead.
1012
type bbSPI struct {
11-
SCK machine.Pin
12-
SDO machine.Pin
13-
Delay uint32
13+
SCK drivers.PinOutput
14+
SDO drivers.PinOutput
15+
Delay uint32
16+
config func()
1417
}
1518

1619
// Configure sets up the SCK and SDO pins as outputs and sets them low
1720
func (s *bbSPI) Configure() {
18-
s.SCK.Configure(machine.PinConfig{Mode: machine.PinOutput})
19-
s.SDO.Configure(machine.PinConfig{Mode: machine.PinOutput})
20-
s.SCK.Low()
21-
s.SDO.Low()
21+
s.config()
22+
s.SCK(false)
23+
s.SDO(false)
2224
if s.Delay == 0 {
2325
s.Delay = 1
2426
}
@@ -47,19 +49,19 @@ func (s *bbSPI) Transfer(b byte) (byte, error) {
4749
for i := uint8(0); i < 8; i++ {
4850

4951
// half clock cycle high to start
50-
s.SCK.High()
52+
s.SCK(true)
5153
s.delay()
5254

5355
// write the value to SDO (MSB first)
5456
if b&(1<<(7-i)) == 0 {
55-
s.SDO.Low()
57+
s.SDO(false)
5658
} else {
57-
s.SDO.High()
59+
s.SDO(true)
5860
}
5961
s.delay()
6062

6163
// half clock cycle low
62-
s.SCK.Low()
64+
s.SCK(false)
6365
s.delay()
6466

6567
// for actual SPI would try to read the SDI value here

bmi160/bmi160.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,43 @@
11
package bmi160
22

33
import (
4-
"machine"
54
"time"
65

76
"tinygo.org/x/drivers"
7+
"tinygo.org/x/drivers/internal/legacy"
88
)
99

1010
// DeviceSPI is the SPI interface to a BMI160 accelerometer/gyroscope. There is
1111
// also an I2C interface, but it is not yet supported.
1212
type DeviceSPI struct {
1313
// Chip select pin
14-
CSB machine.Pin
14+
CSB drivers.PinOutput
1515

1616
buf [7]byte
1717

1818
// SPI bus (requires chip select to be usable).
19-
Bus drivers.SPI
19+
Bus drivers.SPI
20+
config func()
2021
}
2122

2223
// NewSPI returns a new device driver. The pin and SPI interface are not
2324
// touched, provide a fully configured SPI object and call Configure to start
2425
// using this device.
25-
func NewSPI(csb machine.Pin, spi drivers.SPI) *DeviceSPI {
26+
func NewSPI(csb legacy.PinOutput, spi drivers.SPI) *DeviceSPI {
2627
return &DeviceSPI{
27-
CSB: csb, // chip select
28+
CSB: csb.Set, // chip select
2829
Bus: spi,
30+
config: func() {
31+
legacy.ConfigurePinOut(csb)
32+
},
2933
}
3034
}
3135

3236
// Configure configures the BMI160 for use. It configures the CSB pin and
3337
// configures the BMI160, but it does not configure the SPI interface (it is
3438
// assumed to be up and running).
3539
func (d *DeviceSPI) Configure() error {
36-
d.CSB.Configure(machine.PinConfig{Mode: machine.PinOutput})
37-
d.CSB.High()
40+
d.CSB(true)
3841

3942
// The datasheet recommends doing a register read from address 0x7F to get
4043
// SPI communication going:
@@ -86,9 +89,9 @@ func (d *DeviceSPI) ReadTemperature() (temperature int32, err error) {
8689
data[0] = 0x80 | reg_TEMPERATURE_0
8790
data[1] = 0
8891
data[2] = 0
89-
d.CSB.Low()
92+
d.CSB(false)
9093
err = d.Bus.Tx(data, data)
91-
d.CSB.High()
94+
d.CSB(true)
9295
if err != nil {
9396
return
9497
}
@@ -123,9 +126,9 @@ func (d *DeviceSPI) ReadAcceleration() (x int32, y int32, z int32, err error) {
123126
for i := 1; i < len(data); i++ {
124127
data[i] = 0
125128
}
126-
d.CSB.Low()
129+
d.CSB(false)
127130
err = d.Bus.Tx(data, data)
128-
d.CSB.High()
131+
d.CSB(true)
129132
if err != nil {
130133
return
131134
}
@@ -153,9 +156,9 @@ func (d *DeviceSPI) ReadRotation() (x int32, y int32, z int32, err error) {
153156
for i := 1; i < len(data); i++ {
154157
data[i] = 0
155158
}
156-
d.CSB.Low()
159+
d.CSB(false)
157160
err = d.Bus.Tx(data, data)
158-
d.CSB.High()
161+
d.CSB(true)
159162
if err != nil {
160163
return
161164
}
@@ -201,9 +204,9 @@ func (d *DeviceSPI) readRegister(address uint8) uint8 {
201204
data := d.buf[:2]
202205
data[0] = 0x80 | address
203206
data[1] = 0
204-
d.CSB.Low()
207+
d.CSB(false)
205208
d.Bus.Tx(data, data)
206-
d.CSB.High()
209+
d.CSB(true)
207210
return data[1]
208211
}
209212

@@ -217,7 +220,7 @@ func (d *DeviceSPI) writeRegister(address, data uint8) {
217220
buf[0] = address
218221
buf[1] = data
219222

220-
d.CSB.Low()
223+
d.CSB(false)
221224
d.Bus.Tx(buf, buf)
222-
d.CSB.High()
225+
d.CSB(true)
223226
}

buzzer/buzzer.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,38 @@
22
package buzzer // import "tinygo.org/x/drivers/buzzer"
33

44
import (
5-
"machine"
6-
75
"time"
6+
7+
"tinygo.org/x/drivers"
8+
"tinygo.org/x/drivers/internal/legacy"
89
)
910

1011
// Device wraps a GPIO connection to a buzzer.
1112
type Device struct {
12-
pin machine.Pin
13+
pin drivers.PinOutput
1314
High bool
1415
BPM float64
1516
}
1617

1718
// New returns a new buzzer driver given which pin to use
18-
func New(pin machine.Pin) Device {
19+
func New(pin legacy.PinOutput) Device {
1920
return Device{
20-
pin: pin,
21+
pin: pin.Set,
2122
High: false,
2223
BPM: 96.0,
2324
}
2425
}
2526

2627
// On sets the buzzer to a high state.
2728
func (l *Device) On() (err error) {
28-
l.pin.Set(true)
29+
l.pin(true)
2930
l.High = true
3031
return
3132
}
3233

3334
// Off sets the buzzer to a low state.
3435
func (l *Device) Off() (err error) {
35-
l.pin.Set(false)
36+
l.pin(false)
3637
l.High = false
3738
return
3839
}

internal/legacy/pinhal.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package legacy
2+
3+
// PinOutput represents a pin hardware abstraction layer for a pin that can output a digital signal.
4+
// This is an alternative to drivers.PinOutput abstraction which is a function type. Pros and cons
5+
// of both approaches have been discussed in the [relevant issue]. PinOutput should only be used
6+
// to expose an initialization function of a driver that receives pins of this type. Ideally
7+
// driver developers should also expose the initialization with drivers.Pin type:
8+
//
9+
// func New(p1, p2, p3 legacy.PinOutput) *Device {
10+
// return NewWithPinfuncs(p1.Set, p2.Set, p3.Set)
11+
// }
12+
//
13+
// func NewWithPinfuncs(p1, p2, p3 drivers.PinOutput) *Device {
14+
// return &Device{p1:p1, p2:p2, p3:p3}
15+
// }
16+
//
17+
// [relevant issue]: https://github.com/tinygo-org/drivers/pull/749/files
18+
type PinOutput interface {
19+
Set(level bool)
20+
}
21+
22+
// ConfigurePinOut is a legacy function used to configure pins as outputs.
23+
//
24+
// Deprecated: Do not configure pins in drivers.
25+
// This is a legacy feature and should only be used by drivers that
26+
// previously configured pins in initialization to avoid breaking users.
27+
func ConfigurePinOut(p PinOutput) {
28+
configurePinOut(p)
29+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//go:build baremetal
2+
3+
package legacy
4+
5+
import "machine"
6+
7+
func configurePinOut(p PinOutput) {
8+
machinePin, ok := p.(machine.Pin)
9+
if ok {
10+
machinePin.Configure(machine.PinConfig{Mode: machine.PinOutput})
11+
}
12+
}

internal/legacy/pinhal_os.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//go:build !baremetal
2+
3+
package legacy
4+
5+
func configurePinOut(p PinOutput) {}

pins.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package drivers
22

3-
// PinInput is hardware abstraction for a pin which receives a
4-
// digital signal and reads it (high or low voltage).
5-
type PinInput func() (level bool)
6-
73
// PinOutput is hardware abstraction for a pin which outputs a
84
// digital signal (high or low voltage).
95
type PinOutput func(level bool)
6+
7+
// PinInput is hardware abstraction for a pin which receives a
8+
// digital signal and reads it (high or low voltage).
9+
type PinInput func() (level bool)

0 commit comments

Comments
 (0)