Skip to content

Commit fa17041

Browse files
committed
ssd1xxx: break dependency from machine package
1 parent 09fd013 commit fa17041

File tree

6 files changed

+95
-82
lines changed

6 files changed

+95
-82
lines changed

internal/pin/internalpin.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,16 @@ type Output interface {
7070
type Input interface {
7171
Get() (level bool)
7272
}
73+
74+
// OutputStruct implements optional convenience methods
75+
type OutputStruct struct {
76+
Output
77+
}
78+
79+
func (p OutputStruct) High() {
80+
p.Set(true)
81+
}
82+
83+
func (p OutputStruct) Low() {
84+
p.Set(false)
85+
}

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: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,35 @@ 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.OutputStruct
20+
wr pin.OutputStruct
21+
cs pin.OutputStruct
22+
rst pin.OutputStruct
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+
return &Device{
31+
rs: pin.OutputStruct{Output: rs},
32+
wr: pin.OutputStruct{Output: wr},
33+
cs: pin.OutputStruct{Output: cs},
34+
rst: pin.OutputStruct{Output: rst},
3335
bus: bus,
3436
}
35-
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})
40-
41-
cs.High()
42-
rst.High()
43-
wr.High()
44-
45-
return d
4637
}
4738

4839
func (d *Device) lcdWriteCom(cmd Command) {
@@ -71,6 +62,16 @@ func (d *Device) lcdWriteBusInt(data uint16) {
7162
}
7263

7364
func (d *Device) Configure() {
65+
66+
// configure GPIO pins (only on baremetal targets)
67+
legacy.ConfigurePinOut(d.rs)
68+
legacy.ConfigurePinOut(d.wr)
69+
legacy.ConfigurePinOut(d.cs)
70+
legacy.ConfigurePinOut(d.rst)
71+
72+
d.cs.High()
73+
d.wr.High()
74+
7475
d.rst.High()
7576
time.Sleep(time.Millisecond * 5)
7677
d.rst.Low()

ssd1306/ssd1306_spi.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,41 @@
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.OutputStruct
14+
resetPin pin.OutputStruct
15+
csPin pin.OutputStruct
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 {
2321
return &Device{
2422
bus: &SPIBus{
2523
wire: bus,
26-
dcPin: dcPin,
27-
resetPin: resetPin,
28-
csPin: csPin,
24+
dcPin: pin.OutputStruct{Output: dcPin},
25+
resetPin: pin.OutputStruct{Output: resetPin},
26+
csPin: pin.OutputStruct{Output: csPin},
2927
},
3028
}
3129
}
3230

3331
// configure pins with the SPI bus and allocate the buffer
3432
func (b *SPIBus) configure(address uint16, size int16) []byte {
33+
34+
// configure GPIO pins (on baremetal targets only, for backwards compatibility)
35+
legacy.ConfigurePinOut(b.dcPin)
36+
legacy.ConfigurePinOut(b.resetPin)
37+
legacy.ConfigurePinOut(b.csPin)
38+
3539
b.csPin.Low()
3640
b.dcPin.Low()
3741
b.resetPin.Low()

ssd1331/ssd1331.go

Lines changed: 14 additions & 11 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.OutputStruct
24+
resetPin pin.OutputStruct
25+
csPin pin.OutputStruct
2526
width int16
2627
height int16
2728
batchLength int16
@@ -36,15 +37,12 @@ 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 {
4341
return Device{
4442
bus: bus,
45-
dcPin: dcPin,
46-
resetPin: resetPin,
47-
csPin: csPin,
43+
dcPin: pin.OutputStruct{Output: dcPin},
44+
resetPin: pin.OutputStruct{Output: resetPin},
45+
csPin: pin.OutputStruct{Output: csPin},
4846
}
4947
}
5048

@@ -68,6 +66,11 @@ func (d *Device) Configure(cfg Config) {
6866
d.batchLength += d.batchLength & 1
6967
d.batchData = make([]uint8, d.batchLength*2)
7068

69+
// configure GPIO pins (on baremetal targets only, for backwards compatibility)
70+
legacy.ConfigurePinOut(d.dcPin)
71+
legacy.ConfigurePinOut(d.resetPin)
72+
legacy.ConfigurePinOut(d.csPin)
73+
7174
// reset the device
7275
d.resetPin.High()
7376
time.Sleep(100 * time.Millisecond)

ssd1351/ssd1351.go

Lines changed: 19 additions & 18 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 (
@@ -20,11 +21,11 @@ var (
2021
// Device wraps an SPI connection.
2122
type Device struct {
2223
bus drivers.SPI
23-
dcPin machine.Pin
24-
resetPin machine.Pin
25-
csPin machine.Pin
26-
enPin machine.Pin
27-
rwPin machine.Pin
24+
dcPin pin.OutputStruct
25+
resetPin pin.OutputStruct
26+
csPin pin.OutputStruct
27+
enPin pin.OutputStruct
28+
rwPin pin.OutputStruct
2829
width int16
2930
height int16
3031
rowOffset int16
@@ -41,14 +42,14 @@ type Config struct {
4142
}
4243

4344
// 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 {
45+
func New(bus drivers.SPI, resetPin, dcPin, csPin, enPin, rwPin pin.Output) Device {
4546
return Device{
4647
bus: bus,
47-
dcPin: dcPin,
48-
resetPin: resetPin,
49-
csPin: csPin,
50-
enPin: enPin,
51-
rwPin: rwPin,
48+
dcPin: pin.OutputStruct{Output: dcPin},
49+
resetPin: pin.OutputStruct{Output: resetPin},
50+
csPin: pin.OutputStruct{Output: csPin},
51+
enPin: pin.OutputStruct{Output: enPin},
52+
rwPin: pin.OutputStruct{Output: rwPin},
5253
}
5354
}
5455

@@ -72,12 +73,12 @@ func (d *Device) Configure(cfg Config) {
7273
d.bufferLength = d.height
7374
}
7475

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})
76+
// configure GPIO pins (on baremetal targets only, for backwards compatibility)
77+
legacy.ConfigurePinOut(d.dcPin)
78+
legacy.ConfigurePinOut(d.resetPin)
79+
legacy.ConfigurePinOut(d.csPin)
80+
legacy.ConfigurePinOut(d.enPin)
81+
legacy.ConfigurePinOut(d.rwPin)
8182

8283
// reset the device
8384
d.resetPin.High()

0 commit comments

Comments
 (0)