Skip to content

Commit c344e5d

Browse files
deadprogrambgould
authored andcommitted
ssd1306: improvements needed for Thumby SPI display
Signed-off-by: deadprogram <[email protected]>
1 parent 308763f commit c344e5d

File tree

2 files changed

+76
-4
lines changed

2 files changed

+76
-4
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// This example using the SSD1306 OLED display over SPI on the Thumby board
2+
// A very tiny 72x40 display.
3+
package main
4+
5+
import (
6+
"image/color"
7+
"machine"
8+
"time"
9+
10+
"tinygo.org/x/drivers/ssd1306"
11+
)
12+
13+
func main() {
14+
machine.SPI0.Configure(machine.SPIConfig{})
15+
display := ssd1306.NewSPI(machine.SPI0, machine.THUMBY_DC_PIN, machine.THUMBY_RESET_PIN, machine.THUMBY_CS_PIN)
16+
display.Configure(ssd1306.Config{
17+
Width: 72,
18+
Height: 40,
19+
ResetCol: ssd1306.ResetValue{28, 99},
20+
ResetPage: ssd1306.ResetValue{0, 5},
21+
})
22+
23+
display.ClearDisplay()
24+
25+
x := int16(36)
26+
y := int16(20)
27+
deltaX := int16(1)
28+
deltaY := int16(1)
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 == 71 {
42+
deltaX = -deltaX
43+
}
44+
45+
if y == 0 || y == 39 {
46+
deltaY = -deltaY
47+
}
48+
time.Sleep(1 * time.Millisecond)
49+
}
50+
}

ssd1306/ssd1306.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"tinygo.org/x/drivers/internal/legacy"
1414
)
1515

16+
type ResetValue [2]byte
17+
1618
// Device wraps I2C or SPI connection.
1719
type Device struct {
1820
bus Buser
@@ -22,6 +24,8 @@ type Device struct {
2224
bufferSize int16
2325
vccState VccMode
2426
canReset bool
27+
resetCol ResetValue
28+
resetPage ResetValue
2529
}
2630

2731
// Config is the configuration for the display
@@ -30,6 +34,13 @@ type Config struct {
3034
Height int16
3135
VccState VccMode
3236
Address uint16
37+
// ResetCol and ResetPage are used to reset the screen to 0x0
38+
// This is useful for some screens that have a different size than 128x64
39+
// For example, the Thumby's screen is 72x40
40+
// The default values are normally set automatically based on the size.
41+
// If you're using a different size, you might need to set these values manually.
42+
ResetCol ResetValue
43+
ResetPage ResetValue
3344
}
3445

3546
type I2CBus struct {
@@ -79,6 +90,7 @@ func NewSPI(bus drivers.SPI, dcPin, resetPin, csPin machine.Pin) Device {
7990

8091
// Configure initializes the display with default configuration
8192
func (d *Device) Configure(cfg Config) {
93+
var zeroReset ResetValue
8294
if cfg.Width != 0 {
8395
d.width = cfg.Width
8496
} else {
@@ -97,6 +109,16 @@ func (d *Device) Configure(cfg Config) {
97109
} else {
98110
d.vccState = SWITCHCAPVCC
99111
}
112+
if cfg.ResetCol != zeroReset {
113+
d.resetCol = cfg.ResetCol
114+
} else {
115+
d.resetCol = ResetValue{0, uint8(d.width - 1)}
116+
}
117+
if cfg.ResetPage != zeroReset {
118+
d.resetPage = cfg.ResetPage
119+
} else {
120+
d.resetPage = ResetValue{0, uint8(d.height/8) - 1}
121+
}
100122
d.bufferSize = d.width * d.height / 8
101123
d.buffer = make([]byte, d.bufferSize)
102124
d.canReset = cfg.Address != 0 || d.width != 128 || d.height != 64 // I2C or not 128x64
@@ -186,11 +208,11 @@ func (d *Device) Display() error {
186208
// Since we're printing the whole buffer, avoid resetting it in this case
187209
if d.canReset {
188210
d.Command(COLUMNADDR)
189-
d.Command(0)
190-
d.Command(uint8(d.width - 1))
211+
d.Command(d.resetCol[0])
212+
d.Command(d.resetCol[1])
191213
d.Command(PAGEADDR)
192-
d.Command(0)
193-
d.Command(uint8(d.height/8) - 1)
214+
d.Command(d.resetPage[0])
215+
d.Command(d.resetPage[1])
194216
}
195217

196218
return d.Tx(d.buffer, false)

0 commit comments

Comments
 (0)