Skip to content

Commit 0f8584f

Browse files
committed
fix up bitmap example
1 parent 09f16e0 commit 0f8584f

File tree

1 file changed

+42
-23
lines changed

1 file changed

+42
-23
lines changed

examples/epd_bitmap.py

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,34 @@
33
import board
44
from adafruit_epd.epd import Adafruit_EPD
55
from adafruit_epd.il0373 import Adafruit_IL0373
6+
from adafruit_epd.il91874 import Adafruit_IL91874
7+
from adafruit_epd.il0398 import Adafruit_IL0398
68

79
# create the spi device and pins we will need
810
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
911
ecs = digitalio.DigitalInOut(board.D10)
1012
dc = digitalio.DigitalInOut(board.D9)
11-
srcs = digitalio.DigitalInOut(board.D8)
12-
rst = digitalio.DigitalInOut(board.D7)
13-
busy = digitalio.DigitalInOut(board.D6)
13+
srcs = digitalio.DigitalInOut(board.D7) # can be None to use internal memory
14+
rst = digitalio.DigitalInOut(board.D11) # can be None to not use this pin
15+
busy = digitalio.DigitalInOut(board.D12) # can be None to not use this pin
1416

1517
# give them all to our driver
16-
display = Adafruit_IL0373(152, 152, spi,
18+
print("Creating display")
19+
#display = Adafruit_IL91874(176, 264, spi, # 2.7" Tri-color display
20+
#display = Adafruit_IL0373(152, 152, spi, # 1.54" Tri-color display
21+
#display = Adafruit_IL0373(128, 296, spi, # 2.9" Tri-color display
22+
#display = Adafruit_IL0398(400, 300, spi, # 4.2" Tri-color display
23+
display = Adafruit_IL0373(104, 212, spi, # 2.13" Tri-color display
1724
cs_pin=ecs, dc_pin=dc, sramcs_pin=srcs,
1825
rst_pin=rst, busy_pin=busy)
1926

20-
FILENAME = "blinka.bmp"
27+
# IF YOU HAVE A FLEXIBLE DISPLAY (2.13" or 2.9") uncomment these lines!
28+
#display.set_black_buffer(1, False)
29+
#display.set_color_buffer(1, False)
2130

22-
# clear the buffer
23-
display.fill(Adafruit_EPD.WHITE)
31+
display.rotation = 3
32+
33+
FILENAME = "blinka.bmp"
2434

2535
def read_le(s):
2636
# as of this writting, int.from_bytes does not have LE support, DIY!
@@ -35,9 +45,15 @@ class BMPError(Exception):
3545
pass
3646

3747

38-
try:
39-
with open("/" + FILENAME, "rb") as f:
40-
print("File opened")
48+
def display_bitmap(epd, filename):
49+
try:
50+
f = open("/" + filename, "rb")
51+
except OSError:
52+
print("Couldn't open file")
53+
return
54+
55+
print("File opened")
56+
try:
4157
if f.read(2) != b'BM': # check signature
4258
raise BMPError("Not BitMap file")
4359

@@ -68,29 +84,32 @@ class BMPError(Exception):
6884
rowSize = (bmpWidth * 3 + 3) & ~3 # 32-bit line boundary
6985

7086
for row in range(bmpHeight): # For each scanline...
87+
print(row)
7188
if flip: # Bitmap is stored bottom-to-top order (normal BMP)
7289
pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize
7390
else: # Bitmap is stored top-to-bottom
7491
pos = bmpImageoffset + row * rowSize
7592

7693
# print ("seek to %d" % pos)
7794
f.seek(pos)
95+
rowdata = f.read(3*bmpWidth)
7896
for col in range(bmpWidth):
79-
b, g, r = bytearray(f.read(3)) # BMP files store RGB in BGR
97+
b, g, r = rowdata[3*col:3*col+3] # BMP files store RGB in BGR
8098
if r < 0x80 and g < 0x80 and b < 0x80:
81-
display.pixel(row, col, Adafruit_EPD.BLACK)
99+
epd.pixel(col, row, Adafruit_EPD.BLACK)
82100
elif r >= 0x80 and g >= 0x80 and b >= 0x80:
83-
display.pixel(row, col, Adafruit_EPD.WHITE)
101+
pass #epd.pixel(row, col, Adafruit_EPD.WHITE)
84102
elif r >= 0x80:
85-
display.pixel(row, col, Adafruit_EPD.RED)
86-
87-
except OSError as e:
88-
if e.args[0] == 28:
89-
raise OSError("OS Error 28 0.25")
90-
else:
91-
raise OSError("OS Error 0.5")
92-
except BMPError as e:
93-
print("Failed to parse BMP: " + e.args[0])
94-
103+
epd.pixel(col, row, Adafruit_EPD.RED)
104+
except OSError:
105+
print("Couldn't read file")
106+
except BMPError as e:
107+
print("Failed to parse BMP: " + e.args[0])
108+
finally:
109+
f.close()
110+
print("Finished drawing")
95111

112+
# clear the buffer
113+
display.fill(Adafruit_EPD.WHITE)
114+
display_bitmap(display, FILENAME)
96115
display.display()

0 commit comments

Comments
 (0)