-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtftbmp.py
48 lines (46 loc) · 1.64 KB
/
tftbmp.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
from st7735 import TFT, TFTColor
from machine import SPI, Pin
spi = SPI(
1, baudrate=20000000, polarity=0, phase=0, sck=Pin(10), mosi=Pin(11), miso=None
)
tft = TFT(spi, 16, 17, 18)
tft.initr()
tft.rgb(True)
tft.fill(TFT.BLACK)
# Note BMP file needs to be in 24 bit format
f = open("tux.bmp", "rb")
if f.read(2) == b"BM": # header
dummy = f.read(8) # file size(4), creator bytes(4)
offset = int.from_bytes(f.read(4), "little")
hdrsize = int.from_bytes(f.read(4), "little")
width = int.from_bytes(f.read(4), "little")
height = int.from_bytes(f.read(4), "little")
if int.from_bytes(f.read(2), "little") == 1: # planes must be 1
depth = int.from_bytes(f.read(2), "little")
if (
depth == 24 and int.from_bytes(f.read(4), "little") == 0
): # compress method == uncompressed
print("Image size:", width, "x", height)
rowsize = (width * 3 + 3) & ~3
if height < 0:
height = -height
flip = False
else:
flip = True
w, h = width, height
if w > 128:
w = 128
if h > 160:
h = 160
tft._setwindowloc((0, 0), (w - 1, h - 1))
for row in range(h):
if flip:
pos = offset + (height - 1 - row) * rowsize
else:
pos = offset + row * rowsize
if f.tell() != pos:
dummy = f.seek(pos)
for col in range(w):
bgr = f.read(3)
tft._pushcolor(TFTColor(bgr[2], bgr[1], bgr[0]))
spi.deinit()