/ examples / pcd8544_pillow_demo.py
pcd8544_pillow_demo.py
 1  """
 2  This demo will fill the screen with white, draw a black box on top
 3  and then print Hello World! in the center of the display
 4  
 5  This example is for use on (Linux) computers that are using CPython with
 6  Adafruit Blinka to support CircuitPython libraries. CircuitPython does
 7  not support PIL/pillow (python imaging library)!
 8  """
 9  
10  import board
11  import busio
12  import digitalio
13  from PIL import Image, ImageDraw, ImageFont
14  import adafruit_pcd8544
15  
16  # Parameters to Change
17  BORDER = 5
18  FONTSIZE = 10
19  
20  spi = busio.SPI(board.SCK, MOSI=board.MOSI)
21  dc = digitalio.DigitalInOut(board.D6)  # data/command
22  cs = digitalio.DigitalInOut(board.CE0)  # Chip select
23  reset = digitalio.DigitalInOut(board.D5)  # reset
24  
25  display = adafruit_pcd8544.PCD8544(spi, dc, cs, reset)
26  
27  # Contrast and Brightness Settings
28  display.bias = 4
29  display.contrast = 60
30  
31  # Turn on the Backlight LED
32  backlight = digitalio.DigitalInOut(board.D13)  # backlight
33  backlight.switch_to_output()
34  backlight.value = True
35  
36  # Clear display.
37  display.fill(0)
38  display.show()
39  
40  # Create blank image for drawing.
41  # Make sure to create image with mode '1' for 1-bit color.
42  image = Image.new("1", (display.width, display.height))
43  
44  # Get drawing object to draw on image.
45  draw = ImageDraw.Draw(image)
46  
47  # Draw a black background
48  draw.rectangle((0, 0, display.width, display.height), outline=255, fill=255)
49  
50  
51  # Draw a smaller inner rectangle
52  draw.rectangle(
53      (BORDER, BORDER, display.width - BORDER - 1, display.height - BORDER - 1),
54      outline=0,
55      fill=0,
56  )
57  
58  # Load a TTF font.
59  font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", FONTSIZE)
60  
61  # Draw Some Text
62  text = "Hello World!"
63  (font_width, font_height) = font.getsize(text)
64  draw.text(
65      (display.width // 2 - font_width // 2, display.height // 2 - font_height // 2),
66      text,
67      font=font,
68      fill=255,
69  )
70  
71  # Display image
72  display.image(image)
73  display.show()