|
| 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() |
0 commit comments