-
Notifications
You must be signed in to change notification settings - Fork 0
/
epsimplelib.py
158 lines (124 loc) · 3.63 KB
/
epsimplelib.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
"""
ePaperLibrary for Waveshare e-Paper 2.7" Raspberry HAT
Github: https://github.com/lyoko17220/ePaperLibrary
"""
from PIL import Image, ImageFont, ImageDraw, ImageChops
import waveshare_library.epd2in7
# Default font
# FONT_PATH = '/usr/share/fonts/truetype/wqy/wqy-microhei.ttc'
FONT_PATH = '/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-R.ttf'
FONT_SMALL_MAX = ImageFont.truetype(FONT_PATH, 12)
FONT_SMALL = ImageFont.truetype(FONT_PATH, 14)
FONT_NORMAL = ImageFont.truetype(FONT_PATH, 18)
FONT_BIG = ImageFont.truetype(FONT_PATH, 22)
# Colors
BLACK = 0
WHITE = 255
# Size (Portrait mode)
DEVICE_WIDTH = 176
DEVICE_HEIGHT = 264
class EPScreen():
"""
Manager of the screen
"""
def __init__(self, screen_orientation):
self.device = waveshare_library.epd2in7.EPD()
self.device.init()
self.width = None
self.height = None
self.image_live = None
self.image_old = None
self.draw = None
if screen_orientation in ('landscape', 'portrait'):
self.screen_orientation = screen_orientation
self.init_image()
else:
raise Exception('Screen orientation not recognized')
def init_image(self):
"""
Set width and height according to screen orientation and generate empty Image and draw
"""
if self.screen_orientation == 'portrait':
self.width = DEVICE_WIDTH
self.height = DEVICE_HEIGHT
elif self.screen_orientation == 'landscape':
self.width = DEVICE_HEIGHT
self.height = DEVICE_WIDTH
self.image_live = Image.new('1', (self.width, self.height), 255)
self.draw = ImageDraw.Draw(self.image_live)
def update_screen(self):
"""
Send the new image to HAT
:return: Screen reloaded or not
"""
if self.image_old is None or self.need_to_refresh():
if self.screen_orientation == 'landscape':
self.device.display_frame(self.device.get_frame_buffer(self.image_live.rotate(90, expand=1)))
else:
self.device.display_frame(self.device.get_frame_buffer(self.image_live))
self.image_old = self.image_live
self.init_image()
return True
def need_to_refresh(self):
"""
Return if difference between previous image and the newest
:return False or True
"""
return ImageChops.difference(self.image_live, self.image_old).getbbox() is None
def get_width(self):
"""
Return width
:return: Width
"""
return self.width
def get_height(self):
"""
Return height
:return: Height
"""
return self.height
## Drawing functions
def add_text_middle(self, y, text, font=FONT_BIG, fill=0, width=-1, offset_x=0):
"""
Add text to middle of block
:param y: y in screen
:param text: text to add
:param font: font to use
:param fill: fill of text
:param width: width of block
:param offset_x: offset in x of the block
:return:
"""
if width == -1:
width = self.get_width()
h_origin = font.getsize('a')[1]
w, h = font.getsize(text)
# Vertical offset for symbol fonts
offset_y = 0
if h - h_origin > 5:
offset_y = h - h_origin
self.draw.text((offset_x + (width) / 2 - w / 2, y - offset_y), text, font=font, fill=fill)
def set_title(self, text):
"""
Add title to screen
:param text:
:return:
"""
self.draw.rectangle((0, 0, self.width, 28), fill=BLACK)
self.add_text_middle(5, text, FONT_NORMAL, WHITE)
def add_text(self, pos, text, font=FONT_NORMAL):
"""
Add simple text
:param pos: (x,y)
:param text: Text to add
:param font: Font to use
:return:
"""
self.draw.text(pos, text, font=font, fill=BLACK)
def add_line(self, pos):
"""
Add line
:param pos: Position xy start/end point
:return:
"""
self.draw.line((pos[0], pos[1], pos[2], pos[3]), fill=BLACK)