Skip to content

Commit 7f1bd68

Browse files
authored
ssd1306: avoid unnecessary heap allocations (#767)
* ssd1306: avoid unnecessary heap allocations * ssd1306: extract i2c and spi bus implementations * ssd1306: refactor tests -- show fps and heap usage * ssd1306: bring back the lost exported methods * Adjust examples * Fix smoketests for ssd1306
1 parent d02d21e commit 7f1bd68

File tree

12 files changed

+316
-348
lines changed

12 files changed

+316
-348
lines changed

examples/ssd1306/i2c_128x32/main.go

Lines changed: 0 additions & 51 deletions
This file was deleted.

examples/ssd1306/i2c_128x64/main.go

Lines changed: 0 additions & 60 deletions
This file was deleted.

examples/ssd1306/main.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package main
2+
3+
// This example shows how to use SSD1306 OLED display driver over I2C and SPI.
4+
//
5+
// Check the `newSSD1306Display()` functions for I2C and SPI initializations.
6+
7+
import (
8+
"runtime"
9+
10+
"image/color"
11+
"time"
12+
)
13+
14+
func main() {
15+
16+
display := newSSD1306Display()
17+
display.ClearDisplay()
18+
19+
w, h := display.Size()
20+
x := int16(0)
21+
y := int16(0)
22+
deltaX := int16(1)
23+
deltaY := int16(1)
24+
25+
traceTime := time.Now().UnixMilli() + 1000
26+
frames := 0
27+
ms := runtime.MemStats{}
28+
29+
for {
30+
pixel := display.GetPixel(x, y)
31+
c := color.RGBA{255, 255, 255, 255}
32+
if pixel {
33+
c = color.RGBA{0, 0, 0, 255}
34+
}
35+
display.SetPixel(x, y, c)
36+
display.Display()
37+
38+
x += deltaX
39+
y += deltaY
40+
41+
if x == 0 || x == w-1 {
42+
deltaX = -deltaX
43+
}
44+
45+
if y == 0 || y == h-1 {
46+
deltaY = -deltaY
47+
}
48+
49+
frames++
50+
now := time.Now().UnixMilli()
51+
if now >= traceTime {
52+
runtime.ReadMemStats(&ms)
53+
println("TS", now, "| FPS", frames, "| HeapInuse", ms.HeapInuse)
54+
traceTime = now + 1000
55+
frames = 0
56+
}
57+
}
58+
59+
}

examples/ssd1306/main_i2c_xiao-ble.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//go:build xiao_ble
2+
3+
// This initializes SSD1306 OLED display driver over I2C.
4+
//
5+
// Seeed XIAO BLE board + SSD1306 128x32 I2C OLED display.
6+
//
7+
// Wiring:
8+
// - XIAO GND -> OLED GND
9+
// - XIAO 3v3 -> OLED VCC
10+
// - XIAO D4 (SDA) -> OLED SDA
11+
// - XIAO D5 (SCL) -> OLED SCK
12+
//
13+
// For your case:
14+
// - Connect the display to I2C pins on your board.
15+
// - Adjust I2C address and display size as needed.
16+
17+
package main
18+
19+
import (
20+
"machine"
21+
22+
"tinygo.org/x/drivers/ssd1306"
23+
)
24+
25+
func newSSD1306Display() *ssd1306.Device {
26+
machine.I2C0.Configure(machine.I2CConfig{
27+
Frequency: 400 * machine.KHz,
28+
SDA: machine.SDA0_PIN,
29+
SCL: machine.SCL0_PIN,
30+
})
31+
display := ssd1306.NewI2C(machine.I2C0)
32+
display.Configure(ssd1306.Config{
33+
Address: ssd1306.Address_128_32, // or ssd1306.Address
34+
Width: 128,
35+
Height: 32, // or 64
36+
})
37+
return display
38+
}

examples/ssd1306/main_spi_thumby.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//go:build thumby
2+
3+
// This initializes SSD1306 OLED display driver over SPI.
4+
//
5+
// Thumby board has a tiny built-in 72x40 display.
6+
//
7+
// As the display is built-in, no wiring is needed.
8+
9+
package main
10+
11+
import (
12+
"machine"
13+
14+
"tinygo.org/x/drivers/ssd1306"
15+
)
16+
17+
func newSSD1306Display() *ssd1306.Device {
18+
machine.SPI0.Configure(machine.SPIConfig{})
19+
display := ssd1306.NewSPI(machine.SPI0, machine.THUMBY_DC_PIN, machine.THUMBY_RESET_PIN, machine.THUMBY_CS_PIN)
20+
display.Configure(ssd1306.Config{
21+
Width: 72,
22+
Height: 40,
23+
ResetCol: ssd1306.ResetValue{28, 99},
24+
ResetPage: ssd1306.ResetValue{0, 5},
25+
})
26+
return display
27+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//go:build xiao_rp2040
2+
3+
// This initializes SSD1306 OLED display driver over SPI.
4+
//
5+
// Seeed XIAO RP2040 board + SSD1306 128x64 SPI OLED display.
6+
//
7+
// Wiring:
8+
// - XIAO GND -> OLED GND
9+
// - XIAO 3v3 -> OLED VCC
10+
// - XIAO D8 (SCK) -> OLED D0
11+
// - XIAO D10 (SDO) -> OLED D1
12+
// - XIAO D4 -> OLED RES
13+
// - XIAO D5 -> OLED DC
14+
// - XIAO D6 -> OLED CS
15+
//
16+
// For your case:
17+
// - Connect the display to SPI pins on your board.
18+
// - Adjust RES, DC and CS pins as needed.
19+
// - Adjust SPI frequency as needed.
20+
// - Adjust display size as needed.
21+
22+
package main
23+
24+
import (
25+
"machine"
26+
27+
"tinygo.org/x/drivers/ssd1306"
28+
)
29+
30+
func newSSD1306Display() *ssd1306.Device {
31+
machine.SPI0.Configure(machine.SPIConfig{
32+
Frequency: 50 * machine.MHz,
33+
})
34+
display := ssd1306.NewSPI(machine.SPI0, machine.D5, machine.D4, machine.D6)
35+
display.Configure(ssd1306.Config{
36+
Width: 128,
37+
Height: 64,
38+
})
39+
return display
40+
}

examples/ssd1306/spi_128x64/main.go

Lines changed: 0 additions & 48 deletions
This file was deleted.

examples/ssd1306/spi_thumby/main.go

Lines changed: 0 additions & 50 deletions
This file was deleted.

smoketest.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ tinygo build -size short -o ./build/test.hex -target=pybadge ./examples/shifter/
6565
tinygo build -size short -o ./build/test.hex -target=microbit ./examples/sht3x/main.go
6666
tinygo build -size short -o ./build/test.hex -target=microbit ./examples/sht4x/main.go
6767
tinygo build -size short -o ./build/test.hex -target=microbit ./examples/shtc3/main.go
68-
tinygo build -size short -o ./build/test.hex -target=microbit ./examples/ssd1306/i2c_128x32/main.go
69-
tinygo build -size short -o ./build/test.hex -target=microbit ./examples/ssd1306/spi_128x64/main.go
68+
tinygo build -size short -o ./build/test.hex -target=xiao-ble ./examples/ssd1306/
69+
tinygo build -size short -o ./build/test.hex -target=xiao-rp2040 ./examples/ssd1306/
70+
tinygo build -size short -o ./build/test.hex -target=thumby ./examples/ssd1306/
7071
tinygo build -size short -o ./build/test.hex -target=microbit ./examples/ssd1331/main.go
7172
tinygo build -size short -o ./build/test.hex -target=microbit ./examples/st7735/main.go
7273
tinygo build -size short -o ./build/test.hex -target=microbit ./examples/st7789/main.go

0 commit comments

Comments
 (0)