-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfbdrawing.py
229 lines (208 loc) · 7.12 KB
/
fbdrawing.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
from micropython import const
import framebuf
class FrameBufferExtended(framebuf.FrameBuffer):
ROTATE_0 = const(0) # 'portrait', connector up
ROTATE_90 = const(1)
ROTATE_180 = const(2)
ROTATE_270 = const(3)
def __init__(self, buf, w, h, enc):
self._buffer = buf
self._width = w # 'physical' width
self.width = w # processing width
self._height = h
self.height = h
self._rotate = ROTATE_0
self.scale = 1
self.width_scaled = self.width
self.height_scaled = self.height
# super(FrameBufferExtended, self).__init__(buf, w, h, enc)
# crashes on the Lopy
# swap with the following line
#super(FrameBufferExtended, self)
super(FrameBufferExtended, self).__init__(buf, w, h, enc)
@property
def buffer(self):
return self._buffer
def get_absolute_pixel(self, x, y):
if (x < 0 or x >= self._width or y < 0 or y >= self._height):
return
return (self._buffer[(x + y * self._width) // 8] >> (7 - (x % 8))) & 1
def set_absolute_pixel(self, x, y, colored):
# print("set_absolute_pixel", x, y, colored)
# print(" A: w x h: ", self._width , self._height )
if (x < 0 or x >= self._width or y < 0 or y >= self._height):
# print("A: skipped", x, y)
return
if (colored):
self._buffer[(x + y * self._width) // 8] &= ~(0x80 >> (x % 8))
else:
self._buffer[(x + y * self._width) // 8] |= 0x80 >> (x % 8)
def set_scaled_pixel(self, x, y, colored):
if self.scale == 1:
self.set_absolute_pixel(x, y, colored)
else:
xoffset = x * self.scale
yoffset = y * self.scale
for px in range(self.scale):
for py in range(self.scale):
self.set_absolute_pixel(xoffset + px, yoffset + py, colored)
def get_pixel(self, x, y):
if (x < 0 or x >= self.width or y < 0 or y >= self.height):
return
if (self._rotate == ROTATE_0):
return self.get_absolute_pixel(x, y)
elif (self._rotate == ROTATE_90):
return self.get_absolute_pixel(self.height -y -1, x)
elif (self._rotate == ROTATE_180):
return self.get_absolute_pixel(self.width -1 - x, self.height -1 - y)
elif (self._rotate == ROTATE_270):
return self.get_absolute_pixel(y, self.width -1 - x)
def set_pixel(self, x, y, colored):
# print("set_pixel", x, y, colored)
# print(" R: w x h:", self.width, self.height )
if (x < 0 or x >= self.width or y < 0 or y >= self.height):
# print("R: skipped", x, y)
return
if (self._rotate == ROTATE_0):
self.set_scaled_pixel(x, y, colored)
elif (self._rotate == ROTATE_90):
self.set_scaled_pixel(self.height_scaled -y -1, x, colored)
elif (self._rotate == ROTATE_180):
self.set_scaled_pixel(self.width_scaled -1 - x, self.height_scaled -1 - y, colored)
elif (self._rotate == ROTATE_270):
self.set_scaled_pixel(y, self.width_scaled -1 - x, colored)
def get_rotate(self):
return self._rotate
def set_rotate(self, rotate):
if (rotate == ROTATE_0):
self._rotate = ROTATE_0
self.width = self._width
self.height = self._height
elif (rotate == ROTATE_90):
self._rotate = ROTATE_90
self.width = self._height
self.height = self._width
elif (rotate == ROTATE_180):
self._rotate = ROTATE_180
self.width = self._width
self.height = self._height
elif (rotate == ROTATE_270):
self._rotate = ROTATE_270
self.width = self._height
self.height = self._width
else:
raise ValueError('Invalid rotation value')
self.width_scaled = self.width // self.scale
self.height_scaled = self.height // self.scale
# print("Display is now: {} x {}", self.width, self.height)
def draw_line(self, x0, y0, x1, y1, colored):
# Bresenham algorithm
dx = abs(x1 - x0)
sx = 1 if x0 < x1 else -1
dy = -abs(y1 - y0)
sy = 1 if y0 < y1 else -1
err = dx + dy
while((x0 != x1) and (y0 != y1)):
self.set_pixel(x0, y0 , colored)
if (2 * err >= dy):
err += dy
x0 += sx
if (2 * err <= dx):
err += dx
y0 += sy
def draw_horizontal_line(self, x, y, width, colored):
for i in range(x, x + width):
self.set_pixel(i, y, colored)
def draw_vertical_line(self, x, y, height, colored):
for i in range(y, y + height):
self.set_pixel(x, i, colored)
def draw_rectangle(self, x0, y0, x1, y1, colored):
min_x = x0 if x1 > x0 else x1
max_x = x1 if x1 > x0 else x0
min_y = y0 if y1 > y0 else y1
max_y = y1 if y1 > y0 else y0
self.draw_horizontal_line(min_x, min_y, max_x - min_x + 1, colored)
self.draw_horizontal_line(min_x, max_y, max_x - min_x + 1, colored)
self.draw_vertical_line(min_x, min_y, max_y - min_y + 1, colored)
self.draw_vertical_line(max_x, min_y, max_y - min_y + 1, colored)
def draw_filled_rectangle(self, x0, y0, x1, y1, colored):
min_x = x0 if x1 > x0 else x1
max_x = x1 if x1 > x0 else x0
min_y = y0 if y1 > y0 else y1
max_y = y1 if y1 > y0 else y0
for i in range(min_x, max_x + 1):
self.draw_vertical_line(i, min_y, max_y - min_y + 1, colored)
def scroll(self, x, y):
if x > 0:
print("Scrolling x-axis not implemented")
linebytes = abs(y * self.scale) * self.width //8
if linebytes > len(self._buffer):
linebytes = len(self._buffer)
if y > 0:
self._buffer[0 : len(self._buffer) - linebytes ] = self._buffer[ linebytes : ]
else:
self._buffer[ linebytes : len(self._buffer) ] = self._buffer[0: len(self._buffer) - linebytes ]
def draw_circle(self, x, y, radius, colored):
# Bresenham algorithm
x_pos = -radius
y_pos = 0
err = 2 - 2 * radius
if (x >= self.width or y >= self.height):
return
while True:
self.set_pixel(x - x_pos, y + y_pos, colored)
self.set_pixel(x + x_pos, y + y_pos, colored)
self.set_pixel(x + x_pos, y - y_pos, colored)
self.set_pixel(x - x_pos, y - y_pos, colored)
e2 = err
if (e2 <= y_pos):
y_pos += 1
err += y_pos * 2 + 1
if(-x_pos == y_pos and e2 <= x_pos):
e2 = 0
if (e2 > x_pos):
x_pos += 1
err += x_pos * 2 + 1
if x_pos > 0:
break
def draw_filled_circle(self, x, y, radius, colored):
# Bresenham algorithm
x_pos = -radius
y_pos = 0
err = 2 - 2 * radius
if (x >= self.width or y >= self.height):
return
while True:
self.set_pixel(x - x_pos, y + y_pos, colored)
self.set_pixel(x + x_pos, y + y_pos, colored)
self.set_pixel(x + x_pos, y - y_pos, colored)
self.set_pixel(x - x_pos, y - y_pos, colored)
self.draw_horizontal_line(x + x_pos, y + y_pos, 2 * (-x_pos) + 1, colored)
self.draw_horizontal_line(x + x_pos, y - y_pos, 2 * (-x_pos) + 1, colored)
e2 = err
if (e2 <= y_pos):
y_pos += 1
err += y_pos * 2 + 1
if(-x_pos == y_pos and e2 <= x_pos):
e2 = 0
if (e2 > x_pos):
x_pos += 1
err += x_pos * 2 + 1
if x_pos > 0:
break
def set_scale(self, scale):
self.scale = scale
self.width_scaled = self.width // self.scale
self.height_scaled = self.height // self.scale
def draw_string_at(self, x, y, text, font, colored):
image = Image.new('1', (self.width, self.height))
draw = ImageDraw.Draw(image)
draw.text((x, y), text, font = font, fill = 255)
# Set buffer to value of Python Imaging Library image.
# Image must be in mode 1.
pixels = image.load()
for y in range(self.height):
for x in range(self.width):
# Set the bits for the column of pixels at the current position.
if pixels[x, y] != 0:
self.set_pixel(x, y, colored)