-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssd1306main.py
132 lines (109 loc) · 3.28 KB
/
ssd1306main.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Original source/inspiration:
# https://techatronic.com/ssd1306-raspberry-pi-pico/
# Frame buffer stuff:
# https://docs.micropython.org/en/latest/library/framebuf.html
PICO_FOLDER = "/ssd1306"
import sys
sys.path.append(PICO_FOLDER)
#print(sys.path)
from machine import Pin, SPI, RTC
from ssd1306driver import SSD1306_SPI
from ssd1306font import Display_font
import framebuf
from utime import sleep_ms
def show_image(filename):
oled.fill(0)
oled.show()
oled.text(filename, 0, 0)
oled.show()
sleep_ms(1000)
def hlines(c):
for row in range(1,63,2): # shows odd numbered lines
oled.hline(0, row, 128, c)
oled.show()
sleep_ms(50)
sleep_ms(1000)
def vlines(c):
for col in range(128):
oled.vline(col, 0, 64, c)
oled.show()
sleep_ms(20)
sleep_ms(1000)
def grow_square(c):
oled.hline(16, 33, 96, c)
oled.show()
sleep_ms(50)
for i in range(1,16):
oled.rect(16-i, 33-i*2, 96+i*2, i*4+1, c)
oled.show()
sleep_ms(50)
sleep_ms(1000)
def ssd_text(string, font, char_height, offset=1):
oled.fill(0)
# oled.show()
for c in string:
try:
fontchar = font[c] # get char layout
except KeyError:
fontchar = font[":"] # default to ":" if not found.
char_width = len(fontchar)
for i, line in enumerate(fontchar):
ssd_x = i + offset
for j, char_pixel in enumerate(line):
ssd_y = (char_height - j) * 2 + 1
if char_pixel != " ":
oled.pixel(ssd_x, ssd_y, 1)
# oled.show()
offset += char_width + 2
oled.show()
#-------------------------------------------------------------------------------------------
spi = SPI(0, 100000, mosi=Pin(19), sck=Pin(18))
# (width, height, spi, dc, res, cs, external_vcc=False):
oled = SSD1306_SPI(128, 64, spi, Pin(17), Pin(20), Pin(16))
font = Display_font(PICO_FOLDER + "/" + "font28.txt")
fontsize = font.font_height
ssd_font = font.font
while True:
try:
rtc_obj = RTC()
# rtc = (year, month, day, weekday, hours, minutes, seconds, subseconds)
for _ in range(5):
# while True`:
rtc = rtc_obj.datetime()
mytime = f"{rtc[4]:02d}" + ":" + f"{rtc[5]:02d}" + ":" + f"{rtc[6]:02d}"
ssd_text(mytime, ssd_font, fontsize, 30)
sleep_ms(1000)
for i in range(11):
ssd_text(str(i), ssd_font, fontsize, 60)
sleep_ms(500)
for offset in range(1,51,10):
oled.fill(0)
# oled.show()
oled.text("HELLO WORLD",offset,offset)
oled.show()
sleep_ms(250)
oled.fill(0)
oled.rect(0,1,128,63,1)
oled.show()
sleep_ms(1000)
oled.hline(0,33,128,1)
oled.vline(63,0,63,1)
oled.show()
sleep_ms(1000)
oled.fill(0)
oled.show()
hlines(1)
hlines(0)
oled.fill(0)
oled.show()
vlines(1)
vlines(0)
oled.fill(0)
oled.show()
grow_square(1)
grow_square(0)
#
# Read/show image
# show_image("frog.bmp")
except KeyboardInterrupt:
break