epd_pillow_demo.py
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, # 1.54" HD mono display 39 # display = Adafruit_SSD1675(122, 250, # 2.13" HD mono display 40 # display = Adafruit_IL91874(176, 264, # 2.7" Tri-color display 41 # display = Adafruit_IL0373(152, 152, # 1.54" Tri-color display 42 # display = Adafruit_IL0373(128, 296, # 2.9" Tri-color display 43 # display = Adafruit_IL0398(400, 300, # 4.2" Tri-color display 44 display = Adafruit_IL0373( 45 104, 46 212, # 2.13" Tri-color display 47 spi, 48 cs_pin=ecs, 49 dc_pin=dc, 50 sramcs_pin=srcs, 51 rst_pin=rst, 52 busy_pin=busy, 53 ) 54 55 # IF YOU HAVE A FLEXIBLE DISPLAY (2.13" or 2.9") uncomment these lines! 56 # display.set_black_buffer(1, False) 57 # display.set_color_buffer(1, False) 58 59 display.rotation = 1 60 61 image = Image.new("RGB", (display.width, display.height)) 62 63 # Get drawing object to draw on image. 64 draw = ImageDraw.Draw(image) 65 66 # Draw a filled box as the background 67 draw.rectangle((0, 0, display.width, display.height), fill=BACKGROUND_COLOR) 68 69 # Draw a smaller inner foreground rectangle 70 draw.rectangle( 71 (BORDER, BORDER, display.width - BORDER - 1, display.height - BORDER - 1), 72 fill=FOREGROUND_COLOR, 73 ) 74 75 # Load a TTF Font 76 font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", FONTSIZE) 77 78 # Draw Some Text 79 text = "Hello World!" 80 (font_width, font_height) = font.getsize(text) 81 draw.text( 82 (display.width // 2 - font_width // 2, display.height // 2 - font_height // 2), 83 text, 84 font=font, 85 fill=TEXT_COLOR, 86 ) 87 88 # Display image. 89 display.image(image) 90 display.display()