-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreen.py
83 lines (66 loc) · 2.59 KB
/
screen.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
from luma.core.interface.serial import i2c
from luma.oled.device import sh1106
from PIL import ImageFont
from os import path
# Assumes a 128x64 display using the SH1106 driver
class dotdict(dict):
"""dot.notation access to dictionary attributes"""
__getattr__ = dict.get
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__
# Get this script's location
pwd = path.dirname(path.realpath(__file__))
bold = ImageFont.truetype(pwd + "/fonts/pixeloperator-sc-bold.ttf", 15)
# large = ImageFont.truetype(pwd + "/fonts/pixeloperator.ttf", 15)
medium = ImageFont.truetype(pwd + "/fonts/pixelmix.ttf", 8)
small = ImageFont.truetype(pwd + "/fonts/pixelade.ttf", 12)
# Internal module helper functions
def _bar(draw, pos):
draw.rectangle(pos, outline="white", fill="white")
def _frame(draw, pos):
draw.rectangle(pos, outline="white", fill="black")
def _inset(pos, px):
x, y, xmax, ymax = pos
return (x + px, y + px, xmax - px, ymax - px)
def _text(draw, font):
def partial(pos, msg, color="white"):
draw.text(pos, msg, fill=color, font=font)
return partial
# Public functions
def bar_gauge(draw, pos, perc = 0):
(x, y, xmax, ymax) = _inset(pos, 2)
max = xmax - x
width = round(perc / 100 * max)
_frame(draw, pos)
_bar(draw, (x, y, x + width, ymax))
def get_device(sda_addr, i2c_port):
serial = i2c(address=sda_addr, port=i2c_port)
return sh1106(serial)
def get_gauge(draw, text):
def gauge(pos, label, percent):
(x, y) = pos
bar_gauge(draw, (x + 21, y, x + 96, y + 8), percent)
text.small((x, y - 2), label)
text.small((x + 99, y - 2), str(round(percent, 1)) + "%")
return gauge
def get_text(draw):
return dotdict({
"bold": _text(draw, bold),
# "large": _textwhite(draw, large),
"small": _text(draw, small),
"write": _text(draw, medium)
})
def updates_notice(draw, text):
def backfill_box(x, y, w, h):
# rounded corners
dia = 5
draw.ellipse((x, y, x + dia, y + dia), fill="white") # top left corner
draw.ellipse((x, y + dia, x + dia, y + h), fill="white") # bot left corner
draw.ellipse((x + w - dia, y, x + w, y + dia), fill="white") # top right corner
draw.ellipse((x + w - dia, y + h - dia, x + w, y + h), fill="white") # bot right corner
# backfill
inset = round(dia / 2)
draw.rectangle((x + inset, y, x + w - inset, y + h), fill="white")
draw.rectangle((x, y + inset, x + w, y + h - inset), fill="white")
backfill_box(100, 17, 25, 10)
text.write((104, 18), "UPD!", "black")