Skip to content

Commit a7b77c0

Browse files
committed
simplify
1 parent d371232 commit a7b77c0

File tree

5 files changed

+69
-79
lines changed

5 files changed

+69
-79
lines changed

internal/pin/internalpin.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,3 @@ 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/ssd1289.go

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,36 @@ type Bus interface {
1616
}
1717

1818
type Device struct {
19-
rs pin.OutputStruct
20-
wr pin.OutputStruct
21-
cs pin.OutputStruct
22-
rst pin.OutputStruct
19+
rs pin.OutputFunc
20+
wr pin.OutputFunc
21+
cs pin.OutputFunc
22+
rst pin.OutputFunc
2323
bus Bus
2424
}
2525

2626
const width = int16(240)
2727
const height = int16(320)
2828

2929
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},
30+
d := &Device{
31+
rs: rs.Set,
32+
wr: wr.Set,
33+
cs: cs.Set,
34+
rst: rst.Set,
3535
bus: bus,
3636
}
37+
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)
43+
44+
d.cs.High()
45+
d.rst.High()
46+
d.wr.High()
47+
48+
return d
3749
}
3850

3951
func (d *Device) lcdWriteCom(cmd Command) {
@@ -62,16 +74,6 @@ func (d *Device) lcdWriteBusInt(data uint16) {
6274
}
6375

6476
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-
7577
d.rst.High()
7678
time.Sleep(time.Millisecond * 5)
7779
d.rst.Low()

ssd1306/ssd1306_spi.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,30 @@ import (
1010

1111
type SPIBus struct {
1212
wire drivers.SPI
13-
dcPin pin.OutputStruct
14-
resetPin pin.OutputStruct
15-
csPin pin.OutputStruct
13+
dcPin pin.OutputFunc
14+
resetPin pin.OutputFunc
15+
csPin pin.OutputFunc
1616
buffer []byte // buffer to avoid heap allocations
1717
}
1818

1919
// NewSPI creates a new SSD1306 connection. The SPI wire must already be configured.
2020
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)
2125
return &Device{
2226
bus: &SPIBus{
2327
wire: bus,
24-
dcPin: pin.OutputStruct{Output: dcPin},
25-
resetPin: pin.OutputStruct{Output: resetPin},
26-
csPin: pin.OutputStruct{Output: csPin},
28+
dcPin: dcPin.Set,
29+
resetPin: resetPin.Set,
30+
csPin: csPin.Set,
2731
},
2832
}
2933
}
3034

3135
// configure pins with the SPI bus and allocate the buffer
3236
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-
3937
b.csPin.Low()
4038
b.dcPin.Low()
4139
b.resetPin.Low()
@@ -64,7 +62,7 @@ func (b *SPIBus) flush() error {
6462
// tx sends data to the display
6563
func (b *SPIBus) tx(data []byte, isCommand bool) error {
6664
b.csPin.High()
67-
b.dcPin.Set(!isCommand)
65+
b.dcPin(!isCommand)
6866
b.csPin.Low()
6967
err := b.wire.Tx(data, nil)
7068
b.csPin.High()

ssd1331/ssd1331.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ type Rotation uint8
2020
// Device wraps an SPI connection.
2121
type Device struct {
2222
bus drivers.SPI
23-
dcPin pin.OutputStruct
24-
resetPin pin.OutputStruct
25-
csPin pin.OutputStruct
23+
dcPin pin.OutputFunc
24+
resetPin pin.OutputFunc
25+
csPin pin.OutputFunc
2626
width int16
2727
height int16
2828
batchLength int16
@@ -38,11 +38,15 @@ type Config struct {
3838

3939
// New creates a new SSD1331 connection. The SPI wire must already be configured.
4040
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)
4145
return Device{
4246
bus: bus,
43-
dcPin: pin.OutputStruct{Output: dcPin},
44-
resetPin: pin.OutputStruct{Output: resetPin},
45-
csPin: pin.OutputStruct{Output: csPin},
47+
dcPin: dcPin.Set,
48+
resetPin: resetPin.Set,
49+
csPin: csPin.Set,
4650
}
4751
}
4852

@@ -66,11 +70,6 @@ func (d *Device) Configure(cfg Config) {
6670
d.batchLength += d.batchLength & 1
6771
d.batchData = make([]uint8, d.batchLength*2)
6872

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-
7473
// reset the device
7574
d.resetPin.High()
7675
time.Sleep(100 * time.Millisecond)
@@ -254,7 +253,7 @@ func (d *Device) Data(data uint8) {
254253

255254
// Tx sends data to the display
256255
func (d *Device) Tx(data []byte, isCommand bool) {
257-
d.dcPin.Set(!isCommand)
256+
d.dcPin(!isCommand)
258257
d.bus.Tx(data, nil)
259258
}
260259

ssd1351/ssd1351.go

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,18 @@ var (
2020

2121
// Device wraps an SPI connection.
2222
type Device struct {
23-
bus drivers.SPI
24-
dcPin pin.OutputStruct
25-
resetPin pin.OutputStruct
26-
csPin pin.OutputStruct
27-
enPin pin.OutputStruct
28-
rwPin pin.OutputStruct
29-
width int16
30-
height int16
31-
rowOffset int16
32-
columnOffset int16
33-
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()
3435
}
3536

3637
// Config is the configuration for the display
@@ -45,11 +46,18 @@ type Config struct {
4546
func New(bus drivers.SPI, resetPin, dcPin, csPin, enPin, rwPin pin.Output) Device {
4647
return Device{
4748
bus: bus,
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},
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+
},
5361
}
5462
}
5563

@@ -74,11 +82,7 @@ func (d *Device) Configure(cfg Config) {
7482
}
7583

7684
// 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)
85+
d.configurePins()
8286

8387
// reset the device
8488
d.resetPin.High()
@@ -279,7 +283,7 @@ func (d *Device) Data(data uint8) {
279283

280284
// Tx sends data to the display
281285
func (d *Device) Tx(data []byte, isCommand bool) {
282-
d.dcPin.Set(!isCommand)
286+
d.dcPin(!isCommand)
283287
d.csPin.Low()
284288
d.bus.Tx(data, nil)
285289
d.csPin.High()

0 commit comments

Comments
 (0)