-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssd1306clock.py
60 lines (51 loc) · 1.88 KB
/
ssd1306clock.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
# 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 ssd_text(string, font, char_height, offset=1):
oled.fill(0)
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)
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
rtc_obj = RTC()
last_seconds = -1
wait = 100
try:
while True:
# rtc = (year, month, day, weekday, hours, minutes, seconds, subseconds)
rtc = rtc_obj.datetime()
seconds = rtc[6]
if last_seconds != seconds:
mytime = f"{rtc[4]:02d}" + ":" + f"{rtc[5]:02d}" + ":" + f"{seconds:02d}"
ssd_text(mytime, ssd_font, fontsize, 30)
last_seconds = seconds
sleep_ms(wait)
except KeyboardInterrupt:
ssd_text("", ssd_font, fontsize, 30)