|
| 1 | +""" |
| 2 | +ePaper Display Shapes and Text demo using the Pillow Library. |
| 3 | +
|
| 4 | +Written by Melissa LeBlanc-Williams for Adafruit Industries |
| 5 | +""" |
| 6 | + |
| 7 | +import digitalio |
| 8 | +import busio |
| 9 | +import board |
| 10 | +from PIL import Image, ImageDraw, ImageFont |
| 11 | +from adafruit_epd.il0373 import Adafruit_IL0373 |
| 12 | +from adafruit_epd.il91874 import Adafruit_IL91874 # pylint: disable=unused-import |
| 13 | +from adafruit_epd.il0398 import Adafruit_IL0398 # pylint: disable=unused-import |
| 14 | +from adafruit_epd.ssd1608 import Adafruit_SSD1608 # pylint: disable=unused-import |
| 15 | +from adafruit_epd.ssd1675 import Adafruit_SSD1675 # pylint: disable=unused-import |
| 16 | + |
| 17 | +# First define some color constants |
| 18 | +WHITE = (0xFF, 0xFF, 0xFF) |
| 19 | +BLACK = (0x00, 0x00, 0x00) |
| 20 | +RED = (0xFF, 0x00, 0x00) |
| 21 | + |
| 22 | +# Next define some constants to allow easy resizing of shapes and colors |
| 23 | +BORDER = 20 |
| 24 | +FONTSIZE = 24 |
| 25 | +BACKGROUND_COLOR = BLACK |
| 26 | +FOREGROUND_COLOR = WHITE |
| 27 | +TEXT_COLOR = RED |
| 28 | + |
| 29 | +# create the spi device and pins we will need |
| 30 | +spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) |
| 31 | +ecs = digitalio.DigitalInOut(board.CE0) |
| 32 | +dc = digitalio.DigitalInOut(board.D22) |
| 33 | +srcs = None |
| 34 | +rst = digitalio.DigitalInOut(board.D27) |
| 35 | +busy = digitalio.DigitalInOut(board.D17) |
| 36 | + |
| 37 | +# give them all to our driver |
| 38 | +#display = Adafruit_SSD1608(200, 200, spi, # 1.54" HD mono display |
| 39 | +#display = Adafruit_SSD1675(122, 250, spi, # 2.13" HD mono display |
| 40 | +#display = Adafruit_IL91874(176, 264, spi, # 2.7" Tri-color display |
| 41 | +#display = Adafruit_IL0373(152, 152, spi, # 1.54" Tri-color display |
| 42 | +#display = Adafruit_IL0373(128, 296, spi, # 2.9" Tri-color display |
| 43 | +#display = Adafruit_IL0398(400, 300, spi, # 4.2" Tri-color display |
| 44 | +display = Adafruit_IL0373(104, 212, spi, # 2.13" Tri-color display |
| 45 | + cs_pin=ecs, dc_pin=dc, sramcs_pin=srcs, |
| 46 | + rst_pin=rst, busy_pin=busy) |
| 47 | + |
| 48 | +# IF YOU HAVE A FLEXIBLE DISPLAY (2.13" or 2.9") uncomment these lines! |
| 49 | +#display.set_black_buffer(1, False) |
| 50 | +#display.set_color_buffer(1, False) |
| 51 | + |
| 52 | +display.rotation = 1 |
| 53 | + |
| 54 | +image = Image.new('RGB', (display.width, display.height)) |
| 55 | + |
| 56 | +# Get drawing object to draw on image. |
| 57 | +draw = ImageDraw.Draw(image) |
| 58 | + |
| 59 | +# Draw a filled box as the background |
| 60 | +draw.rectangle((0, 0, display.width, display.height), fill=BACKGROUND_COLOR) |
| 61 | + |
| 62 | +# Draw a smaller inner foreground rectangle |
| 63 | +draw.rectangle((BORDER, BORDER, display.width - BORDER - 1, display.height - BORDER - 1), |
| 64 | + fill=FOREGROUND_COLOR) |
| 65 | + |
| 66 | +# Load a TTF Font |
| 67 | +font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', FONTSIZE) |
| 68 | + |
| 69 | +# Draw Some Text |
| 70 | +text = "Hello World!" |
| 71 | +(font_width, font_height) = font.getsize(text) |
| 72 | +draw.text((display.width//2 - font_width//2, display.height//2 - font_height//2), |
| 73 | + text, font=font, fill=TEXT_COLOR) |
| 74 | + |
| 75 | +# Display image. |
| 76 | +display.image(image) |
| 77 | +display.display() |
0 commit comments