-
Notifications
You must be signed in to change notification settings - Fork 3
/
display_buffer.py
191 lines (167 loc) · 5.57 KB
/
display_buffer.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
"""2.9 inch E-Paper display buffer."""
import hershey
class Buffer:
"""2.9 inch E-Paper display buffer."""
WHITE = 255
BLACK = 0
PEN_THIN = 0
PEN_MEDIUM = 1
PEN_THICK = 2
def __init__(self, width, height):
"""Construct buffer with width and height of Waveshare display."""
self._w = width
self._h = height
self._buffer = bytearray(width * height // 8)
def background(self, colour):
"""Set background colour of the display."""
for i in range(0, 16 * self._h):
self._buffer[i] = colour
def plot(self, x, y, colour):
"""Plot point with the given colour."""
tx = y
ty = x
if (x < 0) or (x >= self._h) or (y < 0) or (y >= self._w):
return
if colour == self.WHITE:
self._buffer[int((tx+ty*self._w)/8)] |= (0x80 >> (tx % 8))
elif colour == self.BLACK:
self._buffer[int((tx+ty*self._w)/8)] &= ~(0x80 >> (tx % 8))
def line(self, x, y, x2, y2, colour, weight):
"""Draw line with the given colour and weight."""
if x > x2:
dx = x - x2
else:
dx = x2 - x
if y > y2:
dy = y - y2
else:
dy = y2 - y
if x < x2:
sx = 1
else:
sx = -1
if y < y2:
sy = 1
else:
sy = -1
err = dx - dy
while True:
self.plot(x, y, colour)
if weight == self.PEN_MEDIUM:
if self._h-1 >= x+1:
self.plot(x+1, y, colour)
if 0 <= x-1:
self.plot(x-1, y, colour)
if self._w-1 >= y+1:
self.plot(x, y+1, colour)
if 0 <= y-1:
self.plot(x, y-1, colour)
elif weight == self.PEN_THICK:
self.blob(x, y, colour)
if (x == x2) and (y == y2):
break
e2 = 2*err
if e2 > -dy:
err = err-dy
x = x+sx
if (x == x2) and (y == y2):
self.plot(x, y, colour)
if weight == self.PEN_MEDIUM:
if self._h-1 >= x+1:
self.plot(x+1, y, colour)
if 0 <= x-1:
self.plot(x-1, y, colour)
if self._w-1 >= y+1:
self.plot(x, y+1, colour)
if 0 <= y-1:
self.plot(x, y-1, colour)
elif weight == self.PEN_THICK:
self.blob(x, y, colour)
break
if e2 < dx:
err = err+dx
y = y+sy
def blob(self, x, y, colour):
"""Draw blob with the given colour."""
self.line(x-1, y+2, x+1, y+2, colour, self.PEN_THIN)
self.line(x-2, y+1, x+2, y+1, colour, self.PEN_THIN)
self.line(x-2, y, x+2, y, colour, self.PEN_THIN)
self.line(x-2, y-1, x+2, y-1, colour, self.PEN_THIN)
self.line(x-1, y-2, x+1, y-2, colour, self.PEN_THIN)
def write_text(self, text, x, y, colour, xscale, yscale, dy, weight):
"""Write text with the given colour and size scaling."""
old = False
do_scale = False
not_finished = True
ci = 0
mdy = 0
ox = 0
oy = 0
wt = 0
if (xscale > 1.01) or (xscale < 1.01) or \
(yscale > 1.01) or (yscale < 1.01):
do_scale = True
xx = x
while not_finished:
if ci == len(text):
break
c = ord(text[ci])
ci += 1
if (c > 126) or (c < 32):
continue
c = c-32
nv = hershey.simplex[c][0]
w = hershey.simplex[c][1]
if w < 0:
w = 0-w
if nv == 0:
xx = xx+w
wt = wt+w
continue
im = 2+(nv*2)
old = False
for i in range(2, im, 2):
ipx = hershey.simplex[c][i]
ipy = hershey.simplex[c][i+1]
if ipx >= 0:
px = ipx
else:
px = 0-ipx
if ipy >= 0:
py = ipy
else:
if py == -1:
py = 32767
else:
py = 0+ipy
if do_scale:
px = int(float(px)*xscale)
py = int(float(py)*yscale)
if (py != 32767) and (py > mdy):
mdy = py+1
if (ipx == -1) and (ipy == -1): # pen up
old = False
else:
if old: # a previous point is stored in ox, oy
if ((ox+xx) > (self._h-1)) or ((px+xx) > (self._h-1)):
not_finished = False
break
self.line(ox+xx, oy+y, px+xx, py+y, colour, weight)
ox = px
oy = py
else:
ox = px
oy = py
old = True
if do_scale:
xx = xx+int(float(w)*xscale)
wt = wt+int(float(w)*xscale)
else:
xx = xx+w
wt = wt+w
if dy is not None:
dy = mdy
return wt+1
def get(self):
"""Get the filled in buffer."""
return self._buffer