forked from jairosh/raspberrypi-ssd1680
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.py
529 lines (477 loc) · 19 KB
/
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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
import numpy as np
import logging
from bdfparser import Font
class DisplayBuffer:
"""
Class to act as an abstraction of a display
"""
def __init__(self, width, height, bg=1, fg=0):
"""
Initializes the display buffer. Each pixel is modeled in one bit, the default color is "white" background and
"black" foreground
:param width: Width of the display this buffer models
:param height: Height of the display this buffer models
:param bg: Background value (default is 1=white)
:param fg: Foreground value (default is 0=black)
"""
# Pad the buffer to have whole bytes
self.WIDTH = width if width % 8 == 0 else (int(width / 8) + 1) * 8
self.HEIGHT = height
self._BYTE_WIDTH = self.WIDTH / 8
self._BYTE_HEIGHT = self.HEIGHT / 8
self._buffer = np.zeros((self.WIDTH * self.HEIGHT), dtype=np.uint8)
self._out_of_bounds_error = False
self._foreground = fg
self._background = bg
self._rotation = 0
self.x_length = self.WIDTH
self.y_length = self.HEIGHT
def rotate(self, degrees: int):
"""Virtually rotates the display to draw in different modes. The
rotation is specified in degrees with the values: 0, 90, 180, 270
:param int degrees: The rotation angle
"""
if degrees not in [0, 90, 180, 270]:
logging.warning("Invalid rotation.")
return
if degrees == 90 or degrees == 270:
self.x_length = self.HEIGHT
self.y_length = self.WIDTH
elif degrees == 0 or degrees == 180:
self.x_length = self.WIDTH
self.y_length = self.HEIGHT
self._rotation = degrees
def draw_pixel(self, x: int, y: int, value: np.uint8):
"""
Draw a single pixel in (x, y) with the indicated value
:param int x: X coordinate of the pixel to draw
:param int y: Y Coordinate of the pixel to draw
:param np.uint8 value: Value to set the pixel to (1 or 0)
:return: none
"""
if not self._valid_coords(x, y):
return
xr, yr = self.rotate_coords(x, y)
start, end, bit = self._get_slice(xr, yr)
self._buffer[start + bit] = value
def draw_pixels(self, pixels: list, value: np.uint8):
"""
Draws the pixels specified in a list of 2-tuples with the value specified
:param list pixels: List of 2-tuples that indicate each pixel, the tuples are in the form (x, y)
:param np.uint8 value: The value to set the pixels to
"""
for pixel in pixels:
self.draw_pixel(pixel[0], pixel[1], value)
def set_pixel(self, x, y):
"""Draws a single pixel by setting its representing bit in the buffer to the foreground value
:param int x: X coordinate of the pixel
:param int y: Y coordinate of the pixel
"""
if not self._valid_coords(x, y):
return
s, e, b = self._get_slice(x, y)
self._buffer[s + b] = self._foreground
def set_background(self, value: np.uint8):
"""
Sets the value used as the background (0 or 1). When a pixel is cleared is set to
this value
:param np.uint8 value: The value to be used as background
:return: None
"""
self._background = value
def set_foreground(self, value: np.uint8):
"""
Sets the value used as foreground (0 or 1). When a pixel is set, its value its set to this
:param value: The value to be used as foreground
:return: None
"""
self._foreground = value
def set_group_pixels(self, list_of_pixels):
"""Sets the pixels in the given list
:param list list_of_pixels: List of 2-tuples with the points (x, y)
"""
for pixel in list_of_pixels:
self.set_pixel(pixel[0], pixel[1])
def clear_pixel(self, x, y):
"""Erases a pixel on the display by clearing its representing bit in the buffer
:param int x: X coordinate of the pixel to erase
:param int y: Y coordinate of the pixel to erase
"""
if not self._valid_coords(x, y):
return
s, e, b = self._get_slice(x, y)
self._buffer[s + b] = self._background
def clear_group_pixels(self, list_of_pixels: list):
"""Clears the pixels/bits in the buffer
:param list list_of_pixels: A list of 2-tuples with the points (x, y)
"""
for pixel in list_of_pixels:
self.clear_pixel(pixel[0], pixel[1])
def clear_screen(self, value=0):
"""Sets all the pixels in the screen to the same value
:param int value: the value to fill the screen with (0 or 1)
"""
if value == 1 or value == 0:
self._buffer.fill(np.uint8(value))
else:
logging.warning(f"Incorrect color value {value}")
def get_pixel_value(self, x, y):
"""Reads the value of the given point (x, y)
:param int x: X coordinate of the point
:param int y: Y Coordinate of the point
:return np.uint8: The bit as it exists in the buffer
"""
if not self._valid_coords(x, y):
return np.uint8(0x00)
s, e, b = self._get_slice(x, y)
return self._buffer[s + b]
def get_pixel_byte(self, x, y):
"""Extracts the byte from the buffer that contains the bit of the given pixel
:param int x: X coordinate of the pixel to read
:param int y: Y coordinate of the pixel to read
:return np.uint8: A single byte representation containing the pixel
"""
# Calculate x1 and x2 to get the slice from the buffer
x1, x2, _ = self._get_slice(x, y)
# logging.debug(f'Slicing buffer[{x1}:{x2}]')
pixel_byte = DisplayBuffer.create_byte_from_array(self._buffer[x1:x2])
return pixel_byte
def draw_line(self, x1, y1, x2, y2, value: np.uint8):
"""Implements the Bresenham algorithm to draw a line from (x1, y1) to (x2, y2)
:param int x1: Starting x component
:param int y1: Starting y component
:param int x2: Final x component
:param int y2: Final y component
:param int value: Value (color) to set the bit to
"""
dx = x2 - x1
dy = y2 - y1
if dy >= 0:
y_incr = 1
else:
dy = -dy
y_incr = -1
if dx >= 0:
x_incr = 1
else:
dx = -dx
x_incr = -1
if dx >= dy:
y_incr_s = 0
x_incr_s = x_incr
else:
y_incr_s = y_incr
x_incr_s = 0
# Switch dX and dY
k = dx
dx = dy
dy = k
x = x1
y = y1
a = 2 * dy
b = a - dx
p = b - dx
while True:
self.draw_pixel(x, y, value)
if b >= 0:
x = x + x_incr
y = y + y_incr
b = b + p
else:
x = x + x_incr_s
y = y + y_incr_s
b = b + a
if x == x2 and y == y2:
break
self.draw_pixel(x, y, value)
def draw_circle(self, xc: int, yc: int, r: int, value: np.uint8):
"""Draws a circle with the Midpoint Algorithm
:param int xc: X coordinate of the circle's center
:param int yc: Y coordinate of the circle's center
:param int r: Circle's radius
:param np.uint8 value: Value to set the bit in the buffer
"""
if r == 0:
self.draw_pixel(xc, yc, value)
return
xk, yk = (r, 0)
pk = 1 - r
self.draw_pixels(
[
(xk + xc, yk + yc),
(-xk + xc, yk + yc),
(xk + xc, -yk + yc),
(-xk + xc, -yk + yc),
],
value,
)
self.draw_pixels([(xc, yc + r), (xc, yc - r)], value)
while xk > yk:
yk = yk + 1
if pk <= 0:
pk = pk + 2 * yk + 1
else:
xk -= 1
pk = pk + 2 * yk - 2 * xk + 1
if xk < yk:
break
# self.draw_pixel(xk + xc, yk + yc)
self.draw_pixels(
[
(xk + xc, yk + yc),
(-xk + xc, yk + yc),
(xk + xc, -yk + yc),
(-xk + xc, -yk + yc),
],
value,
)
if xk != yk:
self.draw_pixels(
[
(yk + xc, xk + yc),
(-yk + xc, xk + yc),
(yk + xc, -xk + yc),
(-yk + xc, -xk + yc),
],
value,
)
def draw_bitmap(self, bitmap: np.array, x: int, y: int, w: int, h: int, value: np.uint8):
"""Draws a bitmap on the buffer. The bitmap starts at the upper left corner (x, y)
and the lower right corner is (x+w, y+h)
:param np.array bitmap: A 1-dimmensional array of bytes, has to have at least shape of (w, h)
:param int x: X coordinate where to start drawing the bitmap
:param int y: Y coordinate where to start drawing the bitmap
:param int w: Width of the bitmap
:param int h: Height of the bitmap
:param np.uint8 value: Value to set in the buffer
"""
x_p = 0
y_p = 0
for byte in bitmap:
bitmasks = np.array(
[0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01], dtype=np.uint8
)
bit = 0
for mask in bitmasks:
if mask & byte:
self.draw_pixel(x_p + x, y_p + y, value)
bit = bit + 1
x_p = x_p + 1
if x_p == w:
y_p = y_p + 1
x_p = 0
def draw_text(self, text: str, font: Font, x: int, y: int, value: np.uint8):
"""
Render a bitmap of the text with the provided font and draw it in the buffer
:param text: The string to render
:param font: The Font object to use
:param x: X coordinate of the upper left corner of the bitmap
:param y: Y Coordinate of the upper left corner of the bitmap
:param value: Value to set the pixels in the buffer to
:return: None
"""
text_bitmap = font.draw(text)
array, width, height = self._bitmap_to_bytearray(text_bitmap.todata(4))
self.draw_bitmap(array, x, y, width, height, value)
def draw_rectangle(self, x: int, y: int, w: int, h: int, value: np.uint8):
"""
Draws a rectangle with 1-px wide lines, and no fill
:param int x: X Coordinate of the upper left corner
:param int y: Y Coordinate of the upper left corner
:param int w: Rectangle's width
:param int h: Rectangle's height
:param np.uint8 value: Value to set the pixels to
:return: None
"""
self.draw_line(x, y, x + w, y, value)
self.draw_line(x + w, y, x + w, y + h, value)
self.draw_line(x + w, y + h, x, y + h, value)
self.draw_line(x, y + h, x, y, value)
def _get_slice(self, x, y):
"""
Locates the slice of the buffer that contains a whole byte given x and y coordinates
:param int x: X coordinate of the pixel
:param int y: Y coordinate of the pixel
:return tuple: start position, end position, bit offset
"""
start = (y * self.WIDTH) + int(x / 8) * 8
end = start + 8
bit = x % 8
return start, end, bit
def pixel_address(self, x: int, y: int):
"""
Obtains the byte addr that contains the bit representing the pixel at (x, y)
:param int x: X coordinate of the point of interest
:param int y: Y coordinate of the point of interest
:return: The 0-based address (offset) of the byte
"""
s, e, b = self._get_slice(x, y)
return s
def _valid_coords(self, x, y):
"""
Validates that the given coordinates are within the display (and buffer) bounds.
:param int x: X coordinate
:param int y: Y coordinate
:raises ValueError: If the flag DisplayBuffer._out_of_bounds_error is set to True and
the coordinates are outside of the visible screen
:returns bool: True if they are valid (visible) coordinates, False otherwise
"""
within_x = 0 <= x <= self.x_length
within_y = 0 <= y <= self.y_length
if within_x and within_y:
return True
else:
if self._out_of_bounds_error:
raise ValueError("Coordinates out of bounds")
return False
def rotate_coords(self, x: int, y: int):
"""
Does a tramslation according to the current rotation
:param int x: X Coordinate to transform
:param int y: Y Coordinate to transform
:return tuple: The coordinate pair of the base buffer that corresponds to the input
"""
xn, yn = (0, 0)
if self._rotation == 90:
xn = (self.WIDTH - 1) - y
yn = x
elif self._rotation == 180:
xn = (self.WIDTH - 1) - x
yn = (self.HEIGHT - 1) - y
elif self._rotation == 270:
xn = y
yn = (self.HEIGHT - 1) - x
else:
xn = x
yn = y
return xn, yn
def serialize(self):
"""
Converts the internal buffer to an array of bytes
:returns np.array[np.uint8]: The internal buffer as an array of bytes
"""
bytelist = []
total_pixels = self.WIDTH * self.HEIGHT
logging.debug(f"The size of serialized buffer is {total_pixels}")
for byte in range(int(total_pixels / 8)):
start = byte * 8
byte_nbr = self.create_byte_from_array(self._buffer[start : start + 8])
bytelist.append(np.uint8(byte_nbr))
logging.debug(f"Final size of list: {len(bytelist)}")
return np.array(bytelist, dtype=np.uint8)
def serialize_area(self, x: int, y: int, width: int, height: int):
"""
Serializes an area of the screen
:param int x: X Coordinate of the upper left corner
:param int y: Y Coordinate of the upper left corner
:param int width: Area width
:param int height: Area height
:returns: The array of bytes
:rtype np.array[np.uint8]
"""
if not self._valid_coords(x, y) or not self._valid_coords(
x + width, y + height
):
logging.warning(f"The specified coordinates are invalid")
return
slice_start, _, _ = self._get_slice(x, y)
_, slice_end, _ = self._get_slice(x + width, y + height)
logging.debug(
f"Serializing the area ({x}, {y}, {width}, {height}) at [{slice_start}:{slice_end}]"
)
total_bytes = int((slice_end - slice_start) / 8)
logging.debug(f"Expecting {total_bytes} bytes")
byte_list = []
byte_offset = int(slice_start / 8)
for byte in range(byte_offset, byte_offset + total_bytes):
start = byte * 8
byte_value = self.create_byte_from_array(self._buffer[start : start + 8])
byte_list.append(byte_value)
return np.array(byte_list, dtype=np.uint8)
@staticmethod
def create_byte_from_array(bitarray: np.array):
"""
Converts a binary array (1s and 0s only) into a byte
:param np.array bitarray: An array of size 8 containing only 1s and 0s
:raises ValueError: if the array size isn't exactly 8
:returns: The byte representation
:rtype np.uint8
"""
# logging.debug(f'Bit array: {bitarray}')
if len(bitarray) != 8:
raise ValueError(
f"Incorrect array size. Array needs to be exactly 8 but got {len(bitarray)}"
)
number = 0
for b in bitarray:
number = (2 * number) + b
return np.uint8(number)
def effective_area(self, x: int, y: int, width: int, height: int):
"""
Calculate the intersection of the supplied rectangular area and the available screen space
:param int x: Starting X coordinate (left side of the area)
:param int y: Starting Y coordinate (top side of the area)
:param int width: Width of the area
:param int height: Height of the area
:return: 4-tuple with (x, y, width, height) inside the boundaries of the screen
"""
if width < 0 or height < 0:
return 0, 0, 0, 0
if (x < 0 and (x + width) < 0) or (x > self.WIDTH):
return 0, 0, 0, 0
if (y < 0 and (y + height) < 0) or (y > self.HEIGHT):
return 0, 0, 0, 0
x1 = 0 if x < 0 else x
y1 = 0 if y < 0 else y
x2 = self.WIDTH if x + width > self.WIDTH else x + width
y2 = self.HEIGHT if y + height > self.HEIGHT else y + height
return x1, y1, x2 - x1, y2 - y1
def dump_raw_buffer(self):
"""
Prints the buffer in a matrix of WIDTH*HEIGHT
"""
for y in range(self.HEIGHT):
line_offset = y * self.WIDTH
logging.debug(self._buffer[line_offset : line_offset + self.WIDTH])
# █●
def render(self, on_pixel="█", off_pixel=" "):
"""
Generates a ASCII art representation of this buffer
:param str on_pixel: Character to represent a "set bit"
:param str off_pixel: Character to represent a "cleared bit"
:returns: An ASCII art string
:rtype str
"""
lines = []
for line in range(self.HEIGHT):
line_offset = line * self.WIDTH
sliced_buffer = self._buffer[line_offset : line_offset + self.WIDTH]
ascii_list = [on_pixel if p == 1 else off_pixel for p in sliced_buffer]
ascii_line = "".join(ascii_list)
lines.append(ascii_line)
return "\n".join(lines)
@staticmethod
def _bitmap_to_bytearray(bitmap: list):
"""
Converts a bitmap from a font into a bitmap
:param list bitmap: A list of hex strings, each one represents a line
:returns: np.array(np.uint8): The byte array
int: The width in bits (pixels) of the bitmap as it might be padded to complete a byte
int: The height in bits (pixels) of the bitmap
"""
byte_list = []
bytes_per_line = 0
for line in bitmap:
nibbles = len(line)
bytes_per_line = 0
for byte in range(int(nibbles / 2)):
first_nibble = line[byte * 2]
if byte == nibbles:
second_nibble = 0
else:
second_nibble = line[byte * 2 + 1]
hex_byte = first_nibble + second_nibble
byte_value = np.uint8(int(hex_byte, base=16))
byte_list.append(byte_value)
bytes_per_line = bytes_per_line + 1
return np.array(byte_list), bytes_per_line * 8, len(bitmap)