Skip to content

Commit ac87a39

Browse files
committed
ssd1306: avoid unnecessary heap allocations
1 parent c4ff824 commit ac87a39

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

ssd1306/ssd1306.go

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"time"
1111

1212
"tinygo.org/x/drivers"
13-
"tinygo.org/x/drivers/internal/legacy"
1413
"tinygo.org/x/drivers/pixel"
1514
)
1615

@@ -128,7 +127,7 @@ func (d *Device) Configure(cfg Config) {
128127
d.resetPage = ResetValue{0, uint8(d.height/8) - 1}
129128
}
130129
d.bufferSize = d.width * d.height / 8
131-
d.buffer = make([]byte, d.bufferSize)
130+
d.buffer = make([]byte, d.bufferSize+1) // +1 for the I2C command byte
132131
d.canReset = cfg.Address != 0 || d.width != 128 || d.height != 64 // I2C or not 128x64
133132

134133
d.bus.configure()
@@ -268,7 +267,8 @@ func (d *Device) GetBuffer() []byte {
268267

269268
// Command sends a command to the display
270269
func (d *Device) Command(command uint8) {
271-
d.bus.tx([]byte{command}, true)
270+
d.buffer[1] = command // The second byte is the actual command
271+
d.bus.tx(d.buffer[0:2], true)
272272
}
273273

274274
// setAddress sets the address to the I2C bus
@@ -310,32 +310,24 @@ func (d *Device) Tx(data []byte, isCommand bool) error {
310310
// tx sends data to the display (I2CBus implementation)
311311
func (b *I2CBus) tx(data []byte, isCommand bool) error {
312312
if isCommand {
313-
return legacy.WriteRegister(b.wire, uint8(b.Address), 0x00, data)
313+
data[0] = 0x00 // Command mode
314314
} else {
315-
return legacy.WriteRegister(b.wire, uint8(b.Address), 0x40, data)
315+
data[0] = 0x40 // Data mode
316316
}
317+
return b.wire.Tx(uint16(b.Address), data, nil)
317318
}
318319

319320
// tx sends data to the display (SPIBus implementation)
320321
func (b *SPIBus) tx(data []byte, isCommand bool) error {
321-
var err error
322-
322+
b.csPin.High()
323323
if isCommand {
324-
b.csPin.High()
325324
b.dcPin.Low()
326-
b.csPin.Low()
327-
328-
err = b.wire.Tx(data, nil)
329-
b.csPin.High()
330325
} else {
331-
b.csPin.High()
332326
b.dcPin.High()
333-
b.csPin.Low()
334-
335-
err = b.wire.Tx(data, nil)
336-
b.csPin.High()
337327
}
338-
328+
b.csPin.Low()
329+
err := b.wire.Tx(data[1:], nil) // The first byte is reserved for I2C communcation, strip it
330+
b.csPin.High()
339331
return err
340332
}
341333

0 commit comments

Comments
 (0)