Skip to content

Commit 02f4996

Browse files
committed
Adjust examples
1 parent 26f9403 commit 02f4996

File tree

9 files changed

+107
-95
lines changed

9 files changed

+107
-95
lines changed

examples/ssd1306/i2c_128x32/main.go

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

examples/ssd1306/i2c_128x64/main.go

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

examples/ssd1306/common/common.go renamed to examples/ssd1306/main.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
1-
package common
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.
26

37
import (
48
"runtime"
59

610
"image/color"
711
"time"
8-
9-
"tinygo.org/x/drivers/ssd1306"
1012
)
1113

12-
var ms = runtime.MemStats{}
14+
func main() {
1315

14-
func Loop(display ssd1306.Device) {
16+
display := newSSD1306Display()
1517
display.ClearDisplay()
18+
1619
w, h := display.Size()
1720
x := int16(0)
1821
y := int16(0)
1922
deltaX := int16(1)
2023
deltaY := int16(1)
21-
trace := time.Now().UnixMilli() + 1000
24+
25+
traceTime := time.Now().UnixMilli() + 1000
2226
frames := 0
27+
ms := runtime.MemStats{}
28+
2329
for {
2430
pixel := display.GetPixel(x, y)
2531
c := color.RGBA{255, 255, 255, 255}
@@ -42,11 +48,12 @@ func Loop(display ssd1306.Device) {
4248

4349
frames++
4450
now := time.Now().UnixMilli()
45-
if now >= trace {
51+
if now >= traceTime {
4652
runtime.ReadMemStats(&ms)
4753
println("TS", now, "| FPS", frames, "| HeapInuse", ms.HeapInuse)
48-
trace = now + 1000
54+
traceTime = now + 1000
4955
frames = 0
5056
}
5157
}
58+
5259
}
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+
}
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
// This example using the SSD1306 OLED display over SPI on the Thumby board
2-
// A very tiny 72x40 display.
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+
39
package main
410

511
import (
612
"machine"
713

8-
"tinygo.org/x/drivers/examples/ssd1306/common"
914
"tinygo.org/x/drivers/ssd1306"
1015
)
1116

12-
func main() {
17+
func newSSD1306Display() *ssd1306.Device {
1318
machine.SPI0.Configure(machine.SPIConfig{})
1419
display := ssd1306.NewSPI(machine.SPI0, machine.THUMBY_DC_PIN, machine.THUMBY_RESET_PIN, machine.THUMBY_CS_PIN)
1520
display.Configure(ssd1306.Config{
@@ -18,6 +23,5 @@ func main() {
1823
ResetCol: ssd1306.ResetValue{28, 99},
1924
ResetPage: ssd1306.ResetValue{0, 5},
2025
})
21-
22-
common.Loop(display)
26+
return display
2327
}
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 & 21 deletions
This file was deleted.

ssd1306/ssd1306_i2c.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ type I2CBus struct {
1111
}
1212

1313
// NewI2C creates a new SSD1306 connection. The I2C wire must already be configured.
14-
func NewI2C(bus drivers.I2C) Device {
15-
return Device{
14+
func NewI2C(bus drivers.I2C) *Device {
15+
return &Device{
1616
bus: &I2CBus{
1717
wire: bus,
1818
address: Address,

ssd1306/ssd1306_spi.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ type SPIBus struct {
1616
}
1717

1818
// 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 {
19+
func NewSPI(bus drivers.SPI, dcPin, resetPin, csPin machine.Pin) *Device {
2020
dcPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
2121
resetPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
2222
csPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
23-
return Device{
23+
return &Device{
2424
bus: &SPIBus{
2525
wire: bus,
2626
dcPin: dcPin,

0 commit comments

Comments
 (0)