Skip to content

Commit d3c817e

Browse files
committed
Replace Safe functions with legacy.PinOutput
1 parent c6d83b7 commit d3c817e

File tree

13 files changed

+82
-97
lines changed

13 files changed

+82
-97
lines changed

bmi160/bmi160.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"time"
55

66
"tinygo.org/x/drivers"
7+
"tinygo.org/x/drivers/internal/legacy"
78
)
89

910
// DeviceSPI is the SPI interface to a BMI160 accelerometer/gyroscope. There is
@@ -23,7 +24,7 @@ type DeviceSPI struct {
2324
// using this device.
2425
func NewSPI(csb drivers.PinOutput, spi drivers.SPI) *DeviceSPI {
2526
return &DeviceSPI{
26-
CSB: drivers.SafePinOutput(csb), // chip select
27+
CSB: legacy.PinOutput(csb), // chip select
2728
Bus: spi,
2829
}
2930
}

buzzer/buzzer.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"time"
66

77
"tinygo.org/x/drivers"
8+
"tinygo.org/x/drivers/internal/legacy"
89
)
910

1011
// Device wraps a GPIO connection to a buzzer.
@@ -17,7 +18,7 @@ type Device struct {
1718
// New returns a new buzzer driver given which pin to use
1819
func New(pin drivers.PinOutput) Device {
1920
return Device{
20-
set: drivers.SafePinOutput(pin).Set,
21+
set: legacy.PinOutput(pin).Set,
2122
High: false,
2223
BPM: 96.0,
2324
}

dht/thermometer.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ type device struct {
4545
func (t *device) ReadMeasurements() error {
4646
// initial waiting
4747
state := powerUp(t.pin)
48-
defer func() {
49-
t.pin.Set(state)
50-
}()
48+
defer t.pin.Set(state)
5149
err := t.read()
5250
if err == nil {
5351
t.initialized = true
@@ -213,7 +211,7 @@ func waitForDataTransmission(p drivers.PinInput) error {
213211
func NewDummyDevice(pin drivers.Pin, deviceType DeviceType) DummyDevice {
214212
pin.Set(true)
215213
return &device{
216-
pin: drivers.SafePin(pin),
214+
pin: pin,
217215
measurements: deviceType,
218216
initialized: false,
219217
temperature: 0,

dht/timesafethermometer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func New(pin drivers.Pin, deviceType DeviceType) Device {
129129
pin.Set(true)
130130
return &managedDevice{
131131
t: device{
132-
pin: drivers.SafePin(pin),
132+
pin: pin,
133133
measurements: deviceType,
134134
initialized: false,
135135
},
@@ -146,7 +146,7 @@ func NewWithPolicy(pin drivers.Pin, deviceType DeviceType, updatePolicy UpdatePo
146146
pin.Set(true)
147147
result := &managedDevice{
148148
t: device{
149-
pin: drivers.SafePin(pin),
149+
pin: pin,
150150
measurements: deviceType,
151151
initialized: false,
152152
},

examples/dht/main.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1+
//go:build baremetal && tinygo
2+
13
package main
24

35
import (
46
"fmt"
57
"machine"
68
"time"
9+
710
"tinygo.org/x/drivers/dht"
811
)
912

1013
func main() {
11-
pin := machine.D6
14+
pin := &machinePin{pin: machine.D6}
1215
dhtSensor := dht.New(pin, dht.DHT11)
1316
for {
1417
temp, hum, err := dhtSensor.Measurements()
@@ -21,3 +24,27 @@ func main() {
2124
time.Sleep(time.Second * 2)
2225
}
2326
}
27+
28+
// machinePin wraps machine.Pin to ensure correct pin mode is set when Get or Set methods are called.
29+
30+
type machinePin struct {
31+
pin machine.Pin
32+
mode machine.PinMode
33+
modeSet bool
34+
}
35+
36+
func (p *machinePin) Get() bool {
37+
if !p.modeSet || p.mode != machine.PinInput {
38+
p.pin.Configure(machine.PinConfig{Mode: machine.PinInput})
39+
p.mode = machine.PinInput
40+
}
41+
return p.pin.Get()
42+
}
43+
44+
func (p *machinePin) Set(high bool) {
45+
if !p.modeSet || p.mode != machine.PinOutput {
46+
p.pin.Configure(machine.PinConfig{Mode: machine.PinOutput})
47+
p.mode = machine.PinOutput
48+
}
49+
p.pin.Set(high)
50+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//go:build !(baremetal && tinygo)
2+
3+
package legacy
4+
5+
import "tinygo.org/x/drivers"
6+
7+
func PinOutput(pin drivers.PinOutput) drivers.PinOutput {
8+
return pin
9+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//go:build baremetal && tinygo
2+
3+
package legacy
4+
5+
import (
6+
"machine"
7+
8+
"tinygo.org/x/drivers"
9+
)
10+
11+
func PinOutput(pin drivers.PinOutput) drivers.PinOutput {
12+
if p, ok := pin.(machine.Pin); ok {
13+
p.Configure(machine.PinConfig{Mode: machine.PinOutput})
14+
}
15+
return pin
16+
}

pin.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ package drivers
44
// allowing the use of different hardware or libraries without changing driver code.
55
//
66
// Note, pin mode functionality is not part of the Pin interface.
7-
// Implementations must ensure correct pin mode is set when Get or Set methods are called.
7+
// Client code is responsible for configuring pin modes correctly.
8+
// This can be done either before passing the pin to a driver constructor
9+
// or by ensuring correct pin mode is set when Get or Set methods are called.
10+
// See rpio package for an example of a pin implementation that does this.
811
//
9-
// Drivers must use SafePin(), SafePinInput() and SafePinOutput() wrappers in constructors.
10-
// The client code can pass either machine.Pin or a custom implementation of the Pin interface.
11-
// Wrappers for TinyGo's machine.Pin are provided in pin_tinygo.go.
12-
// It's custom implementation's responsibility to configure the pin modes correctly as
13-
// Wrappers in pin_generic.go are no-ops.
12+
// Drivers that used to configure output pin mode in their constructors can use
13+
// legacy.PinOutput() wrapper to keep the same behavior for machine.Pin.
14+
// See internal/legacy/pinlegacy_tinygo.go and internal/legacy/pinlegacy_generic.go for details.
15+
//
16+
// All new drivers are encouraged to not configure pin modes in their constructors and
17+
// do not depend on either machine package or legacy wrappers.
1418

1519
// PinInput is an interface for reading the state of a pin.
1620
type PinInput interface {

pin_generic.go

Lines changed: 0 additions & 18 deletions
This file was deleted.

pin_tinygo.go

Lines changed: 0 additions & 57 deletions
This file was deleted.

0 commit comments

Comments
 (0)