-
Notifications
You must be signed in to change notification settings - Fork 13
/
demo_pbm.py
113 lines (90 loc) · 3.85 KB
/
demo_pbm.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
"""SSD1351 demo (PBM - Portable Bitmap).
This demo is based on code provided by MimiVRC
"""
from ssd1351 import Display, color565
from struct import pack, unpack
from framebuf import FrameBuffer, MONO_HLSB, RGB565 # type: ignore
from machine import Pin, SPI # type: ignore
from time import sleep
def create_palette(foreground, background=0, invert=False):
"""Create framebuffer palette to translate between MONO_HLSB and RGB565.
Args:
foreground(int): Foreground color in RGB656 format
background(int): Background color in RGB656 format (default Black)
invert(bool): Invert foreground and background (default False)
Returns:
FrameBuffer: Color palette
"""
# Need to swap endian colors
foreground = unpack('>H', pack('<H', foreground))[0]
background = unpack('>H', pack('<H', background))[0]
# Buffer size equals 2 pixels (MONO_HLSB) x 2 byte color depth (RGB565)
buffer_size = 4
# Define framebuffer
palette = FrameBuffer(bytearray(buffer_size), 2, 1, RGB565)
# Set foreground & background color pixels (swap if inverted)
palette.pixel(0 if invert else 1, 0, background)
palette.pixel(1 if invert else 0, 0, foreground)
return palette
def load_pbm(filename):
"""Load portable bitmap file.
Args:
filename(str): Path to bitmap
Returns:
tuple: A tuple containing the following:
- FrameBuffer: The image data in MONO_HLSB format.
- int: The width of the image.
- int: The height of the image.
"""
with open(filename, 'rb') as f:
# Read and discard the first 2 lines
for _ in range(2):
f.readline()
# Read dimensions from the third line
dimensions = f.readline().split()
width = int(dimensions[0])
height = int(dimensions[1])
# Read the bitmap data
data = bytearray(f.read())
return FrameBuffer(data, width, height, MONO_HLSB), width, height
def test():
"""Test code."""
# Baud rate of 40000000 seems about the max
spi = SPI(2, baudrate=14500000, sck=Pin(18), mosi=Pin(23))
display = Display(spi, dc=Pin(17), cs=Pin(5), rst=Pin(16))
display.clear()
# Define palettes
red = create_palette(color565(255, 0, 0))
magenta = create_palette(color565(255, 0, 255))
green = create_palette(color565(0, 255, 0), invert=True)
blue = create_palette(color565(0, 0, 255), invert=True)
yellow = create_palette(color565(255, 255, 0),
background=color565(0, 0, 255),
invert=True)
white = create_palette(color565(255, 255, 255),
background=color565(0, 255, 255))
# Load invader .PBM image to framebuffer and get dimensions
invader_fb, w, h = load_pbm('images/invaders48x36.pbm')
# Create RGB565 placeholder
placeholder = bytearray(w * h * 2)
# Create frame buffer for placeholder
placeholder_fb = FrameBuffer(placeholder, w, h, RGB565)
# Draw 6 invaders in different color palettes
placeholder_fb.blit(invader_fb, 0, 0, -1, red)
display.draw_sprite(placeholder, 0, 0, w, h)
placeholder_fb.blit(invader_fb, 0, 0, -1, magenta)
display.draw_sprite(placeholder, display.width - w, 0, w, h)
placeholder_fb.blit(invader_fb, 0, 0, -1, green)
display.draw_sprite(placeholder, 0,
(display.height - h) // 2, w, h)
placeholder_fb.blit(invader_fb, 0, 0, -1, blue)
display.draw_sprite(placeholder, display.width - w,
(display.height - h) // 2, w, h)
placeholder_fb.blit(invader_fb, 0, 0, -1, yellow)
display.draw_sprite(placeholder, 0, display.height - h, w, h)
placeholder_fb.blit(invader_fb, 0, 0, -1, white)
display.draw_sprite(placeholder, display.width - w,
display.height - h, w, h)
sleep(10)
display.cleanup()
test()