Skip to content

Commit 6acd0c0

Browse files
committed
DM: add bitmap example
1 parent f0b4909 commit 6acd0c0

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

examples/epd_bitmap.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import digitalio
2+
import busio
3+
import board
4+
from adafruit_epd.epd import Adafruit_EPD
5+
from adafruit_epd.il0373 import Adafruit_IL0373
6+
7+
# create the spi device and pins we will need
8+
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
9+
ecs = digitalio.DigitalInOut(board.D10)
10+
dc = digitalio.DigitalInOut(board.D9)
11+
srcs = digitalio.DigitalInOut(board.D8)
12+
rst = digitalio.DigitalInOut(board.D7)
13+
busy = digitalio.DigitalInOut(board.D6)
14+
15+
# give them all to our driver
16+
display = Adafruit_IL0373(152, 152, rst, dc, busy, srcs, ecs, spi)
17+
18+
FILENAME = "blinka.bmp"
19+
20+
# clear the buffer
21+
display.clear_buffer()
22+
23+
def read_le(s):
24+
# as of this writting, int.from_bytes does not have LE support, DIY!
25+
result = 0
26+
shift = 0
27+
for byte in bytearray(s):
28+
result += byte << shift
29+
shift += 8
30+
return result
31+
32+
class BMPError(Exception):
33+
pass
34+
35+
36+
try:
37+
with open("/" + FILENAME, "rb") as f:
38+
print("File opened")
39+
if f.read(2) != b'BM': # check signature
40+
raise BMPError("Not BitMap file")
41+
42+
bmpFileSize = read_le(f.read(4))
43+
f.read(4) # Read & ignore creator bytes
44+
45+
bmpImageoffset = read_le(f.read(4)) # Start of image data
46+
headerSize = read_le(f.read(4))
47+
bmpWidth = read_le(f.read(4))
48+
bmpHeight = read_le(f.read(4))
49+
flip = True
50+
51+
print("Size: %d\nImage offset: %d\nHeader size: %d" %
52+
(bmpFileSize, bmpImageoffset, headerSize))
53+
print("Width: %d\nHeight: %d" % (bmpWidth, bmpHeight))
54+
55+
if read_le(f.read(2)) != 1:
56+
raise BMPError("Not singleplane")
57+
bmpDepth = read_le(f.read(2)) # bits per pixel
58+
print("Bit depth: %d" % (bmpDepth))
59+
if bmpDepth != 24:
60+
raise BMPError("Not 24-bit")
61+
if read_le(f.read(2)) != 0:
62+
raise BMPError("Compressed file")
63+
64+
print("Image OK! Drawing...")
65+
66+
rowSize = (bmpWidth * 3 + 3) & ~3 # 32-bit line boundary
67+
68+
for row in range(bmpHeight): # For each scanline...
69+
if flip: # Bitmap is stored bottom-to-top order (normal BMP)
70+
pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize
71+
else: # Bitmap is stored top-to-bottom
72+
pos = bmpImageoffset + row * rowSize
73+
74+
# print ("seek to %d" % pos)
75+
f.seek(pos)
76+
for col in range(bmpWidth):
77+
b, g, r = bytearray(f.read(3)) # BMP files store RGB in BGR
78+
if r < 0x80 and g < 0x80 and b < 0x80:
79+
display.draw_pixel(row, col, Adafruit_EPD.BLACK)
80+
elif r >= 0x80 and g >= 0x80 and b >= 0x80:
81+
display.draw_pixel(row, col, Adafruit_EPD.WHITE)
82+
elif r >= 0x80:
83+
display.draw_pixel(row, col, Adafruit_EPD.RED)
84+
85+
except OSError as e:
86+
if e.args[0] == 28:
87+
raise OSError("OS Error 28 0.25")
88+
else:
89+
raise OSError("OS Error 0.5")
90+
except BMPError as e:
91+
print("Failed to parse BMP: " + e.args[0])
92+
93+
94+
display.display()

0 commit comments

Comments
 (0)