-
Notifications
You must be signed in to change notification settings - Fork 1
/
spi_oled_sh1106_basics.py
101 lines (77 loc) · 2.27 KB
/
spi_oled_sh1106_basics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from micropython import const
from machine import Pin, SPI
from lib.sh1106 import SH1106_SPI
from urandom import getrandbits
from framebuf import FrameBuffer, MONO_HLSB
from utime import sleep_ms
SDA_PIN = const(23)
SCL_PIN = const(18)
DC_PIN = const(2)
RES_PIN = const(4)
CS_PIN = const(5)
DP_WIDTH = const(128)
DP_HEIGHT = const(64)
def matrix() -> None:
"""
create matrix effect
:return: None
"""
global display
for px_i in range(DP_WIDTH):
for px_j in range(DP_HEIGHT):
display.pixel(px_i, px_j, getrandbits(1))
display.show()
def scroll_text(text: str) -> None:
"""
scroll string from left to right
:param text: message to display
:return: None
"""
global display
for item in range(len(text) * 6):
display.fill(0)
display.text(text, -item, 30)
display.show()
sleep_ms(5)
def load_image(filename: str, w: int, h: int):
"""
load pbm images
:param filename: path/filename of pbm
:param w: width of image
:param h: height of image
:return: FrameBuffer object
"""
with open(filename, 'rb') as f:
f.readline()
f.readline()
f.readline()
data = bytearray(f.read())
return FrameBuffer(data, w, h, MONO_HLSB)
if __name__ == '__main__':
spi = SPI(1, baudrate=1000000, sck=Pin(SCL_PIN), mosi=Pin(SDA_PIN))
display = SH1106_SPI(width=DP_WIDTH, height=DP_HEIGHT,
spi=spi, dc=Pin(DC_PIN), res=Pin(RES_PIN), cs=Pin(CS_PIN), rotate=0, delay=0)
img_armchair = load_image('armchair.pbm', 128, 64)
img_morpheus = load_image('morpheus.pbm', 128, 64)
img_pill = load_image('pill.pbm', 128, 64)
display.fill(0)
while True:
display.invert(0)
scroll_text(" Wake up, Neo... ")
for i in range(0, 5):
matrix()
sleep_ms(5)
display.invert(1)
display.blit(img_armchair, 0, 0)
display.show()
sleep_ms(500)
for i in range(0, 2):
matrix()
sleep_ms(5)
display.blit(img_morpheus, 0, 0)
display.show()
sleep_ms(1000)
scroll_text(" Decide, blue or red... ")
display.blit(img_pill, 0, 0)
display.show()
sleep_ms(1500)