Skip to content

Commit bd2530a

Browse files
ysoldakdeadprogram
authored andcommitted
Example of ssd1306 with 128x64 display over I2C
1 parent 3bcde14 commit bd2530a

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// This example shows how to use 128x64 display over I2C
2+
// Tested on Seeeduino XIAO Expansion Board https://wiki.seeedstudio.com/Seeeduino-XIAO-Expansion-Board/
3+
//
4+
// According to manual, I2C address of the display is 0x78, but that's 8-bit address.
5+
// TinyGo operates on 7-bit addresses and respective 7-bit address would be 0x3C, which we use below.
6+
//
7+
// To learn more about different types of I2C addresses, please see following page
8+
// https://www.totalphase.com/support/articles/200349176-7-bit-8-bit-and-10-bit-I2C-Slave-Addressing
9+
10+
package main
11+
12+
import (
13+
"machine"
14+
15+
"image/color"
16+
"time"
17+
18+
"tinygo.org/x/drivers/ssd1306"
19+
)
20+
21+
func main() {
22+
machine.I2C0.Configure(machine.I2CConfig{
23+
Frequency: machine.TWI_FREQ_400KHZ,
24+
})
25+
26+
display := ssd1306.NewI2C(machine.I2C0)
27+
display.Configure(ssd1306.Config{
28+
Address: 0x3C,
29+
Width: 128,
30+
Height: 64,
31+
})
32+
33+
display.ClearDisplay()
34+
35+
x := int16(0)
36+
y := int16(0)
37+
deltaX := int16(1)
38+
deltaY := int16(1)
39+
for {
40+
pixel := display.GetPixel(x, y)
41+
c := color.RGBA{255, 255, 255, 255}
42+
if pixel {
43+
c = color.RGBA{0, 0, 0, 255}
44+
}
45+
display.SetPixel(x, y, c)
46+
display.Display()
47+
48+
x += deltaX
49+
y += deltaY
50+
51+
if x == 0 || x == 127 {
52+
deltaX = -deltaX
53+
}
54+
55+
if y == 0 || y == 63 {
56+
deltaY = -deltaY
57+
}
58+
time.Sleep(1 * time.Millisecond)
59+
}
60+
}

0 commit comments

Comments
 (0)