Skip to content

Commit e36942c

Browse files
committed
adding eink demos for bonnet
1 parent 49f2981 commit e36942c

File tree

3 files changed

+166
-0
lines changed

3 files changed

+166
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# SPDX-FileCopyrightText: 2019 Melissa LeBlanc-Williams for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
"""
5+
ePaper Display Shapes and Text demo using the Pillow Library.
6+
7+
"""
8+
9+
import board
10+
import busio
11+
import digitalio
12+
from PIL import Image, ImageDraw, ImageFont
13+
from adafruit_epd.epd import Adafruit_EPD
14+
from adafruit_epd.uc8179 import Adafruit_UC8179
15+
16+
# First define some color constants
17+
WHITE = (0xFF, 0xFF, 0xFF)
18+
BLACK = (0x00, 0x00, 0x00)
19+
RED = (0xFF, 0x00, 0x00)
20+
21+
# Next define some constants to allow easy resizing of shapes and colors
22+
BORDER = 20
23+
FONTSIZE = 24
24+
BACKGROUND_COLOR = BLACK
25+
FOREGROUND_COLOR = WHITE
26+
TEXT_COLOR = RED
27+
28+
# create the spi device and pins we will need
29+
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
30+
ecs = digitalio.DigitalInOut(board.CE0)
31+
dc = digitalio.DigitalInOut(board.D22)
32+
srcs = None
33+
rst = digitalio.DigitalInOut(board.D27)
34+
busy = digitalio.DigitalInOut(board.D17)
35+
36+
37+
display = Adafruit_UC8179( # 7.5" tricolor 800x480 display
38+
800,
39+
480,
40+
spi,
41+
cs_pin=ecs,
42+
dc_pin=dc,
43+
sramcs_pin=srcs,
44+
rst_pin=rst,
45+
busy_pin=busy,
46+
tri_color = True)
47+
48+
width = display.width
49+
height = display.height
50+
image = Image.new("RGB", (width, height))
51+
52+
WHITE = (0xFF, 0xFF, 0xFF)
53+
RED = (0xFF, 0x00, 0x00)
54+
BLACK = (0x00, 0x00, 0x00)
55+
56+
# clear the buffer
57+
display.fill(Adafruit_EPD.WHITE)
58+
59+
# Get drawing object to draw on image.
60+
draw = ImageDraw.Draw(image)
61+
# empty it
62+
draw.rectangle((0, 0, width, height), fill=WHITE)
63+
64+
# Draw an outline box
65+
draw.rectangle((1, 1, width - 2, height - 2), outline=BLACK, fill=WHITE)
66+
67+
# Draw some shapes.
68+
# First define some constants to allow easy resizing of shapes.
69+
padding = 20
70+
shape_width = 120
71+
top = padding
72+
bottom = height - padding
73+
# Move left to right keeping track of the current x position for drawing shapes.
74+
x = padding
75+
# Draw an ellipse.
76+
draw.ellipse((x, top, x + shape_width, bottom), outline=RED, fill=WHITE)
77+
x += shape_width + padding
78+
# Draw a rectangle.
79+
draw.rectangle((x, top, x + shape_width, bottom), outline=RED, fill=BLACK)
80+
x += shape_width + padding
81+
# Draw a triangle.
82+
draw.polygon(
83+
[(x, bottom), (x + shape_width / 2, top), (x + shape_width, bottom)],
84+
outline=BLACK,
85+
fill=RED,
86+
)
87+
x += shape_width + padding
88+
# Draw an X.
89+
draw.line((x, bottom, x + shape_width, top), fill=BLACK)
90+
draw.line((x, top, x + shape_width, bottom), fill=RED)
91+
x += shape_width + padding
92+
93+
# Load default font.
94+
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 40)
95+
96+
draw.text((x, top), "Hello", font=font, fill=BLACK)
97+
draw.text((x, top + 40), "World!", font=font, fill=BLACK)
98+
99+
# Display image.
100+
display.image(image)
101+
102+
display.display()
7.87 KB
Loading
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# SPDX-FileCopyrightText: 2019 Melissa LeBlanc-Williams for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
Image resizing and drawing using the Pillow Library
5+
"""
6+
import board
7+
import busio
8+
import digitalio
9+
from PIL import Image
10+
from adafruit_epd.uc8179 import Adafruit_UC8179
11+
12+
# First define some color constants
13+
WHITE = (0xFF, 0xFF, 0xFF)
14+
BLACK = (0x00, 0x00, 0x00)
15+
RED = (0xFF, 0x00, 0x00)
16+
17+
# Next define some constants to allow easy resizing of shapes and colors
18+
BORDER = 20
19+
FONTSIZE = 24
20+
BACKGROUND_COLOR = BLACK
21+
FOREGROUND_COLOR = WHITE
22+
TEXT_COLOR = RED
23+
24+
# create the spi device and pins we will need
25+
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
26+
ecs = digitalio.DigitalInOut(board.CE0)
27+
dc = digitalio.DigitalInOut(board.D22)
28+
srcs = None
29+
rst = digitalio.DigitalInOut(board.D27)
30+
busy = digitalio.DigitalInOut(board.D17)
31+
32+
33+
display = Adafruit_UC8179( # 7.5" tri-color 800x480 display
34+
800,
35+
480,
36+
spi,
37+
cs_pin=ecs,
38+
dc_pin=dc,
39+
sramcs_pin=srcs,
40+
rst_pin=rst,
41+
busy_pin=busy,
42+
tri_color = True)
43+
44+
image = Image.open("blinka.png")
45+
46+
# Scale the image to the smaller screen dimension
47+
image_ratio = image.width / image.height
48+
screen_ratio = display.width / display.height
49+
print(image_ratio, screen_ratio)
50+
if screen_ratio < image_ratio:
51+
scaled_width = image.width * display.height // image.height
52+
scaled_height = display.height
53+
else:
54+
scaled_width = display.width
55+
scaled_height = image.height * display.width // image.width
56+
image = image.resize((scaled_width, scaled_height), Image.BICUBIC)
57+
58+
# Crop and center the image
59+
x = scaled_width // 2 - display.width // 2
60+
y = scaled_height // 2 - display.height // 2
61+
image = image.crop((x, y, x + display.width, y + display.height)).convert("RGB")
62+
63+
display.image(image)
64+
display.display()

0 commit comments

Comments
 (0)