Skip to content

Commit c710cc8

Browse files
authored
ssd1xxx: break dependency from machine package (#812)
* ssd1xxx: break dependency from machine package * fix ssd1289 example * simplify
1 parent 4b831af commit c710cc8

File tree

6 files changed

+92
-88
lines changed

6 files changed

+92
-88
lines changed

examples/ssd1289/main.go

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

8+
"tinygo.org/x/drivers/internal/pin"
89
"tinygo.org/x/drivers/ssd1289"
910
)
1011

@@ -16,7 +17,7 @@ func main() {
1617
//consider creating a more efficient bus implementation that uses
1718
//your microcontrollers built in "ports"
1819
//see rp2040bus.go for an example for the rapsberry pi pico
19-
bus := ssd1289.NewPinBus([16]machine.Pin{
20+
bus := ssd1289.NewPinBus([16]pin.Output{
2021
machine.GP4, //DB0
2122
machine.GP5, //DB1
2223
machine.GP6, //DB2

ssd1289/pinbus.go

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
package ssd1289
22

3-
import "machine"
3+
import (
4+
"tinygo.org/x/drivers/internal/legacy"
5+
"tinygo.org/x/drivers/internal/pin"
6+
)
47

58
type pinBus struct {
6-
pins [16]machine.Pin
9+
pins [16]pin.Output
710
}
811

9-
func NewPinBus(pins [16]machine.Pin) pinBus {
12+
func NewPinBus(pins [16]pin.Output) pinBus {
1013

14+
// configure GPIO pins (on baremetal targets only, for backwards compatibility)
1115
for i := 0; i < 16; i++ {
12-
pins[i].Configure(machine.PinConfig{Mode: machine.PinOutput})
16+
legacy.ConfigurePinOut(pins[i])
1317
}
1418

1519
return pinBus{
@@ -18,20 +22,7 @@ func NewPinBus(pins [16]machine.Pin) pinBus {
1822
}
1923

2024
func (b pinBus) Set(data uint16) {
21-
b.pins[15].Set((data & (1 << 15)) != 0)
22-
b.pins[14].Set((data & (1 << 14)) != 0)
23-
b.pins[13].Set((data & (1 << 13)) != 0)
24-
b.pins[12].Set((data & (1 << 12)) != 0)
25-
b.pins[11].Set((data & (1 << 11)) != 0)
26-
b.pins[10].Set((data & (1 << 10)) != 0)
27-
b.pins[9].Set((data & (1 << 9)) != 0)
28-
b.pins[8].Set((data & (1 << 8)) != 0)
29-
b.pins[7].Set((data & (1 << 7)) != 0)
30-
b.pins[6].Set((data & (1 << 6)) != 0)
31-
b.pins[5].Set((data & (1 << 5)) != 0)
32-
b.pins[4].Set((data & (1 << 4)) != 0)
33-
b.pins[3].Set((data & (1 << 3)) != 0)
34-
b.pins[2].Set((data & (1 << 2)) != 0)
35-
b.pins[1].Set((data & (1 << 1)) != 0)
36-
b.pins[0].Set((data & (1 << 0)) != 0)
25+
for i := 15; i >= 0; i-- {
26+
b.pins[i].Set((data & (1 << i)) != 0)
27+
}
3728
}

ssd1289/ssd1289.go

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,45 @@ package ssd1289
55

66
import (
77
"image/color"
8-
"machine"
98
"time"
9+
10+
"tinygo.org/x/drivers/internal/legacy"
11+
"tinygo.org/x/drivers/internal/pin"
1012
)
1113

1214
type Bus interface {
1315
Set(data uint16)
1416
}
1517

1618
type Device struct {
17-
rs machine.Pin
18-
wr machine.Pin
19-
cs machine.Pin
20-
rst machine.Pin
19+
rs pin.OutputFunc
20+
wr pin.OutputFunc
21+
cs pin.OutputFunc
22+
rst pin.OutputFunc
2123
bus Bus
2224
}
2325

2426
const width = int16(240)
2527
const height = int16(320)
2628

27-
func New(rs machine.Pin, wr machine.Pin, cs machine.Pin, rst machine.Pin, bus Bus) Device {
28-
d := Device{
29-
rs: rs,
30-
wr: wr,
31-
cs: cs,
32-
rst: rst,
29+
func New(rs, wr, cs, rst pin.Output, bus Bus) *Device {
30+
d := &Device{
31+
rs: rs.Set,
32+
wr: wr.Set,
33+
cs: cs.Set,
34+
rst: rst.Set,
3335
bus: bus,
3436
}
3537

36-
rs.Configure(machine.PinConfig{Mode: machine.PinOutput})
37-
wr.Configure(machine.PinConfig{Mode: machine.PinOutput})
38-
cs.Configure(machine.PinConfig{Mode: machine.PinOutput})
39-
rst.Configure(machine.PinConfig{Mode: machine.PinOutput})
38+
// configure GPIO pins (only on baremetal targets, for backwards compatibility)
39+
legacy.ConfigurePinOut(rs)
40+
legacy.ConfigurePinOut(wr)
41+
legacy.ConfigurePinOut(cs)
42+
legacy.ConfigurePinOut(rst)
4043

41-
cs.High()
42-
rst.High()
43-
wr.High()
44+
d.cs.High()
45+
d.rst.High()
46+
d.wr.High()
4447

4548
return d
4649
}

ssd1306/ssd1306_spi.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
11
package ssd1306
22

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

76
"tinygo.org/x/drivers"
7+
"tinygo.org/x/drivers/internal/legacy"
8+
"tinygo.org/x/drivers/internal/pin"
89
)
910

1011
type SPIBus struct {
1112
wire drivers.SPI
12-
dcPin machine.Pin
13-
resetPin machine.Pin
14-
csPin machine.Pin
13+
dcPin pin.OutputFunc
14+
resetPin pin.OutputFunc
15+
csPin pin.OutputFunc
1516
buffer []byte // buffer to avoid heap allocations
1617
}
1718

1819
// NewSPI creates a new SSD1306 connection. The SPI wire must already be configured.
19-
func NewSPI(bus drivers.SPI, dcPin, resetPin, csPin machine.Pin) *Device {
20-
dcPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
21-
resetPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
22-
csPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
20+
func NewSPI(bus drivers.SPI, dcPin, resetPin, csPin pin.Output) *Device {
21+
// configure GPIO pins (on baremetal targets only, for backwards compatibility)
22+
legacy.ConfigurePinOut(dcPin)
23+
legacy.ConfigurePinOut(resetPin)
24+
legacy.ConfigurePinOut(csPin)
2325
return &Device{
2426
bus: &SPIBus{
2527
wire: bus,
26-
dcPin: dcPin,
27-
resetPin: resetPin,
28-
csPin: csPin,
28+
dcPin: dcPin.Set,
29+
resetPin: resetPin.Set,
30+
csPin: csPin.Set,
2931
},
3032
}
3133
}
@@ -60,7 +62,7 @@ func (b *SPIBus) flush() error {
6062
// tx sends data to the display
6163
func (b *SPIBus) tx(data []byte, isCommand bool) error {
6264
b.csPin.High()
63-
b.dcPin.Set(!isCommand)
65+
b.dcPin(!isCommand)
6466
b.csPin.Low()
6567
err := b.wire.Tx(data, nil)
6668
b.csPin.High()

ssd1331/ssd1331.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ package ssd1331 // import "tinygo.org/x/drivers/ssd1331"
55

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

109
"errors"
1110
"time"
1211

1312
"tinygo.org/x/drivers"
13+
"tinygo.org/x/drivers/internal/legacy"
14+
"tinygo.org/x/drivers/internal/pin"
1415
)
1516

1617
type Model uint8
@@ -19,9 +20,9 @@ type Rotation uint8
1920
// Device wraps an SPI connection.
2021
type Device struct {
2122
bus drivers.SPI
22-
dcPin machine.Pin
23-
resetPin machine.Pin
24-
csPin machine.Pin
23+
dcPin pin.OutputFunc
24+
resetPin pin.OutputFunc
25+
csPin pin.OutputFunc
2526
width int16
2627
height int16
2728
batchLength int16
@@ -36,15 +37,16 @@ type Config struct {
3637
}
3738

3839
// New creates a new SSD1331 connection. The SPI wire must already be configured.
39-
func New(bus drivers.SPI, resetPin, dcPin, csPin machine.Pin) Device {
40-
dcPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
41-
resetPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
42-
csPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
40+
func New(bus drivers.SPI, resetPin, dcPin, csPin pin.Output) Device {
41+
// configure GPIO pins (on baremetal targets only, for backwards compatibility)
42+
legacy.ConfigurePinOut(dcPin)
43+
legacy.ConfigurePinOut(resetPin)
44+
legacy.ConfigurePinOut(csPin)
4345
return Device{
4446
bus: bus,
45-
dcPin: dcPin,
46-
resetPin: resetPin,
47-
csPin: csPin,
47+
dcPin: dcPin.Set,
48+
resetPin: resetPin.Set,
49+
csPin: csPin.Set,
4850
}
4951
}
5052

@@ -251,7 +253,7 @@ func (d *Device) Data(data uint8) {
251253

252254
// Tx sends data to the display
253255
func (d *Device) Tx(data []byte, isCommand bool) {
254-
d.dcPin.Set(!isCommand)
256+
d.dcPin(!isCommand)
255257
d.bus.Tx(data, nil)
256258
}
257259

ssd1351/ssd1351.go

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ package ssd1351 // import "tinygo.org/x/drivers/ssd1351"
66
import (
77
"errors"
88
"image/color"
9-
"machine"
109
"time"
1110

1211
"tinygo.org/x/drivers"
12+
"tinygo.org/x/drivers/internal/legacy"
13+
"tinygo.org/x/drivers/internal/pin"
1314
)
1415

1516
var (
@@ -19,17 +20,18 @@ var (
1920

2021
// Device wraps an SPI connection.
2122
type Device struct {
22-
bus drivers.SPI
23-
dcPin machine.Pin
24-
resetPin machine.Pin
25-
csPin machine.Pin
26-
enPin machine.Pin
27-
rwPin machine.Pin
28-
width int16
29-
height int16
30-
rowOffset int16
31-
columnOffset int16
32-
bufferLength int16
23+
bus drivers.SPI
24+
dcPin pin.OutputFunc
25+
resetPin pin.OutputFunc
26+
csPin pin.OutputFunc
27+
enPin pin.OutputFunc
28+
rwPin pin.OutputFunc
29+
width int16
30+
height int16
31+
rowOffset int16
32+
columnOffset int16
33+
bufferLength int16
34+
configurePins func()
3335
}
3436

3537
// Config is the configuration for the display
@@ -41,14 +43,21 @@ type Config struct {
4143
}
4244

4345
// New creates a new SSD1351 connection. The SPI wire must already be configured.
44-
func New(bus drivers.SPI, resetPin, dcPin, csPin, enPin, rwPin machine.Pin) Device {
46+
func New(bus drivers.SPI, resetPin, dcPin, csPin, enPin, rwPin pin.Output) Device {
4547
return Device{
4648
bus: bus,
47-
dcPin: dcPin,
48-
resetPin: resetPin,
49-
csPin: csPin,
50-
enPin: enPin,
51-
rwPin: rwPin,
49+
dcPin: dcPin.Set,
50+
resetPin: resetPin.Set,
51+
csPin: csPin.Set,
52+
enPin: enPin.Set,
53+
rwPin: rwPin.Set,
54+
configurePins: func() {
55+
legacy.ConfigurePinOut(dcPin)
56+
legacy.ConfigurePinOut(resetPin)
57+
legacy.ConfigurePinOut(csPin)
58+
legacy.ConfigurePinOut(enPin)
59+
legacy.ConfigurePinOut(rwPin)
60+
},
5261
}
5362
}
5463

@@ -72,12 +81,8 @@ func (d *Device) Configure(cfg Config) {
7281
d.bufferLength = d.height
7382
}
7483

75-
// configure GPIO pins
76-
d.dcPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
77-
d.resetPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
78-
d.csPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
79-
d.enPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
80-
d.rwPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
84+
// configure GPIO pins (on baremetal targets only, for backwards compatibility)
85+
d.configurePins()
8186

8287
// reset the device
8388
d.resetPin.High()
@@ -278,7 +283,7 @@ func (d *Device) Data(data uint8) {
278283

279284
// Tx sends data to the display
280285
func (d *Device) Tx(data []byte, isCommand bool) {
281-
d.dcPin.Set(!isCommand)
286+
d.dcPin(!isCommand)
282287
d.csPin.Low()
283288
d.bus.Tx(data, nil)
284289
d.csPin.High()

0 commit comments

Comments
 (0)