Skip to content

Commit 453ff96

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

File tree

16 files changed

+112
-108
lines changed

16 files changed

+112
-108
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type Device struct {
1717
// New returns a new buzzer driver given which pin to use
1818
func New(pin drivers.PinOutput) Device {
1919
return Device{
20-
set: drivers.SafePinOutput(pin).Set,
20+
set: pin.Set,
2121
High: false,
2222
BPM: 96.0,
2323
}

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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
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"
11+
"tinygo.org/x/drivers/tinygo"
812
)
913

1014
func main() {
11-
pin := machine.D6
15+
pin := tinygo.New(machine.D6)
1216
dhtSensor := dht.New(pin, dht.DHT11)
1317
for {
1418
temp, hum, err := dhtSensor.Measurements()

examples/hcsr04/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ import (
55
"time"
66

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

1011
func main() {
11-
sensor := hcsr04.New(machine.D10, machine.D9)
12+
trigger := tinygo.New(machine.D10) // automatically configures pin as output
13+
echo := tinygo.New(machine.D9) // automatically configures pin as input
14+
sensor := hcsr04.New(trigger, echo)
1215
sensor.Configure()
1316

1417
println("Ultrasonic starts")

hcsr04/hcsr04.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@
55
package hcsr04
66

77
import (
8-
"machine"
98
"time"
9+
10+
"tinygo.org/x/drivers"
1011
)
1112

1213
const TIMEOUT = 23324 // max sensing distance (4m)
1314

1415
// Device holds the pins
1516
type Device struct {
16-
trigger machine.Pin
17-
echo machine.Pin
17+
trigger drivers.PinOutput
18+
echo drivers.PinInput
1819
}
1920

20-
// New returns a new ultrasonic driver given 2 pins
21-
func New(trigger, echo machine.Pin) Device {
21+
// New returns a new ultrasonic driver given 2 pins.
22+
// Pins must be configured as output (trigger) and input (echo).
23+
func New(trigger drivers.PinOutput, echo drivers.PinInput) Device {
2224
return Device{
2325
trigger: trigger,
2426
echo: echo,
@@ -27,8 +29,7 @@ func New(trigger, echo machine.Pin) Device {
2729

2830
// Configure configures the pins of the Device
2931
func (d *Device) Configure() {
30-
d.trigger.Configure(machine.PinConfig{Mode: machine.PinOutput})
31-
d.echo.Configure(machine.PinConfig{Mode: machine.PinInput})
32+
// no-op, left for API compatibility
3233
}
3334

3435
// ReadDistance returns the distance of the object in mm
@@ -45,11 +46,11 @@ func (d *Device) ReadDistance() int32 {
4546
// ReadPulse returns the time of the pulse (roundtrip) in microseconds
4647
func (d *Device) ReadPulse() int32 {
4748
t := time.Now()
48-
d.trigger.Low()
49+
d.trigger.Set(false)
4950
time.Sleep(2 * time.Microsecond)
50-
d.trigger.High()
51+
d.trigger.Set(true)
5152
time.Sleep(10 * time.Microsecond)
52-
d.trigger.Low()
53+
d.trigger.Set(false)
5354
i := uint8(0)
5455
for {
5556
if d.echo.Get() {
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 {

0 commit comments

Comments
 (0)